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