-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] Consider preferred_type in bitfield warnings (#116760) #116785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5f26072
[Clang] Consider preferred_type in bitfield warnings (#116760)
ojhunt e9aa612
Attempt to appease the windows bot
ojhunt 55e70a4
Tidy up test cases
ojhunt 186d404
Merge remote-tracking branch 'origin/llvm.org/main' into users/ojhunt…
ojhunt fe3d824
Code style
ojhunt 163fd02
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt 0cd5127
Redoing diagnostics to make this an error by default
ojhunt 2c01e3a
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt cbb6a20
Sigh, i realised this change was not necessary but had not added it t…
ojhunt e9853a7
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt c83935a
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt 9297ce3
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt a3a6a25
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt 477d826
Merge branch 'main' into users/ojhunt/pr-116760
ojhunt f45ff2c
Addressing feedback
ojhunt e131e4f
Updated release notes, made the diagnostics default on, not default e…
ojhunt d9f1421
format errors once again
ojhunt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11234,9 +11234,16 @@ static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, | |
// The RHS is not constant. If the RHS has an enum type, make sure the | ||
// bitfield is wide enough to hold all the values of the enum without | ||
// truncation. | ||
if (const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>()) { | ||
const auto *EnumTy = OriginalInit->getType()->getAs<EnumType>(); | ||
const PreferredTypeAttr *PTAttr = nullptr; | ||
if (!EnumTy) { | ||
PTAttr = Bitfield->getAttr<PreferredTypeAttr>(); | ||
if (PTAttr) | ||
EnumTy = PTAttr->getType()->getAs<EnumType>(); | ||
} | ||
if (EnumTy) { | ||
EnumDecl *ED = EnumTy->getDecl(); | ||
bool SignedBitfield = BitfieldType->isSignedIntegerType(); | ||
bool SignedBitfield = BitfieldType->isSignedIntegerOrEnumerationType(); | ||
|
||
// Enum types are implicitly signed on Windows, so check if there are any | ||
// negative enumerators to see if the enum was intended to be signed or | ||
|
@@ -11250,19 +11257,28 @@ static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, | |
// on Windows where unfixed enums always use an underlying type of 'int'. | ||
unsigned DiagID = 0; | ||
if (SignedEnum && !SignedBitfield) { | ||
DiagID = diag::warn_unsigned_bitfield_assigned_signed_enum; | ||
DiagID = | ||
PTAttr == nullptr | ||
? diag::warn_unsigned_bitfield_assigned_signed_enum | ||
: diag:: | ||
warn_preferred_type_unsigned_bitfield_assigned_signed_enum; | ||
} else if (SignedBitfield && !SignedEnum && | ||
ED->getNumPositiveBits() == FieldWidth) { | ||
DiagID = diag::warn_signed_bitfield_enum_conversion; | ||
DiagID = | ||
PTAttr == nullptr | ||
? diag::warn_signed_bitfield_enum_conversion | ||
: diag::warn_preferred_type_signed_bitfield_enum_conversion; | ||
Comment on lines
+11260
to
+11270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AaronBallman @cor3ntin @erichkeane I was tempted to do this as a set of defs like: unsigned assigned_signed_enum_diag[] = {diag::warn_unsigned_bitfield_assigned_signed_enum,
diag::warn_preferred_type_unsigned_bitfield_assigned_signed_enum};
...
DiagID = assigned_signed_enum_diag[PTAttr != nullptr]; But that seemed like while being more concise it might be confusing, and potentially error prone? |
||
} | ||
|
||
if (DiagID) { | ||
S.Diag(InitLoc, DiagID) << Bitfield << ED; | ||
TypeSourceInfo *TSI = Bitfield->getTypeSourceInfo(); | ||
SourceRange TypeRange = | ||
TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange(); | ||
S.Diag(Bitfield->getTypeSpecStartLoc(), diag::note_change_bitfield_sign) | ||
<< SignedEnum << TypeRange; | ||
if (PTAttr) | ||
S.Diag(PTAttr->getLocation(), diag::note_bitfield_preferred_type) | ||
<< ED; | ||
} | ||
|
||
// Compute the required bitwidth. If the enum has negative values, we need | ||
|
@@ -11275,10 +11291,16 @@ static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, | |
// Check the bitwidth. | ||
if (BitsNeeded > FieldWidth) { | ||
Expr *WidthExpr = Bitfield->getBitWidth(); | ||
S.Diag(InitLoc, diag::warn_bitfield_too_small_for_enum) | ||
<< Bitfield << ED; | ||
auto DiagID = | ||
PTAttr == nullptr | ||
? diag::warn_bitfield_too_small_for_enum | ||
: diag::warn_preferred_type_bitfield_too_small_for_enum; | ||
S.Diag(InitLoc, DiagID) << Bitfield << ED; | ||
S.Diag(WidthExpr->getExprLoc(), diag::note_widen_bitfield) | ||
<< BitsNeeded << ED << WidthExpr->getSourceRange(); | ||
if (PTAttr) | ||
S.Diag(PTAttr->getLocation(), diag::note_bitfield_preferred_type) | ||
<< ED; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.