[cfe-commits] r105621 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaOverload.cpp test/SemaCXX/warn_false_to_pointer.cpp

Douglas Gregor dgregor at apple.com
Tue Jun 8 10:35:15 PDT 2010


Author: dgregor
Date: Tue Jun  8 12:35:15 2010
New Revision: 105621

URL: http://llvm.org/viewvc/llvm-project?rev=105621&view=rev
Log:
Implement a warning when converting the literal 'false' to a
pointer. Original patch by Troy D. Straszheim; fixes PR7283.

Added:
    cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp   (with props)
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaOverload.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=105621&r1=105620&r2=105621&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Jun  8 12:35:15 2010
@@ -23,6 +23,7 @@
 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 @@
 // -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",

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=105621&r1=105620&r2=105621&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jun  8 12:35:15 2010
@@ -1098,6 +1098,10 @@
     "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: "

Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=105621&r1=105620&r2=105621&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Tue Jun  8 12:35:15 2010
@@ -1622,6 +1622,12 @@
                                   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 @@
                          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

Added: 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=105621&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp Tue Jun  8 12:35:15 2010
@@ -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'}}
+}
+

Propchange: cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/warn_false_to_pointer.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list