r282156 - Fix Wbitfield-constant-conversion false positives
Daniel Marjamaki via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 22 07:13:47 PDT 2016
Author: danielmarjamaki
Date: Thu Sep 22 09:13:46 2016
New Revision: 282156
URL: http://llvm.org/viewvc/llvm-project?rev=282156&view=rev
Log:
Fix Wbitfield-constant-conversion false positives
Summary:
The diagnostic did not handle ~ well. An expression such as ~0 is often used when 'all ones' is needed.
Differential Revision: https://reviews.llvm.org/D24232
Modified:
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/test/Sema/constant-conversion.c
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=282156&r1=282155&r2=282156&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Sep 22 09:13:46 2016
@@ -8006,11 +8006,10 @@ bool AnalyzeBitFieldAssignment(Sema &S,
unsigned OriginalWidth = Value.getBitWidth();
unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context);
- if (Value.isSigned() && Value.isNegative())
+ if (!Value.isSigned() || Value.isNegative())
if (UnaryOperator *UO = dyn_cast<UnaryOperator>(OriginalInit))
- if (UO->getOpcode() == UO_Minus)
- if (isa<IntegerLiteral>(UO->getSubExpr()))
- OriginalWidth = Value.getMinSignedBits();
+ if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not)
+ OriginalWidth = Value.getMinSignedBits();
if (OriginalWidth <= FieldWidth)
return false;
Modified: cfe/trunk/test/Sema/constant-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-conversion.c?rev=282156&r1=282155&r2=282156&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-conversion.c (original)
+++ cfe/trunk/test/Sema/constant-conversion.c Thu Sep 22 09:13:46 2016
@@ -69,7 +69,8 @@ void test7() {
unsigned int reserved:28;
} f;
- f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
+ f.twoBits1 = ~0; // no-warning
+ f.twoBits1 = ~1; // no-warning
f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
f.twoBits1 &= ~1; // no-warning
f.twoBits2 &= ~2; // no-warning
@@ -114,6 +115,8 @@ void test9() {
char array_init[] = { 255, 127, 128, 129, 0 };
}
+#define A 1
+
void test10() {
struct S {
unsigned a : 4;
@@ -121,7 +124,10 @@ void test10() {
s.a = -1;
s.a = 15;
s.a = -8;
+ s.a = ~0;
+ s.a = ~0U;
+ s.a = ~(1<<A);
- s.a = -9; // expected-warning{{implicit truncation from 'int' to bitfield changes value from -9 to 7}}
+s.a = -9; // expected-warning{{implicit truncation from 'int' to bitfield changes value from -9 to 7}}
s.a = 16; // expected-warning{{implicit truncation from 'int' to bitfield changes value from 16 to 0}}
}
More information about the cfe-commits
mailing list