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 "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
24 #include "llvm/Support/Process.h"
26 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 // DominatorsTreeDumper
31 //===----------------------------------------------------------------------===//
34 class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
36 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
37 BugReporter &BR) const {
38 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
40 dom.buildDominatorTree(*AC);
47 void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
48 mgr.registerChecker<DominatorsTreeDumper>();
51 //===----------------------------------------------------------------------===//
52 // LiveVariablesDumper
53 //===----------------------------------------------------------------------===//
56 class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
58 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
59 BugReporter &BR) const {
60 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
61 L->dumpBlockLiveness(mgr.getSourceManager());
67 void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
68 mgr.registerChecker<LiveVariablesDumper>();
71 //===----------------------------------------------------------------------===//
72 // LiveStatementsDumper
73 //===----------------------------------------------------------------------===//
76 class LiveStatementsDumper : public Checker<check::ASTCodeBody> {
78 void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
79 BugReporter &BR) const {
80 if (LiveVariables *L = Mgr.getAnalysis<RelaxedLiveVariables>(D))
81 L->dumpStmtLiveness(Mgr.getSourceManager());
86 void ento::registerLiveStatementsDumper(CheckerManager &mgr) {
87 mgr.registerChecker<LiveStatementsDumper>();
90 //===----------------------------------------------------------------------===//
92 //===----------------------------------------------------------------------===//
95 class CFGViewer : public Checker<check::ASTCodeBody> {
97 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
98 BugReporter &BR) const {
99 if (CFG *cfg = mgr.getCFG(D)) {
100 cfg->viewCFG(mgr.getLangOpts());
106 void ento::registerCFGViewer(CheckerManager &mgr) {
107 mgr.registerChecker<CFGViewer>();
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
115 class CFGDumper : public Checker<check::ASTCodeBody> {
117 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
118 BugReporter &BR) const {
119 PrintingPolicy Policy(mgr.getLangOpts());
120 Policy.TerseOutput = true;
121 Policy.PolishForDeclaration = true;
122 D->print(llvm::errs(), Policy);
124 if (CFG *cfg = mgr.getCFG(D)) {
125 cfg->dump(mgr.getLangOpts(),
126 llvm::sys::Process::StandardErrHasColors());
132 void ento::registerCFGDumper(CheckerManager &mgr) {
133 mgr.registerChecker<CFGDumper>();
136 //===----------------------------------------------------------------------===//
138 //===----------------------------------------------------------------------===//
141 class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
143 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
144 BugReporter &BR) const {
146 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
152 void ento::registerCallGraphViewer(CheckerManager &mgr) {
153 mgr.registerChecker<CallGraphViewer>();
156 //===----------------------------------------------------------------------===//
158 //===----------------------------------------------------------------------===//
161 class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
163 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
164 BugReporter &BR) const {
166 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
172 void ento::registerCallGraphDumper(CheckerManager &mgr) {
173 mgr.registerChecker<CallGraphDumper>();
177 //===----------------------------------------------------------------------===//
179 //===----------------------------------------------------------------------===//
182 class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
183 typedef AnalyzerOptions::ConfigTable Table;
185 static int compareEntry(const Table::MapEntryTy *const *LHS,
186 const Table::MapEntryTy *const *RHS) {
187 return (*LHS)->getKey().compare((*RHS)->getKey());
191 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
192 AnalysisManager& mgr,
193 BugReporter &BR) const {
194 const Table &Config = mgr.options.Config;
196 SmallVector<const Table::MapEntryTy *, 32> Keys;
197 for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
200 llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
202 llvm::errs() << "[config]\n";
203 for (unsigned I = 0, E = Keys.size(); I != E; ++I)
204 llvm::errs() << Keys[I]->getKey() << " = "
205 << (Keys[I]->second.empty() ? "\"\"" : Keys[I]->second)
208 llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
213 void ento::registerConfigDumper(CheckerManager &mgr) {
214 mgr.registerChecker<ConfigDumper>();
217 //===----------------------------------------------------------------------===//
218 // ExplodedGraph Viewer
219 //===----------------------------------------------------------------------===//
222 class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
224 ExplodedGraphViewer() {}
225 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
232 void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
233 mgr.registerChecker<ExplodedGraphViewer>();