]> granicus.if.org Git - clang/commitdiff
Add support for warning on general null pointer expressions of boolean
authorChandler Carruth <chandlerc@gmail.com>
Sat, 9 Apr 2011 07:32:05 +0000 (07:32 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sat, 9 Apr 2011 07:32:05 +0000 (07:32 +0000)
type rather than just the literal 'false'. This begins fixing PR9612,
but the message is now wrong. WIP, the cleanup of the messaging is next.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129204 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaOverload.cpp
test/SemaCXX/warn_false_to_pointer.cpp

index 3212c36b9ab092634edbbaa37860a528d03b2ad8..905b555381aeba6fabbad4fe51a7784d8266230d 100644 (file)
@@ -1982,11 +1982,11 @@ bool Sema::CheckPointerConversion(Expr *From, QualType ToType,
 
   Kind = CK_BitCast;
 
-  if (CXXBoolLiteralExpr* LitBool
-                          = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens()))
-    if (!IsCStyleOrFunctionalCast && LitBool->getValue() == false)
-      DiagRuntimeBehavior(LitBool->getExprLoc(), From,
-                          PDiag(diag::warn_init_pointer_from_false) << ToType);
+  if (!IsCStyleOrFunctionalCast &&
+      Context.hasSameUnqualifiedType(From->getType(), Context.BoolTy) &&
+      From->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
+    DiagRuntimeBehavior(From->getExprLoc(), From,
+                        PDiag(diag::warn_init_pointer_from_false) << ToType);
 
   if (const PointerType *FromPtrType = FromType->getAs<PointerType>())
     if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) {
index 26b54f6e685b3588b5b0940fcfab64b5d5e7d6a2..20d4b3a52d4601318cc079e20e746be924fd523b 100644 (file)
@@ -5,7 +5,14 @@ int* j = false; // expected-warning{{ initialization of pointer of type 'int *'
 void foo(int* i, int *j=(false)) // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
 {
   foo(false); // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
-  foo((int*)false);
+  foo((int*)false); // no-warning: explicit cast
+  foo(0); // no-warning: not a bool, even though its convertible to bool
+
+  foo(false == true); // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
+  foo((42 + 24) < 32); // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
+
+  const bool kFlag = false;
+  foo(kFlag); // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
 }
 
 char f(struct Undefined*);