History

tccpp.c: restore whitespace after failed macro

This fixes test7 described in: http://lists.nongnu.org/archive/html/tinycc-devel/2015-05/msg00002.html Note that the current code still adds excessive forced blank characters to its output, so this patch might not change visible behaviour.

Philip committed 11 years ago

Commit 2f50cef1 parent a6e6a951 file · +35 −12Browse files

Unified · Split

tccpp.c+35−12
@@ -2877,19 +2877,26 @@ static int macro_subst_tok(TokenString *tok_str, · 2876 unmodified lines
28772877 tok_str_add2(tok_str, t1, &cval);
28782878 cstr_free(&cstr);
28792879 } else {
2880+ int mtok = tok;
28802881 int saved_parse_flags = parse_flags;
28812882 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
28822883
28832884 mstr = s->d;
28842885 mstr_allocated = 0;
28852886 if (s->type.t == MACRO_FUNC) {
2887+ TokenString ws_str; /* whitespace between macro name and
2888+ * argument list */
2889+ tok_str_new(&ws_str);
2890+ spc = 0;
28862891 /* NOTE: we do not use next_nomacro to avoid eating the
28872892 next token. XXX: find better solution */
28882893 redo:
28892894 if (macro_ptr) {
28902895 p = macro_ptr;
2891− while (is_space(t = *p) || TOK_LINEFEED == t)
2896+ while (is_space(t = *p) || TOK_LINEFEED == t) {
2897+ tok_str_add(&ws_str, t);
28922898 ++p;
2899+ }
28932900 if (t == 0 && can_read_stream) {
28942901 /* end of macro stream: we must look at the token
28952902 after in the file */
@@ -2910,7 +2917,7 @@ static int macro_subst_tok(TokenString *tok_str, · 14 unmodified lines
29102917 while (is_space(ch) || ch == '\n' || ch == '/')
29112918 {
29122919 if (ch == '/')
2913− {
2920+ {
29142921 int c;
29152922 uint8_t *p = file->buf_ptr;
29162923 PEEKC(c, p);
@@ -2921,18 +2928,29 @@ static int macro_subst_tok(TokenString *tok_str, · 4 unmodified lines
29212928 p = parse_line_comment(p);
29222929 file->buf_ptr = p - 1;
29232930 } else
2924− break;
2925− }
2926− cinp();
2927− }
2931+ break;
2932+ ch = ' ';
2933+ }
2934+ tok_str_add(&ws_str, ch);
2935+ cinp();
2936+ }
29282937 t = ch;
29292938 }
29302939 if (t != '(') {
2931− /* no macro subst */
2940+ /* not a macro substitution after all, restore the
2941+ * macro token plus all whitespace we've read.
2942+ * whitespace is intentionally not merged to preserve
2943+ * newlines. */
2944+ int i;
2945+ tok_str_add(tok_str, mtok);
2946+ for(i=0; i<ws_str.len; i++)
2947+ tok_str_add(tok_str, ws_str.str[i]);
2948+ tok_str_free(ws_str.str);
29322949 parse_flags = saved_parse_flags;
29332950 return -1;
2951+ } else {
2952+ tok_str_free(ws_str.str);
29342953 }
2935−
29362954 /* argument macro */
29372955 next_nomacro();
29382956 next_nomacro();
@@ -3101,7 +3119,7 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 162 unmodified lines
31013119 Sym *s;
31023120 int *macro_str1;
31033121 const int *ptr;
3104− int t, ret, spc;
3122+ int t, spc;
31053123 CValue cval;
31063124 struct macro_level ml;
31073125 int force_blank;
@@ -3145,14 +3163,12 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, · 37 unmodified lines
31453163 ml.prev = *can_read_stream, *can_read_stream = &ml;
31463164 macro_ptr = (int *)ptr;
31473165 tok = t;
3148− ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3166+ macro_subst_tok(tok_str, nested_list, s, can_read_stream);
31493167 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
31503168 ptr = (int *)macro_ptr;
31513169 macro_ptr = ml.p;
31523170 if (can_read_stream && *can_read_stream == &ml)
31533171 *can_read_stream = ml.prev;
3154− if (ret != 0)
3155− goto no_subst;
31563172 if (parse_flags & PARSE_FLAG_SPACES)
31573173 force_blank = 1;
31583174 } else {
@@ -3199,6 +3215,13 @@ ST_FUNC void next(void) · 40 unmodified lines
31993215 macro_ptr = str.str;
32003216 macro_ptr_allocated = str.str;
32013217 goto redo;
3218+ } else {
3219+ tok_str_add(&str, 0);
3220+ if (str.len > 1) {
3221+ macro_ptr = str.str + 1;
3222+ macro_ptr_allocated = str.str;
3223+ }
3224+ tok = str.str[0];
32023225 }
32033226 }
32043227 }

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