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