[cfe-commits] r53573 - in /cfe/trunk: include/clang/Analysis/PathSensitive/BugReporter.h lib/Analysis/BugReporter.cpp lib/Analysis/DeadStores.cpp

Ted Kremenek kremenek at apple.com
Mon Jul 14 13:56:05 PDT 2008


Author: kremenek
Date: Mon Jul 14 15:56:04 2008
New Revision: 53573

URL: http://llvm.org/viewvc/llvm-project?rev=53573&view=rev
Log:
Refactor Dead Stores error reporting to use the simplified BugReporter::EmitBasicReport interface.

Modified:
    cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h
    cfe/trunk/lib/Analysis/BugReporter.cpp
    cfe/trunk/lib/Analysis/DeadStores.cpp

Modified: cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h?rev=53573&r1=53572&r2=53573&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h (original)
+++ cfe/trunk/include/clang/Analysis/PathSensitive/BugReporter.h Mon Jul 14 15:56:04 2008
@@ -190,7 +190,18 @@
   void EmitWarning(BugReport& R);
   
   void EmitBasicReport(const char* BugName, const char* BugStr,
-                       SourceLocation Loc);
+                       SourceLocation Loc,
+                       SourceRange* RangeBeg, unsigned NumRanges);
+
+  void EmitBasicReport(const char* BugName, const char* BugStr,
+                       SourceLocation Loc) {
+    EmitBasicReport(BugName, BugStr, Loc, 0, 0);
+  }
+  
+  void EmitBasicReport(const char* BugName, const char* BugStr,
+                       SourceLocation Loc, SourceRange R) {
+    EmitBasicReport(BugName, BugStr, Loc, &R, 1);
+  }
   
   static bool classof(const BugReporter* R) { return true; }
 };

Modified: cfe/trunk/lib/Analysis/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/BugReporter.cpp?rev=53573&r1=53572&r2=53573&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/BugReporter.cpp (original)
+++ cfe/trunk/lib/Analysis/BugReporter.cpp Mon Jul 14 15:56:04 2008
@@ -767,14 +767,15 @@
 
 void
 BugReporter::EmitBasicReport(const char* name, const char* str,
-                             SourceLocation Loc) {
+                             SourceLocation Loc,
+                             SourceRange* RBeg, unsigned NumRanges) {
   
   SimpleBugType BT(name);
   DiagCollector C(BT);
   Diagnostic& Diag = getDiagnostic();
   Diag.Report(&C, getContext().getFullLoc(Loc),
               Diag.getCustomDiagID(Diagnostic::Warning, str),
-              0, 0, 0, 0);
+              0, 0, RBeg, NumRanges);
   
   for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
     EmitWarning(*I);

Modified: cfe/trunk/lib/Analysis/DeadStores.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/DeadStores.cpp?rev=53573&r1=53572&r2=53573&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/DeadStores.cpp (original)
+++ cfe/trunk/lib/Analysis/DeadStores.cpp Mon Jul 14 15:56:04 2008
@@ -28,27 +28,25 @@
 
 class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy {
   ASTContext &Ctx;
-  Diagnostic &Diags;
-  DiagnosticClient &Client;
+  BugReporter& BR;
   ParentMap& Parents;
     
 public:
-  DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client,
-               ParentMap& parents)
-    : Ctx(ctx), Diags(diags), Client(client), Parents(parents) {}
+  DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents)
+    : Ctx(ctx), BR(br), Parents(parents) {}
   
   virtual ~DeadStoreObs() {}
   
-  unsigned GetDiag(VarDecl* VD, bool inEnclosing = false) {    
-    std::string name(VD->getName());
-    
+  void Report(VarDecl* V, bool inEnclosing, SourceLocation L, SourceRange R) {
+
+    std::string name(V->getName());    
     std::string msg = inEnclosing
       ? "Although the value stored to '" + name +
         "' is used in the enclosing expression, the value is never actually"
         " read from '" + name + "'"
       : "Value stored to '" + name + "' is never read";
     
-    return Diags.getCustomDiagID(Diagnostic::Warning, msg.c_str());                               
+    BR.EmitBasicReport("dead store", msg.c_str(), L, R);    
   }
   
   void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val,
@@ -56,12 +54,9 @@
                     const LiveVariables::AnalysisDataTy& AD,
                     const LiveVariables::ValTy& Live) {
 
-    if (VD->hasLocalStorage() && !Live(VD, AD)) {
-      SourceRange R = Val->getSourceRange();        
-      Diags.Report(&Client,
-                   Ctx.getFullLoc(Ex->getSourceRange().getBegin()),
-                   GetDiag(VD, hasEnclosing), 0, 0, &R, 1);
-    }
+    if (VD->hasLocalStorage() && !Live(VD, AD))
+      Report(VD, hasEnclosing, Ex->getSourceRange().getBegin(),
+             Val->getSourceRange());      
   }
   
   void CheckDeclRef(DeclRefExpr* DR, Expr* Val,
@@ -119,7 +114,7 @@
         if (!V) continue;
         
         if (V->hasLocalStorage())
-          if (Expr* E = V->getInit()) {
+          if (Expr* E = V->getInit())
             if (!Live(V, AD)) {
               // Special case: check for initializations with constants.
               //
@@ -128,15 +123,9 @@
               // If x is EVER assigned a new value later, don't issue
               // a warning.  This is because such initialization can be
               // due to defensive programming.
-              if (!E->isConstantExpr(Ctx,NULL)) {
-                // Flag a warning.
-                SourceRange R = E->getSourceRange();
-                Diags.Report(&Client,
-                             Ctx.getFullLoc(V->getLocation()),
-                             GetDiag(V), 0, 0, &R, 1);
-              }
+              if (!E->isConstantExpr(Ctx,NULL))
+                Report(V, false, V->getLocation(), E->getSourceRange());
             }
-          }
       }
   }
 };
@@ -148,16 +137,6 @@
 //===----------------------------------------------------------------------===//
 
 void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) {  
-
-  SimpleBugType BT("dead store");
-  DiagCollector C(BT);  
-
-  DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap());
+  DeadStoreObs A(BR.getContext(), BR, BR.getParentMap());
   L.runOnAllBlocks(*BR.getCFG(), &A);
-  
-  // Emit the bug reports.
-  
-  for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
-    BR.EmitWarning(*I);  
 }
-





More information about the cfe-commits mailing list