Error out when two local variable with same name are defined in the same scope. This fixes bug #15597 in savannah's BTS.
Unified · Split
| @@ -50,6 +50,7 @@ ST_DATA int nb_sym_pools; · 49 unmodified lines | ||
| 50 | 50 | |
| 51 | 51 | ST_DATA Sym *global_stack; |
| 52 | 52 | ST_DATA Sym *local_stack; |
| 53 | +ST_DATA Sym *scope_stack_bottom; | |
| 53 | 54 | ST_DATA Sym *define_stack; |
| 54 | 55 | ST_DATA Sym *global_label_stack; |
| 55 | 56 | ST_DATA Sym *local_label_stack; |
| @@ -147,6 +148,13 @@ ST_INLN void sym_free(Sym *sym) · 91 unmodified lines | ||
| 147 | 148 | ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c) |
| 148 | 149 | { |
| 149 | 150 | Sym *s; |
| 151 | + if (ps == &local_stack) { | |
| 152 | + for (s = *ps; s && s != scope_stack_bottom; s = s->prev) | |
| 153 | + if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v) | |
| 154 | + tcc_error("incompatible types for redefinition of '%s'", | |
| 155 | + get_tok_str(v, NULL)); | |
| 156 | + } | |
| 157 | + s = *ps; | |
| 150 | 158 | s = sym_malloc(); |
| 151 | 159 | s->asm_label = NULL; |
| 152 | 160 | s->v = v; |
| @@ -4347,7 +4355,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 4194 unmodified lines | ||
| 4347 | 4355 | int case_reg, int is_expr) |
| 4348 | 4356 | { |
| 4349 | 4357 | int a, b, c, d; |
| 4350 | − Sym *s; | |
| 4358 | + Sym *s, *frame_bottom; | |
| 4351 | 4359 | |
| 4352 | 4360 | /* generate line number info */ |
| 4353 | 4361 | if (tcc_state->do_debug && |
| @@ -4398,6 +4406,9 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 44 unmodified lines | ||
| 4398 | 4406 | next(); |
| 4399 | 4407 | /* record local declaration stack position */ |
| 4400 | 4408 | s = local_stack; |
| 4409 | + frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0); | |
| 4410 | + frame_bottom->next = scope_stack_bottom; | |
| 4411 | + scope_stack_bottom = frame_bottom; | |
| 4401 | 4412 | llabel = local_label_stack; |
| 4402 | 4413 | /* handle local labels declarations */ |
| 4403 | 4414 | if (tok == TOK_LABEL) { |
| @@ -4440,6 +4451,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 36 unmodified lines | ||
| 4440 | 4451 | } |
| 4441 | 4452 | } |
| 4442 | 4453 | /* pop locally defined symbols */ |
| 4454 | + scope_stack_bottom = scope_stack_bottom->next; | |
| 4443 | 4455 | sym_pop(&local_stack, s); |
| 4444 | 4456 | next(); |
| 4445 | 4457 | } else if (tok == TOK_RETURN) { |
| @@ -4510,6 +4522,9 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 64 unmodified lines | ||
| 4510 | 4522 | next(); |
| 4511 | 4523 | skip('('); |
| 4512 | 4524 | s = local_stack; |
| 4525 | + frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0); | |
| 4526 | + frame_bottom->next = scope_stack_bottom; | |
| 4527 | + scope_stack_bottom = frame_bottom; | |
| 4513 | 4528 | if (tok != ';') { |
| 4514 | 4529 | /* c99 for-loop init decl? */ |
| 4515 | 4530 | if (!decl0(VT_LOCAL, 1)) { |
| @@ -4541,6 +4556,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, · 25 unmodified lines | ||
| 4541 | 4556 | gjmp_addr(c); |
| 4542 | 4557 | gsym(a); |
| 4543 | 4558 | gsym_addr(b, c); |
| 4559 | + scope_stack_bottom = scope_stack_bottom->next; | |
| 4544 | 4560 | sym_pop(&local_stack, s); |
| 4545 | 4561 | } else |
| 4546 | 4562 | if (tok == TOK_DO) { |
| @@ -5509,7 +5525,9 @@ static void gen_function(Sym *sym) · 962 unmodified lines | ||
| 5509 | 5525 | gfunc_epilog(); |
| 5510 | 5526 | cur_text_section->data_offset = ind; |
| 5511 | 5527 | label_pop(&global_label_stack, NULL); |
| 5512 | − sym_pop(&local_stack, NULL); /* reset local stack */ | |
| 5528 | + /* reset local stack */ | |
| 5529 | + scope_stack_bottom = NULL; | |
| 5530 | + sym_pop(&local_stack, NULL); | |
| 5513 | 5531 | /* end of function */ |
| 5514 | 5532 | /* patch symbol size */ |
| 5515 | 5533 | ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size = |
| @@ -5577,7 +5595,7 @@ static int decl0(int l, int is_for_loop_init) · 61 unmodified lines | ||
| 5577 | 5595 | CType type, btype; |
| 5578 | 5596 | Sym *sym; |
| 5579 | 5597 | AttributeDef ad; |
| 5580 | − | |
| 5598 | + | |
| 5581 | 5599 | while (1) { |
| 5582 | 5600 | if (!parse_btype(&btype, &ad)) { |
| 5583 | 5601 | if (is_for_loop_init) |