blob: a2c8d1fd8f3943d4792cfa0a06b903e1cc3210fd [file] [log] [blame]
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +00001//==- DebugCheckers.cpp - Debugging Checkers ---------------------*- 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//
Jordan Rose73b75e02013-04-12 00:44:24 +000010// This file defines checkers that display debugging information.
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Analysis/Analyses/Dominators.h"
16#include "clang/Analysis/Analyses/LiveVariables.h"
17#include "clang/Analysis/CallGraph.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Anna Zaks7925e3d2013-06-24 18:12:12 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek72be32a2011-12-22 23:33:52 +000022#include "llvm/Support/Process.h"
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000023
24using namespace clang;
25using namespace ento;
26
27//===----------------------------------------------------------------------===//
Ted Kremenek0062e742011-10-25 00:25:24 +000028// DominatorsTreeDumper
29//===----------------------------------------------------------------------===//
30
31namespace {
32class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
33public:
34 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
35 BugReporter &BR) const {
36 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
Anna Zaks02a1fc12011-12-05 21:33:11 +000037 DominatorTree dom;
38 dom.buildDominatorTree(*AC);
Ted Kremenek0062e742011-10-25 00:25:24 +000039 dom.dump();
40 }
41 }
42};
43}
44
45void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
46 mgr.registerChecker<DominatorsTreeDumper>();
47}
48
49//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000050// LiveVariablesDumper
51//===----------------------------------------------------------------------===//
52
53namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000054class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000055public:
56 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
57 BugReporter &BR) const {
Ted Kremenekdccc2b22011-10-07 22:21:02 +000058 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000059 L->dumpBlockLiveness(mgr.getSourceManager());
60 }
61 }
62};
63}
64
65void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
66 mgr.registerChecker<LiveVariablesDumper>();
67}
68
69//===----------------------------------------------------------------------===//
70// CFGViewer
71//===----------------------------------------------------------------------===//
72
73namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000074class CFGViewer : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000075public:
76 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
77 BugReporter &BR) const {
78 if (CFG *cfg = mgr.getCFG(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000079 cfg->viewCFG(mgr.getLangOpts());
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000080 }
81 }
82};
83}
84
85void ento::registerCFGViewer(CheckerManager &mgr) {
86 mgr.registerChecker<CFGViewer>();
87}
88
89//===----------------------------------------------------------------------===//
90// CFGDumper
91//===----------------------------------------------------------------------===//
92
93namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000094class CFGDumper : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000095public:
96 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
97 BugReporter &BR) const {
98 if (CFG *cfg = mgr.getCFG(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000099 cfg->dump(mgr.getLangOpts(),
Ted Kremenek72be32a2011-12-22 23:33:52 +0000100 llvm::sys::Process::StandardErrHasColors());
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +0000101 }
102 }
103};
104}
105
106void ento::registerCFGDumper(CheckerManager &mgr) {
107 mgr.registerChecker<CFGDumper>();
108}
Anna Zaksc000e7e2012-03-08 00:42:23 +0000109
110//===----------------------------------------------------------------------===//
111// CallGraphViewer
112//===----------------------------------------------------------------------===//
113
114namespace {
115class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
116public:
117 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
118 BugReporter &BR) const {
119 CallGraph CG;
120 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
121 CG.viewGraph();
122 }
123};
124}
125
126void ento::registerCallGraphViewer(CheckerManager &mgr) {
127 mgr.registerChecker<CallGraphViewer>();
128}
129
130//===----------------------------------------------------------------------===//
131// CallGraphDumper
132//===----------------------------------------------------------------------===//
133
134namespace {
135class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
136public:
137 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
138 BugReporter &BR) const {
139 CallGraph CG;
140 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
141 CG.dump();
142 }
143};
144}
145
146void ento::registerCallGraphDumper(CheckerManager &mgr) {
147 mgr.registerChecker<CallGraphDumper>();
148}
Ted Kremenek86917fd2012-10-01 18:28:14 +0000149
150
151//===----------------------------------------------------------------------===//
152// ConfigDumper
153//===----------------------------------------------------------------------===//
154
155namespace {
156class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000157 typedef AnalyzerOptions::ConfigTable Table;
158
Benjamin Kramer04bf1872013-09-22 14:10:29 +0000159 static int compareEntry(const Table::MapEntryTy *const *LHS,
160 const Table::MapEntryTy *const *RHS) {
161 return (*LHS)->getKey().compare((*RHS)->getKey());
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000162 }
163
Ted Kremenek86917fd2012-10-01 18:28:14 +0000164public:
165 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
166 AnalysisManager& mgr,
167 BugReporter &BR) const {
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000168 const Table &Config = mgr.options.Config;
Ted Kremenek86917fd2012-10-01 18:28:14 +0000169
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000170 SmallVector<const Table::MapEntryTy *, 32> Keys;
171 for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
172 ++I)
173 Keys.push_back(&*I);
174 llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
Ted Kremenek86917fd2012-10-01 18:28:14 +0000175
Ted Kremenek86917fd2012-10-01 18:28:14 +0000176 llvm::errs() << "[config]\n";
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000177 for (unsigned I = 0, E = Keys.size(); I != E; ++I)
178 llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
179
Ted Kremenek86917fd2012-10-01 18:28:14 +0000180 llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
181 }
182};
183}
184
185void ento::registerConfigDumper(CheckerManager &mgr) {
186 mgr.registerChecker<ConfigDumper>();
187}
Anna Zaks7925e3d2013-06-24 18:12:12 +0000188
189//===----------------------------------------------------------------------===//
190// ExplodedGraph Viewer
191//===----------------------------------------------------------------------===//
192
193namespace {
194class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
195public:
196 ExplodedGraphViewer() {}
197 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
198 Eng.ViewGraph(0);
199 }
200};
201
202}
203
204void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
205 mgr.registerChecker<ExplodedGraphViewer>();
206}