blob: 2eef1688d4c4b22c9d1df5a0f9e23017cfbd4d6f [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"
Gabor Horvathefec1632015-10-22 11:53:04 +000019#include "clang/StaticAnalyzer/Core/IssueHash.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Gabor Horvathefec1632015-10-22 11:53:04 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Anna Zaks7925e3d2013-06-24 18:12:12 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
24#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Ted Kremenek72be32a2011-12-22 23:33:52 +000025#include "llvm/Support/Process.h"
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000026
27using namespace clang;
28using namespace ento;
29
30//===----------------------------------------------------------------------===//
Ted Kremenek0062e742011-10-25 00:25:24 +000031// DominatorsTreeDumper
32//===----------------------------------------------------------------------===//
33
34namespace {
35class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
36public:
37 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
38 BugReporter &BR) const {
39 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
Anna Zaks02a1fc12011-12-05 21:33:11 +000040 DominatorTree dom;
41 dom.buildDominatorTree(*AC);
Ted Kremenek0062e742011-10-25 00:25:24 +000042 dom.dump();
43 }
44 }
45};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000046}
Ted Kremenek0062e742011-10-25 00:25:24 +000047
48void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
49 mgr.registerChecker<DominatorsTreeDumper>();
50}
51
52//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000053// LiveVariablesDumper
54//===----------------------------------------------------------------------===//
55
56namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000057class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000058public:
59 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
60 BugReporter &BR) const {
Ted Kremenekdccc2b22011-10-07 22:21:02 +000061 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000062 L->dumpBlockLiveness(mgr.getSourceManager());
63 }
64 }
65};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000066}
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000067
68void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
69 mgr.registerChecker<LiveVariablesDumper>();
70}
71
72//===----------------------------------------------------------------------===//
73// CFGViewer
74//===----------------------------------------------------------------------===//
75
76namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000077class CFGViewer : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000078public:
79 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
80 BugReporter &BR) const {
81 if (CFG *cfg = mgr.getCFG(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +000082 cfg->viewCFG(mgr.getLangOpts());
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000083 }
84 }
85};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000086}
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000087
88void ento::registerCFGViewer(CheckerManager &mgr) {
89 mgr.registerChecker<CFGViewer>();
90}
91
92//===----------------------------------------------------------------------===//
93// CFGDumper
94//===----------------------------------------------------------------------===//
95
96namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000097class CFGDumper : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +000098public:
99 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
100 BugReporter &BR) const {
Jordan Rosee02e96a2014-01-15 17:25:05 +0000101 PrintingPolicy Policy(mgr.getLangOpts());
102 Policy.TerseOutput = true;
103 Policy.PolishForDeclaration = true;
104 D->print(llvm::errs(), Policy);
105
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +0000106 if (CFG *cfg = mgr.getCFG(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000107 cfg->dump(mgr.getLangOpts(),
Ted Kremenek72be32a2011-12-22 23:33:52 +0000108 llvm::sys::Process::StandardErrHasColors());
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +0000109 }
110 }
111};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000112}
Argyrios Kyrtzidis57d736f2011-02-17 21:39:39 +0000113
114void ento::registerCFGDumper(CheckerManager &mgr) {
115 mgr.registerChecker<CFGDumper>();
116}
Anna Zaksc000e7e2012-03-08 00:42:23 +0000117
118//===----------------------------------------------------------------------===//
119// CallGraphViewer
120//===----------------------------------------------------------------------===//
121
122namespace {
123class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
124public:
125 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
126 BugReporter &BR) const {
127 CallGraph CG;
128 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
129 CG.viewGraph();
130 }
131};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000132}
Anna Zaksc000e7e2012-03-08 00:42:23 +0000133
134void ento::registerCallGraphViewer(CheckerManager &mgr) {
135 mgr.registerChecker<CallGraphViewer>();
136}
137
138//===----------------------------------------------------------------------===//
139// CallGraphDumper
140//===----------------------------------------------------------------------===//
141
142namespace {
143class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
144public:
145 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
146 BugReporter &BR) const {
147 CallGraph CG;
148 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
149 CG.dump();
150 }
151};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000152}
Anna Zaksc000e7e2012-03-08 00:42:23 +0000153
154void ento::registerCallGraphDumper(CheckerManager &mgr) {
155 mgr.registerChecker<CallGraphDumper>();
156}
Ted Kremenek86917fd2012-10-01 18:28:14 +0000157
158
159//===----------------------------------------------------------------------===//
160// ConfigDumper
161//===----------------------------------------------------------------------===//
162
163namespace {
164class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000165 typedef AnalyzerOptions::ConfigTable Table;
166
Benjamin Kramer4cadf292014-03-07 21:51:58 +0000167 static int compareEntry(const Table::MapEntryTy *const *LHS,
168 const Table::MapEntryTy *const *RHS) {
169 return (*LHS)->getKey().compare((*RHS)->getKey());
170 }
171
Ted Kremenek86917fd2012-10-01 18:28:14 +0000172public:
173 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
174 AnalysisManager& mgr,
175 BugReporter &BR) const {
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000176 const Table &Config = mgr.options.Config;
Ted Kremenek86917fd2012-10-01 18:28:14 +0000177
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000178 SmallVector<const Table::MapEntryTy *, 32> Keys;
179 for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
180 ++I)
181 Keys.push_back(&*I);
Benjamin Kramer4cadf292014-03-07 21:51:58 +0000182 llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
Ted Kremenek86917fd2012-10-01 18:28:14 +0000183
Ted Kremenek86917fd2012-10-01 18:28:14 +0000184 llvm::errs() << "[config]\n";
Benjamin Kramera92b80f2013-09-22 12:41:24 +0000185 for (unsigned I = 0, E = Keys.size(); I != E; ++I)
186 llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
187
Ted Kremenek86917fd2012-10-01 18:28:14 +0000188 llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
189 }
190};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000191}
Ted Kremenek86917fd2012-10-01 18:28:14 +0000192
193void ento::registerConfigDumper(CheckerManager &mgr) {
194 mgr.registerChecker<ConfigDumper>();
195}
Anna Zaks7925e3d2013-06-24 18:12:12 +0000196
197//===----------------------------------------------------------------------===//
198// ExplodedGraph Viewer
199//===----------------------------------------------------------------------===//
200
201namespace {
202class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
203public:
204 ExplodedGraphViewer() {}
205 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
206 Eng.ViewGraph(0);
207 }
208};
209
210}
211
212void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
213 mgr.registerChecker<ExplodedGraphViewer>();
214}
Gabor Horvathefec1632015-10-22 11:53:04 +0000215
216//===----------------------------------------------------------------------===//
217// DumpBugHash
218//===----------------------------------------------------------------------===//
219
220namespace {
221class BugHashDumper : public Checker<check::PostStmt<Stmt>> {
222public:
223 mutable std::unique_ptr<BugType> BT;
224
225 void checkPostStmt(const Stmt *S, CheckerContext &C) const {
226 if (!BT)
227 BT.reset(new BugType(this, "Dump hash components", "debug"));
228
229 ExplodedNode *N = C.generateNonFatalErrorNode();
230 if (!N)
231 return;
232
Gabor Horvathfe2c0ff2015-12-01 09:00:41 +0000233 const LangOptions &Opts = C.getLangOpts();
Gabor Horvathefec1632015-10-22 11:53:04 +0000234 const SourceManager &SM = C.getSourceManager();
235 FullSourceLoc FL(S->getLocStart(), SM);
236 std::string HashContent =
237 GetIssueString(SM, FL, getCheckName().getName(), BT->getCategory(),
Gabor Horvathfe2c0ff2015-12-01 09:00:41 +0000238 C.getLocationContext()->getDecl(), Opts);
Gabor Horvathefec1632015-10-22 11:53:04 +0000239
240 C.emitReport(llvm::make_unique<BugReport>(*BT, HashContent, N));
241 }
242};
243}
244
245void ento::registerBugHashDumper(CheckerManager &mgr) {
246 mgr.registerChecker<BugHashDumper>();
247}