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