History

Generate an error when a function is redefined

Use one more bit in AttributeDef to differenciate between declared function (only its prototype is known) and defined function (its body is also known). This allows to generate an error in cases like: int f(){return 0;} int f(){return 1;}

Thomas Preud'homme committed 13 years ago

Commit 76cb1141 parent 13b99762 files · +9 −1Browse files

Unified · Split

2 files
  1. tcc.h +3 −1
  2. tccgen.c +6 −0
tcc.h+3−1
@@ -389,9 +389,10 @@ typedef struct AttributeDef { · 388 unmodified lines
389389 func_export : 1,
390390 func_import : 1,
391391 func_args : 5,
392+ func_proto : 1,
392393 mode : 4,
393394 weak : 1,
394− fill : 11;
395+ fill : 10;
395396 struct Section *section;
396397 int alias_target; /* token */
397398 } AttributeDef;
@@ -401,6 +402,7 @@ typedef struct AttributeDef { · 3 unmodified lines
401402 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
402403 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
403404 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
405+#define FUNC_PROTO(r) (((AttributeDef*)&(r))->func_proto)
404406 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
405407 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
406408 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
tccgen.c+6−0
@@ -5818,6 +5818,10 @@ static int decl0(int l, int is_for_loop_init) · 5817 unmodified lines
58185818 goto func_error1;
58195819
58205820 r = sym->type.ref->r;
5821+
5822+ if (!FUNC_PROTO(r))
5823+ tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5824+
58215825 /* use func_call from prototype if not defined */
58225826 if (FUNC_CALL(r) != FUNC_CDECL
58235827 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
@@ -5836,6 +5840,7 @@ static int decl0(int l, int is_for_loop_init) · 12 unmodified lines
58365840 tcc_error("incompatible types for redefinition of '%s'",
58375841 get_tok_str(v, NULL));
58385842 }
5843+ FUNC_PROTO(type.ref->r) = 0;
58395844 /* if symbol is already defined, then put complete type */
58405845 sym->type = type;
58415846 } else {
@@ -5901,6 +5906,7 @@ static int decl0(int l, int is_for_loop_init) · 59 unmodified lines
59015906 if ((type.t & VT_BTYPE) == VT_FUNC) {
59025907 /* external function definition */
59035908 /* specific case for func_call attribute */
5909+ ad.func_proto = 1;
59045910 type.ref->r = INT_ATTR(&ad);
59055911 } else if (!(type.t & VT_ARRAY)) {
59065912 /* not lvalue if array */

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