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);
 


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);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to