]> granicus.if.org Git - clang/commitdiff
Improve the diagnostic text for -Wmissing-noreturn to include the name
authorChandler Carruth <chandlerc@gmail.com>
Wed, 31 Aug 2011 09:01:53 +0000 (09:01 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 31 Aug 2011 09:01:53 +0000 (09:01 +0000)
of the function in question when applicable (that is, not for blocks).
Patch by Joerg Sonnenberger with some stylistic tweaks by me.

When discussing this weth Joerg, streaming the decl directly into the
diagnostic didn't work because we have a pointer-to-const, and the
overload doesn't accept such. In order to make my style tweaks to the
patch, I first changed the overload to accept a pointer-to-const, and
then changed the diagnostic printing layer to also use
a pointer-to-const, cleaning up a gross line of code along the way.

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

include/clang/AST/Decl.h
include/clang/Basic/DiagnosticSemaKinds.td
lib/AST/ASTDiagnostic.cpp
lib/Sema/AnalysisBasedWarnings.cpp
test/Sema/return-noreturn.c
test/SemaCXX/warn-missing-noreturn.cpp
test/SemaObjC/return.m

index 487a7ae242421605ee72bb607da901f9fb2dc097..934f20ff4320451448f11d08b869d59dd4ab15ab 100644 (file)
@@ -3030,7 +3030,7 @@ public:
 /// Insertion operator for diagnostics.  This allows sending NamedDecl's
 /// into a diagnostic with <<.
 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
-                                           NamedDecl* ND) {
+                                           const NamedDecl* ND) {
   DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl);
   return DB;
 }
index 224853c34b7b4e51bb0ecf88f73c3dddf2caafee..89bc1064508739ac3c0cd8b52d58859f4e8d0bb5 100644 (file)
@@ -236,10 +236,10 @@ def err_maybe_falloff_nonvoid_block : Error<
 def err_falloff_nonvoid_block : Error<
   "control reaches end of non-void block">;
 def warn_suggest_noreturn_function : Warning<
-  "function could be attribute 'noreturn'">,
+  "function %0 could be declared with attribute 'noreturn'">,
   InGroup<DiagGroup<"missing-noreturn">>, DefaultIgnore;
 def warn_suggest_noreturn_block : Warning<
-  "block could be attribute 'noreturn'">,
+  "block could be declared with attribute 'noreturn'">,
   InGroup<DiagGroup<"missing-noreturn">>, DefaultIgnore;
 def warn_unreachable : Warning<"will never be executed">,
   InGroup<DiagGroup<"unreachable-code">>, DefaultIgnore;
index c93bbe60abb080d38866a5a8ee727cc295120d6d..e6de54c907df1d1c0d95470e4d61558788850a10 100644 (file)
@@ -269,8 +269,8 @@ void clang::FormatASTNodeDiagnosticArgument(
                "Invalid modifier for NamedDecl* argument");
         Qualified = false;
       }
-      reinterpret_cast<NamedDecl*>(Val)->
-      getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
+      const NamedDecl *ND = reinterpret_cast<const NamedDecl*>(Val);
+      ND->getNameForDiagnostic(S, Context.PrintingPolicy, Qualified);
       break;
     }
     case Diagnostic::ak_nestednamespec: {
index f3f168a5c1925a83b3c19b52424b20008fb37edb..b11089cc1b302c9991a09068196d543b03a3c4d8 100644 (file)
@@ -376,9 +376,14 @@ static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body,
                  CD.diag_AlwaysFallThrough_ReturnsNonVoid);
         break;
       case NeverFallThroughOrReturn:
-        if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn)
-          S.Diag(Compound->getLBracLoc(),
-                 CD.diag_NeverFallThroughOrReturn);
+        if (ReturnsVoid && !HasNoReturn && CD.diag_NeverFallThroughOrReturn) {
+          if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+            S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn)
+              << FD;
+          } else {
+            S.Diag(Compound->getLBracLoc(), CD.diag_NeverFallThroughOrReturn);
+          }
+        }
         break;
       case NeverFallThrough:
         break;
index ff43754a429941802ef2d20d67dd11a5cbc6e2d8..448fce77cd858dcb37e59df365e11dec819ac1cd 100644 (file)
@@ -1,8 +1,8 @@
 // RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wmissing-noreturn -Wno-unreachable-code
 
 int j;
-void test1() { // expected-warning {{function could be attribute 'noreturn'}}
-  ^ (void) { while (1) { } }(); // expected-warning {{block could be attribute 'noreturn'}}
+void test1() { // expected-warning {{function 'test1' could be declared with attribute 'noreturn'}}
+  ^ (void) { while (1) { } }(); // expected-warning {{block could be declared with attribute 'noreturn'}}
   ^ (void) { if (j) while (1) { } }();
   while (1) { }
 }
index 4caff66af703fd1292451d31250d4b72e4bfd492..ac568a5d81c90d1d0fe7fb2b8834f70e3494a518 100644 (file)
@@ -1,14 +1,14 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -Wmissing-noreturn -Wreturn-type
 void f() __attribute__((noreturn));
 
-template<typename T> void g(T) { // expected-warning {{function could be attribute 'noreturn'}}
+template<typename T> void g(T) { // expected-warning {{function 'g<int>' could be declared with attribute 'noreturn'}}
   f();
 }
 
 template void g<int>(int); // expected-note {{in instantiation of function template specialization 'g<int>' requested here}}
 
 template<typename T> struct A {
-  void g() { // expected-warning {{function could be attribute 'noreturn'}}
+  void g() { // expected-warning {{function 'g' could be declared with attribute 'noreturn'}}
     f();
   }
 };
@@ -16,7 +16,7 @@ template<typename T> struct A {
 template struct A<int>; // expected-note {{in instantiation of member function 'A<int>::g' requested here}}
 
 struct B {
-  template<typename T> void g(T) { // expected-warning {{function could be attribute 'noreturn'}}
+  template<typename T> void g(T) { // expected-warning {{function 'g<int>' could be declared with attribute 'noreturn'}}
     f();
   }
 };
@@ -61,7 +61,7 @@ namespace test2 {
     void *f;
 
     A() : f(0) { }
-    A(int) : f(h()) { } // expected-warning {{function could be attribute 'noreturn'}}
+    A(int) : f(h()) { } // expected-warning {{function 'A' could be declared with attribute 'noreturn'}}
     A(char) : f(j()) { }
     A(bool b) : f(b ? h() : j()) { }
   };
index 3a626e3696035d9f0520596137092d0943d33a65..88e6e6381e60b76393e124598d1437819c0fea80 100644 (file)
@@ -14,7 +14,7 @@ void test2(int a) {
 }
 
 // PR5286
-void test3(int a) {  // expected-warning {{function could be attribute 'noreturn'}}
+void test3(int a) {  // expected-warning {{function 'test3' could be declared with attribute 'noreturn'}}
   while (1) {
     if (a)
       @throw (id)0;