[cfe-commits] r146280 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/CXX/over/over.over/p2-resolve-single-template-id.cpp test/SemaCXX/condition.cpp test/SemaCXX/warn-func-as-bool.cpp test/SemaTemplate/resolve-single-template-id.cpp

David Blaikie dblaikie at gmail.com
Fri Dec 9 13:42:37 PST 2011


Author: dblaikie
Date: Fri Dec  9 15:42:37 2011
New Revision: 146280

URL: http://llvm.org/viewvc/llvm-project?rev=146280&view=rev
Log:
Add notes for suppressing and (if it's a zero-arg function returning bool) fixing the function-to-bool conversion warning.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/CXX/over/over.over/p2-resolve-single-template-id.cpp
    cfe/trunk/test/SemaCXX/condition.cpp
    cfe/trunk/test/SemaCXX/warn-func-as-bool.cpp
    cfe/trunk/test/SemaTemplate/resolve-single-template-id.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Dec  9 15:42:37 2011
@@ -2692,6 +2692,7 @@
   explicit BinaryOperator(EmptyShell Empty)
     : Expr(BinaryOperatorClass, Empty), Opc(BO_Comma) { }
 
+  SourceLocation getExprLoc() const { return OpLoc; }
   SourceLocation getOperatorLoc() const { return OpLoc; }
   void setOperatorLoc(SourceLocation L) { OpLoc = L; }
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Dec  9 15:42:37 2011
@@ -1657,6 +1657,10 @@
 def warn_impcast_function_to_bool : Warning<
     "address of function %q0 will always evaluate to 'true'">,
     InGroup<BoolConversions>;
+def note_function_to_bool_silence : Note<
+    "prefix with the address-of operator to silence this warning">;
+def note_function_to_bool_call : Note<
+    "suffix with parentheses to turn this into a function call">;
 
 def warn_cast_align : Warning<
   "cast from %0 to %1 increases required alignment from %2 to %3">,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Dec  9 15:42:37 2011
@@ -3771,6 +3771,16 @@
         if (FunctionDecl* F = dyn_cast<FunctionDecl>(D)) {
           S.Diag(E->getExprLoc(), diag::warn_impcast_function_to_bool)
             << F << E->getSourceRange() << SourceRange(CC);
+          S.Diag(E->getExprLoc(), diag::note_function_to_bool_silence)
+            << FixItHint::CreateInsertion(E->getExprLoc(), "&");
+          QualType ReturnType;
+          UnresolvedSet<4> NonTemplateOverloads;
+          S.isExprCallable(*E, ReturnType, NonTemplateOverloads);
+          if (!ReturnType.isNull() 
+              && ReturnType->isSpecificBuiltinType(BuiltinType::Bool))
+            S.Diag(E->getExprLoc(), diag::note_function_to_bool_call)
+              << FixItHint::CreateInsertion(
+                 S.getPreprocessor().getLocForEndOfToken(E->getLocEnd()), "()");
           return;
         }
       }

Modified: cfe/trunk/test/CXX/over/over.over/p2-resolve-single-template-id.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/over/over.over/p2-resolve-single-template-id.cpp?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/test/CXX/over/over.over/p2-resolve-single-template-id.cpp (original)
+++ cfe/trunk/test/CXX/over/over.over/p2-resolve-single-template-id.cpp Fri Dec  9 15:42:37 2011
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-bool-conversions %s
 
 typedef __typeof__(((int*)0)-((int*)0)) ptrdiff_t;
 
@@ -100,8 +100,8 @@
   { (void) reinterpret_cast<int>(two); } //expected-error {{reinterpret_cast}}
   { (void) reinterpret_cast<int (*)(char, double)>(two); } //expected-error {{reinterpret_cast}}
 
-  { bool b = (twoT<int>); } // expected-warning {{address of function 'twoT<int>' will always evaluate to 'true'}}
-  { bool b = (twoT<int, int>); } // expected-warning {{address of function 'twoT<int, int>' will always evaluate to 'true'}}
+  { bool b = (twoT<int>); } 
+  { bool b = (twoT<int, int>); } 
 
   { bool b = &twoT<int>; //&foo<int>; }
     b = &(twoT<int>); }
@@ -142,20 +142,20 @@
     if (&s.f<char>) return; // expected-error {{cannot create a non-constant pointer to member function}}
     if (&s.f<int>) return; // expected-error {{cannot create a non-constant pointer to member function}}
 
-    if (S::g<char>) return; // expected-warning {{address of function 'member_pointers::S::g<char>' will always evaluate to 'true'}}
-    if (S::g<int>) return; // expected-warning {{address of function 'member_pointers::S::g<int>' will always evaluate to 'true'}}
+    if (S::g<char>) return;
+    if (S::g<int>) return;
     if (&S::g<char>) return;
     if (&S::g<int>) return;
-    if (s.g<char>) return; // expected-warning {{address of function 'member_pointers::S::g<char>' will always evaluate to 'true'}}
-    if (s.g<int>) return; // expected-warning {{address of function 'member_pointers::S::g<int>' will always evaluate to 'true'}}
+    if (s.g<char>) return;
+    if (s.g<int>) return;
     if (&s.g<char>) return;
     if (&s.g<int>) return;
 
