[PATCH] D131255: Fix Wbitfield-constant-conversion on 1-bit signed bitfield

Shawn Zhong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 5 05:43:06 PDT 2022


ShawnZhong updated this revision to Diff 450274.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131255/new/

https://reviews.llvm.org/D131255

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


Index: clang/test/Sema/constant-conversion.c
===================================================================
--- clang/test/Sema/constant-conversion.c
+++ clang/test/Sema/constant-conversion.c
@@ -15,8 +15,16 @@
 }
 
 void test(void) {
-  struct { int bit : 1; } a;
-  a.bit = 1; // shouldn't warn
+  struct S {
+    signed char c : 1;  // the only valid values are 0 and -1
+  } s;
+
+  s.c = -3; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to -1}}
+  s.c = -2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -2 to 0}}
+  s.c = -1; // no-warning
+  s.c = 0;  // no-warning
+  s.c = 1;  // expected-warning {{implicit truncation from 'int' to bit-field changes value from 1 to -1}}
+  s.c = 2;  // expected-warning {{implicit truncation from 'int' to bit-field changes value from 2 to 0}}
 }
 
 enum Test2 { K_zero, K_one };
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -13067,11 +13067,6 @@
   if (llvm::APSInt::isSameValue(Value, TruncatedValue))
     return false;
 
-  // Special-case bitfields of width 1: booleans are naturally 0/1, and
-  // therefore don't strictly fit into a signed bitfield of width 1.
-  if (FieldWidth == 1 && Value == 1)
-    return false;
-
   std::string PrettyValue = toString(Value, 10);
   std::string PrettyTrunc = toString(TruncatedValue, 10);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131255.450274.patch
Type: text/x-patch
Size: 1506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220805/174385af/attachment-0001.bin>


More information about the cfe-commits mailing list