Allow typedef redefinition with different types in C if the types are equally qualified, sized and aligned. MSVC allows such redefinition, emits warning C4142: "benign redefinition of type" and propagates the type from the first typedef declaration in the entire redeclaration chain.
Diff Detail
Event Timeline
MSVC permits:
typedef int foo; typedef float foo;
This is crazy and most certainly not 'benign'. I think that we should insist that the underlying types not just be the same size and alignment but the same kind of scalar type. If two types are integral or enumerator types, they should have the same signedness.
I believe this would be enough to get things like vortex working without harming developer productivity.
include/clang/Basic/DiagnosticSemaKinds.td | ||
---|---|---|
4267–4269 | I think giving a warning that 'benign' sends the message that the code is OK and that the warning should be disabled. I think we should say something like "typedef redefinition is ignored due to conflicting underlying type". | |
lib/AST/ASTContext.cpp | ||
6733–6734 | Shouldn't this be MSVCCompat, not MicrosoftExt? | |
lib/Sema/SemaDecl.cpp | ||
1867–1868 | Please use auto here. |
include/clang/AST/ASTContext.h | ||
---|---|---|
1772–1783 | Please update the comments to reflect that this only applies to typedef types. | |
include/clang/Basic/DiagnosticSemaKinds.td | ||
4267–4269 | We aren't ignoring it because the underyling types differ, we're ignoring it because they are the same size and signedness and we're in MSVC quirks mode. Maybe "ignoring conflicting integer typedef redefinition%diff{...} as a Microsoft extension; types have the same width and signedness"? Is that too much text? | |
lib/AST/ASTContext.cpp | ||
6733–6734 | +1 |
Thanks for the review!
Updated the patch according to the comments: allowed only integer, enum and pointer types which have the same qualifiers, signedness, width and alignment. Changed the diagnostic text.
Please take a look.
include/clang/AST/ASTContext.h | ||
---|---|---|
1772–1783 | By the way, MSVC allows to redefine not anly typedefs, but variables as well (under the same conditions as typedefs): int x; long x; Typedef redefinition seems to be just an example of MSVC's permissive behavior. The same for return types, etc. So, do you think we need to compatible with MSVC, for example, also considering only integer types? | |
include/clang/Basic/DiagnosticSemaKinds.td | ||
4267–4269 | Done. I also tried to move the phrase "types have the same ..." to a note, but I thought the only warning was better. |
include/clang/AST/ASTContext.h | ||
---|---|---|
1772–1783 |
I'd rather not for now. I can imagine that there's lots of code out there with crazy mixed up typedefs that it's useful to accept, but it's a lot less common to re-prototype someone else's functions incorrectly. | |
lib/Sema/SemaDecl.cpp | ||
1898–1902 | Hm, this seems like an AST fidelity issue that Richard would care about. The TSI should reflect the type the user wrote at this position. Maybe you could wrap an AdjustedType node around the original type and install that instead. |
Please update the comments to reflect that this only applies to typedef types.