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