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; |
| 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 | |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame^] | 463 | if (!ObjCImplementationActions.empty()) { |
| 464 | TranslationUnitDecl *TUD = TU.getContext().getTranslationUnitDecl(); |
| 465 | |
| 466 | for (DeclContext::decl_iterator I = TUD->decls_begin(),E = TUD->decls_end(); |
| 467 | I != E; ++I) |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 468 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 469 | HandleCode(ID, 0, ObjCImplementationActions); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame^] | 470 | } |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 471 | |
| 472 | // Delete the PathDiagnosticClient here just in case the AnalysisConsumer |
| 473 | // object doesn't get released. This will cause any side-effects in the |
| 474 | // destructor of the PathDiagnosticClient to get executed. |
| 475 | PD.reset(); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 478 | void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 479 | |
| 480 | // Don't run the actions if an error has occured with parsing the file. |
| 481 | if (Diags.hasErrorOccurred()) |
| 482 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 484 | // Don't run the actions on declarations in header files unless |
| 485 | // otherwise specified. |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 486 | if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 487 | return; |
| 488 | |
| 489 | // Create an AnalysisManager that will manage the state for analyzing |
| 490 | // this method/function. |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 491 | AnalysisManager mgr(*this, D, Body, AnalyzerDisplayProgress); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 492 | |
| 493 | // Dispatch on the actions. |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 494 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 495 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | //===----------------------------------------------------------------------===// |
| 499 | // Analyses |
| 500 | //===----------------------------------------------------------------------===// |
| 501 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 502 | static void ActionWarnDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 503 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 504 | BugReporter BR(mgr); |
| 505 | CheckDeadStores(*L, BR); |
| 506 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 509 | static void ActionWarnUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 510 | if (CFG* c = mgr.getCFG()) |
| 511 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 515 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf, |
| 516 | bool StandardWarnings = true) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 518 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 519 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 520 | |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 521 | // Display progress. |
| 522 | mgr.DisplayFunction(); |
| 523 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 524 | // Construct the analysis engine. |
| 525 | LiveVariables* L = mgr.getLiveVariables(); |
| 526 | if (!L) return; |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 528 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr, |
Ted Kremenek | 48af2a9 | 2009-02-25 22:32:02 +0000 | [diff] [blame] | 529 | PurgeDead, EagerlyAssume, |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 530 | mgr.getStoreManagerCreator(), |
| 531 | mgr.getConstraintManagerCreator()); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 533 | Eng.setTransferFunctions(tf); |
| 534 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 535 | if (StandardWarnings) { |
| 536 | Eng.RegisterInternalChecks(); |
| 537 | RegisterAppleChecks(Eng); |
| 538 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 539 | |
| 540 | // Set the graph auditor. |
| 541 | llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor; |
| 542 | if (mgr.shouldVisualizeUbigraph()) { |
| 543 | Auditor.reset(CreateUbiViz()); |
| 544 | ExplodedNodeImpl::SetAuditor(Auditor.get()); |
| 545 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 546 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 547 | // Execute the worklist algorithm. |
| 548 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 549 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 550 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 551 | // created BugReporter. |
| 552 | ExplodedNodeImpl::SetAuditor(0); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 553 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 554 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 555 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 556 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 557 | |
| 558 | // Display warnings. |
| 559 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 562 | static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled, |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 563 | bool StandardWarnings) { |
| 564 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 565 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 566 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 567 | mgr.getLangOptions()); |
| 568 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 569 | ActionGRExprEngine(mgr, TF, StandardWarnings); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 572 | static void ActionCheckerCFRef(AnalysisManager& mgr) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 573 | |
| 574 | switch (mgr.getLangOptions().getGCMode()) { |
| 575 | default: |
| 576 | assert (false && "Invalid GC mode."); |
| 577 | case LangOptions::NonGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 578 | ActionCheckerCFRefAux(mgr, false, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case LangOptions::GCOnly: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 582 | ActionCheckerCFRefAux(mgr, true, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 583 | break; |
| 584 | |
| 585 | case LangOptions::HybridGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 586 | ActionCheckerCFRefAux(mgr, false, true); |
| 587 | ActionCheckerCFRefAux(mgr, true, false); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 592 | static void ActionCheckerSimple(AnalysisManager& mgr) { |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 593 | ActionGRExprEngine(mgr, MakeGRSimpleValsTF()); |
| 594 | } |
| 595 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 596 | static void ActionDisplayLiveVariables(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 597 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 598 | mgr.DisplayFunction(); |
| 599 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 600 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 603 | static void ActionCFGDump(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->dump(); |
| 607 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 611 | if (CFG* c = mgr.getCFG()) { |
| 612 | mgr.DisplayFunction(); |
| 613 | c->viewCFG(); |
| 614 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 617 | static void ActionWarnObjCDealloc(AnalysisManager& mgr) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 618 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 619 | return; |
| 620 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 621 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 622 | |
| 623 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 624 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 627 | static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) { |
| 628 | BugReporter BR(mgr); |
| 629 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR); |
| 630 | } |
| 631 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 632 | static void ActionWarnObjCMethSigs(AnalysisManager& mgr) { |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 633 | BugReporter BR(mgr); |
| 634 | |
| 635 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 636 | BR); |
| 637 | } |
| 638 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 639 | //===----------------------------------------------------------------------===// |
| 640 | // AnalysisConsumer creation. |
| 641 | //===----------------------------------------------------------------------===// |
| 642 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 643 | ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 644 | PreprocessorFactory* ppf, |
| 645 | const LangOptions& lopts, |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 646 | const std::string& OutDir) { |
| 647 | |
| 648 | llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf, |
| 649 | lopts, OutDir)); |
| 650 | |
| 651 | for (unsigned i = 0; i < AnalysisList.size(); ++i) |
| 652 | switch (AnalysisList[i]) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 653 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 654 | case NAME:\ |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 655 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 656 | break; |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 657 | #include "Analyses.def" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 658 | default: break; |
| 659 | } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 661 | return C.take(); |
| 662 | } |
| 663 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
| 665 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
| 668 | namespace { |
| 669 | |
| 670 | class UbigraphViz : public ExplodedNodeImpl::Auditor { |
| 671 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 672 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 673 | unsigned Cntr; |
| 674 | |
| 675 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 676 | VMap M; |
| 677 | |
| 678 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 679 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 680 | llvm::sys::Path& filename); |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 681 | |
| 682 | ~UbigraphViz(); |
| 683 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 684 | virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst); |
| 685 | }; |
| 686 | |
| 687 | } // end anonymous namespace |
| 688 | |
| 689 | static ExplodedNodeImpl::Auditor* CreateUbiViz() { |
| 690 | std::string ErrMsg; |
| 691 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 692 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 693 | if (!ErrMsg.empty()) |
| 694 | return 0; |
| 695 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 696 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 697 | Filename.appendComponent("llvm_ubi"); |
| 698 | Filename.makeUnique(true,&ErrMsg); |
| 699 | |
| 700 | if (!ErrMsg.empty()) |
| 701 | return 0; |
| 702 | |
| 703 | llvm::cerr << "Writing '" << Filename << "'.\n"; |
| 704 | |
| 705 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 706 | std::string filename = Filename.toString(); |
Daniel Dunbar | 26fb272 | 2008-11-13 05:09:21 +0000 | [diff] [blame] | 707 | Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 708 | |
| 709 | if (!ErrMsg.empty()) |
| 710 | return 0; |
| 711 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 712 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) { |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 716 | |
| 717 | assert (Src != Dst && "Self-edges are not allowed."); |
| 718 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 719 | // Lookup the Src. If it is a new node, it's a root. |
| 720 | VMap::iterator SrcI= M.find(Src); |
| 721 | unsigned SrcID; |
| 722 | |
| 723 | if (SrcI == M.end()) { |
| 724 | M[Src] = SrcID = Cntr++; |
| 725 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 726 | } |
| 727 | else |
| 728 | SrcID = SrcI->second; |
| 729 | |
| 730 | // Lookup the Dst. |
| 731 | VMap::iterator DstI= M.find(Dst); |
| 732 | unsigned DstID; |
| 733 | |
| 734 | if (DstI == M.end()) { |
| 735 | M[Dst] = DstID = Cntr++; |
| 736 | *Out << "('vertex', " << DstID << ")\n"; |
| 737 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 738 | else { |
| 739 | // 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] | 740 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 741 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 742 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 743 | |
| 744 | // Add the edge. |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 745 | *Out << "('edge', " << SrcID << ", " << DstID |
| 746 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 749 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 750 | llvm::sys::Path& filename) |
| 751 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 752 | |
| 753 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 754 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 755 | " ('size', '1.5'))\n"; |
| 756 | } |
| 757 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 758 | UbigraphViz::~UbigraphViz() { |
| 759 | Out.reset(0); |
| 760 | llvm::cerr << "Running 'ubiviz' program... "; |
| 761 | std::string ErrMsg; |
| 762 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 763 | std::vector<const char*> args; |
| 764 | args.push_back(Ubiviz.c_str()); |
| 765 | args.push_back(Filename.c_str()); |
| 766 | args.push_back(0); |
| 767 | |
| 768 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 769 | llvm::cerr << "Error viewing graph: " << ErrMsg << "\n"; |
| 770 | } |
| 771 | |
| 772 | // Delete the directory. |
| 773 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 774 | } |