xref: /minix3/external/bsd/llvm/dist/clang/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //==- DebugCheckers.cpp - Debugging Checkers ---------------------*- C++ -*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file defines checkers that display debugging information.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
15f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/Dominators.h"
16f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/LiveVariables.h"
17f4a2713aSLionel Sambuc #include "clang/Analysis/CallGraph.h"
18f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
19f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
21f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/Process.h"
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc using namespace ento;
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
28f4a2713aSLionel Sambuc // DominatorsTreeDumper
29f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc namespace {
32f4a2713aSLionel Sambuc class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
33f4a2713aSLionel Sambuc public:
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const34f4a2713aSLionel Sambuc   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
35f4a2713aSLionel Sambuc                         BugReporter &BR) const {
36f4a2713aSLionel Sambuc     if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
37f4a2713aSLionel Sambuc       DominatorTree dom;
38f4a2713aSLionel Sambuc       dom.buildDominatorTree(*AC);
39f4a2713aSLionel Sambuc       dom.dump();
40f4a2713aSLionel Sambuc     }
41f4a2713aSLionel Sambuc   }
42f4a2713aSLionel Sambuc };
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc 
registerDominatorsTreeDumper(CheckerManager & mgr)45f4a2713aSLionel Sambuc void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
46f4a2713aSLionel Sambuc   mgr.registerChecker<DominatorsTreeDumper>();
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
50f4a2713aSLionel Sambuc // LiveVariablesDumper
51f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc namespace {
54f4a2713aSLionel Sambuc class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
55f4a2713aSLionel Sambuc public:
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const56f4a2713aSLionel Sambuc   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
57f4a2713aSLionel Sambuc                         BugReporter &BR) const {
58f4a2713aSLionel Sambuc     if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
59f4a2713aSLionel Sambuc       L->dumpBlockLiveness(mgr.getSourceManager());
60f4a2713aSLionel Sambuc     }
61f4a2713aSLionel Sambuc   }
62f4a2713aSLionel Sambuc };
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
registerLiveVariablesDumper(CheckerManager & mgr)65f4a2713aSLionel Sambuc void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
66f4a2713aSLionel Sambuc   mgr.registerChecker<LiveVariablesDumper>();
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
70f4a2713aSLionel Sambuc // CFGViewer
71f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc namespace {
74f4a2713aSLionel Sambuc class CFGViewer : public Checker<check::ASTCodeBody> {
75f4a2713aSLionel Sambuc public:
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const76f4a2713aSLionel Sambuc   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
77f4a2713aSLionel Sambuc                         BugReporter &BR) const {
78f4a2713aSLionel Sambuc     if (CFG *cfg = mgr.getCFG(D)) {
79f4a2713aSLionel Sambuc       cfg->viewCFG(mgr.getLangOpts());
80f4a2713aSLionel Sambuc     }
81f4a2713aSLionel Sambuc   }
82f4a2713aSLionel Sambuc };
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc 
registerCFGViewer(CheckerManager & mgr)85f4a2713aSLionel Sambuc void ento::registerCFGViewer(CheckerManager &mgr) {
86f4a2713aSLionel Sambuc   mgr.registerChecker<CFGViewer>();
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
90f4a2713aSLionel Sambuc // CFGDumper
91f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc namespace {
94f4a2713aSLionel Sambuc class CFGDumper : public Checker<check::ASTCodeBody> {
95f4a2713aSLionel Sambuc public:
checkASTCodeBody(const Decl * D,AnalysisManager & mgr,BugReporter & BR) const96f4a2713aSLionel Sambuc   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
97f4a2713aSLionel Sambuc                         BugReporter &BR) const {
98*0a6a1f1dSLionel Sambuc     PrintingPolicy Policy(mgr.getLangOpts());
99*0a6a1f1dSLionel Sambuc     Policy.TerseOutput = true;
100*0a6a1f1dSLionel Sambuc     Policy.PolishForDeclaration = true;
101*0a6a1f1dSLionel Sambuc     D->print(llvm::errs(), Policy);
102*0a6a1f1dSLionel Sambuc 
103f4a2713aSLionel Sambuc     if (CFG *cfg = mgr.getCFG(D)) {
104f4a2713aSLionel Sambuc       cfg->dump(mgr.getLangOpts(),
105f4a2713aSLionel Sambuc                 llvm::sys::Process::StandardErrHasColors());
106f4a2713aSLionel Sambuc     }
107f4a2713aSLionel Sambuc   }
108f4a2713aSLionel Sambuc };
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
registerCFGDumper(CheckerManager & mgr)111f4a2713aSLionel Sambuc void ento::registerCFGDumper(CheckerManager &mgr) {
112f4a2713aSLionel Sambuc   mgr.registerChecker<CFGDumper>();
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
116f4a2713aSLionel Sambuc // CallGraphViewer
117f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc namespace {
120f4a2713aSLionel Sambuc class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
121f4a2713aSLionel Sambuc public:
checkASTDecl(const TranslationUnitDecl * TU,AnalysisManager & mgr,BugReporter & BR) const122f4a2713aSLionel Sambuc   void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
123f4a2713aSLionel Sambuc                     BugReporter &BR) const {
124f4a2713aSLionel Sambuc     CallGraph CG;
125f4a2713aSLionel Sambuc     CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
126f4a2713aSLionel Sambuc     CG.viewGraph();
127f4a2713aSLionel Sambuc   }
128f4a2713aSLionel Sambuc };
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
registerCallGraphViewer(CheckerManager & mgr)131f4a2713aSLionel Sambuc void ento::registerCallGraphViewer(CheckerManager &mgr) {
132f4a2713aSLionel Sambuc   mgr.registerChecker<CallGraphViewer>();
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
136f4a2713aSLionel Sambuc // CallGraphDumper
137f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc namespace {
140f4a2713aSLionel Sambuc class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
141f4a2713aSLionel Sambuc public:
checkASTDecl(const TranslationUnitDecl * TU,AnalysisManager & mgr,BugReporter & BR) const142f4a2713aSLionel Sambuc   void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
143f4a2713aSLionel Sambuc                     BugReporter &BR) const {
144f4a2713aSLionel Sambuc     CallGraph CG;
145f4a2713aSLionel Sambuc     CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
146f4a2713aSLionel Sambuc     CG.dump();
147f4a2713aSLionel Sambuc   }
148f4a2713aSLionel Sambuc };
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc 
registerCallGraphDumper(CheckerManager & mgr)151f4a2713aSLionel Sambuc void ento::registerCallGraphDumper(CheckerManager &mgr) {
152f4a2713aSLionel Sambuc   mgr.registerChecker<CallGraphDumper>();
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
157f4a2713aSLionel Sambuc // ConfigDumper
158f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc namespace {
161f4a2713aSLionel Sambuc class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
162f4a2713aSLionel Sambuc   typedef AnalyzerOptions::ConfigTable Table;
163f4a2713aSLionel Sambuc 
compareEntry(const Table::MapEntryTy * const * LHS,const Table::MapEntryTy * const * RHS)164f4a2713aSLionel Sambuc   static int compareEntry(const Table::MapEntryTy *const *LHS,
165f4a2713aSLionel Sambuc                           const Table::MapEntryTy *const *RHS) {
166f4a2713aSLionel Sambuc     return (*LHS)->getKey().compare((*RHS)->getKey());
167f4a2713aSLionel Sambuc   }
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc public:
checkEndOfTranslationUnit(const TranslationUnitDecl * TU,AnalysisManager & mgr,BugReporter & BR) const170f4a2713aSLionel Sambuc   void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
171f4a2713aSLionel Sambuc                                  AnalysisManager& mgr,
172f4a2713aSLionel Sambuc                                  BugReporter &BR) const {
173f4a2713aSLionel Sambuc     const Table &Config = mgr.options.Config;
174f4a2713aSLionel Sambuc 
175f4a2713aSLionel Sambuc     SmallVector<const Table::MapEntryTy *, 32> Keys;
176f4a2713aSLionel Sambuc     for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
177f4a2713aSLionel Sambuc          ++I)
178f4a2713aSLionel Sambuc       Keys.push_back(&*I);
179f4a2713aSLionel Sambuc     llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc     llvm::errs() << "[config]\n";
182f4a2713aSLionel Sambuc     for (unsigned I = 0, E = Keys.size(); I != E; ++I)
183f4a2713aSLionel Sambuc       llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
184f4a2713aSLionel Sambuc 
185f4a2713aSLionel Sambuc     llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
186f4a2713aSLionel Sambuc   }
187f4a2713aSLionel Sambuc };
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc 
registerConfigDumper(CheckerManager & mgr)190f4a2713aSLionel Sambuc void ento::registerConfigDumper(CheckerManager &mgr) {
191f4a2713aSLionel Sambuc   mgr.registerChecker<ConfigDumper>();
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc 
194f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
195f4a2713aSLionel Sambuc // ExplodedGraph Viewer
196f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc namespace {
199f4a2713aSLionel Sambuc class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
200f4a2713aSLionel Sambuc public:
ExplodedGraphViewer()201f4a2713aSLionel Sambuc   ExplodedGraphViewer() {}
checkEndAnalysis(ExplodedGraph & G,BugReporter & B,ExprEngine & Eng) const202f4a2713aSLionel Sambuc   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
203f4a2713aSLionel Sambuc     Eng.ViewGraph(0);
204f4a2713aSLionel Sambuc   }
205f4a2713aSLionel Sambuc };
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc 
registerExplodedGraphViewer(CheckerManager & mgr)209f4a2713aSLionel Sambuc void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
210f4a2713aSLionel Sambuc   mgr.registerChecker<ExplodedGraphViewer>();
211f4a2713aSLionel Sambuc }
212