llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang allows illegal redeclaration of function?

Open efriedma-quic opened this issue 18 years ago • 7 comments

Bugzilla Link 1890
Resolution FIXED
Resolved on Mar 12, 2010 00:59
Version unspecified
OS Linux
CC @akyrtzi

Extended Description

int b(int c) {return 1;}

int f(float b) { return 0; }

void a(void){ int f(int (int)); f(b); }

I think this code should give a compile error. Both gcc and the comeau c compiler give an error on this code, but clang does not; instead, clang emits code with an rather dangerous function cast.

Not sure if this is the right component.

efriedma-quic avatar Jan 01 '08 10:01 efriedma-quic

This was fixed recently, at least for C.

efriedma-quic avatar Feb 26 '09 06:02 efriedma-quic

Ok, it's about time this ancient bug gets revived :)

void f(void) { void ss(void); } float ss; // error: 'ss' redeclared as different kind of symbol

gcc and comeau emitting error for the above seems to indicate that Steve is right and the function decl should be added to the top level scope.

Also, comeau emits this kind of error for C++ too, while g++ doesn't; should clang follow the comeau example ?

akyrtzi avatar Oct 07 '08 02:10 akyrtzi

o.k. I'm not surprised the behavior is undefined. I agree a diagnostic is in order. I'm going to leave this open for now...I have more important fish to fry. Will get back to it...my partial fix yesterday pushes the ball forward a bit (and handles a more common case, I believe).

llvmbot avatar Jan 10 '08 22:01 llvmbot

Adding the function to the top-level scope doesn't sound completely right... the first declaration of ss isn't actually visible at the point where the error should occur. It's just that it's not legal to have multiple conflicting definitions for the same function... According to the standard, "If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.", and "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined." So the behavior of the program in comment 2 appears to be undefined by the standard... although a diagnostic definitely seems like the most reasonable way to deal with it.

efriedma-quic avatar Jan 10 '08 22:01 efriedma-quic

Well if you wanted both "bugs" fixed, you should have included the second case originally (just kidding:-).

It looks like we should always add the function decl to the top level scope. Before we make this change, we should consult the spec. If you feel motivated to work on this, let me know (I think it would be a good "first commit" project). If not, I'm happy finish it. Your call.

llvmbot avatar Jan 10 '08 17:01 llvmbot

Not really fixed; try the following:

int r (int) { int ss(void); } int ss(int);

efriedma-quic avatar Jan 10 '08 07:01 efriedma-quic

You are correct...this is an error. Fixed in r45789.

llvmbot avatar Jan 10 '08 01:01 llvmbot