DebugCheckers.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 checkers that display debugging information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ClangSACheckers.h"
  14. #include "clang/Analysis/Analyses/Dominators.h"
  15. #include "clang/Analysis/Analyses/LiveVariables.h"
  16. #include "clang/Analysis/CallGraph.h"
  17. #include "clang/StaticAnalyzer/Core/Checker.h"
  18. #include "clang/StaticAnalyzer/Core/IssueHash.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  21. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  22. #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  24. #include "llvm/Support/Process.h"
  25. using namespace clang;
  26. using namespace ento;
  27. //===----------------------------------------------------------------------===//
  28. // DominatorsTreeDumper
  29. //===----------------------------------------------------------------------===//
  30. namespace {
  31. class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
  32. public:
  33. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  34. BugReporter &BR) const {
  35. if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
  36. DominatorTree dom;
  37. dom.buildDominatorTree(*AC);
  38. dom.dump();
  39. }
  40. }
  41. };
  42. }
  43. void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
  44. mgr.registerChecker<DominatorsTreeDumper>();
  45. }
  46. //===----------------------------------------------------------------------===//
  47. // LiveVariablesDumper
  48. //===----------------------------------------------------------------------===//
  49. namespace {
  50. class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
  51. public:
  52. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  53. BugReporter &BR) const {
  54. if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
  55. L->dumpBlockLiveness(mgr.getSourceManager());
  56. }
  57. }
  58. };
  59. }
  60. void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
  61. mgr.registerChecker<LiveVariablesDumper>();
  62. }
  63. //===----------------------------------------------------------------------===//
  64. // CFGViewer
  65. //===----------------------------------------------------------------------===//
  66. namespace {
  67. class CFGViewer : public Checker<check::ASTCodeBody> {
  68. public:
  69. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  70. BugReporter &BR) const {
  71. if (CFG *cfg = mgr.getCFG(D)) {
  72. cfg->viewCFG(mgr.getLangOpts());
  73. }
  74. }
  75. };
  76. }
  77. void ento::registerCFGViewer(CheckerManager &mgr) {
  78. mgr.registerChecker<CFGViewer>();
  79. }
  80. //===----------------------------------------------------------------------===//
  81. // CFGDumper
  82. //===----------------------------------------------------------------------===//
  83. namespace {
  84. class CFGDumper : public Checker<check::ASTCodeBody> {
  85. public:
  86. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  87. BugReporter &BR) const {
  88. PrintingPolicy Policy(mgr.getLangOpts());
  89. Policy.TerseOutput = true;
  90. Policy.PolishForDeclaration = true;
  91. D->print(llvm::errs(), Policy);
  92. if (CFG *cfg = mgr.getCFG(D)) {
  93. cfg->dump(mgr.getLangOpts(),
  94. llvm::sys::Process::StandardErrHasColors());
  95. }
  96. }
  97. };
  98. }
  99. void ento::registerCFGDumper(CheckerManager &mgr) {
  100. mgr.registerChecker<CFGDumper>();
  101. }
  102. //===----------------------------------------------------------------------===//
  103. // CallGraphViewer
  104. //===----------------------------------------------------------------------===//
  105. namespace {
  106. class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
  107. public:
  108. void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
  109. BugReporter &BR) const {
  110. CallGraph CG;
  111. CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
  112. CG.viewGraph();
  113. }
  114. };
  115. }
  116. void ento::registerCallGraphViewer(CheckerManager &mgr) {
  117. mgr.registerChecker<CallGraphViewer>();
  118. }
  119. //===----------------------------------------------------------------------===//
  120. // CallGraphDumper
  121. //===----------------------------------------------------------------------===//
  122. namespace {
  123. class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
  124. public:
  125. void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
  126. BugReporter &BR) const {
  127. CallGraph CG;
  128. CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
  129. CG.dump();
  130. }
  131. };
  132. }
  133. void ento::registerCallGraphDumper(CheckerManager &mgr) {
  134. mgr.registerChecker<CallGraphDumper>();
  135. }
  136. //===----------------------------------------------------------------------===//
  137. // ConfigDumper
  138. //===----------------------------------------------------------------------===//
  139. namespace {
  140. class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
  141. typedef AnalyzerOptions::ConfigTable Table;
  142. static int compareEntry(const Table::MapEntryTy *const *LHS,
  143. const Table::MapEntryTy *const *RHS) {
  144. return (*LHS)->getKey().compare((*RHS)->getKey());
  145. }
  146. public:
  147. void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
  148. AnalysisManager& mgr,
  149. BugReporter &BR) const {
  150. const Table &Config = mgr.options.Config;
  151. SmallVector<const Table::MapEntryTy *, 32> Keys;
  152. for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
  153. ++I)
  154. Keys.push_back(&*I);
  155. llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
  156. llvm::errs() << "[config]\n";
  157. for (unsigned I = 0, E = Keys.size(); I != E; ++I)
  158. llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
  159. llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
  160. }
  161. };
  162. }
  163. void ento::registerConfigDumper(CheckerManager &mgr) {
  164. mgr.registerChecker<ConfigDumper>();
  165. }
  166. //===----------------------------------------------------------------------===//
  167. // ExplodedGraph Viewer
  168. //===----------------------------------------------------------------------===//
  169. namespace {
  170. class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
  171. public:
  172. ExplodedGraphViewer() {}
  173. void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
  174. Eng.ViewGraph(0);
  175. }
  176. };
  177. }
  178. void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
  179. mgr.registerChecker<ExplodedGraphViewer>();
  180. }
  181. //===----------------------------------------------------------------------===//
  182. // DumpBugHash
  183. //===----------------------------------------------------------------------===//
  184. namespace {
  185. class BugHashDumper : public Checker<check::PostStmt<Stmt>> {
  186. public:
  187. mutable std::unique_ptr<BugType> BT;
  188. void checkPostStmt(const Stmt *S, CheckerContext &C) const {
  189. if (!BT)
  190. BT.reset(new BugType(this, "Dump hash components", "debug"));
  191. ExplodedNode *N = C.generateNonFatalErrorNode();
  192. if (!N)
  193. return;
  194. const LangOptions &Opts = C.getLangOpts();
  195. const SourceManager &SM = C.getSourceManager();
  196. FullSourceLoc FL(S->getLocStart(), SM);
  197. std::string HashContent =
  198. GetIssueString(SM, FL, getCheckName().getName(), BT->getCategory(),
  199. C.getLocationContext()->getDecl(), Opts);
  200. C.emitReport(llvm::make_unique<BugReport>(*BT, HashContent, N));
  201. }
  202. };
  203. }
  204. void ento::registerBugHashDumper(CheckerManager &mgr) {
  205. mgr.registerChecker<BugHashDumper>();
  206. }