History

work on local extern declarations

Example: int a = 1; void f(void) { int a = 2; { extern int a; // = 1 !! .... To get this (more) correctly there is a new function to copy syms between local to global stacks. Also, this patch changes the meaning of VT_EXTERN back to the simpler and IMO more useful notion of DECLARED but not (yet) DEFINED. and that for both variables and functions. That is, VT_EXTERN in tcc doesn't have to do with the keyword 'extern' necessarily. Also this patch does allow int x[]; as alias for extern int x[]; (as do gcc and msvc)

grischka committed 7 years ago

Commit 85690481 parent cbbba014 files · +214 −86Browse files

Unified · Split

4 files
  1. tccasm.c +1 −1
  2. tccgen.c +103 −82
  3. tests/tests2/60_errors_and_warnings.c +77 −1
  4. tests/tests2/60_errors_and_warnings.expect +33 −2
tccasm.c+1−1
@@ -38,7 +38,7 @@ static Sym* asm_new_label1(TCCState *s1, int label, int is_local, int sh_num, in · 37 unmodified lines
3838 static Sym *asm_label_find(int v)
3939 {
4040 Sym *sym = sym_find(v);
41− while (sym && sym->sym_scope)
41+ while (sym && sym->sym_scope && !(sym->type.t & VT_STATIC))
4242 sym = sym->prev_tok;
4343 return sym;
4444 }
tccgen.c+103−82
@@ -263,6 +263,7 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 262 unmodified lines
263263 section_sym = 0;
264264 const_wanted = 0;
265265 nocode_wanted = 0x80000000;
266+ local_scope = 0;
266267
267268 /* define some often used types */
268269 int_type.t = VT_INT;
@@ -325,8 +326,7 @@ ST_FUNC void update_storage(Sym *sym) · 56 unmodified lines
325326 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
326327 | sym->a.visibility;
327328
328− if ((sym->type.t & VT_STATIC)
329− || (sym->type.t & (VT_EXTERN | VT_INLINE)) == VT_INLINE)
329+ if (sym->type.t & (VT_STATIC | VT_INLINE))
330330 sym_bind = STB_LOCAL;
331331 else if (sym->a.weak)
332332 sym_bind = STB_WEAK;
@@ -408,7 +408,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 75 unmodified lines
408408 } else {
409409 sym_type = STT_OBJECT;
410410 }
411− if ((t & VT_STATIC) || (t & (VT_EXTERN | VT_INLINE)) == VT_INLINE)
411+ if (t & (VT_STATIC | VT_INLINE))
412412 sym_bind = STB_LOCAL;
413413 else
414414 sym_bind = STB_GLOBAL;
@@ -927,8 +927,7 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 512 unmodified lines
927927 /* Merge some type attributes. */
928928 static void patch_type(Sym *sym, CType *type)
929929 {
930− if ((!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t))
931− && (type->t & VT_BTYPE) != VT_FUNC) {
930+ if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
932931 if (!(sym->type.t & VT_EXTERN))
933932 tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
934933 sym->type.t &= ~VT_EXTERN;
@@ -955,29 +954,30 @@ static void patch_type(Sym *sym, CType *type) · 20 unmodified lines
955954 tcc_warning("static storage ignored for redefinition of '%s'",
956955 get_tok_str(sym->v, NULL));
957956
958− /* Force external definition if unequal inline specifier
959− or an explicit extern one. */
960− if ((sym->type.t | type->t) & VT_STATIC) {
961− type->t |= sym->type.t & VT_INLINE;
962− sym->type.t |= type->t & VT_INLINE;
963− } else if (((type->t & VT_INLINE) != (sym->type.t & VT_INLINE)
964− || (type->t | sym->type.t) & VT_EXTERN)
965− && !static_proto) {
966− type->t &= ~VT_INLINE;
967− sym->type.t &= ~VT_INLINE;
957+ /* set 'inline' if both agree or if one has static */
958+ if ((type->t | sym->type.t) & VT_INLINE) {
959+ if (!((type->t ^ sym->type.t) & VT_INLINE)
960+ || ((type->t | sym->type.t) & VT_STATIC))
961+ static_proto |= VT_INLINE;
968962 }
963+
969964 if (0 == (type->t & VT_EXTERN)) {
970− /* put complete type, use static from prototype, but don't
971− overwrite type.ref, it might contain parameter names */
972− sym->type.t = (type->t & ~VT_STATIC) | static_proto;
965+ /* put complete type, use static from prototype */
966+ sym->type.t = (type->t & ~(VT_STATIC|VT_INLINE)) | static_proto;
967+ sym->type.ref = type->ref;
968+ } else {
969+ sym->type.t &= ~VT_INLINE | static_proto;
970+ }
971+
972+ if (sym->type.ref->f.func_type == FUNC_OLD
973+ && type->ref->f.func_type != FUNC_OLD) {
974+ sym->type.ref = type->ref;
973975 }
976+
974977 } else {
975978 if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
976979 /* set array size if it was omitted in extern declaration */
977− if (sym->type.ref->c < 0)
978− sym->type.ref->c = type->ref->c;
979− else if (sym->type.ref->c != type->ref->c)
980− tcc_error("conflicting type for '%s'", get_tok_str(sym->v, NULL));
980+ sym->type.ref->c = type->ref->c;
981981 }
982982 if ((type->t ^ sym->type.t) & VT_STATIC)
983983 tcc_warning("storage mismatch for redefinition of '%s'",
@@ -985,7 +985,6 @@ static void patch_type(Sym *sym, CType *type) · 1 unmodified line
985985 }
986986 }
987987
988−
989988 /* Merge some storage attributes. */
990989 static void patch_storage(Sym *sym, AttributeDef *ad, CType *type)
991990 {
@@ -1003,29 +1002,55 @@ static void patch_storage(Sym *sym, AttributeDef *ad, CType *type) · 11 unmodified lines
10031002 update_storage(sym);
10041003 }
10051004
1005+/* copy sym to other stack */
1006+static Sym *sym_copy(Sym *s0, Sym **ps)
1007+{
1008+ Sym *s;
1009+ s = sym_malloc(), *s = *s0;
1010+ s->prev = *ps, *ps = s;
1011+ if (s->v < SYM_FIRST_ANOM) {
1012+ ps = &table_ident[s->v - TOK_IDENT]->sym_identifier;
1013+ s->prev_tok = *ps, *ps = s;
1014+ }
1015+ return s;
1016+}
1017+
1018+/* copy a list of syms */
1019+static void sym_copy_ref(Sym *s0, Sym **ps)
1020+{
1021+ Sym *s, **sp = &s0->type.ref;
1022+ for (s = *sp, *sp = NULL; s; s = s->next)
1023+ sp = &(*sp = sym_copy(s, ps))->next;
1024+}
1025+
10061026 /* define a new external reference to a symbol 'v' */
10071027 static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
10081028 {
1009− Sym *s;
1029+ Sym *s; int bt;
1030+
1031+ /* look for global symbol */
10101032 s = sym_find(v);
1011− if (!s || (!IS_ASM_SYM(s) && !(s->type.t & VT_EXTERN)
1012− && (!(type->t & VT_EXTERN) || s->sym_scope)
1013− && (s->type.t & VT_BTYPE) != VT_FUNC)) {
1014− if (s && !is_compatible_types(&s->type, type))
1015− tcc_error("conflicting types for '%s'", get_tok_str(s->v, NULL));
1033+ while (s && s->sym_scope)
1034+ s = s->prev_tok;
1035+
1036+ if (!s) {
10161037 /* push forward reference */
1017− s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
1038+ s = global_identifier_push(v, type->t, 0);
1039+ s->r |= r;
10181040 s->a = ad->a;
10191041 s->asm_label = ad->asm_label;
1020− s->sym_scope = 0;
1042+ s->type.ref = type->ref;
1043+ bt = s->type.t & (VT_BTYPE|VT_ARRAY);
1044+ /* copy type to the global stack also */
1045+ if (local_scope && (bt == VT_FUNC || (bt & VT_ARRAY)))
1046+ sym_copy_ref(s, &global_stack);
10211047 } else {
1022− if (s->type.ref == func_old_type.ref) {
1023− s->type.ref = type->ref;
1024− s->r = r | VT_CONST | VT_SYM;
1025− s->type.t |= VT_EXTERN;
1026− }
10271048 patch_storage(s, ad, type);
1049+ bt = s->type.t & VT_BTYPE;
10281050 }
1051+ /* push variables to local scope if any */
1052+ if (local_stack && bt != VT_FUNC)
1053+ s = sym_copy(s, &local_stack);
10291054 return s;
10301055 }
10311056
@@ -2880,27 +2905,29 @@ static int is_compatible_func(CType *type1, CType *type2) · 1848 unmodified lines
28802905
28812906 s1 = type1->ref;
28822907 s2 = type2->ref;
2883− if (!is_compatible_types(&s1->type, &s2->type))
2884− return 0;
2885− /* check func_call */
28862908 if (s1->f.func_call != s2->f.func_call)
28872909 return 0;
2888− /* XXX: not complete */
2889− if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD)
2890− return 1;
2891− if (s1->f.func_type != s2->f.func_type)
2910+ if (s1->f.func_type != s2->f.func_type
2911+ && s1->f.func_type != FUNC_OLD
2912+ && s2->f.func_type != FUNC_OLD)
28922913 return 0;
2893− while (s1 != NULL) {
2894− if (s2 == NULL)
2895− return 0;
2914+ /* we should check the function return type for FUNC_OLD too
2915+ but that causes problems with the internally used support
2916+ functions such as TOK_memmove */
2917+ if (s1->f.func_type == FUNC_OLD && !s1->next)
2918+ return 1;
2919+ if (s2->f.func_type == FUNC_OLD && !s2->next)
2920+ return 1;
2921+ for (;;) {
28962922 if (!is_compatible_unqualified_types(&s1->type, &s2->type))
28972923 return 0;
28982924 s1 = s1->next;
28992925 s2 = s2->next;
2926+ if (!s1)
2927+ return !s2;
2928+ if (!s2)
2929+ return 0;
29002930 }
2901− if (s2)
2902− return 0;
2903− return 1;
29042931 }
29052932
29062933 /* return true if type1 and type2 are the same. If unqualified is
@@ -2926,15 +2953,19 @@ static int compare_types(CType *type1, CType *type2, int unqualified) · 19 unmodified lines
29262953 /* XXX: bitfields ? */
29272954 if (t1 != t2)
29282955 return 0;
2956+
2957+ if ((t1 & VT_ARRAY)
2958+ && !(type1->ref->c < 0
2959+ || type2->ref->c < 0
2960+ || type1->ref->c == type2->ref->c))
2961+ return 0;
2962+
29292963 /* test more complicated cases */
2930− bt1 = t1 & (VT_BTYPE | VT_ARRAY);
2964+ bt1 = t1 & VT_BTYPE;
29312965 if (bt1 == VT_PTR) {
29322966 type1 = pointed_type(type1);
29332967 type2 = pointed_type(type2);
29342968 return is_compatible_types(type1, type2);
2935− } else if (bt1 & VT_ARRAY) {
2936− return type1->ref->c < 0 || type2->ref->c < 0
2937− || type1->ref->c == type2->ref->c;
29382969 } else if (bt1 == VT_STRUCT) {
29392970 return (type1->ref == type2->ref);
29402971 } else if (bt1 == VT_FUNC) {
@@ -7285,9 +7316,6 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 4344 unmodified lines
72857316 sym = sym_push(v, type, r | VT_SYM, 0);
72867317 patch_storage(sym, ad, NULL);
72877318 }
7288− /* Local statics have a scope until now (for
7289− warnings), remove it here. */
7290− sym->sym_scope = 0;
72917319 /* update symbol definition */
72927320 put_extern_sym(sym, sec, addr, size);
72937321 } else {
@@ -7431,7 +7459,7 @@ static void gen_inline_functions(TCCState *s) · 137 unmodified lines
74317459 for (i = 0; i < s->nb_inline_fns; ++i) {
74327460 fn = s->inline_fns[i];
74337461 sym = fn->sym;
7434− if (sym && (sym->c || !(sym->type.t & (VT_INLINE | VT_STATIC)) )) {
7462+ if (sym && (sym->c || !(sym->type.t & VT_INLINE))) {
74357463 /* the function was used or forced (and then not internal):
74367464 generate its code and convert it to a normal function */
74377465 fn->sym = NULL;
@@ -7544,22 +7572,26 @@ static int decl0(int l, int is_for_loop_init, Sym *func_sym) · 106 unmodified lines
75447572 }
75457573 #endif
75467574 if ((type.t & VT_BTYPE) == VT_FUNC) {
7547− if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
7548− tcc_error("function without file scope cannot be static");
7549− }
75507575 /* if old style function prototype, we accept a
75517576 declaration list */
75527577 sym = type.ref;
75537578 if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
75547579 decl0(VT_CMP, 0, sym);
7580+ /* always compile 'extern inline' */
7581+ if (type.t & VT_EXTERN)
7582+ type.t &= ~VT_INLINE;
75557583 }
75567584
75577585 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
75587586 ad.asm_label = asm_label_instr();
75597587 /* parse one last attribute list, after asm label */
75607588 parse_attribute(&ad);
7589+ #if 0
7590+ /* gcc does not allow __asm__("label") with function definition,
7591+ but why not ... */
75617592 if (tok == '{')
75627593 expect(";");
7594+ #endif
75637595 }
75647596
75657597 #ifdef TCC_TARGET_PE
@@ -7591,18 +7623,12 @@ static int decl0(int l, int is_for_loop_init, Sym *func_sym) · 25 unmodified lines
75917623 }
75927624
75937625 /* put function symbol */
7626+ type.t &= ~VT_EXTERN;
75947627 sym = external_sym(v, &type, 0, &ad);
7595− /* This is the def, so overwrite any other parameter names
7596− we got from prototypes. */
7597− sym->type.ref = type.ref;
7598− if (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF
7599− && !(elfsym(sym)->st_other & ST_ASM_SET))
7600− tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
7601−
76027628 /* static inline functions are just recorded as a kind
76037629 of macro. Their code will be emitted at the end of
76047630 the compilation unit only if they are used */
7605− if ((sym->type.t & (VT_INLINE | VT_EXTERN)) == VT_INLINE) {
7631+ if (sym->type.t & VT_INLINE) {
76067632 struct InlineFunc *fn;
76077633 const char *filename;
76087634
@@ -7669,16 +7695,15 @@ found: · 60 unmodified lines
76697695 has_init = (tok == '=');
76707696 if (has_init && (type.t & VT_VLA))
76717697 tcc_error("variable length array cannot be initialized");
7672− if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) ||
7673− ((type.t & VT_BTYPE) == VT_FUNC) ||
7674− ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
7675− !has_init && l == VT_CONST && type.ref->c < 0)) {
7698+ if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST))
7699+ || (type.t & VT_BTYPE) == VT_FUNC
7700+ /* as with GCC, uninitialized global arrays with no size
7701+ are considered extern: */
7702+ || ((type.t & VT_ARRAY) && !has_init
7703+ && l == VT_CONST && type.ref->c < 0)
7704+ ) {
76767705 /* external variable or function */
7677− /* NOTE: as GCC, uninitialized global static
7678− arrays of null size are considered as
7679− extern */
7680− if ((type.t & VT_BTYPE) != VT_FUNC)
7681− type.t |= VT_EXTERN;
7706+ type.t |= VT_EXTERN;
76827707 sym = external_sym(v, &type, r, &ad);
76837708 if (ad.alias_target) {
76847709 ElfSym *esym;
@@ -7687,9 +7712,6 @@ found: · 2 unmodified lines
76877712 esym = elfsym(alias_target);
76887713 if (!esym)
76897714 tcc_error("unsupported forward __alias__ attribute");
7690− /* Local statics have a scope until now (for
7691− warnings), remove it here. */
7692− sym->sym_scope = 0;
76937715 put_extern_sym2(sym, esym->st_shndx, esym->st_value, esym->st_size, 0);
76947716 }
76957717 } else {
@@ -7699,8 +7721,7 @@ found: · 3 unmodified lines
76997721 r |= l;
77007722 if (has_init)
77017723 next();
7702− else if (l == VT_CONST
7703− && (type.t & VT_BTYPE) != VT_FUNC)
7724+ else if (l == VT_CONST)
77047725 /* uninitialized global variables may be overridden */
77057726 type.t |= VT_EXTERN;
77067727 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
tests/tests2/60_errors_and_warnings.c+77−1
@@ -196,6 +196,82 @@ void * _Alignas(16) p1; · 195 unmodified lines
196196 const f t[3];
197197
198198 #elif defined test_incomplete_array_array
199− int t[][3];
199+ int t[][3]; // gr: not an error, see below
200+
201+/******************************************************************/
202+#elif defined test_extern_array
203+int iii[] = { 1,2,3 };
204+extern int iii[];
205+int x[];
206+int x[2];
207+int x[];
208+int x[2];
209+int x[];
210+extern int x[2];
211+extern int x[];
212+int x[3];
213+
214+/******************************************************************/
215+#elif defined test_func_1 \
216+ || defined test_func_2 \
217+ || defined test_func_3 \
218+ || defined test_func_4 \
219+ || defined test_func_5
220+#if defined test_func_1
221+int hello(int);
222+#elif defined test_func_4
223+static int hello(int);
224+#endif
225+int main () {
226+#if defined test_func_5
227+ static
228+#endif
229+ int hello(int);
230+ hello(123);
231+ return 0;
232+}
233+int printf(const char*, ...);
234+#if defined test_func_3
235+static int hello(int a)
236+#elif defined test_func_5
237+int hello(int a, int b)
238+#else
239+int hello(int a)
240+#endif
241+{ printf("%s: a = %d\n", __FUNCTION__, a); return 0; }
242+
243+/******************************************************************/
244+#elif defined test_var_1 \
245+ || defined test_var_2 \
246+ || defined test_var_3
247+#define P(n,v) printf("%-5s: %d ; %d\n", __FUNCTION__, n, v)
248+#if defined test_var_1
249+int xxx[];
250+#endif
251+int bar();
252+int printf(const char*, ...);
253+int main ()
254+{
255+#if !defined test_var_3
256+ int xxx = 2;
257+#endif
258+ {
259+ extern int xxx[
260+#if defined test_var_3
261+ 2
262+#endif
263+ ];
264+ P(1, xxx[0]);
265+ xxx[0] += 2;
266+ }
267+#if !defined test_var_3
268+ P(2, xxx);
269+#endif
270+ bar(123);
271+ return 0;
272+}
273+int xxx[1] = {1};
274+int bar() { P(3, xxx[0]); return 0; }
200275
276+/******************************************************************/
201277 #endif
tests/tests2/60_errors_and_warnings.expect+33−2
@@ -73,7 +73,7 @@ · 72 unmodified lines
7373 60_errors_and_warnings.c:153: error: ',' expected (got "a")
7474
7575 [test_conflicting_types]
76−60_errors_and_warnings.c:159: error: conflicting types for 'i'
76+60_errors_and_warnings.c:159: error: incompatible types for redefinition of 'i'
7777
7878 [test_nested_types]
7979 60_errors_and_warnings.c:166: error: struct/union/enum already defined
@@ -100,4 +100,35 @@ · 20 unmodified lines
100100 60_errors_and_warnings.c:196: error: declaration of an array of functions
101101
102102 [test_incomplete_array_array]
103−60_errors_and_warnings.c:199: error: unknown type size
103+
104+[test_extern_array]
105+60_errors_and_warnings.c:212: error: incompatible types for redefinition of 'x'
106+
107+[test_func_1]
108+hello: a = 123
109+
110+[test_func_2]
111+hello: a = 123
112+
113+[test_func_3]
114+60_errors_and_warnings.c:241: warning: static storage ignored for redefinition of 'hello'
115+hello: a = 123
116+
117+[test_func_4]
118+hello: a = 123
119+
120+[test_func_5]
121+60_errors_and_warnings.c:241: error: incompatible types for redefinition of 'hello'
122+
123+[test_var_1]
124+main : 1 ; 1
125+main : 2 ; 2
126+bar : 3 ; 3
127+
128+[test_var_2]
129+main : 1 ; 1
130+main : 2 ; 2
131+bar : 3 ; 3
132+
133+[test_var_3]
134+60_errors_and_warnings.c:273: error: incompatible types for redefinition of 'xxx'

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