1 //==- DebugCheckers.cpp - Debugging Checkers ---------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines checkers that display debugging information.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/Analysis/Analyses/Dominators.h"
16 #include "clang/Analysis/Analyses/LiveVariables.h"
17 #include "clang/Analysis/CallGraph.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/IssueHash.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
25 #include "llvm/Support/Process.h"
27 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // DominatorsTreeDumper
32 //===----------------------------------------------------------------------===//
35 class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
37 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
38 BugReporter &BR) const {
39 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
41 dom.buildDominatorTree(*AC);
48 void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
49 mgr.registerChecker<DominatorsTreeDumper>();
52 //===----------------------------------------------------------------------===//
53 // LiveVariablesDumper
54 //===----------------------------------------------------------------------===//
57 class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
59 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
60 BugReporter &BR) const {
61 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
62 L->dumpBlockLiveness(mgr.getSourceManager());
68 void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
69 mgr.registerChecker<LiveVariablesDumper>();
72 //===----------------------------------------------------------------------===//
74 //===----------------------------------------------------------------------===//
77 class CFGViewer : public Checker<check::ASTCodeBody> {
79 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
80 BugReporter &BR) const {
81 if (CFG *cfg = mgr.getCFG(D)) {
82 cfg->viewCFG(mgr.getLangOpts());
88 void ento::registerCFGViewer(CheckerManager &mgr) {
89 mgr.registerChecker<CFGViewer>();
92 //===----------------------------------------------------------------------===//
94 //===----------------------------------------------------------------------===//
97 class CFGDumper : public Checker<check::ASTCodeBody> {
99 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
100 BugReporter &BR) const {
101 PrintingPolicy Policy(mgr.getLangOpts());
102 Policy.TerseOutput = true;
103 Policy.PolishForDeclaration = true;
104 D->print(llvm::errs(), Policy);
106 if (CFG *cfg = mgr.getCFG(D)) {
107 cfg->dump(mgr.getLangOpts(),
108 llvm::sys::Process::StandardErrHasColors());
114 void ento::registerCFGDumper(CheckerManager &mgr) {
115 mgr.registerChecker<CFGDumper>();
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
123 class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
125 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
126 BugReporter &BR) const {
128 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
134 void ento::registerCallGraphViewer(CheckerManager &mgr) {
135 mgr.registerChecker<CallGraphViewer>();
138 //===----------------------------------------------------------------------===//
140 //===----------------------------------------------------------------------===//
143 class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
145 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
146 BugReporter &BR) const {
148 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
154 void ento::registerCallGraphDumper(CheckerManager &mgr) {
155 mgr.registerChecker<CallGraphDumper>();
159 //===----------------------------------------------------------------------===//
161 //===----------------------------------------------------------------------===//
164 class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
165 typedef AnalyzerOptions::ConfigTable Table;
167 static int compareEntry(const Table::MapEntryTy *const *LHS,
168 const Table::MapEntryTy *const *RHS) {
169 return (*LHS)->getKey().compare((*RHS)->getKey());
173 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
174 AnalysisManager& mgr,
175 BugReporter &BR) const {
176 const Table &Config = mgr.options.Config;
178 SmallVector<const Table::MapEntryTy *, 32> Keys;
179 for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
182 llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
184 llvm::errs() << "[config]\n";
185 for (unsigned I = 0, E = Keys.size(); I != E; ++I)
186 llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
188 llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
193 void ento::registerConfigDumper(CheckerManager &mgr) {
194 mgr.registerChecker<ConfigDumper>();
197 //===----------------------------------------------------------------------===//
198 // ExplodedGraph Viewer
199 //===----------------------------------------------------------------------===//
202 class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
204 ExplodedGraphViewer() {}
205 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
212 void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
213 mgr.registerChecker<ExplodedGraphViewer>();
216 //===----------------------------------------------------------------------===//
218 //===----------------------------------------------------------------------===//
221 class BugHashDumper : public Checker<check::PostStmt<Stmt>> {
223 mutable std::unique_ptr<BugType> BT;
225 void checkPostStmt(const Stmt *S, CheckerContext &C) const {
227 BT.reset(new BugType(this, "Dump hash components", "debug"));
229 ExplodedNode *N = C.generateNonFatalErrorNode();
233 const LangOptions &Opts = C.getLangOpts();
234 const SourceManager &SM = C.getSourceManager();
235 FullSourceLoc FL(S->getLocStart(), SM);
236 std::string HashContent =
237 GetIssueString(SM, FL, getCheckName().getName(), BT->getCategory(),
238 C.getLocationContext()->getDecl(), Opts);
240 C.emitReport(llvm::make_unique<BugReport>(*BT, HashContent, N));
245 void ento::registerBugHashDumper(CheckerManager &mgr) {
246 mgr.registerChecker<BugHashDumper>();