Prevent ## to appear at start or end of macro

master
Thomas Preud'homme 2014-04-08 22:19:48 +08:00
parent 91d4db600b
commit a715d7143d
6 changed files with 20 additions and 4 deletions

14
tccpp.c
View File

@ -1213,9 +1213,9 @@ static void tok_print(int *str)
ST_FUNC void parse_define(void)
{
Sym *s, *first, **ps;
int v, t, varg, is_vaargs, spc;
int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
TokenString str;
v = tok;
if (v < TOK_IDENT)
tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
@ -1254,8 +1254,13 @@ ST_FUNC void parse_define(void)
tok_str_new(&str);
spc = 2;
/* EOF testing necessary for '-D' handling */
ptok = 0;
macro_list_start = 1;
while (tok != TOK_LINEFEED && tok != TOK_EOF) {
/* remove spaces around ## and after '#' */
if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
tcc_error("'##' invalid at start of macro");
ptok = tok;
/* remove spaces around ## and after '#' */
if (TOK_TWOSHARPS == tok) {
if (1 == spc)
--str.len;
@ -1268,7 +1273,10 @@ ST_FUNC void parse_define(void)
tok_str_add2(&str, tok, &tokc);
skip:
next_nomacro_spc();
macro_list_start = 0;
}
if (ptok == TOK_TWOSHARPS)
tcc_error("'##' invalid at end of macro");
if (spc == 1)
--str.len; /* remove trailing space */
tok_str_add(&str, 0);

View File

@ -0,0 +1,2 @@
#define paste(A,B) ##A B
paste(x,y)

View File

@ -0,0 +1 @@
65_macro_concat_start.c:1: error: '##' invalid at start of macro

View File

@ -0,0 +1,2 @@
#define paste(A,B) A B##
paste(x,y)

View File

@ -0,0 +1 @@
66_macro_concat_end.c:2: error: '##' invalid at end of macro

View File

@ -79,7 +79,9 @@ TESTS = \
61_undefined_enum.test \
62_enumerator_redefinition.test \
63_local_enumerator_redefinition.test \
64_macro_nesting.test
64_macro_nesting.test \
65_macro_concat_start.test \
66_macro_concat_end.test
# 30_hanoi.test -- seg fault in the code, gcc as well
# 34_array_assignment.test -- array assignment is not in C standard