]> granicus.if.org Git - clang/commitdiff
Improve dead stores diagnostics to include the variable name.
authorTed Kremenek <kremenek@apple.com>
Wed, 21 May 2008 22:59:16 +0000 (22:59 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 21 May 2008 22:59:16 +0000 (22:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51395 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Analysis/DeadStores.cpp
test/Analysis/dead-stores.c

index 3eb08e4b5577ca3812d255bbcf0b0f1832106b3b..1eb36e4b51f37651a500538de238623a9fc94c25 100644 (file)
@@ -1007,9 +1007,6 @@ DIAG(warn_floatingpoint_eq, WARNING,
 DIAG(warn_selfcomparison,WARNING,
      "self-comparison always results in a constant value.")
 
-// CHECK: stores to variables that are no longer live (dead stores)
-DIAG(warn_dead_store, WARNING, "value stored to variable is never used")
-
 // CHECK: use of uninitialized values
 DIAG(warn_uninit_val, WARNING, "use of uninitialized variable")
 
index f7523e508f08ddb3cd3243e82bad6adfc9e61e87..fb241fd5525b3c5abf6da1ba42877ca31a802b17 100644 (file)
@@ -20,6 +20,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/AST/ASTContext.h"
 #include "llvm/Support/Compiler.h"
+#include <sstream>
 
 using namespace clang;
 
@@ -35,16 +36,22 @@ public:
   
   virtual ~DeadStoreObs() {}
   
+  unsigned GetDiag(VarDecl* VD) {
+    std::ostringstream os;
+    os << "value stored to '" << VD->getName() << "' is never used";        
+    return Diags.getCustomDiagID(Diagnostic::Warning, os.str().c_str());     
+  }
+  
   void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
                     const LiveVariables::AnalysisDataTy& AD,
                     const LiveVariables::ValTy& Live) {
     
     if (VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()))
       if (VD->hasLocalStorage() && !Live(VD, AD)) {
-        SourceRange R = Val->getSourceRange();
+        SourceRange R = Val->getSourceRange();        
         Diags.Report(&Client,
                      Ctx.getFullLoc(DR->getSourceRange().getBegin()),
-                     diag::warn_dead_store, 0, 0, &R, 1);
+                     GetDiag(VD), 0, 0, &R, 1);
       }
   }
   
@@ -94,7 +101,7 @@ public:
                 SourceRange R = E->getSourceRange();
                 Diags.Report(&Client,
                              Ctx.getFullLoc(V->getLocation()),
-                             diag::warn_dead_store, 0, 0, &R, 1);
+                             GetDiag(V), 0, 0, &R, 1);
               }
             }
           }
index 2ec9b48f0065b25c75c3d691ffb3f8d76bb5a4d2..21427c69106b32f2e3bf13966b8446a924120f4b 100644 (file)
@@ -3,12 +3,12 @@
 void f1() {
   int k, y;
   int abc=1;
-  long idx=abc+3*5; // expected-warning {{value stored to variable is never used}}
+  long idx=abc+3*5; // expected-warning {{never used}}
 }
 
 void f2(void *b) {
  char *c = (char*)b; // no-warning
- char *d = b+1; // expected-warning {{value stored to variable is never used}}
+ char *d = b+1; // expected-warning {{never used}}
  printf("%s", c);
 }
 
@@ -27,19 +27,19 @@ void f4(int k) {
   if (k)
     f1();
     
-  k = 2;  // expected-warning {{value stored to variable is never used}}
+  k = 2;  // expected-warning {{never used}}
 }
 
 void f5() {
 
   int x = 4; // no-warning
-  int *p = &x; // expected-warning{{value stored to variable is never used}}
+  int *p = &x; // expected-warning{{never used}}
 
 }
 
 int f6() {
   
   int x = 4;
-  ++x; // expected-warning{{value stored to variable is never used}}
+  ++x; // expected-warning{{never used}}
   return 1;
 }