[clang] 8bfb353 - [Sema] Fix a -Wobjc-signed-char-bool false-positive

Erik Pilkington via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 18 12:15:28 PST 2019


Author: Erik Pilkington
Date: 2019-11-18T12:15:20-08:00
New Revision: 8bfb353bb33cd2bcd2ef28e36eb8b90123b153c4

URL: https://github.com/llvm/llvm-project/commit/8bfb353bb33cd2bcd2ef28e36eb8b90123b153c4
DIFF: https://github.com/llvm/llvm-project/commit/8bfb353bb33cd2bcd2ef28e36eb8b90123b153c4.diff

LOG: [Sema] Fix a -Wobjc-signed-char-bool false-positive

Unsigned bit-field flags can only have boolean values, so handle that case in
Expr::isKnownToHaveBooleanValue.

rdar://56256999

Added: 
    

Modified: 
    clang/lib/AST/Expr.cpp
    clang/test/SemaObjC/signed-char-bool-conversion.m

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 3438c3aadc6b..4fd5fed5beef 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -191,6 +191,12 @@ bool Expr::isKnownToHaveBooleanValue() const {
   if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E))
     return OVE->getSourceExpr()->isKnownToHaveBooleanValue();
 
+  if (const FieldDecl *FD = E->getSourceBitField())
+    if (FD->getType()->isUnsignedIntegerType() &&
+        !FD->getBitWidth()->isValueDependent() &&
+        FD->getBitWidthValue(FD->getASTContext()) == 1)
+      return true;
+
   return false;
 }
 

diff  --git a/clang/test/SemaObjC/signed-char-bool-conversion.m b/clang/test/SemaObjC/signed-char-bool-conversion.m
index 476ecc6a0603..6945d86fc26d 100644
--- a/clang/test/SemaObjC/signed-char-bool-conversion.m
+++ b/clang/test/SemaObjC/signed-char-bool-conversion.m
@@ -47,3 +47,59 @@ void t2(BoolProp *bp) {
   bp.p = 1;
   bp.p = 2; // expected-warning {{implicit conversion from constant value 2 to 'BOOL'; the only well defined values for 'BOOL' are YES and NO}}
 }
+
+struct has_bf {
+  int signed_bf1 : 1;
+  int signed_bf2 : 2;
+  unsigned unsigned_bf1 : 1;
+  unsigned unsigned_bf2 : 2;
+
+  struct has_bf *nested;
+};
+
+void t3(struct has_bf *bf) {
+  b = bf->signed_bf1; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
+  b = bf->signed_bf2; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
+  b = bf->unsigned_bf1; // no warning
+  b = bf->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+  struct has_bf local;
+  b = local.unsigned_bf1;
+  b = local.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+  b = local.nested->unsigned_bf1;
+  b = local.nested->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+}
+
+__attribute__((objc_root_class))
+ at interface BFIvar {
+  struct has_bf bf;
+  unsigned unsigned_bf1 : 1;
+  unsigned unsigned_bf2 : 2;
+}
+ at end
+
+ at implementation BFIvar
+-(void)m {
+  b = bf.unsigned_bf1;
+  b = bf.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+  b = unsigned_bf1;
+  b = unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+}
+ at end
+
+#ifdef __cplusplus
+template <class T>
+struct S {
+  unsigned i : sizeof(T);
+};
+
+template <class T>
+void f() {
+  S<T> i;
+  BOOL b = i.i; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
+}
+
+int main() {
+  f<char>();
+  f<short>(); // expected-note {{in instantiation of function template specialization 'f<short>' requested here}}
+}
+#endif


        


More information about the cfe-commits mailing list