-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Sema] Add check for bitfield assignments to integral types #69049
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
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: %clang_cc1 -Wconversion -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -Wbitfield-conversion -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple armebv7-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple arm64-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple arm-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple aarch64-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple mipsel-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
// RUN: %clang_cc1 -triple mips64el-unknown-linux -Wbitfield-conversion \ | ||
// RUN: -fsyntax-only -verify %s | ||
|
||
typedef struct _xx { | ||
int bf:9; // expected-note 3{{declared here}} | ||
} xx, *pxx; | ||
|
||
xx vxx; | ||
|
||
void foo1(int x) { | ||
vxx.bf = x; // expected-warning{{conversion from 'int' (32 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
void foo2(short x) { | ||
vxx.bf = x; // expected-warning{{conversion from 'short' (16 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
void foo3(char x) { | ||
vxx.bf = x; // no warning expected | ||
} | ||
void foo4(short x) { | ||
vxx.bf = 0xff & x; // no warning expected | ||
} | ||
void foo5(short x) { | ||
vxx.bf = 0x1ff & x; // no warning expected | ||
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. This is a really non-ergonomic code pattern. Are we sure we can't come up with a better recommended code pattern for detecting and handling bitfield truncation? |
||
} | ||
void foo6(short x) { | ||
vxx.bf = 0x3ff & x; // expected-warning{{conversion from 'int' (10 bits) to bit-field 'bf' (9 bits) may change value}} | ||
} | ||
int fee(void) { | ||
return 0; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fire on the code pattern that I have been recommending in Clang to detect bitfield truncation:
Example:
llvm-project/clang/include/clang/AST/Decl.h
Lines 1901 to 1902 in c35939b
This isn't appropriate for all projects because not everyone uses assertions, but what's nice about it is that you don't have to do the truncation twice: the compiler figures out the bitwidth and truncates appropriately, so the developer doesn't have to duplicate information or create an extra enum to track the bitwidth to compute a mask. The assert can't really inform the warning because they are pre-processed away in release builds, but I wonder if we could detect equality comparisons between the assigned bitfield and the RHS of the assignment to suppress the warning. On second thought, that seems infeasible.
Without a better code pattern to recommend for developers, I'm worried that most of our users are going to turn this warning off. Can anyone more creative than me come up with a better recommended code pattern to suppress the warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm certainly not opposed to reducing the chattiness if there's an idea on how to do so. However, this is an off-by-default warning grouped under
-Wconversion
with its own diagnostic group (-Wbitfield-conversion
) specifically to allow people to opt into conversion warnings while opting out of this new class of diagnostics. Is that insufficient?Also, this is diagnosing code that GCC also diagnoses under
-Wconversion
.https://godbolt.org/z/qYE8Kzr7f