History

tccgen.c: local extern decls: copy s->ref for VT_PTR too

This fixes the issue int main() { extern char *x; } void main1() { extern char *x; } t2.c:5: error: incompatible types for redefinition of 'x' (reported by Giovanni Mascellani 2019/07/16)

grischka committed 7 years ago

Commit 7b8799e1 parent 92985554 files · +42 −14Browse files

Unified · Split

4 files
  1. Makefile +1 −1
  2. tccgen.c +17 −13
  3. tests/tests2/60_errors_and_warnings.c +22 −0
  4. tests/tests2/60_errors_and_warnings.expect +2 −0
Makefile+1−1
@@ -98,7 +98,7 @@ TCC_X += riscv64 · 97 unmodified lines
9898
9999 # cross libtcc1.a targets to build
100100 LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince
101−LIBTCC1_X = riscv64
101+LIBTCC1_X += riscv64
102102
103103 PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF))
104104 LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),$X-libtcc1.a)
tccgen.c+17−13
@@ -1096,18 +1096,24 @@ static Sym *sym_copy(Sym *s0, Sym **ps) · 1095 unmodified lines
10961096 return s;
10971097 }
10981098
1099−/* copy a list of syms */
1100−static void sym_copy_ref(Sym *s0, Sym **ps)
1101−{
1102− Sym *s, **sp = &s0->type.ref;
1103− for (s = *sp, *sp = NULL; s; s = s->next)
1104− sp = &(*sp = sym_copy(s, ps))->next;
1099+/* copy s->type.ref to stack 'ps' for VT_FUNC and VT_PTR */
1100+static void sym_copy_ref(Sym *s, Sym **ps)
1101+{
1102+ int bt = s->type.t & VT_BTYPE;
1103+ if (bt == VT_FUNC || bt == VT_PTR) {
1104+ Sym **sp = &s->type.ref;
1105+ for (s = *sp, *sp = NULL; s; s = s->next) {
1106+ Sym *s2 = sym_copy(s, ps);
1107+ sp = &(*sp = s2)->next;
1108+ sym_copy_ref(s2, ps);
1109+ }
1110+ }
11051111 }
11061112
11071113 /* define a new external reference to a symbol 'v' */
11081114 static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
11091115 {
1110− Sym *s; int bt;
1116+ Sym *s;
11111117
11121118 /* look for global symbol */
11131119 s = sym_find(v);
@@ -1121,16 +1127,14 @@ static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad) · 7 unmodified lines
11211127 s->a = ad->a;
11221128 s->asm_label = ad->asm_label;
11231129 s->type.ref = type->ref;
1124− bt = s->type.t & (VT_BTYPE|VT_ARRAY);
1125− /* copy type to the global stack also */
1126− if (local_scope && (bt == VT_FUNC || (bt & VT_ARRAY)))
1130+ /* copy type to the global stack */
1131+ if (local_stack)
11271132 sym_copy_ref(s, &global_stack);
11281133 } else {
11291134 patch_storage(s, ad, type);
1130− bt = s->type.t & VT_BTYPE;
11311135 }
1132− /* push variables to local scope if any */
1133− if (local_stack && bt != VT_FUNC)
1136+ /* push variables on local_stack if any */
1137+ if (local_stack && (s->type.t & VT_BTYPE) != VT_FUNC)
11341138 s = sym_copy(s, &local_stack);
11351139 return s;
11361140 }
tests/tests2/60_errors_and_warnings.c+22−0
@@ -273,5 +273,27 @@ int main () · 272 unmodified lines
273273 int xxx[1] = {1};
274274 int bar() { P(3, xxx[0]); return 0; }
275275
276+#elif defined test_var_4
277+struct yyy { int y; };
278+struct zzz;
279+void f1() {
280+ extern char *x;
281+ extern char **xx;
282+ extern struct yyy y;
283+ extern struct yyy *yy;
284+ extern struct zzz z;
285+ extern struct zzz *zz;
286+}
287+void f2() {
288+ extern char *x;
289+ extern char **xx;
290+ extern struct yyy y;
291+ extern struct yyy *yy;
292+ extern struct zzz z;
293+ extern struct zzz *zz;
294+}
295+struct yyy y, *yy;
296+struct zzz { int z; } z, *zz;
297+
276298 /******************************************************************/
277299 #endif
tests/tests2/60_errors_and_warnings.expect+2−0
@@ -132,3 +132,5 @@ bar : 3 ; 3 · 131 unmodified lines
132132
133133 [test_var_3]
134134 60_errors_and_warnings.c:273: error: incompatible types for redefinition of 'xxx'
135+
136+[test_var_4]

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