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