Hi Clang,

This patch add a new check position checkTranslationUnit for a check after
all the decls in the translation unit analyzed, then we can do some
statistical analysis in this check position.

This patch is preparation for statistical UncheckedRenturn checker.
I'll appreciate it if there are any advice about this patch.

-- 
Best regards!

Lei Zhang
Index: include/clang/StaticAnalyzer/Core/CheckerManager.h
===================================================================
--- include/clang/StaticAnalyzer/Core/CheckerManager.h	(revision 130748)
+++ include/clang/StaticAnalyzer/Core/CheckerManager.h	(working copy)
@@ -252,6 +252,10 @@
                               const ExplodedNodeSet &Src,
                               const CallExpr *CE, ExprEngine &Eng,
                               GraphExpander *defaultEval = 0);
+  
+  /// \brief Run checkers for the entire Translation Unit.
+  void runCheckersOnTranslationUnit(const Decl* D, AnalysisManager &mgr,
+                                    BugReporter &BR);
 
 //===----------------------------------------------------------------------===//
 // Internal registration functions for AST traversing.
@@ -312,6 +316,9 @@
   typedef CheckerFn<bool (const CallExpr *, CheckerContext &)>
       EvalCallFunc;
 
+  typedef CheckerFn<void (const Decl *, AnalysisManager&, BugReporter &)>
+      CheckTranslationUnit;
+
   typedef bool (*HandlesStmtFunc)(const Stmt *D);
   void _registerForPreStmt(CheckStmtFunc checkfn,
                            HandlesStmtFunc isForStmtFn);
@@ -342,6 +349,8 @@
 
   void _registerForEvalCall(EvalCallFunc checkfn);
 
+  void _registerForTranslationUnit(CheckTranslationUnit checkfn);
+
 //===----------------------------------------------------------------------===//
 // Internal registration functions for events.
 //===----------------------------------------------------------------------===//
@@ -462,6 +471,8 @@
 
   std::vector<EvalCallFunc> EvalCallCheckers;
 
+  std::vector<CheckTranslationUnit> TranslationUnitCheckers;
+
   struct EventInfo {
     llvm::SmallVector<CheckEventFunc, 4> Checkers;
     bool HasDispatcher;
Index: include/clang/StaticAnalyzer/Core/Checker.h
===================================================================
--- include/clang/StaticAnalyzer/Core/Checker.h	(revision 130748)
+++ include/clang/StaticAnalyzer/Core/Checker.h	(working copy)
@@ -63,6 +63,21 @@
   }
 };
 
+class TranslationUnit {
+  template <typename CHECKER>
+  static void _checkTranslationUnit(void *checker, const Decl *D, 
+                                    AnalysisManager& mgr, BugReporter &BR) {
+    ((const CHECKER *)checker)->checkTranslationUnit(D, mgr, BR);
+  }
+
+public:
+  template <typename CHECKER>
+  static void _register(CHECKER *checker, CheckerManager &mgr){
+    mgr._registerForTranslationUnit(
+        CheckerManager::CheckDeclFunc(checker, _checkTranslationUnit<CHECKER>));
+  }
+};
+
 template <typename STMT>
 class PreStmt {
   template <typename CHECKER>
Index: lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
===================================================================
--- lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp	(revision 130748)
+++ lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp	(working copy)
@@ -232,6 +232,9 @@
   checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
   HandleDeclContext(C, TU);
 
+  // After all decls handled, run checkers on the entire TranslationUnit.
+  checkerMgr->runCheckersOnTranslationUnit(TU, *Mgr, BR);
+
   // Explicitly destroy the PathDiagnosticClient.  This will flush its output.
   // FIXME: This should be replaced with something that doesn't rely on
   // side-effects in PathDiagnosticClient's destructor. This is required when
Index: lib/StaticAnalyzer/Core/CheckerManager.cpp
===================================================================
--- lib/StaticAnalyzer/Core/CheckerManager.cpp	(revision 130748)
+++ lib/StaticAnalyzer/Core/CheckerManager.cpp	(working copy)
@@ -416,6 +416,14 @@
   }
 }
 
+/// \brief Run checkers for the entire Translation Unit.
+void CheckerManager::runCheckersOnTranslationUnit(const Decl *D, 
+                                                  AnalysisManager &mgr,
+                                                  BugReporter &BR) {
+  for (unsigned i = 0, e = TranslationUnitCheckers.size(); i != e; ++i)
+    TranslationUnitCheckers[i](D, mgr, BR);
+}
+
 //===----------------------------------------------------------------------===//
 // Internal registration functions for AST traversing.
 //===----------------------------------------------------------------------===//
@@ -495,6 +503,10 @@
   EvalCallCheckers.push_back(checkfn);
 }
 
+void CheckerManager::_registerForTranslationUnit(CheckTranslationUnit checkfn) {
+  TranslationUnitCheckers.push_back(checkfn);
+}
+
 //===----------------------------------------------------------------------===//
 // Implementation details.
 //===----------------------------------------------------------------------===//
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to