ivlpp: Handle multi-line comments in macros
Make sure that comments spanning multiple lines are supported in multi-line macros. Since the lexer parses line by line we need a flag to track whether a multi-line comment is currently active. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
parent
d12a74beec
commit
c4daf11fac
|
|
@ -1130,12 +1130,14 @@ void free_macros(void)
|
||||||
* variables. When the define is over, the def_finish() function
|
* variables. When the define is over, the def_finish() function
|
||||||
* executes the define and clears this text. The define_continue_flag
|
* executes the define and clears this text. The define_continue_flag
|
||||||
* is set if do_define detects that the definition is to be continued
|
* is set if do_define detects that the definition is to be continued
|
||||||
* on the next line.
|
* on the next line. The define_comment_flag is set when a multi-line comment is
|
||||||
|
* active in a define.
|
||||||
*/
|
*/
|
||||||
static char* define_text = 0;
|
static char* define_text = 0;
|
||||||
static size_t define_cnt = 0;
|
static size_t define_cnt = 0;
|
||||||
|
|
||||||
static int define_continue_flag = 0;
|
static int define_continue_flag = 0;
|
||||||
|
static int define_comment_flag = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The do_magic function puts the expansions of magic macros into
|
* The do_magic function puts the expansions of magic macros into
|
||||||
|
|
@ -1189,6 +1191,33 @@ static char *find_arg(char*ptr, char*head, char*arg)
|
||||||
return cp;
|
return cp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns 1 if the comment continues on the next line
|
||||||
|
*/
|
||||||
|
static int do_define_multiline_comment(char *replace_start,
|
||||||
|
const char *search_start)
|
||||||
|
{
|
||||||
|
char *tail = strstr(search_start, "*/");
|
||||||
|
|
||||||
|
if (!tail) {
|
||||||
|
if (search_start[strlen(search_start) - 1] == '\\') {
|
||||||
|
define_continue_flag = 1;
|
||||||
|
define_comment_flag = 1;
|
||||||
|
*replace_start++ = '\\';
|
||||||
|
} else {
|
||||||
|
define_comment_flag = 0;
|
||||||
|
fprintf(stderr, "%s:%u: Unterminated comment in define\n",
|
||||||
|
istack->path, istack->lineno+1);
|
||||||
|
}
|
||||||
|
*replace_start = '\0';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
define_comment_flag = 0;
|
||||||
|
tail += 2;
|
||||||
|
memmove(replace_start, tail, strlen(tail) + 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Collect the definition. Normally, this returns 0. If there is a
|
* Collect the definition. Normally, this returns 0. If there is a
|
||||||
* continuation, then return 1 and this function may be called again
|
* continuation, then return 1 and this function may be called again
|
||||||
|
|
@ -1204,6 +1233,12 @@ static void do_define(void)
|
||||||
|
|
||||||
define_continue_flag = 0;
|
define_continue_flag = 0;
|
||||||
|
|
||||||
|
/* Are we in an multi-line comment? Look for the end */
|
||||||
|
if (define_comment_flag) {
|
||||||
|
if (do_define_multiline_comment(yytext, yytext))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Look for comments in the definition, and remove them. */
|
/* Look for comments in the definition, and remove them. */
|
||||||
cp = strchr(yytext, '/');
|
cp = strchr(yytext, '/');
|
||||||
|
|
||||||
|
|
@ -1216,20 +1251,11 @@ static void do_define(void)
|
||||||
*cp = 0;
|
*cp = 0;
|
||||||
break;
|
break;
|
||||||
} else if (cp[1] == '*') {
|
} else if (cp[1] == '*') {
|
||||||
tail = strstr(cp+2, "*/");
|
if (do_define_multiline_comment(cp, cp + 2))
|
||||||
|
|
||||||
if (tail == 0) {
|
|
||||||
*cp = 0;
|
|
||||||
fprintf(stderr, "%s:%u: Unterminated comment in define\n",
|
|
||||||
istack->path, istack->lineno+1
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
memmove(cp, tail+2, strlen(tail+2)+1);
|
|
||||||
} else {
|
} else {
|
||||||
cp++;
|
cp++;
|
||||||
}
|
}
|
||||||
|
|
||||||
cp = strchr(cp, '/');
|
cp = strchr(cp, '/');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue