This is an archive of the discontinued LLVM Phabricator instance.

[MS] PR26234: Allow typedef redefinition of equally qualified, sized and aligned types in C
Needs ReviewPublic

Authored by d.zobnin.bugzilla on Feb 1 2016, 6:47 AM.

Details

Reviewers
majnemer
rnk
Summary

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

d.zobnin.bugzilla retitled this revision from to [MS] PR26234: Allow typedef redefinition of equally qualified, sized and aligned types in C.
d.zobnin.bugzilla updated this object.
d.zobnin.bugzilla added a subscriber: cfe-commits.
majnemer edited edge metadata.Feb 1 2016, 8:44 AM

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.

rnk added inline comments.Feb 1 2016, 9:13 AM
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

d.zobnin.bugzilla edited edge metadata.

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.

d.zobnin.bugzilla marked 5 inline comments as done.Feb 3 2016, 7:03 AM
d.zobnin.bugzilla added inline comments.
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.

rnk added inline comments.Feb 3 2016, 9:49 AM
include/clang/AST/ASTContext.h
1772–1783

So, do you think we need to compatible with MSVC, for example, also considering only integer types?

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.

thakis added a subscriber: thakis.Feb 3 2016, 9:54 AM

Out of interest, what is this needed for? System headers?

d.zobnin.bugzilla marked 2 inline comments as done.Feb 3 2016, 10:05 AM

@thakis, it is needed to be able to compile SPEC2000: 255.vortex.