[cfe-commits] r129204 - in /cfe/trunk: lib/Sema/SemaOverload.cpp test/SemaCXX/warn_false_to_pointer.cpp

Chandler Carruth chandlerc at gmail.com
Sat Apr 9 00:32:05 PDT 2011


Author: chandlerc
Date: Sat Apr  9 02:32:05 2011
New Revision: 129204

URL: http://llvm.org/viewvc/llvm-project?rev=129204&view=rev
Log:
Add support for warning on general null pointer expressions of boolean
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.

Modified:
    cfe/trunk/lib/Sema/SemaOverload.cpp
    cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp

Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=129204&r1=129203&r2=129204&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Sat Apr  9 02:32:05 2011
@@ -1982,11 +1982,11 @@
 
   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>()) {

Modified: cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp?rev=129204&r1=129203&r2=129204&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp Sat Apr  9 02:32:05 2011
@@ -5,7 +5,14 @@
 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*);





More information about the cfe-commits mailing list