diff options
| author | Michael Matz <matz@suse.de> | 2017-07-09 05:30:47 +0200 |
|---|---|---|
| committer | Michael Matz <matz@suse.de> | 2017-07-09 05:30:47 +0200 |
| commit | 824dcebe59f52a686b003214c1af0633cf5fe89e (patch) | |
| tree | c3f0e9be1b79309bd2e02932c1ebe52b9609e8b2 /tccpp.c | |
| parent | 1f3d3957c4aa4d403b07245fe4f0a49ac8fefcc4 (diff) | |
| download | tinycc-824dcebe59f52a686b003214c1af0633cf5fe89e.tar.gz tinycc-824dcebe59f52a686b003214c1af0633cf5fe89e.tar.bz2 | |
tccpp: Implement __COUNTER__
This requires one more change in how macro arguments are expanded:
the standard requires that macro args are expanded before substituting
into the replacement list. This implies expanding them only once
even when they occur multiple times in the list. TCC expanded
them repeatedly in that case. Without __COUNTER__ that's harmless.
So, simply always expand arguments (when used without # and ##) once
and store the resulting tokens.
Diffstat (limited to 'tccpp.c')
| -rw-r--r-- | tccpp.c | 39 |
1 files changed, 28 insertions, 11 deletions
@@ -2980,18 +2980,29 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) str.len--; goto add_var; } - } else { - for(;;) { - int t1; - TOK_GET(&t1, &st, &cval); - if (t1 <= 0) - break; - tok_str_add2(&str, t1, &cval); - } } } else { add_var: - macro_subst(&str, nested_list, st); + if (!s->next) { + /* Expand arguments tokens and store them. In most + cases we could also re-expand each argument if + used multiple times, but not if the argument + contains the __COUNTER__ macro. */ + TokenString str2; + sym_push2(&s->next, s->v, s->type.t, 0); + tok_str_new(&str2); + macro_subst(&str2, nested_list, st); + tok_str_add(&str2, 0); + s->next->d = str2.str; + } + st = s->next->d; + } + for(;;) { + int t2; + TOK_GET(&t2, &st, &cval); + if (t2 <= 0) + break; + tok_str_add2(&str, t2, &cval); } if (str.len == l0) /* expanded to empty string */ tok_str_add(&str, TOK_PLCHLDR); @@ -3180,11 +3191,13 @@ static int macro_subst_tok( CValue cval; CString cstr; char buf[32]; + static int counter; /* if symbol is a macro, prepare substitution */ /* special macros */ - if (tok == TOK___LINE__) { - snprintf(buf, sizeof(buf), "%d", file->line_num); + if (tok == TOK___LINE__ || tok == TOK___COUNTER__) { + t = tok == TOK___LINE__ ? file->line_num : counter++; + snprintf(buf, sizeof(buf), "%d", t); cstrval = buf; t1 = TOK_PPNUM; goto add_cstr1; @@ -3316,6 +3329,10 @@ static int macro_subst_tok( while (sa) { sa1 = sa->prev; tok_str_free_str(sa->d); + if (sa->next) { + tok_str_free_str(sa->next->d); + sym_free(sa->next); + } sym_free(sa); sa = sa1; } |