-    if (S::h<42>) return; // expected-warning {{address of function 'member_pointers::S::h<42>' will always evaluate to 'true'}}
+    if (S::h<42>) return;
     if (S::h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
     if (&S::h<42>) return;
     if (&S::h<int>) return;
-    if (s.h<42>) return;  // expected-warning {{address of function 'member_pointers::S::h<42>' will always evaluate to 'true'}}
+    if (s.h<42>) return;
     if (s.h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
     if (&s.h<42>) return;
     if (&s.h<int>) return; // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
@@ -170,20 +170,20 @@
     { bool b = &s.f<char>; } // expected-error {{cannot create a non-constant pointer to member function}}
     { bool b = &s.f<int>; } // expected-error {{cannot create a non-constant pointer to member function}}
 
-    { bool b = S::g<char>; } // expected-warning {{address of function 'member_pointers::S::g<char>' will always evaluate to 'true'}}
-    { bool b = S::g<int>; } // expected-warning {{address of function 'member_pointers::S::g<int>' will always evaluate to 'true'}}
+    { bool b = S::g<char>; }
+    { bool b = S::g<int>; }
     { bool b = &S::g<char>; }
     { bool b = &S::g<int>; }
-    { bool b = s.g<char>; }  // expected-warning {{address of function 'member_pointers::S::g<char>' will always evaluate to 'true'}}
-    { bool b = s.g<int>; } // expected-warning {{address of function 'member_pointers::S::g<int>' will always evaluate to 'true'}}
+    { bool b = s.g<char>; }
+    { bool b = s.g<int>; }
     { bool b = &s.g<char>; }
     { bool b = &s.g<int>; }
 
-    { bool b = S::h<42>; } // expected-warning {{address of function 'member_pointers::S::h<42>' will always evaluate to 'true'}}
+    { bool b = S::h<42>; }
     { bool b = S::h<int>; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}}
     { bool b = &S::h<42>; }
     { bool b = &S::h<int>; }
-    { bool b = s.h<42>; }  // expected-warning {{address of function 'member_pointers::S::h<42>' will always evaluate to 'true'}}
+    { bool b = s.h<42>; }
     { bool b = s.h<int>; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}}
     { bool b = &s.h<42>; }
     { bool b = &s.h<int>; } // expected-error {{can't form member pointer of type 'bool' without '&' and class name}}

Modified: cfe/trunk/test/SemaCXX/condition.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/condition.cpp?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/condition.cpp (original)
+++ cfe/trunk/test/SemaCXX/condition.cpp Fri Dec  9 15:42:37 2011
@@ -49,7 +49,8 @@
   if ("help")
     (void) 0;
 
-  if (test3) // expected-warning {{address of function 'test3' will always evaluate to 'true'}}
+  if (test3) // expected-warning {{address of function 'test3' will always evaluate to 'true'}} \
+                expected-note {{prefix with the address-of operator to silence this warning}}
     (void) 0;
 }
 

Modified: cfe/trunk/test/SemaCXX/warn-func-as-bool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-func-as-bool.cpp?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-func-as-bool.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-func-as-bool.cpp Fri Dec  9 15:42:37 2011
@@ -12,13 +12,25 @@
   static void f4() __attribute__((weak_import));
 };
 
+bool f5();
+bool f6(int);
+
 void bar() {
   bool b;
 
-  b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}}
-  if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}}
-  b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}}
-  if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}}
+  b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
+             expected-note {{prefix with the address-of operator to silence this warning}}
+  if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
+                expected-note {{prefix with the address-of operator to silence this warning}}
+  b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
+                expected-note {{prefix with the address-of operator to silence this warning}}
+  if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
+                   expected-note {{prefix with the address-of operator to silence this warning}}
+  b = f5; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \
+             expected-note {{prefix with the address-of operator to silence this warning}} \
+             expected-note {{suffix with parentheses to turn this into a function call}}
+  b = f6; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \
+             expected-note {{prefix with the address-of operator to silence this warning}}
 
   // implicit casts of weakly imported symbols are ok:
   b = f3;

Modified: cfe/trunk/test/SemaTemplate/resolve-single-template-id.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/resolve-single-template-id.cpp?rev=146280&r1=146279&r2=146280&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/resolve-single-template-id.cpp (original)
+++ cfe/trunk/test/SemaTemplate/resolve-single-template-id.cpp Fri Dec  9 15:42:37 2011
@@ -41,7 +41,7 @@
   *oneT<int>;   // expected-warning {{expression result unused}}
   *two;  //expected-error {{reference to overloaded function could not be resolved; did you mean to call it with no arguments?}} expected-error {{indirection requires pointer operand}}
   *twoT<int>; //expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
-  !oneT<int>;  // expected-warning {{expression result unused}} expected-warning {{address of function 'oneT<int>' will always evaluate to 'true'}}
+  !oneT<int>;  // expected-warning {{expression result unused}} expected-warning {{address of function 'oneT<int>' will always evaluate to 'true'}} expected-note {{prefix with the address-of operator to silence this warning}}
   +oneT<int>;  // expected-warning {{expression result unused}}
   -oneT<int>;  //expected-error {{invalid argument type}}
   oneT<int> == 0;   // expected-warning {{equality comparison result unused}} \
@@ -53,7 +53,7 @@
   
   int i = (int) (false ? (void (*)(int))twoT<int> : oneT<int>); //expected-error {{incompatible operand}}
   (twoT<int>) == oneT<int>; //expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}} {{cannot resolve overloaded function 'twoT' from context}}
-  bool b = oneT<int>; // expected-warning {{address of function 'oneT<int>' will always evaluate to 'true'}}
+  bool b = oneT<int>; // expected-warning {{address of function 'oneT<int>' will always evaluate to 'true'}} expected-note {{prefix with the address-of operator to silence this warning}}
   void (*p)() = oneT<int>;
   test<oneT<int> > ti;
   void (*u)(int) = oneT<int>;





More information about the cfe-commits mailing list