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