Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1 | //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// |
| 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 | // "Meta" ASTConsumer for running different source analyses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 14 | #define DEBUG_TYPE "AnalysisConsumer" |
| 15 | |
Argyrios Kyrtzidis | e6348c3 | 2011-02-14 18:13:11 +0000 | [diff] [blame] | 16 | #include "AnalysisConsumer.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/AST/Decl.h" |
Zhongxing Xu | 802be99 | 2009-12-16 05:29:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 21 | #include "clang/AST/ParentMap.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/CFG.h" |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 23 | #include "clang/Analysis/CallGraph.h" |
Argyrios Kyrtzidis | 27af04b | 2011-02-15 16:54:12 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 25 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Ted Kremenek | 2114258 | 2010-12-23 19:38:26 +0000 | [diff] [blame] | 26 | #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 27 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
| 28 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 29 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 30 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
David Blaikie | f39d962 | 2011-09-27 01:43:33 +0000 | [diff] [blame] | 31 | #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" |
Argyrios Kyrtzidis | a7af5ea | 2010-12-22 18:52:56 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 33 | #include "clang/Basic/FileManager.h" |
| 34 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 9b414d3 | 2010-06-15 17:48:49 +0000 | [diff] [blame] | 35 | #include "clang/Frontend/AnalyzerOptions.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 36 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Path.h" |
| 39 | #include "llvm/Support/Program.h" |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Timer.h" |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 41 | #include "llvm/ADT/DepthFirstIterator.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/OwningPtr.h" |
Anna Zaks | 81fb169 | 2012-02-27 21:33:16 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/Statistic.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 45 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 46 | using namespace ento; |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 47 | using llvm::SmallPtrSet; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 49 | static ExplodedNode::Auditor* CreateUbiViz(); |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 50 | |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 51 | STATISTIC(NumFunctionsAnalyzed, "The # of functions analysed (as top level)."); |
| 52 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
David Blaikie | f39d962 | 2011-09-27 01:43:33 +0000 | [diff] [blame] | 54 | // Special PathDiagnosticConsumers. |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
| 56 | |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 57 | static PathDiagnosticConsumer* |
| 58 | createPlistHTMLDiagnosticConsumer(const std::string& prefix, |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 59 | const Preprocessor &PP) { |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 60 | PathDiagnosticConsumer *PD = |
| 61 | createHTMLDiagnosticConsumer(llvm::sys::path::parent_path(prefix), PP); |
| 62 | return createPlistDiagnosticConsumer(prefix, PP, PD); |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 66 | // AnalysisConsumer declaration. |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
| 69 | namespace { |
| 70 | |
Zhongxing Xu | ed8afac | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 71 | class AnalysisConsumer : public ASTConsumer { |
| 72 | public: |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 73 | ASTContext *Ctx; |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 74 | const Preprocessor &PP; |
| 75 | const std::string OutDir; |
| 76 | AnalyzerOptions Opts; |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 77 | ArrayRef<std::string> Plugins; |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 79 | // PD is owned by AnalysisManager. |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 80 | PathDiagnosticConsumer *PD; |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 82 | StoreManagerCreator CreateStoreMgr; |
| 83 | ConstraintManagerCreator CreateConstraintMgr; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 84 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 85 | OwningPtr<CheckerManager> checkerMgr; |
| 86 | OwningPtr<AnalysisManager> Mgr; |
Zhongxing Xu | c471e7b | 2009-08-03 03:13:46 +0000 | [diff] [blame] | 87 | |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 88 | /// Time the analyzes time of each translation unit. |
| 89 | static llvm::Timer* TUTotalTimer; |
| 90 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 91 | AnalysisConsumer(const Preprocessor& pp, |
| 92 | const std::string& outdir, |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 93 | const AnalyzerOptions& opts, |
| 94 | ArrayRef<std::string> plugins) |
| 95 | : Ctx(0), PP(pp), OutDir(outdir), Opts(opts), Plugins(plugins), PD(0) { |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 96 | DigestAnalyzerOptions(); |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 97 | if (Opts.PrintStats) { |
| 98 | llvm::EnableStatistics(); |
| 99 | TUTotalTimer = new llvm::Timer("Analyzer Total Time"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ~AnalysisConsumer() { |
| 104 | if (Opts.PrintStats) |
| 105 | delete TUTotalTimer; |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 106 | } |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 108 | void DigestAnalyzerOptions() { |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 109 | // Create the PathDiagnosticConsumer. |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 110 | if (!OutDir.empty()) { |
| 111 | switch (Opts.AnalysisDiagOpt) { |
| 112 | default: |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 113 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 114 | case PD_##NAME: PD = CREATEFN(OutDir, PP); break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 115 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 116 | } |
Argyrios Kyrtzidis | a599ae8 | 2010-12-03 01:17:19 +0000 | [diff] [blame] | 117 | } else if (Opts.AnalysisDiagOpt == PD_TEXT) { |
| 118 | // Create the text client even without a specified output file since |
| 119 | // it just uses diagnostic notes. |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 120 | PD = createTextPathDiagnosticConsumer("", PP); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 121 | } |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 123 | // Create the analyzer component creators. |
Argyrios Kyrtzidis | 5f83d6f | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 124 | switch (Opts.AnalysisStoreOpt) { |
| 125 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 126 | llvm_unreachable("Unknown store manager."); |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 127 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
Argyrios Kyrtzidis | 5f83d6f | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 128 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 129 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Argyrios Kyrtzidis | 5f83d6f | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 132 | switch (Opts.AnalysisConstraintsOpt) { |
| 133 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 134 | llvm_unreachable("Unknown store manager."); |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 135 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
Argyrios Kyrtzidis | 5f83d6f | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 136 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 137 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 138 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 141 | void DisplayFunction(const Decl *D) { |
Ted Kremenek | c4a1437 | 2010-06-25 20:59:24 +0000 | [diff] [blame] | 142 | if (!Opts.AnalyzerDisplayProgress) |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 143 | return; |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 145 | SourceManager &SM = Mgr->getASTContext().getSourceManager(); |
| 146 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 147 | if (Loc.isValid()) { |
| 148 | llvm::errs() << "ANALYZE: " << Loc.getFilename(); |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 150 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 151 | const NamedDecl *ND = cast<NamedDecl>(D); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 152 | llvm::errs() << ' ' << *ND << '\n'; |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 153 | } |
| 154 | else if (isa<BlockDecl>(D)) { |
| 155 | llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:" |
| 156 | << Loc.getColumn() << '\n'; |
| 157 | } |
| 158 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 159 | Selector S = MD->getSelector(); |
| 160 | llvm::errs() << ' ' << S.getAsString(); |
| 161 | } |
Ted Kremenek | 35fa76d | 2010-10-22 22:08:29 +0000 | [diff] [blame] | 162 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 165 | virtual void Initialize(ASTContext &Context) { |
| 166 | Ctx = &Context; |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 167 | checkerMgr.reset(createCheckerManager(Opts, PP.getLangOptions(), Plugins, |
| 168 | PP.getDiagnostics())); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 169 | Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(), |
| 170 | PP.getLangOptions(), PD, |
| 171 | CreateStoreMgr, CreateConstraintMgr, |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 172 | checkerMgr.get(), |
Zhongxing Xu | c6238d2 | 2010-07-19 01:31:21 +0000 | [diff] [blame] | 173 | /* Indexer */ 0, |
Zhongxing Xu | 6362b89 | 2010-05-18 00:28:37 +0000 | [diff] [blame] | 174 | Opts.MaxNodes, Opts.MaxLoop, |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 175 | Opts.VisualizeEGDot, Opts.VisualizeEGUbi, |
Anna Zaks | d309528 | 2011-09-30 02:03:00 +0000 | [diff] [blame] | 176 | Opts.AnalysisPurgeOpt, Opts.EagerlyAssume, |
Anna Zaks | 6625335 | 2012-03-08 23:16:35 +0000 | [diff] [blame] | 177 | Opts.TrimGraph, |
Marcin Swiderski | 9121ba2 | 2010-09-30 07:41:24 +0000 | [diff] [blame] | 178 | Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors, |
Ted Kremenek | d767d81 | 2011-02-09 01:27:33 +0000 | [diff] [blame] | 179 | Opts.CFGAddInitializers, |
Anna Zaks | 8235f9c | 2012-03-02 19:05:03 +0000 | [diff] [blame] | 180 | Opts.EagerlyTrimEGraph, |
Anna Zaks | 6625335 | 2012-03-08 23:16:35 +0000 | [diff] [blame] | 181 | Opts.IPAMode, |
Anna Zaks | 8235f9c | 2012-03-02 19:05:03 +0000 | [diff] [blame] | 182 | Opts.InlineMaxStackDepth, |
Anna Zaks | 6625335 | 2012-03-08 23:16:35 +0000 | [diff] [blame] | 183 | Opts.InlineMaxFunctionSize, |
| 184 | Opts.InliningMode)); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 187 | virtual void HandleTranslationUnit(ASTContext &C); |
Ted Kremenek | 14cc945 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 188 | void HandleDeclContext(ASTContext &C, DeclContext *dc); |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 189 | void HandleDeclContextDecl(ASTContext &C, Decl *D); |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 190 | void HandleDeclContextDeclFunction(ASTContext &C, Decl *D); |
Ted Kremenek | 14cc945 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 191 | |
Argyrios Kyrtzidis | c367a87 | 2011-02-28 22:30:38 +0000 | [diff] [blame] | 192 | void HandleCode(Decl *D); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 193 | }; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 194 | } // end anonymous namespace |
| 195 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | // AnalysisConsumer implementation. |
| 198 | //===----------------------------------------------------------------------===// |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 199 | llvm::Timer* AnalysisConsumer::TUTotalTimer = 0; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 14cc945 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 201 | void AnalysisConsumer::HandleDeclContext(ASTContext &C, DeclContext *dc) { |
| 202 | for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); |
Zhongxing Xu | ed8afac | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 203 | I != E; ++I) { |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 204 | HandleDeclContextDecl(C, *I); |
| 205 | } |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 206 | |
| 207 | // If inlining is not turned on, use the simplest function order. |
| 208 | if (!Mgr->shouldInlineCall()) { |
| 209 | for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); |
| 210 | I != E; ++I) |
| 211 | HandleDeclContextDeclFunction(C, *I); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // Otherwise, use the Callgraph to derive the order. |
| 216 | // Build the Call Graph. |
| 217 | CallGraph CG; |
| 218 | CG.addToCallGraph(dc); |
| 219 | |
| 220 | // Find the top level nodes - children of root + the unreachable (parentless) |
| 221 | // nodes. |
| 222 | llvm::SmallVector<CallGraphNode*, 24> TopLevelFunctions; |
| 223 | CallGraphNode *Entry = CG.getRoot(); |
| 224 | for (CallGraphNode::iterator I = Entry->begin(), |
| 225 | E = Entry->end(); I != E; ++I) |
| 226 | TopLevelFunctions.push_back(*I); |
| 227 | |
| 228 | for (CallGraph::nodes_iterator TI = CG.parentless_begin(), |
| 229 | TE = CG.parentless_end(); TI != TE; ++TI) |
| 230 | TopLevelFunctions.push_back(*TI); |
| 231 | |
| 232 | // TODO: Sort TopLevelFunctions. |
| 233 | |
| 234 | // DFS over all of the top level nodes. Use external Visited set, which is |
| 235 | // also modified when we inline a function. |
| 236 | SmallPtrSet<CallGraphNode*,24> Visited; |
| 237 | for (llvm::SmallVector<CallGraphNode*, 24>::iterator |
| 238 | TI = TopLevelFunctions.begin(), TE = TopLevelFunctions.end(); |
| 239 | TI != TE; ++TI) { |
| 240 | for (llvm::df_ext_iterator<CallGraphNode*, SmallPtrSet<CallGraphNode*,24> > |
| 241 | DFI = llvm::df_ext_begin(*TI, Visited), |
| 242 | E = llvm::df_ext_end(*TI, Visited); |
| 243 | DFI != E; ++DFI) { |
| 244 | Decl *D = (*DFI)->getDecl(); |
| 245 | assert(D); |
| 246 | HandleCode(D); |
| 247 | } |
| 248 | } |
| 249 | |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void AnalysisConsumer::HandleDeclContextDecl(ASTContext &C, Decl *D) { |
| 253 | { // Handle callbacks for arbitrary decls. |
| 254 | BugReporter BR(*Mgr); |
Argyrios Kyrtzidis | 9fb9474 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 255 | checkerMgr->runCheckersOnASTDecl(D, *Mgr, BR); |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 256 | } |
Argyrios Kyrtzidis | 9fb9474 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 258 | switch (D->getKind()) { |
| 259 | case Decl::Namespace: { |
| 260 | HandleDeclContext(C, cast<NamespaceDecl>(D)); |
| 261 | break; |
Zhongxing Xu | ed8afac | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 262 | } |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 263 | case Decl::ObjCCategoryImpl: |
| 264 | case Decl::ObjCImplementation: { |
| 265 | ObjCImplDecl *ID = cast<ObjCImplDecl>(D); |
| 266 | for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), |
| 267 | ME = ID->meth_end(); MI != ME; ++MI) { |
| 268 | BugReporter BR(*Mgr); |
| 269 | checkerMgr->runCheckersOnASTDecl(*MI, *Mgr, BR); |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | default: |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void AnalysisConsumer::HandleDeclContextDeclFunction(ASTContext &C, Decl *D) { |
| 280 | switch (D->getKind()) { |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 281 | case Decl::CXXConstructor: |
| 282 | case Decl::CXXDestructor: |
| 283 | case Decl::CXXConversion: |
| 284 | case Decl::CXXMethod: |
| 285 | case Decl::Function: { |
| 286 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 287 | // We skip function template definitions, as their semantics is |
| 288 | // only determined when they are instantiated. |
| 289 | if (FD->isThisDeclarationADefinition() && |
| 290 | !FD->isDependentContext()) { |
| 291 | if (!Opts.AnalyzeSpecificFunction.empty() && |
| 292 | FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction) |
| 293 | break; |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 294 | HandleCode(FD); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | case Decl::ObjCCategoryImpl: |
| 300 | case Decl::ObjCImplementation: { |
| 301 | ObjCImplDecl *ID = cast<ObjCImplDecl>(D); |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 302 | for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), |
| 303 | ME = ID->meth_end(); MI != ME; ++MI) { |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 304 | if ((*MI)->isThisDeclarationADefinition()) { |
| 305 | if (!Opts.AnalyzeSpecificFunction.empty() && |
| 306 | Opts.AnalyzeSpecificFunction != |
| 307 | (*MI)->getSelector().getAsString()) |
Anna Zaks | d1fe529 | 2011-09-10 00:12:23 +0000 | [diff] [blame] | 308 | continue; |
Ted Kremenek | fee618a | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 309 | HandleCode(*MI); |
| 310 | } |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | default: |
| 316 | break; |
| 317 | } |
Ted Kremenek | 14cc945 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 318 | } |
Zhongxing Xu | ed8afac | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 14cc945 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 320 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
Anna Zaks | c5bdc55 | 2012-01-07 16:49:46 +0000 | [diff] [blame] | 321 | { |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 322 | if (TUTotalTimer) TUTotalTimer->startTimer(); |
| 323 | |
Anna Zaks | c5bdc55 | 2012-01-07 16:49:46 +0000 | [diff] [blame] | 324 | // Introduce a scope to destroy BR before Mgr. |
| 325 | BugReporter BR(*Mgr); |
| 326 | TranslationUnitDecl *TU = C.getTranslationUnitDecl(); |
| 327 | checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR); |
| 328 | HandleDeclContext(C, TU); |
Zhongxing Xu | ed8afac | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 329 | |
Anna Zaks | c5bdc55 | 2012-01-07 16:49:46 +0000 | [diff] [blame] | 330 | // After all decls handled, run checkers on the entire TranslationUnit. |
| 331 | checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR); |
| 332 | } |
Ted Kremenek | 9be6e7c | 2011-05-05 03:41:17 +0000 | [diff] [blame] | 333 | |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 334 | // Explicitly destroy the PathDiagnosticConsumer. This will flush its output. |
Ted Kremenek | 690a7f4 | 2009-08-02 05:43:14 +0000 | [diff] [blame] | 335 | // FIXME: This should be replaced with something that doesn't rely on |
David Blaikie | ef3643f | 2011-09-26 00:51:36 +0000 | [diff] [blame] | 336 | // side-effects in PathDiagnosticConsumer's destructor. This is required when |
Zhongxing Xu | da17fd5 | 2009-12-15 09:32:42 +0000 | [diff] [blame] | 337 | // used with option -disable-free. |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 338 | Mgr.reset(NULL); |
Anna Zaks | d38f795 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 339 | |
| 340 | if (TUTotalTimer) TUTotalTimer->stopTimer(); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 343 | static void FindBlocks(DeclContext *D, SmallVectorImpl<Decl*> &WL) { |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 344 | if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 345 | WL.push_back(BD); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 347 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 348 | I!=E; ++I) |
| 349 | if (DeclContext *DC = dyn_cast<DeclContext>(*I)) |
| 350 | FindBlocks(DC, WL); |
| 351 | } |
| 352 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 353 | static void RunPathSensitiveChecks(AnalysisConsumer &C, AnalysisManager &mgr, |
| 354 | Decl *D); |
Argyrios Kyrtzidis | d655ab2 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 355 | |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 356 | static std::string getFunctionName(const Decl *D) { |
| 357 | if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) { |
| 358 | return ID->getSelector().getAsString(); |
| 359 | } |
| 360 | if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) { |
| 361 | IdentifierInfo *II = ND->getIdentifier(); |
| 362 | if (II) |
| 363 | return II->getName(); |
| 364 | } |
| 365 | return ""; |
| 366 | } |
| 367 | |
Argyrios Kyrtzidis | c367a87 | 2011-02-28 22:30:38 +0000 | [diff] [blame] | 368 | void AnalysisConsumer::HandleCode(Decl *D) { |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 369 | if (!Opts.AnalyzeSpecificFunction.empty() && |
| 370 | getFunctionName(D) != Opts.AnalyzeSpecificFunction) |
| 371 | return; |
| 372 | |
| 373 | DisplayFunction(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 375 | // Don't run the actions if an error has occurred with parsing the file. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 376 | DiagnosticsEngine &Diags = PP.getDiagnostics(); |
Ted Kremenek | 99e8192 | 2010-04-30 21:49:25 +0000 | [diff] [blame] | 377 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 378 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 380 | // Don't run the actions on declarations in header files unless |
| 381 | // otherwise specified. |
Ted Kremenek | fcd783d | 2010-06-15 00:55:40 +0000 | [diff] [blame] | 382 | SourceManager &SM = Ctx->getSourceManager(); |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 383 | SourceLocation SL = SM.getExpansionLoc(D->getLocation()); |
Ted Kremenek | fcd783d | 2010-06-15 00:55:40 +0000 | [diff] [blame] | 384 | if (!Opts.AnalyzeAll && !SM.isFromMainFile(SL)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | return; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 387 | // Clear the AnalysisManager of old AnalysisDeclContexts. |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 388 | Mgr->ClearContexts(); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 389 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | // Dispatch on the actions. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 391 | SmallVector<Decl*, 10> WL; |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 392 | WL.push_back(D); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 393 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 394 | if (D->hasBody() && Opts.AnalyzeNestedBlocks) |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 395 | FindBlocks(cast<DeclContext>(D), WL); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 396 | |
Argyrios Kyrtzidis | 9fb9474 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 397 | BugReporter BR(*Mgr); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 398 | for (SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); |
Argyrios Kyrtzidis | 9fb9474 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 399 | WI != WE; ++WI) |
Argyrios Kyrtzidis | d655ab2 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 400 | if ((*WI)->hasBody()) { |
Argyrios Kyrtzidis | 9fb9474 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 401 | checkerMgr->runCheckersOnASTBody(*WI, *Mgr, BR); |
Argyrios Kyrtzidis | d655ab2 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 402 | if (checkerMgr->hasPathSensitiveCheckers()) |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 403 | RunPathSensitiveChecks(*this, *Mgr, *WI); |
Argyrios Kyrtzidis | d655ab2 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 404 | } |
Anna Zaks | d95e0b8 | 2012-03-08 23:16:38 +0000 | [diff] [blame^] | 405 | NumFunctionsAnalyzed++; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | d655ab2 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 409 | // Path-sensitive checking. |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 410 | //===----------------------------------------------------------------------===// |
| 411 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 412 | static void ActionExprEngine(AnalysisConsumer &C, AnalysisManager &mgr, |
| 413 | Decl *D, bool ObjCGCEnabled) { |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 414 | // Construct the analysis engine. First check if the CFG is valid. |
Ted Kremenek | 75d03cf | 2009-09-18 22:29:35 +0000 | [diff] [blame] | 415 | // FIXME: Inter-procedural analysis will need to handle invalid CFGs. |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 416 | if (!mgr.getCFG(D)) |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 417 | return; |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 418 | ExprEngine Eng(mgr, ObjCGCEnabled); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 420 | // Set the graph auditor. |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 421 | OwningPtr<ExplodedNode::Auditor> Auditor; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 422 | if (mgr.shouldVisualizeUbigraph()) { |
| 423 | Auditor.reset(CreateUbiViz()); |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 424 | ExplodedNode::SetAuditor(Auditor.get()); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 427 | // Execute the worklist algorithm. |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 428 | Eng.ExecuteWorkList(mgr.getAnalysisDeclContextManager().getStackFrame(D, 0), |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 429 | mgr.getMaxNodes()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 431 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 432 | // created BugReporter. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 433 | ExplodedNode::SetAuditor(0); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 434 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 435 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 436 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 437 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 439 | // Display warnings. |
| 440 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 443 | static void RunPathSensitiveChecks(AnalysisConsumer &C, AnalysisManager &mgr, |
| 444 | Decl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 446 | switch (mgr.getLangOptions().getGC()) { |
Jordy Rose | 17a38e2 | 2011-09-02 05:55:19 +0000 | [diff] [blame] | 447 | case LangOptions::NonGC: |
| 448 | ActionExprEngine(C, mgr, D, false); |
| 449 | break; |
| 450 | |
| 451 | case LangOptions::GCOnly: |
| 452 | ActionExprEngine(C, mgr, D, true); |
| 453 | break; |
| 454 | |
| 455 | case LangOptions::HybridGC: |
| 456 | ActionExprEngine(C, mgr, D, false); |
| 457 | ActionExprEngine(C, mgr, D, true); |
| 458 | break; |
| 459 | } |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 462 | //===----------------------------------------------------------------------===// |
| 463 | // AnalysisConsumer creation. |
| 464 | //===----------------------------------------------------------------------===// |
| 465 | |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 466 | ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp, |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 467 | const std::string& outDir, |
| 468 | const AnalyzerOptions& opts, |
| 469 | ArrayRef<std::string> plugins) { |
| 470 | // Disable the effects of '-Werror' when using the AnalysisConsumer. |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 471 | pp.getDiagnostics().setWarningsAsErrors(false); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 472 | |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 473 | return new AnalysisConsumer(pp, outDir, opts, plugins); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 476 | //===----------------------------------------------------------------------===// |
| 477 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
| 480 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 482 | class UbigraphViz : public ExplodedNode::Auditor { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 483 | OwningPtr<raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 484 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 485 | unsigned Cntr; |
| 486 | |
| 487 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 488 | VMap M; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 490 | public: |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 491 | UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 492 | llvm::sys::Path& filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 494 | ~UbigraphViz(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 496 | virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 497 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 499 | } // end anonymous namespace |
| 500 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 501 | static ExplodedNode::Auditor* CreateUbiViz() { |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 502 | std::string ErrMsg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 504 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 505 | if (!ErrMsg.empty()) |
| 506 | return 0; |
| 507 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 508 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 509 | Filename.appendComponent("llvm_ubi"); |
| 510 | Filename.makeUnique(true,&ErrMsg); |
| 511 | |
| 512 | if (!ErrMsg.empty()) |
| 513 | return 0; |
| 514 | |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 515 | llvm::errs() << "Writing '" << Filename.str() << "'.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 517 | OwningPtr<llvm::raw_fd_ostream> Stream; |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 518 | Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 519 | |
| 520 | if (!ErrMsg.empty()) |
| 521 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 523 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 526 | void UbigraphViz::AddEdge(ExplodedNode *Src, ExplodedNode *Dst) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 528 | assert (Src != Dst && "Self-edges are not allowed."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 530 | // Lookup the Src. If it is a new node, it's a root. |
| 531 | VMap::iterator SrcI= M.find(Src); |
| 532 | unsigned SrcID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 534 | if (SrcI == M.end()) { |
| 535 | M[Src] = SrcID = Cntr++; |
| 536 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 537 | } |
| 538 | else |
| 539 | SrcID = SrcI->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 541 | // Lookup the Dst. |
| 542 | VMap::iterator DstI= M.find(Dst); |
| 543 | unsigned DstID; |
| 544 | |
| 545 | if (DstI == M.end()) { |
| 546 | M[Dst] = DstID = Cntr++; |
| 547 | *Out << "('vertex', " << DstID << ")\n"; |
| 548 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 549 | else { |
| 550 | // We have hit DstID before. Change its style to reflect a cache hit. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 551 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 552 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 553 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 554 | |
| 555 | // Add the edge. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | *Out << "('edge', " << SrcID << ", " << DstID |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 557 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 560 | UbigraphViz::UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 561 | llvm::sys::Path& filename) |
| 562 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 563 | |
| 564 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 565 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 566 | " ('size', '1.5'))\n"; |
| 567 | } |
| 568 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 569 | UbigraphViz::~UbigraphViz() { |
| 570 | Out.reset(0); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 571 | llvm::errs() << "Running 'ubiviz' program... "; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 572 | std::string ErrMsg; |
| 573 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 574 | std::vector<const char*> args; |
| 575 | args.push_back(Ubiviz.c_str()); |
| 576 | args.push_back(Filename.c_str()); |
| 577 | args.push_back(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 579 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 580 | llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 583 | // Delete the directory. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 585 | } |