1 //==- DebugCheckers.cpp - Debugging Checkers ---------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines checkers that display debugging information.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/Analysis/Analyses/Dominators.h"
16 #include "clang/Analysis/Analyses/LiveVariables.h"
17 #include "clang/Analysis/CallGraph.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
22 #include "llvm/Support/Process.h"
24 using namespace clang;
27 //===----------------------------------------------------------------------===//
28 // DominatorsTreeDumper
29 //===----------------------------------------------------------------------===//
32 class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
34 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
35 BugReporter &BR) const {
36 if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
38 dom.buildDominatorTree(*AC);
45 void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
46 mgr.registerChecker<DominatorsTreeDumper>();
49 //===----------------------------------------------------------------------===//
50 // LiveVariablesDumper
51 //===----------------------------------------------------------------------===//
54 class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
56 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
57 BugReporter &BR) const {
58 if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
59 L->dumpBlockLiveness(mgr.getSourceManager());
65 void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
66 mgr.registerChecker<LiveVariablesDumper>();
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
74 class CFGViewer : public Checker<check::ASTCodeBody> {
76 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
77 BugReporter &BR) const {
78 if (CFG *cfg = mgr.getCFG(D)) {
79 cfg->viewCFG(mgr.getLangOpts());
85 void ento::registerCFGViewer(CheckerManager &mgr) {
86 mgr.registerChecker<CFGViewer>();
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
94 class CFGDumper : public Checker<check::ASTCodeBody> {
96 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
97 BugReporter &BR) const {
98 if (CFG *cfg = mgr.getCFG(D)) {
99 cfg->dump(mgr.getLangOpts(),
100 llvm::sys::Process::StandardErrHasColors());
106 void ento::registerCFGDumper(CheckerManager &mgr) {
107 mgr.registerChecker<CFGDumper>();
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
115 class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
117 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
118 BugReporter &BR) const {
120 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
126 void ento::registerCallGraphViewer(CheckerManager &mgr) {
127 mgr.registerChecker<CallGraphViewer>();
130 //===----------------------------------------------------------------------===//
132 //===----------------------------------------------------------------------===//
135 class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
137 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
138 BugReporter &BR) const {
140 CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
146 void ento::registerCallGraphDumper(CheckerManager &mgr) {
147 mgr.registerChecker<CallGraphDumper>();
151 //===----------------------------------------------------------------------===//
153 //===----------------------------------------------------------------------===//
156 class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
157 typedef AnalyzerOptions::ConfigTable Table;
159 static int compareEntry(const Table::MapEntryTy *const *LHS,
160 const Table::MapEntryTy *const *RHS) {
161 return (*LHS)->getKey().compare((*RHS)->getKey());
165 void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
166 AnalysisManager& mgr,
167 BugReporter &BR) const {
168 const Table &Config = mgr.options.Config;
170 SmallVector<const Table::MapEntryTy *, 32> Keys;
171 for (Table::const_iterator I = Config.begin(), E = Config.end(); I != E;
174 llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
176 llvm::errs() << "[config]\n";
177 for (unsigned I = 0, E = Keys.size(); I != E; ++I)
178 llvm::errs() << Keys[I]->getKey() << " = " << Keys[I]->second << '\n';
180 llvm::errs() << "[stats]\n" << "num-entries = " << Keys.size() << '\n';
185 void ento::registerConfigDumper(CheckerManager &mgr) {
186 mgr.registerChecker<ConfigDumper>();
189 //===----------------------------------------------------------------------===//
190 // ExplodedGraph Viewer
191 //===----------------------------------------------------------------------===//
194 class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
196 ExplodedGraphViewer() {}
197 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
204 void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
205 mgr.registerChecker<ExplodedGraphViewer>();