[cfe-commits] r99063 - in /cfe/trunk: include/clang/Checker/BugReporter/BugReporter.h lib/Checker/BugReporter.cpp lib/Checker/BugReporterVisitors.cpp

Ted Kremenek kremenek at apple.com
Sat Mar 20 11:01:57 PDT 2010


Author: kremenek
Date: Sat Mar 20 13:01:57 2010
New Revision: 99063

URL: http://llvm.org/viewvc/llvm-project?rev=99063&view=rev
Log:
Check if a BugReporterVisitor has already been added to a BugReporterContext.
This avoids redundant diagnostics.

Modified:
    cfe/trunk/include/clang/Checker/BugReporter/BugReporter.h
    cfe/trunk/lib/Checker/BugReporter.cpp
    cfe/trunk/lib/Checker/BugReporterVisitors.cpp

Modified: cfe/trunk/include/clang/Checker/BugReporter/BugReporter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Checker/BugReporter/BugReporter.h?rev=99063&r1=99062&r2=99063&view=diff
==============================================================================
--- cfe/trunk/include/clang/Checker/BugReporter/BugReporter.h (original)
+++ cfe/trunk/include/clang/Checker/BugReporter/BugReporter.h Sat Mar 20 13:01:57 2010
@@ -17,14 +17,15 @@
 
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceLocation.h"
-#include "clang/Checker/PathSensitive/GRState.h"
-#include "clang/Checker/PathSensitive/ExplodedGraph.h"
 #include "clang/Checker/BugReporter/BugType.h"
+#include "clang/Checker/PathSensitive/ExplodedGraph.h"
+#include "clang/Checker/PathSensitive/GRState.h"
+#include "llvm/ADT/FoldingSet.h"
+#include "llvm/ADT/ImmutableList.h"
+#include "llvm/ADT/ImmutableSet.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallString.h"
-#include "llvm/ADT/ImmutableSet.h"
-#include "llvm/ADT/ImmutableList.h"
 #include <list>
 
 namespace clang {
@@ -46,7 +47,7 @@
 // Interface for individual bug reports.
 //===----------------------------------------------------------------------===//
 
-class BugReporterVisitor {
+class BugReporterVisitor : public llvm::FoldingSetNode {
 public:
   virtual ~BugReporterVisitor();
   virtual PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
@@ -54,6 +55,7 @@
                                          BugReporterContext& BRC) = 0;
 
   virtual bool isOwnedByReporterContext() { return true; }
+  virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0;
 };
 
 // FIXME: Combine this with RangedBugReport and remove RangedBugReport.
@@ -390,13 +392,12 @@
   // Callbacks because it is safe to make additions to list during iteration.
   llvm::ImmutableList<BugReporterVisitor*>::Factory F;
   llvm::ImmutableList<BugReporterVisitor*> Callbacks;
+  llvm::FoldingSet<BugReporterVisitor> CallbacksSet;
 public:
   BugReporterContext(GRBugReporter& br) : BR(br), Callbacks(F.GetEmptyList()) {}
   virtual ~BugReporterContext();
 
-  void addVisitor(BugReporterVisitor* visitor) {
-    if (visitor) Callbacks = F.Add(visitor, Callbacks);
-  }
+  void addVisitor(BugReporterVisitor* visitor);
 
   typedef llvm::ImmutableList<BugReporterVisitor*>::iterator visitor_iterator;
   visitor_iterator visitor_begin() { return Callbacks.begin(); }

Modified: cfe/trunk/lib/Checker/BugReporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/BugReporter.cpp?rev=99063&r1=99062&r2=99063&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/BugReporter.cpp (original)
+++ cfe/trunk/lib/Checker/BugReporter.cpp Sat Mar 20 13:01:57 2010
@@ -36,6 +36,21 @@
     if ((*I)->isOwnedByReporterContext()) delete *I;
 }
 
+void BugReporterContext::addVisitor(BugReporterVisitor* visitor) {
+  if (!visitor)
+    return;
+
+  llvm::FoldingSetNodeID ID;
+  visitor->Profile(ID);
+  void *InsertPos;
+
+  if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos))
+    return;
+
+  CallbacksSet.InsertNode(visitor, InsertPos);
+  Callbacks = F.Add(visitor, Callbacks);
+}
+
 //===----------------------------------------------------------------------===//
 // Helper routines for walking the ExplodedGraph and fetching statements.
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/lib/Checker/BugReporterVisitors.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/BugReporterVisitors.cpp?rev=99063&r1=99062&r2=99063&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/BugReporterVisitors.cpp (original)
+++ cfe/trunk/lib/Checker/BugReporterVisitors.cpp Sat Mar 20 13:01:57 2010
@@ -92,6 +92,13 @@
   FindLastStoreBRVisitor(SVal v, const MemRegion *r)
   : R(r), V(v), satisfied(false), StoreSite(0) {}
 
+  virtual void Profile(llvm::FoldingSetNodeID &ID) const {
+    static int tag = 0;
+    ID.AddPointer(&tag);
+    ID.AddPointer(R);
+    ID.Add(V);
+  }
+
   PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
                                  const ExplodedNode *PrevN,
                                  BugReporterContext& BRC) {
@@ -129,8 +136,8 @@
       return NULL;
 
     satisfied = true;
-    std::string sbuf;
-    llvm::raw_string_ostream os(sbuf);
+    llvm::SmallString<256> sbuf;
+    llvm::raw_svector_ostream os(sbuf);
 
     if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
       if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
@@ -239,6 +246,13 @@
   TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
   : Constraint(constraint), Assumption(assumption), isSatisfied(false) {}
 
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+    static int tag = 0;
+    ID.AddPointer(&tag);
+    ID.AddBoolean(Assumption);
+    ID.Add(Constraint);
+  }
+
   PathDiagnosticPiece* VisitNode(const ExplodedNode *N,
                                  const ExplodedNode *PrevN,
                                  BugReporterContext& BRC) {





More information about the cfe-commits mailing list