[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:28:49 PDT 2022


ShawnZhong created this revision.
ShawnZhong added a reviewer: rsmith.
ShawnZhong added a project: clang.
Herald added a project: All.
ShawnZhong requested review of this revision.
Herald added a subscriber: cfe-commits.

Fix https://github.com/llvm/llvm-project/issues/53253


Repository:
  rG LLVM Github Monorepo

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
@@ -131,3 +131,15 @@
   s.a = -9;  // expected-warning{{implicit truncation from 'int' to bit-field changes value from -9 to 7}}
   s.a = 16;  // expected-warning{{implicit truncation from 'int' to bit-field changes value from 16 to 0}}
 }
+
+void test11(void) {
+  struct S {
+    signed char c : 1;  // the only valid values are 0 and -1
+  } s;
+
+  s.c = -2; // expected-warning {{implicit conversion from 'int' to 'char' changes value from -2 to 0}}
+  s.c = -1; // no-warning
+  s.c = 0;  // no-warning
+  s.c = 1;  // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1 to -1}}
+  s.c = 2;  // expected-warning {{implicit conversion from 'int' to 'char' changes value from 2 to 0}}
+}
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.450272.patch
Type: text/x-patch
Size: 1513 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220805/c1841977/attachment.bin>


More information about the cfe-commits mailing list