[cfe-dev] PATCH: Cleanup function redeclaration representations

Neil Booth neil at daikokuya.co.uk
Thu Apr 24 06:45:12 PDT 2008


Doug Gregor wrote:-

> In C++, it isn't a redeclaration if it's not in the same scope
> ([over.dcl]p1 says as such). One can have multiple declarations at
> different scopes that end up referring to the same function, e.g.,
> 
>   extern "C" void foo(int);
> 
> could occur in several local scopes. However, this doesn't meet the
> C++ definition of a redeclaration. Perhaps C is different?

C doesn't define redeclaration; I'm using it to mean a declaration
of an entity already declared.  C does specify the (type) semantics in
such cases.

> Clang currently considers declarations from different scopes to be
> completely different entities. A function declared in a local scope
> will be popped off the stack once Clang leaves that local scope, so it
> "does the right thing" with these.

For semantic type, provided you get the composite type correct, that
is almost fine (beware interposed local variables blocking visibility
of the outer declaration.  Yay for corner cases - there are a scary
number in this area).

However, it's nice to complain about / reject the "x" in things like

void foo1 (void) { extern int x(void); }
void foo2 (void) { extern double x(void); }

$ gcc /tmp/bug.c
/tmp/bug.c: In function 'foo2':
/tmp/bug.c:2: error: conflicting types for 'x'
/tmp/bug.c:1: error: previous declaration of 'x' was here

This is undefined behaviour according to the C standard; but there
is little value in letting such stuff pass.

GCC does this really well now; it gets almost anything no matter how
you might try to fool it.  It used to (<3.4 ?) be much less precise.

Neil.



More information about the cfe-dev mailing list