tccpp: restore -D symbols for multiple sources

... also for built-in defines

The case:
    $ tcc -D FOO a.c b.c
with
    // a.c
    #undef FOO

    // b.c
    #ifndef FOO
    # error -D FOO has been lost
    #endif
master
grischka 2016-10-01 19:58:13 +02:00
parent cf32bb8812
commit 07e47b3dd6
2 changed files with 18 additions and 22 deletions

View File

@ -964,8 +964,8 @@ static int tcc_compile(TCCState *s1)
decl(VT_CONST);
if (tok != TOK_EOF)
expect("declaration");
gen_inline_functions();
check_vstack();
/* end of translation unit info */
if (s1->do_debug) {
put_stabs_r(NULL, N_SO, 0, 0,
@ -974,16 +974,10 @@ static int tcc_compile(TCCState *s1)
}
s1->error_set_jmp_enabled = 0;
/* reset define stack, but leave -Dsymbols (may be incorrect if
they are undefined) */
/* reset define stack, but keep -D and built-ins */
free_defines(define_start);
gen_inline_functions();
sym_pop(&global_stack, NULL);
sym_pop(&local_stack, NULL);
return s1->nb_errors != 0 ? -1 : 0;
}

30
tccpp.c
View File

@ -1282,22 +1282,24 @@ ST_INLN Sym *define_find(int v)
/* free define stack until top reaches 'b' */
ST_FUNC void free_defines(Sym *b)
{
Sym *top, *top1;
int v;
top = define_stack;
while (top != b) {
top1 = top->prev;
/* do not free args or predefined defines */
if (top->d)
tok_str_free(top->d);
v = top->v;
if (v >= TOK_IDENT && v < tok_ident)
table_ident[v - TOK_IDENT]->sym_define = NULL;
while (define_stack != b) {
Sym *top = define_stack;
define_stack = top->prev;
tok_str_free(top->d);
define_undef(top);
sym_free(top);
top = top1;
}
define_stack = b;
/* restore remaining (-D or predefined) symbols */
while (b) {
int v = b->v;
if (v >= TOK_IDENT && v < tok_ident) {
Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
if (!*d)
*d = b;
}
b = b->prev;
}
}
/* label lookup */