Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 1 | //==- 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 | // |
| 10 | // This file defines a checkers that display debugging information. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 15 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 17 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 58f6f1e | 2011-10-25 00:25:24 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/Analyses/Dominators.h" |
Anna Zaks | 196b8cf | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/CallGraph.h" |
Ted Kremenek | 682060c | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Process.h" |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | using namespace ento; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 58f6f1e | 2011-10-25 00:25:24 +0000 | [diff] [blame] | 26 | // DominatorsTreeDumper |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | namespace { |
| 30 | class DominatorsTreeDumper : public Checker<check::ASTCodeBody> { |
| 31 | public: |
| 32 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 33 | BugReporter &BR) const { |
| 34 | if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) { |
Anna Zaks | 02f34c5 | 2011-12-05 21:33:11 +0000 | [diff] [blame] | 35 | DominatorTree dom; |
| 36 | dom.buildDominatorTree(*AC); |
Ted Kremenek | 58f6f1e | 2011-10-25 00:25:24 +0000 | [diff] [blame] | 37 | dom.dump(); |
| 38 | } |
| 39 | } |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | void ento::registerDominatorsTreeDumper(CheckerManager &mgr) { |
| 44 | mgr.registerChecker<DominatorsTreeDumper>(); |
| 45 | } |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 48 | // LiveVariablesDumper |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 52 | class LiveVariablesDumper : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 53 | public: |
| 54 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 55 | BugReporter &BR) const { |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 56 | if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) { |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 57 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 58 | } |
| 59 | } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | void ento::registerLiveVariablesDumper(CheckerManager &mgr) { |
| 64 | mgr.registerChecker<LiveVariablesDumper>(); |
| 65 | } |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | // CFGViewer |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 72 | class CFGViewer : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 73 | public: |
| 74 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 75 | BugReporter &BR) const { |
| 76 | if (CFG *cfg = mgr.getCFG(D)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 77 | cfg->viewCFG(mgr.getLangOpts()); |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | void ento::registerCFGViewer(CheckerManager &mgr) { |
| 84 | mgr.registerChecker<CFGViewer>(); |
| 85 | } |
| 86 | |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | // CFGDumper |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
| 91 | namespace { |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 92 | class CFGDumper : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 93 | public: |
| 94 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 95 | BugReporter &BR) const { |
| 96 | if (CFG *cfg = mgr.getCFG(D)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame^] | 97 | cfg->dump(mgr.getLangOpts(), |
Ted Kremenek | 682060c | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 98 | llvm::sys::Process::StandardErrHasColors()); |
Argyrios Kyrtzidis | 2d67b90 | 2011-02-17 21:39:39 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | void ento::registerCFGDumper(CheckerManager &mgr) { |
| 105 | mgr.registerChecker<CFGDumper>(); |
| 106 | } |
Anna Zaks | 196b8cf | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // CallGraphViewer |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | namespace { |
| 113 | class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > { |
| 114 | public: |
| 115 | void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr, |
| 116 | BugReporter &BR) const { |
| 117 | CallGraph CG; |
| 118 | CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU)); |
| 119 | CG.viewGraph(); |
| 120 | } |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | void ento::registerCallGraphViewer(CheckerManager &mgr) { |
| 125 | mgr.registerChecker<CallGraphViewer>(); |
| 126 | } |
| 127 | |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | // CallGraphDumper |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
| 132 | namespace { |
| 133 | class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > { |
| 134 | public: |
| 135 | void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr, |
| 136 | BugReporter &BR) const { |
| 137 | CallGraph CG; |
| 138 | CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU)); |
| 139 | CG.dump(); |
| 140 | } |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | void ento::registerCallGraphDumper(CheckerManager &mgr) { |
| 145 | mgr.registerChecker<CallGraphDumper>(); |
| 146 | } |