Skip to content

Commit e69ab05

Browse files
committed
Add Checker callback for running a checker at the end of processing an entire TranslationUnit. Patch by Lei Zhang.
llvm-svn: 130913
1 parent 1adeff9 commit e69ab05

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

clang/include/clang/StaticAnalyzer/Core/Checker.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ class ASTCodeBody {
6363
}
6464
};
6565

66+
class EndOfTranslationUnit {
67+
template <typename CHECKER>
68+
static void _checkEndOfTranslationUnit(void *checker,
69+
const TranslationUnitDecl *TU,
70+
AnalysisManager& mgr,
71+
BugReporter &BR) {
72+
((const CHECKER *)checker)->checkEndOfTranslationUnit(TU, mgr, BR);
73+
}
74+
75+
public:
76+
template <typename CHECKER>
77+
static void _register(CHECKER *checker, CheckerManager &mgr){
78+
mgr._registerForEndOfTranslationUnit(
79+
CheckerManager::CheckEndOfTranslationUnit(checker,
80+
_checkEndOfTranslationUnit<CHECKER>));
81+
}
82+
};
83+
6684
template <typename STMT>
6785
class PreStmt {
6886
template <typename CHECKER>

clang/include/clang/StaticAnalyzer/Core/CheckerManager.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ class CheckerManager {
252252
const ExplodedNodeSet &Src,
253253
const CallExpr *CE, ExprEngine &Eng,
254254
GraphExpander *defaultEval = 0);
255+
256+
/// \brief Run checkers for the entire Translation Unit.
257+
void runCheckersOnEndOfTranslationUnit(const TranslationUnitDecl* TU,
258+
AnalysisManager &mgr,
259+
BugReporter &BR);
255260

256261
//===----------------------------------------------------------------------===//
257262
// Internal registration functions for AST traversing.
@@ -312,6 +317,10 @@ class CheckerManager {
312317
typedef CheckerFn<bool (const CallExpr *, CheckerContext &)>
313318
EvalCallFunc;
314319

320+
typedef CheckerFn<void (const TranslationUnitDecl *,
321+
AnalysisManager&, BugReporter &)>
322+
CheckEndOfTranslationUnit;
323+
315324
typedef bool (*HandlesStmtFunc)(const Stmt *D);
316325
void _registerForPreStmt(CheckStmtFunc checkfn,
317326
HandlesStmtFunc isForStmtFn);
@@ -342,6 +351,8 @@ class CheckerManager {
342351

343352
void _registerForEvalCall(EvalCallFunc checkfn);
344353

354+
void _registerForEndOfTranslationUnit(CheckEndOfTranslationUnit checkfn);
355+
345356
//===----------------------------------------------------------------------===//
346357
// Internal registration functions for events.
347358
//===----------------------------------------------------------------------===//
@@ -462,6 +473,8 @@ class CheckerManager {
462473

463474
std::vector<EvalCallFunc> EvalCallCheckers;
464475

476+
std::vector<CheckEndOfTranslationUnit> EndOfTranslationUnitCheckers;
477+
465478
struct EventInfo {
466479
llvm::SmallVector<CheckEventFunc, 4> Checkers;
467480
bool HasDispatcher;

clang/lib/StaticAnalyzer/Core/CheckerManager.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ void CheckerManager::runCheckersForEvalCall(ExplodedNodeSet &Dst,
416416
}
417417
}
418418

419+
/// \brief Run checkers for the entire Translation Unit.
420+
void CheckerManager::runCheckersOnEndOfTranslationUnit(
421+
const TranslationUnitDecl *TU,
422+
AnalysisManager &mgr,
423+
BugReporter &BR) {
424+
for (unsigned i = 0, e = EndOfTranslationUnitCheckers.size(); i != e; ++i)
425+
EndOfTranslationUnitCheckers[i](TU, mgr, BR);
426+
}
427+
419428
//===----------------------------------------------------------------------===//
420429
// Internal registration functions for AST traversing.
421430
//===----------------------------------------------------------------------===//
@@ -495,6 +504,11 @@ void CheckerManager::_registerForEvalCall(EvalCallFunc checkfn) {
495504
EvalCallCheckers.push_back(checkfn);
496505
}
497506

507+
void CheckerManager::_registerForEndOfTranslationUnit(
508+
CheckEndOfTranslationUnit checkfn) {
509+
EndOfTranslationUnitCheckers.push_back(checkfn);
510+
}
511+
498512
//===----------------------------------------------------------------------===//
499513
// Implementation details.
500514
//===----------------------------------------------------------------------===//

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
232232
checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
233233
HandleDeclContext(C, TU);
234234

235+
// After all decls handled, run checkers on the entire TranslationUnit.
236+
checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
237+
235238
// Explicitly destroy the PathDiagnosticClient. This will flush its output.
236239
// FIXME: This should be replaced with something that doesn't rely on
237240
// side-effects in PathDiagnosticClient's destructor. This is required when

0 commit comments

Comments
 (0)