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 | |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/AnalysisConsumer.h" |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 16 | #include "clang/Frontend/ManagerRegistry.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" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "clang/AST/CFG.h" |
| 23 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 24 | #include "clang/Analysis/PathDiagnostic.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
| 26 | #include "clang/Basic/FileManager.h" |
| 27 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 29 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 30 | #include "clang/Analysis/LocalCheckers.h" |
| 31 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
| 32 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Streams.h" |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | #include "llvm/System/Path.h" |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 37 | #include "llvm/System/Program.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 41 | static ExplodedNodeImpl::Auditor* CreateUbiViz(); |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 44 | // Basic type definitions. |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 47 | namespace { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 48 | class AnalysisManager; |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 49 | typedef void (*CodeAction)(AnalysisManager& Mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 50 | } // end anonymous namespace |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // AnalysisConsumer declaration. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | namespace { |
| 57 | |
| 58 | class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 59 | typedef std::vector<CodeAction> Actions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 60 | Actions FunctionActions; |
| 61 | Actions ObjCMethodActions; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 62 | Actions ObjCImplementationActions; |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 63 | Actions TranslationUnitActions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 66 | const LangOptions& LOpts; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 67 | Diagnostic &Diags; |
| 68 | ASTContext* Ctx; |
| 69 | Preprocessor* PP; |
| 70 | PreprocessorFactory* PPF; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 71 | const std::string OutDir; |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 72 | AnalyzerOptions Opts; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 73 | llvm::OwningPtr<PathDiagnosticClient> PD; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 74 | |
| 75 | AnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
| 76 | PreprocessorFactory* ppf, |
| 77 | const LangOptions& lopts, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 78 | const std::string& outdir, |
| 79 | const AnalyzerOptions& opts) |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 80 | : LOpts(lopts), Diags(diags), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 81 | Ctx(0), PP(pp), PPF(ppf), |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 82 | OutDir(outdir), Opts(opts) {} |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 83 | |
| 84 | void addCodeAction(CodeAction action) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 85 | FunctionActions.push_back(action); |
| 86 | ObjCMethodActions.push_back(action); |
| 87 | } |
| 88 | |
| 89 | void addObjCImplementationAction(CodeAction action) { |
| 90 | ObjCImplementationActions.push_back(action); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 93 | void addTranslationUnitAction(CodeAction action) { |
| 94 | TranslationUnitActions.push_back(action); |
| 95 | } |
| 96 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 97 | virtual void Initialize(ASTContext &Context) { |
| 98 | Ctx = &Context; |
| 99 | } |
| 100 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 101 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 102 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 103 | HandleTopLevelSingleDecl(*I); |
| 104 | } |
| 105 | |
| 106 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 107 | virtual void HandleTranslationUnit(ASTContext &C); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 109 | void HandleCode(Decl* D, Stmt* Body, Actions& actions); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 113 | class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData { |
Ted Kremenek | f304ddc | 2008-11-05 19:05:06 +0000 | [diff] [blame] | 114 | Decl* D; Stmt* Body; |
Ted Kremenek | f304ddc | 2008-11-05 19:05:06 +0000 | [diff] [blame] | 115 | |
| 116 | enum AnalysisScope { ScopeTU, ScopeDecl } AScope; |
| 117 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 118 | AnalysisConsumer& C; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 119 | bool DisplayedFunction; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 120 | |
| 121 | llvm::OwningPtr<CFG> cfg; |
| 122 | llvm::OwningPtr<LiveVariables> liveness; |
| 123 | llvm::OwningPtr<ParentMap> PM; |
| 124 | |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 125 | // Configurable components creators. |
| 126 | StoreManagerCreator CreateStoreMgr; |
| 127 | ConstraintManagerCreator CreateConstraintMgr; |
| 128 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 129 | public: |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 130 | AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress) |
Chris Lattner | 678dc3b | 2009-03-28 04:05:05 +0000 | [diff] [blame] | 131 | : D(d), Body(b), AScope(ScopeDecl), C(c), |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 132 | DisplayedFunction(!displayProgress) { |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 133 | setManagerCreators(); |
| 134 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 678dc3b | 2009-03-28 04:05:05 +0000 | [diff] [blame] | 136 | AnalysisManager(AnalysisConsumer& c, bool displayProgress) |
| 137 | : D(0), Body(0), AScope(ScopeTU), C(c), |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 138 | DisplayedFunction(!displayProgress) { |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 139 | setManagerCreators(); |
| 140 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | f304ddc | 2008-11-05 19:05:06 +0000 | [diff] [blame] | 142 | Decl* getCodeDecl() const { |
| 143 | assert (AScope == ScopeDecl); |
| 144 | return D; |
| 145 | } |
| 146 | |
| 147 | Stmt* getBody() const { |
| 148 | assert (AScope == ScopeDecl); |
| 149 | return Body; |
| 150 | } |
| 151 | |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 152 | StoreManagerCreator getStoreManagerCreator() { |
| 153 | return CreateStoreMgr; |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 154 | }; |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 155 | |
| 156 | ConstraintManagerCreator getConstraintManagerCreator() { |
| 157 | return CreateConstraintMgr; |
| 158 | } |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 160 | virtual CFG* getCFG() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 161 | if (!cfg) cfg.reset(CFG::buildCFG(getBody())); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 162 | return cfg.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 165 | virtual ParentMap& getParentMap() { |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 166 | if (!PM) |
| 167 | PM.reset(new ParentMap(getBody())); |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 168 | return *PM.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 171 | virtual ASTContext& getContext() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 172 | return *C.Ctx; |
| 173 | } |
| 174 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 175 | virtual SourceManager& getSourceManager() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 176 | return getContext().getSourceManager(); |
| 177 | } |
| 178 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 179 | virtual Diagnostic& getDiagnostic() { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 180 | return C.Diags; |
| 181 | } |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 182 | |
| 183 | const LangOptions& getLangOptions() const { |
| 184 | return C.LOpts; |
| 185 | } |
| 186 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 187 | virtual PathDiagnosticClient* getPathDiagnosticClient() { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 188 | if (C.PD.get() == 0 && !C.OutDir.empty()) { |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 189 | switch (C.Opts.AnalysisDiagOpt) { |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 190 | default: |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 191 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\ |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 192 | case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 193 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Ted Kremenek | e207558 | 2008-07-02 23:16:33 +0000 | [diff] [blame] | 196 | return C.PD.get(); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 197 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 199 | virtual LiveVariables* getLiveVariables() { |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 200 | if (!liveness) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 201 | CFG* c = getCFG(); |
| 202 | if (!c) return 0; |
| 203 | |
Ted Kremenek | ca9bab0 | 2008-12-09 00:17:51 +0000 | [diff] [blame] | 204 | liveness.reset(new LiveVariables(getContext(), *c)); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 205 | liveness->runOnCFG(*c); |
| 206 | liveness->runOnAllBlocks(*c, 0, true); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 207 | } |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 209 | return liveness.get(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 210 | } |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 211 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 212 | bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 213 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 214 | bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 215 | |
| 216 | bool shouldVisualize() const { |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 217 | return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 218 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 219 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 220 | bool shouldTrimGraph() const { return C.Opts.TrimGraph; } |
| 221 | |
| 222 | bool shouldPurgeDead() const { return C.Opts.PurgeDead; } |
| 223 | |
| 224 | bool shouldEagerlyAssume() const { return C.Opts.EagerlyAssume; } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 226 | void DisplayFunction() { |
| 227 | |
| 228 | if (DisplayedFunction) |
| 229 | return; |
| 230 | |
| 231 | DisplayedFunction = true; |
| 232 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 233 | // FIXME: Is getCodeDecl() always a named decl? |
| 234 | if (isa<FunctionDecl>(getCodeDecl()) || |
| 235 | isa<ObjCMethodDecl>(getCodeDecl())) { |
| 236 | NamedDecl *ND = cast<NamedDecl>(getCodeDecl()); |
| 237 | SourceManager &SM = getContext().getSourceManager(); |
Ted Kremenek | a88fcef | 2008-11-20 16:14:48 +0000 | [diff] [blame] | 238 | llvm::cerr << "ANALYZE: " |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 239 | << SM.getPresumedLoc(ND->getLocation()).getFilename() |
| 240 | << ' ' << ND->getNameAsString() << '\n'; |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 243 | |
| 244 | private: |
| 245 | /// Set configurable analyzer components creators. First check if there are |
| 246 | /// components registered at runtime. Otherwise fall back to builtin |
| 247 | /// components. |
| 248 | void setManagerCreators() { |
| 249 | if (ManagerRegistry::StoreMgrCreator != 0) { |
| 250 | CreateStoreMgr = ManagerRegistry::StoreMgrCreator; |
| 251 | } |
| 252 | else { |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 253 | switch (C.Opts.AnalysisStoreOpt) { |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 254 | default: |
| 255 | assert(0 && "Unknown store manager."); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 256 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 257 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 258 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | if (ManagerRegistry::ConstraintMgrCreator != 0) |
| 263 | CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 264 | else { |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 265 | switch (C.Opts.AnalysisConstraintsOpt) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 266 | default: |
| 267 | assert(0 && "Unknown store manager."); |
| 268 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 269 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 270 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 274 | |
| 275 | // Some DiagnosticClients should be created all the time instead of |
| 276 | // lazily. Create those now. |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 277 | switch (C.Opts.AnalysisDiagOpt) { |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 278 | default: break; |
| 279 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\ |
| 280 | case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 281 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 282 | } |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 285 | }; |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 287 | } // end anonymous namespace |
| 288 | |
| 289 | namespace llvm { |
| 290 | template <> struct FoldingSetTrait<CodeAction> { |
| 291 | static inline void Profile(CodeAction X, FoldingSetNodeID& ID) { |
| 292 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 293 | } |
| 294 | }; |
| 295 | } |
| 296 | |
| 297 | //===----------------------------------------------------------------------===// |
| 298 | // AnalysisConsumer implementation. |
| 299 | //===----------------------------------------------------------------------===// |
| 300 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 301 | void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 302 | switch (D->getKind()) { |
| 303 | case Decl::Function: { |
| 304 | FunctionDecl* FD = cast<FunctionDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 305 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 306 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 307 | Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName()) |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 308 | break; |
| 309 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 310 | Stmt* Body = FD->getBody(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 311 | if (Body) HandleCode(FD, Body, FunctionActions); |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | case Decl::ObjCMethod: { |
| 316 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 317 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 318 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 319 | Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString()) |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 320 | return; |
| 321 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 322 | Stmt* Body = MD->getBody(); |
| 323 | if (Body) HandleCode(MD, Body, ObjCMethodActions); |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | default: |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 332 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 334 | if(!TranslationUnitActions.empty()) { |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 335 | AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress); |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 336 | for (Actions::iterator I = TranslationUnitActions.begin(), |
| 337 | E = TranslationUnitActions.end(); I != E; ++I) |
| 338 | (*I)(mgr); |
| 339 | } |
| 340 | |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 341 | if (!ObjCImplementationActions.empty()) { |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 342 | TranslationUnitDecl *TUD = C.getTranslationUnitDecl(); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 343 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 344 | for (DeclContext::decl_iterator I = TUD->decls_begin(), |
| 345 | E = TUD->decls_end(); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 346 | I != E; ++I) |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 347 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 348 | HandleCode(ID, 0, ObjCImplementationActions); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 349 | } |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 350 | |
| 351 | // Delete the PathDiagnosticClient here just in case the AnalysisConsumer |
| 352 | // object doesn't get released. This will cause any side-effects in the |
| 353 | // destructor of the PathDiagnosticClient to get executed. |
| 354 | PD.reset(); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 357 | void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 358 | |
| 359 | // Don't run the actions if an error has occured with parsing the file. |
| 360 | if (Diags.hasErrorOccurred()) |
| 361 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 363 | // Don't run the actions on declarations in header files unless |
| 364 | // otherwise specified. |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 365 | if (!Opts.AnalyzeAll && |
| 366 | !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 367 | return; |
| 368 | |
| 369 | // Create an AnalysisManager that will manage the state for analyzing |
| 370 | // this method/function. |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 371 | AnalysisManager mgr(*this, D, Body, Opts.AnalyzerDisplayProgress); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 372 | |
| 373 | // Dispatch on the actions. |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 374 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 375 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | // Analyses |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 382 | static void ActionWarnDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 383 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 384 | BugReporter BR(mgr); |
| 385 | CheckDeadStores(*L, BR); |
| 386 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 389 | static void ActionWarnUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 390 | if (CFG* c = mgr.getCFG()) |
| 391 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 395 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf, |
| 396 | bool StandardWarnings = true) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 399 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 401 | // Display progress. |
| 402 | mgr.DisplayFunction(); |
| 403 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 404 | // Construct the analysis engine. |
| 405 | LiveVariables* L = mgr.getLiveVariables(); |
| 406 | if (!L) return; |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 407 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 408 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 409 | mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(), |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 410 | mgr.getStoreManagerCreator(), |
| 411 | mgr.getConstraintManagerCreator()); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 413 | Eng.setTransferFunctions(tf); |
| 414 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 415 | if (StandardWarnings) { |
| 416 | Eng.RegisterInternalChecks(); |
| 417 | RegisterAppleChecks(Eng); |
| 418 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 419 | |
| 420 | // Set the graph auditor. |
| 421 | llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor; |
| 422 | if (mgr.shouldVisualizeUbigraph()) { |
| 423 | Auditor.reset(CreateUbiViz()); |
| 424 | ExplodedNodeImpl::SetAuditor(Auditor.get()); |
| 425 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 427 | // Execute the worklist algorithm. |
| 428 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 429 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 430 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 431 | // created BugReporter. |
| 432 | ExplodedNodeImpl::SetAuditor(0); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 434 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 435 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 436 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 437 | |
| 438 | // Display warnings. |
| 439 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 442 | static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled, |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 443 | bool StandardWarnings) { |
| 444 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 445 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 446 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 447 | mgr.getLangOptions()); |
| 448 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 449 | ActionGRExprEngine(mgr, TF, StandardWarnings); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 452 | static void ActionCheckerCFRef(AnalysisManager& mgr) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 453 | |
| 454 | switch (mgr.getLangOptions().getGCMode()) { |
| 455 | default: |
| 456 | assert (false && "Invalid GC mode."); |
| 457 | case LangOptions::NonGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 458 | ActionCheckerCFRefAux(mgr, false, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 459 | break; |
| 460 | |
| 461 | case LangOptions::GCOnly: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 462 | ActionCheckerCFRefAux(mgr, true, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | case LangOptions::HybridGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 466 | ActionCheckerCFRefAux(mgr, false, true); |
| 467 | ActionCheckerCFRefAux(mgr, true, false); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 472 | static void ActionDisplayLiveVariables(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 473 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 474 | mgr.DisplayFunction(); |
| 475 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 476 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 479 | static void ActionCFGDump(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 480 | if (CFG* c = mgr.getCFG()) { |
| 481 | mgr.DisplayFunction(); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 482 | LangOptions LO; // FIXME! |
| 483 | c->dump(LO); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 484 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 488 | if (CFG* c = mgr.getCFG()) { |
| 489 | mgr.DisplayFunction(); |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 490 | LangOptions LO; // FIXME! |
| 491 | c->viewCFG(LO); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 492 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 495 | static void ActionWarnObjCDealloc(AnalysisManager& mgr) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 496 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 497 | return; |
| 498 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 499 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 500 | |
| 501 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 502 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 505 | static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) { |
| 506 | BugReporter BR(mgr); |
| 507 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR); |
| 508 | } |
| 509 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 510 | static void ActionWarnObjCMethSigs(AnalysisManager& mgr) { |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 511 | BugReporter BR(mgr); |
| 512 | |
| 513 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 514 | BR); |
| 515 | } |
| 516 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 517 | //===----------------------------------------------------------------------===// |
| 518 | // AnalysisConsumer creation. |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 521 | ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 522 | PreprocessorFactory* ppf, |
| 523 | const LangOptions& lopts, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 524 | const std::string& OutDir, |
| 525 | const AnalyzerOptions& Opts) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 526 | |
| 527 | llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 528 | lopts, OutDir, |
| 529 | Opts)); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 530 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 531 | for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i) |
| 532 | switch (Opts.AnalysisList[i]) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 533 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 534 | case NAME:\ |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 535 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 536 | break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 537 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 538 | default: break; |
| 539 | } |
Ted Kremenek | 2c4036e | 2009-05-07 19:02:53 +0000 | [diff] [blame] | 540 | |
| 541 | // Last, disable the effects of '-Werror' when using the AnalysisConsumer. |
| 542 | diags.setWarningsAsErrors(false); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 544 | return C.take(); |
| 545 | } |
| 546 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 547 | //===----------------------------------------------------------------------===// |
| 548 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 549 | //===----------------------------------------------------------------------===// |
| 550 | |
| 551 | namespace { |
| 552 | |
| 553 | class UbigraphViz : public ExplodedNodeImpl::Auditor { |
| 554 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 555 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 556 | unsigned Cntr; |
| 557 | |
| 558 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 559 | VMap M; |
| 560 | |
| 561 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 562 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 563 | llvm::sys::Path& filename); |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 564 | |
| 565 | ~UbigraphViz(); |
| 566 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 567 | virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst); |
| 568 | }; |
| 569 | |
| 570 | } // end anonymous namespace |
| 571 | |
| 572 | static ExplodedNodeImpl::Auditor* CreateUbiViz() { |
| 573 | std::string ErrMsg; |
| 574 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 575 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 576 | if (!ErrMsg.empty()) |
| 577 | return 0; |
| 578 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 579 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 580 | Filename.appendComponent("llvm_ubi"); |
| 581 | Filename.makeUnique(true,&ErrMsg); |
| 582 | |
| 583 | if (!ErrMsg.empty()) |
| 584 | return 0; |
| 585 | |
| 586 | llvm::cerr << "Writing '" << Filename << "'.\n"; |
| 587 | |
| 588 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 589 | std::string filename = Filename.toString(); |
Dan Gohman | 92db284 | 2009-07-15 17:32:18 +0000 | [diff] [blame^] | 590 | Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, |
| 591 | /*Force=*/true, ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 592 | |
| 593 | if (!ErrMsg.empty()) |
| 594 | return 0; |
| 595 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 596 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) { |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 600 | |
| 601 | assert (Src != Dst && "Self-edges are not allowed."); |
| 602 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 603 | // Lookup the Src. If it is a new node, it's a root. |
| 604 | VMap::iterator SrcI= M.find(Src); |
| 605 | unsigned SrcID; |
| 606 | |
| 607 | if (SrcI == M.end()) { |
| 608 | M[Src] = SrcID = Cntr++; |
| 609 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 610 | } |
| 611 | else |
| 612 | SrcID = SrcI->second; |
| 613 | |
| 614 | // Lookup the Dst. |
| 615 | VMap::iterator DstI= M.find(Dst); |
| 616 | unsigned DstID; |
| 617 | |
| 618 | if (DstI == M.end()) { |
| 619 | M[Dst] = DstID = Cntr++; |
| 620 | *Out << "('vertex', " << DstID << ")\n"; |
| 621 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 622 | else { |
| 623 | // 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] | 624 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 625 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 626 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 627 | |
| 628 | // Add the edge. |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 629 | *Out << "('edge', " << SrcID << ", " << DstID |
| 630 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 633 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 634 | llvm::sys::Path& filename) |
| 635 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 636 | |
| 637 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 638 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 639 | " ('size', '1.5'))\n"; |
| 640 | } |
| 641 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 642 | UbigraphViz::~UbigraphViz() { |
| 643 | Out.reset(0); |
| 644 | llvm::cerr << "Running 'ubiviz' program... "; |
| 645 | std::string ErrMsg; |
| 646 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 647 | std::vector<const char*> args; |
| 648 | args.push_back(Ubiviz.c_str()); |
| 649 | args.push_back(Filename.c_str()); |
| 650 | args.push_back(0); |
| 651 | |
| 652 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 653 | llvm::cerr << "Error viewing graph: " << ErrMsg << "\n"; |
| 654 | } |
| 655 | |
| 656 | // Delete the directory. |
| 657 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 658 | } |