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