tccgen: scopes levels for local symbols (update 2)

allow
    typedef int xxx;
    typedef int xxx;
in the same scope as long as it is the same type
master
grischka 2016-05-06 08:32:54 +02:00
parent d48662d496
commit a94e8d439a
3 changed files with 21 additions and 17 deletions

View File

@ -2927,13 +2927,10 @@ static void struct_decl(CType *type, AttributeDef *ad, int u)
if (v < TOK_IDENT) if (v < TOK_IDENT)
expect("struct/union/enum name"); expect("struct/union/enum name");
s = struct_find(v); s = struct_find(v);
if (s && s->type.t == a) { if (s && (s->scope == local_scope || (tok != '{' && tok != ';'))) {
if (0 == local_scope) if (s->type.t != a)
goto do_decl; /* compatibility with past behavior */ tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
if (tok != '{' && tok != ';') goto do_decl;
goto do_decl; /* variable declaration: 'struct s x;' */
if (s->scope == local_scope && (s->c == -1 || tok != '{'))
goto do_decl; /* at least one must be incomplete type */
} }
} else { } else {
v = anon_sym++; v = anon_sym++;
@ -3435,8 +3432,6 @@ static void post_type(CType *type, AttributeDef *ad)
plast = &first; plast = &first;
arg_size = 0; arg_size = 0;
if (tok != ')') { if (tok != ')') {
int ls = local_scope;
local_scope = 1; /* for struct decl inside function params */
for(;;) { for(;;) {
/* read param name and compute offset */ /* read param name and compute offset */
if (l != FUNC_OLD) { if (l != FUNC_OLD) {
@ -3476,7 +3471,6 @@ static void post_type(CType *type, AttributeDef *ad)
break; break;
} }
} }
local_scope = ls;
} }
/* if no parameters, then old type prototype */ /* if no parameters, then old type prototype */
if (l == 0) if (l == 0)
@ -6434,7 +6428,16 @@ static int decl0(int l, int is_for_loop_init)
if (btype.t & VT_TYPEDEF) { if (btype.t & VT_TYPEDEF) {
/* save typedefed type */ /* save typedefed type */
/* XXX: test storage specifiers ? */ /* XXX: test storage specifiers ? */
sym = sym_push(v, &type, 0, 0); sym = sym_find(v);
if (sym && sym->scope == local_scope) {
if (!is_compatible_types(&sym->type, &type)
|| !(sym->type.t & VT_TYPEDEF))
tcc_error("incompatible redefinition of '%s'",
get_tok_str(v, NULL));
sym->type = type;
} else {
sym = sym_push(v, &type, 0, 0);
}
sym->a = ad.a; sym->a = ad.a;
sym->type.t |= VT_TYPEDEF; sym->type.t |= VT_TYPEDEF;
} else { } else {

View File

@ -250,6 +250,7 @@ cache: tcc_g
# clean # clean
clean: clean:
$(MAKE) -C tests2 $@ $(MAKE) -C tests2 $@
$(MAKE) -C pp $@
rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \ rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \
*-cc *-tcc *.exe \ *-cc *-tcc *.exe \
hello libtcc_test vla_test tcctest[1234] ex? tcc_g hello libtcc_test vla_test tcctest[1234] ex? tcc_g

View File

@ -8,16 +8,18 @@ TESTS += $(patsubst %.S,%.test,$(wildcard *.S))
all test : $(sort $(TESTS)) all test : $(sort $(TESTS))
DIFF_OPTS = -Nu -b -B -I "^\#"
%.test: %.c %.expect %.test: %.c %.expect
@echo PPTest $* ... @echo PPTest $* ...
-@$(TCC) -E -P $< >$*.output 2>&1 ; \ -@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \ diff $(DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output && rm -f $*.output
%.test: %.S %.expect %.test: %.S %.expect
@echo PPTest $* ... @echo PPTest $* ...
-@$(TCC) -E -P $< >$*.output 2>&1 ; \ -@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \ diff $(DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output && rm -f $*.output
# automatically generate .expect files with gcc: # automatically generate .expect files with gcc:
@ -33,10 +35,8 @@ all test : $(sort $(TESTS))
clean: clean:
rm -vf *.output rm -vf *.output
# 02.test : EXTRA_DIFF_OPTS = -w 02.test : DIFF_OPTS += -w
# 03.test : EXTRA_DIFF_OPTS = -w 15.test : DIFF_OPTS += -w
# 04.test : EXTRA_DIFF_OPTS = -w
# 10.test : EXTRA_DIFF_OPTS = -w
# diff options: # diff options:
# -b ighore space changes # -b ighore space changes