[PATCH] D24232: Wbitfiled-constant-conversion : allow ~0 , ~(1<<A), etc

Daniel Marjamäki via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 22 07:22:30 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL282156: Fix Wbitfield-constant-conversion false positives (authored by danielmarjamaki).

Changed prior to commit:
  https://reviews.llvm.org/D24232?vs=70325&id=72169#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D24232

Files:
  cfe/trunk/lib/Sema/SemaChecking.cpp
  cfe/trunk/test/Sema/constant-conversion.c

Index: cfe/trunk/test/Sema/constant-conversion.c
===================================================================
--- cfe/trunk/test/Sema/constant-conversion.c
+++ cfe/trunk/test/Sema/constant-conversion.c
@@ -69,7 +69,8 @@
 		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,14 +115,19 @@
   char array_init[] = { 255, 127, 128, 129, 0 };
 }
 
+#define A 1
+
 void test10() {
   struct S {
     unsigned a : 4;
   } s;
   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}}
 }
Index: cfe/trunk/lib/Sema/SemaChecking.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp
+++ cfe/trunk/lib/Sema/SemaChecking.cpp
@@ -8006,11 +8006,10 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24232.72169.patch
Type: text/x-patch
Size: 2000 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160922/580b8fa8/attachment.bin>


More information about the cfe-commits mailing list