1 //===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This checker prints callbacks that are called during analysis. 11 // This is required to ensure that callbacks are fired in order 12 // and do not duplicate or get lost. 13 // Feel free to extend this checker with any callback you need to check. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "ClangSACheckers.h" 18 #include "clang/StaticAnalyzer/Core/Checker.h" 19 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 21 22 using namespace clang; 23 using namespace ento; 24 25 namespace { 26 27 class AnalysisOrderChecker : public Checker< check::PreStmt<CastExpr>, 28 check::PostStmt<CastExpr>, 29 check::PreStmt<ArraySubscriptExpr>, 30 check::PostStmt<ArraySubscriptExpr>> { 31 bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const { 32 AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions(); 33 return Opts.getBooleanOption("*", false, this) || 34 Opts.getBooleanOption(CallbackName, false, this); 35 } 36 37 public: 38 void checkPreStmt(const CastExpr *CE, CheckerContext &C) const { 39 if (isCallbackEnabled(C, "PreStmtCastExpr")) 40 llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName() 41 << ")\n"; 42 } 43 44 void checkPostStmt(const CastExpr *CE, CheckerContext &C) const { 45 if (isCallbackEnabled(C, "PostStmtCastExpr")) 46 llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName() 47 << ")\n"; 48 } 49 50 void checkPreStmt(const ArraySubscriptExpr *SubExpr, CheckerContext &C) const { 51 if (isCallbackEnabled(C, "PreStmtArraySubscriptExpr")) 52 llvm::errs() << "PreStmt<ArraySubscriptExpr>\n"; 53 } 54 55 void checkPostStmt(const ArraySubscriptExpr *SubExpr, CheckerContext &C) const { 56 if (isCallbackEnabled(C, "PostStmtArraySubscriptExpr")) 57 llvm::errs() << "PostStmt<ArraySubscriptExpr>\n"; 58 } 59 }; 60 } 61 62 //===----------------------------------------------------------------------===// 63 // Registration. 64 //===----------------------------------------------------------------------===// 65 66 void ento::registerAnalysisOrderChecker(CheckerManager &mgr) { 67 mgr.registerChecker<AnalysisOrderChecker>(); 68 } 69