scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit ef6b767b7d3ea2088f8652fe5ea76117661edef0
parent cbdbbf1076c80ffe2281b687d6539905c485c49b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  9 Jan 2016 21:30:52 +0100

Detect redefinition of external symbols.

An external declaration is a tentative definition, and a file can contain
several tentative definition for a symbol, but when an external
declaration also has an initializer, it is a definition. Due to the order
in the if this error was not catched.

Diffstat:
Mcc1/expr.c | 5++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/cc1/expr.c b/cc1/expr.c @@ -1247,10 +1247,13 @@ initializer(Symbol *sym, Type *tp, int nelem) np = assignop(OINIT, varnode(sym), np); - if ((flags & (ISGLOBAL|ISLOCAL|ISPRIVATE)) != 0) { + if (flags & ISDEFINED) { + errorp("redeclaration of '%s'", sym->name); + } else if ((flags & (ISGLOBAL|ISLOCAL|ISPRIVATE)) != 0) { if (!np->right->constant) errorp("initializer element is not constant"); emit(OINIT, np); + sym->flags |= ISDEFINED; } else if ((flags & (ISEXTERN|ISTYPEDEF)) != 0) { errorp("'%s' has both '%s' and initializer", sym->name, (flags&ISEXTERN) ? "extern" : "typedef");