clang API Documentation
00001 //==- DebugCheckers.cpp - Debugging Checkers ---------------------*- C++ -*-==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a checkers that display debugging information. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "ClangSACheckers.h" 00015 #include "clang/StaticAnalyzer/Core/Checker.h" 00016 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 00017 #include "clang/Analysis/Analyses/LiveVariables.h" 00018 #include "clang/Analysis/Analyses/Dominators.h" 00019 #include "clang/Analysis/CallGraph.h" 00020 #include "llvm/Support/Process.h" 00021 00022 using namespace clang; 00023 using namespace ento; 00024 00025 //===----------------------------------------------------------------------===// 00026 // DominatorsTreeDumper 00027 //===----------------------------------------------------------------------===// 00028 00029 namespace { 00030 class DominatorsTreeDumper : public Checker<check::ASTCodeBody> { 00031 public: 00032 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 00033 BugReporter &BR) const { 00034 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) { 00035 DominatorTree dom; 00036 dom.buildDominatorTree(*AC); 00037 dom.dump(); 00038 } 00039 } 00040 }; 00041 } 00042 00043 void ento::registerDominatorsTreeDumper(CheckerManager &mgr) { 00044 mgr.registerChecker<DominatorsTreeDumper>(); 00045 } 00046 00047 //===----------------------------------------------------------------------===// 00048 // LiveVariablesDumper 00049 //===----------------------------------------------------------------------===// 00050 00051 namespace { 00052 class LiveVariablesDumper : public Checker<check::ASTCodeBody> { 00053 public: 00054 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 00055 BugReporter &BR) const { 00056 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) { 00057 L->dumpBlockLiveness(mgr.getSourceManager()); 00058 } 00059 } 00060 }; 00061 } 00062 00063 void ento::registerLiveVariablesDumper(CheckerManager &mgr) { 00064 mgr.registerChecker<LiveVariablesDumper>(); 00065 } 00066 00067 //===----------------------------------------------------------------------===// 00068 // CFGViewer 00069 //===----------------------------------------------------------------------===// 00070 00071 namespace { 00072 class CFGViewer : public Checker<check::ASTCodeBody> { 00073 public: 00074 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 00075 BugReporter &BR) const { 00076 if (CFG *cfg = mgr.getCFG(D)) { 00077 cfg->viewCFG(mgr.getLangOpts()); 00078 } 00079 } 00080 }; 00081 } 00082 00083 void ento::registerCFGViewer(CheckerManager &mgr) { 00084 mgr.registerChecker<CFGViewer>(); 00085 } 00086 00087 //===----------------------------------------------------------------------===// 00088 // CFGDumper 00089 //===----------------------------------------------------------------------===// 00090 00091 namespace { 00092 class CFGDumper : public Checker<check::ASTCodeBody> { 00093 public: 00094 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, 00095 BugReporter &BR) const { 00096 if (CFG *cfg = mgr.getCFG(D)) { 00097 cfg->dump(mgr.getLangOpts(), 00098 llvm::sys::Process::StandardErrHasColors()); 00099 } 00100 } 00101 }; 00102 } 00103 00104 void ento::registerCFGDumper(CheckerManager &mgr) { 00105 mgr.registerChecker<CFGDumper>(); 00106 } 00107 00108 //===----------------------------------------------------------------------===// 00109 // CallGraphViewer 00110 //===----------------------------------------------------------------------===// 00111 00112 namespace { 00113 class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > { 00114 public: 00115 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr, 00116 BugReporter &BR) const { 00117 CallGraph CG; 00118 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU)); 00119 CG.viewGraph(); 00120 } 00121 }; 00122 } 00123 00124 void ento::registerCallGraphViewer(CheckerManager &mgr) { 00125 mgr.registerChecker<CallGraphViewer>(); 00126 } 00127 00128 //===----------------------------------------------------------------------===// 00129 // CallGraphDumper 00130 //===----------------------------------------------------------------------===// 00131 00132 namespace { 00133 class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > { 00134 public: 00135 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr, 00136 BugReporter &BR) const { 00137 CallGraph CG; 00138 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU)); 00139 CG.dump(); 00140 } 00141 }; 00142 } 00143 00144 void ento::registerCallGraphDumper(CheckerManager &mgr) { 00145 mgr.registerChecker<CallGraphDumper>(); 00146 }