]> granicus.if.org Git - clang/commitdiff
Implement a warning when converting the literal 'false' to a
authorDouglas Gregor <dgregor@apple.com>
Tue, 8 Jun 2010 17:35:15 +0000 (17:35 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 8 Jun 2010 17:35:15 +0000 (17:35 +0000)
pointer. Original patch by Troy D. Straszheim; fixes PR7283.

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

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaOverload.cpp
test/SemaCXX/warn_false_to_pointer.cpp [new file with mode: 0644]

index 430ad96efb860db424f60a0568c0180ad5466cd9..5dc613a7a9a7e0707814bb4f1d11c39e2f11a8c9 100644 (file)
@@ -23,6 +23,7 @@ def : DiagGroup<"aggregate-return">;
 def AmbigMemberTemplate : DiagGroup<"ambiguous-member-template">;
 def : DiagGroup<"attributes">;
 def : DiagGroup<"bad-function-cast">;
+def BoolConversions : DiagGroup<"bool-conversions">;
 def : DiagGroup<"c++-compat">;
 def : DiagGroup<"cast-align">;
 def : DiagGroup<"cast-qual">;
@@ -141,7 +142,7 @@ def Parentheses : DiagGroup<"parentheses", [DiagGroup<"idiomatic-parentheses">]>
 // -Wconversion has its own warnings, but we split this one out for
 // legacy reasons.
 def Conversion : DiagGroup<"conversion",
-                           [DiagGroup<"shorten-64-to-32">]>,
+                           [DiagGroup<"shorten-64-to-32">, BoolConversions]>,
                  DiagCategory<"Value Conversion Issue">;
 
 def Unused : DiagGroup<"unused",
index 2412267bb6fec96e6702b79d798a18ae483c9521..efc171f3bba899dcb700b1d9c01fac5da1566339 100644 (file)
@@ -1098,6 +1098,10 @@ def note_ovl_candidate : Note<"candidate "
     "is the implicit copy constructor|"
     "is the implicit copy assignment operator}0%1">;
 
+def warn_init_pointer_from_false : Warning<
+    "initialization of pointer of type %0 from literal 'false'">,
+    InGroup<BoolConversions>;
+
 def note_ovl_candidate_bad_deduction : Note<
     "candidate template ignored: failed template argument deduction">;
 def note_ovl_candidate_incomplete_deduction : Note<"candidate template ignored: "
index 2754d443b25a54504a189c8a74c23189dc55a456..6a020d57d3778ac86f6d740a007f319fbe953cfc 100644 (file)
@@ -1622,6 +1622,12 @@ bool Sema::CheckPointerConversion(Expr *From, QualType ToType,
                                   bool IgnoreBaseAccess) {
   QualType FromType = From->getType();
 
+  if (CXXBoolLiteralExpr* LitBool
+                          = dyn_cast<CXXBoolLiteralExpr>(From->IgnoreParens()))
+    if (LitBool->getValue() == false)
+      Diag(LitBool->getExprLoc(), diag::warn_init_pointer_from_false)
+        << ToType;
+
   if (const PointerType *FromPtrType = FromType->getAs<PointerType>())
     if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) {
       QualType FromPointeeType = FromPtrType->getPointeeType(),
@@ -6159,7 +6165,7 @@ BuildRecoveryCallExpr(Sema &SemaRef, Scope *S, Expr *Fn,
                          Sema::MultiExprArg(SemaRef, (void**) Args, NumArgs),
                                CommaLocs, RParenLoc);
 }
-  
+
 /// ResolveOverloadedCallFn - Given the call expression that calls Fn
 /// (which eventually refers to the declaration Func) and the call
 /// arguments Args/NumArgs, attempt to resolve the function call down
diff --git a/test/SemaCXX/warn_false_to_pointer.cpp b/test/SemaCXX/warn_false_to_pointer.cpp
new file mode 100644 (file)
index 0000000..3a873d8
--- /dev/null
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+int* j = false; // expected-warning{{ initialization of pointer of type 'int *' from literal 'false'}}
+
+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'}}
+}
+