History

tccpp.c: correct # stringification

Fix handling of escape characters, spaces, and line feeds in macros or macro arguments that might yet be subject to # stringification. Should this be an -f option? I think memory usage increases only very slightly (in particular, while line feeds, stray \s, and spaces are preserved, comments are not), so it's probably not worth it to make it one. Note that macro_subst now checks for stray \s which are still left in the input stream after macro substitution, if desired. This patch depends on the previous patch, so if you revert that, please revert this patch, too. See http://lists.nongnu.org/archive/html/tinycc-devel/2015-05/msg00002.html

Philip committed 11 years ago

Commit a6e6a951 parent 0877ba71 file · +23 −4Browse files

Unified · Split

tccpp.c+23−4
@@ -1254,6 +1254,7 @@ ST_FUNC void parse_define(void) · 1253 unmodified lines
12541254 {
12551255 Sym *s, *first, **ps;
12561256 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1257+ int saved_parse_flags;
12571258 TokenString str;
12581259
12591260 v = tok;
@@ -1295,6 +1296,8 @@ ST_FUNC void parse_define(void) · 35 unmodified lines
12951296 /* EOF testing necessary for '-D' handling */
12961297 ptok = 0;
12971298 macro_list_start = 1;
1299+ saved_parse_flags = parse_flags;
1300+ parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
12981301 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
12991302 if (macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
13001303 tcc_error("'##' invalid at start of macro");
@@ -1314,6 +1317,7 @@ ST_FUNC void parse_define(void) · 13 unmodified lines
13141317 skip:
13151318 next_nomacro_spc();
13161319 }
1320+ parse_flags = saved_parse_flags;
13171321 if (ptok == TOK_TWOSHARPS)
13181322 tcc_error("'##' invalid at end of macro");
13191323 if (spc == 1)
@@ -2729,6 +2733,7 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 1409 unmodified lines
27292733 CValue cval;
27302734 TokenString str;
27312735 CString cstr;
2736+ CString cstr2;
27322737
27332738 tok_str_new(&str);
27342739 last_tok = 0;
@@ -2757,9 +2762,13 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) · 22 unmodified lines
27572762 printf("stringize: %s\n", (char *)cstr.data);
27582763 #endif
27592764 /* add string */
2760− cval.cstr = &cstr;
2761− tok_str_add2(&str, TOK_STR, &cval);
2765+ cstr_new(&cstr2);
2766+ /* emulate GCC behaviour and parse escapes in the token string */
2767+ parse_escape_string(&cstr2, cstr.data, 0);
27622768 cstr_free(&cstr);
2769+ cval.cstr = &cstr2;
2770+ tok_str_add2(&str, TOK_STR, &cval);
2771+ cstr_free(cval.cstr);
27632772 } else {
27642773 tok_str_add2(&str, t, &cval);
27652774 }
@@ -2868,6 +2877,9 @@ static int macro_subst_tok(TokenString *tok_str, · 102 unmodified lines
28682877 tok_str_add2(tok_str, t1, &cval);
28692878 cstr_free(&cstr);
28702879 } else {
2880+ int saved_parse_flags = parse_flags;
2881+ parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
2882+
28712883 mstr = s->d;
28722884 mstr_allocated = 0;
28732885 if (s->type.t == MACRO_FUNC) {
@@ -2915,9 +2927,12 @@ static int macro_subst_tok(TokenString *tok_str, · 41 unmodified lines
29152927 }
29162928 t = ch;
29172929 }
2918− if (t != '(') /* no macro subst */
2930+ if (t != '(') {
2931+ /* no macro subst */
2932+ parse_flags = saved_parse_flags;
29192933 return -1;
2920−
2934+ }
2935+
29212936 /* argument macro */
29222937 next_nomacro();
29232938 next_nomacro();
@@ -2986,6 +3001,7 @@ static int macro_subst_tok(TokenString *tok_str, · 62 unmodified lines
29863001 mstr_allocated = 1;
29873002 }
29883003 sym_push2(nested_list, s->v, 0, 0);
3004+ parse_flags = saved_parse_flags;
29893005 macro_subst(tok_str, nested_list, mstr, can_read_stream);
29903006 /* pop nested defined symbol */
29913007 sa1 = *nested_list;
@@ -3107,6 +3123,9 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 115 unmodified lines
31073123 TOK_GET(&t, &ptr, &cval);
31083124 if (t == 0)
31093125 break;
3126+ if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) {
3127+ tcc_error("stray '\\' in program");
3128+ }
31103129 if (t == TOK_NOSUBST) {
31113130 /* following token has already been subst'd. just copy it on */
31123131 tok_str_add2(tok_str, TOK_NOSUBST, NULL);

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