History

Prevent ## to appear at start or end of macro

Thomas Preud'homme committed 12 years ago

Commit a715d711 parent 91d4db66 files · +20 −4Browse files

Unified · Split

6 files
  1. tccpp.c +11 −3
  2. tests/tests2/65_macro_concat_start.c +2 −0
  3. tests/tests2/65_macro_concat_start.expect +1 −0
  4. tests/tests2/66_macro_concat_end.c +2 −0
  5. tests/tests2/66_macro_concat_end.expect +1 −0
  6. tests/tests2/Makefile +3 −1
tccpp.c+11−3
@@ -1213,9 +1213,9 @@ static void tok_print(int *str) · 1212 unmodified lines
12131213 ST_FUNC void parse_define(void)
12141214 {
12151215 Sym *s, *first, **ps;
1216− int v, t, varg, is_vaargs, spc;
1216+ int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
12171217 TokenString str;
1218−
1218+
12191219 v = tok;
12201220 if (v < TOK_IDENT)
12211221 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
@@ -1254,8 +1254,13 @@ ST_FUNC void parse_define(void) · 32 unmodified lines
12541254 tok_str_new(&str);
12551255 spc = 2;
12561256 /* EOF testing necessary for '-D' handling */
1257+ ptok = 0;
1258+ macro_list_start = 1;
12571259 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1258− /* remove spaces around ## and after '#' */
1260+ if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1261+ tcc_error("'##' invalid at start of macro");
1262+ ptok = tok;
1263+ /* remove spaces around ## and after '#' */
12591264 if (TOK_TWOSHARPS == tok) {
12601265 if (1 == spc)
12611266 --str.len;
@@ -1268,7 +1273,10 @@ ST_FUNC void parse_define(void) · 6 unmodified lines
12681273 tok_str_add2(&str, tok, &tokc);
12691274 skip:
12701275 next_nomacro_spc();
1276+ macro_list_start = 0;
12711277 }
1278+ if (ptok == TOK_TWOSHARPS)
1279+ tcc_error("'##' invalid at end of macro");
12721280 if (spc == 1)
12731281 --str.len; /* remove trailing space */
12741282 tok_str_add(&str, 0);
tests/tests2/65_macro_concat_start.c+2−0
@@ -0,0 +1,2 @@
1+#define paste(A,B) ##A B
2+paste(x,y)
tests/tests2/65_macro_concat_start.expect+1−0
@@ -0,0 +1 @@
1+65_macro_concat_start.c:1: error: '##' invalid at start of macro
tests/tests2/66_macro_concat_end.c+2−0
@@ -0,0 +1,2 @@
1+#define paste(A,B) A B##
2+paste(x,y)
tests/tests2/66_macro_concat_end.expect+1−0
@@ -0,0 +1 @@
1+66_macro_concat_end.c:2: error: '##' invalid at end of macro
tests/tests2/Makefile+3−1
@@ -79,7 +79,9 @@ TESTS = \ · 78 unmodified lines
7979 61_undefined_enum.test \
8080 62_enumerator_redefinition.test \
8181 63_local_enumerator_redefinition.test \
82− 64_macro_nesting.test
82+ 64_macro_nesting.test \
83+ 65_macro_concat_start.test \
84+ 66_macro_concat_end.test
8385
8486 # 30_hanoi.test -- seg fault in the code, gcc as well
8587 # 34_array_assignment.test -- array assignment is not in C standard

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t