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 | |
Eli Friedman | 0ec78fa | 2009-05-19 21:10:40 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/AnalysisConsumer.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" |
Ted Kremenek | e41611a | 2009-07-16 18:13:04 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 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" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/PathSensitive/AnalysisManager.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 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 42 | static ExplodedNodeImpl::Auditor* CreateUbiViz(); |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 45 | // Basic type definitions. |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 48 | namespace { |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 49 | typedef void (*CodeAction)(AnalysisManager& Mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 50 | } // end anonymous namespace |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 53 | // Special PathDiagnosticClients. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | static PathDiagnosticClient* |
| 57 | CreatePlistHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, |
| 58 | PreprocessorFactory* PPF) { |
| 59 | llvm::sys::Path F(prefix); |
| 60 | PathDiagnosticClientFactory *PF = |
| 61 | CreateHTMLDiagnosticClientFactory(F.getDirname(), PP, PPF); |
| 62 | return CreatePlistDiagnosticClient(prefix, PP, PPF, PF); |
| 63 | } |
| 64 | |
| 65 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 66 | // AnalysisConsumer declaration. |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
| 69 | namespace { |
| 70 | |
| 71 | class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 72 | typedef std::vector<CodeAction> Actions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 73 | Actions FunctionActions; |
| 74 | Actions ObjCMethodActions; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 75 | Actions ObjCImplementationActions; |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 76 | Actions TranslationUnitActions; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 77 | |
| 78 | public: |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 79 | const LangOptions& LOpts; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 80 | Diagnostic &Diags; |
| 81 | ASTContext* Ctx; |
| 82 | Preprocessor* PP; |
| 83 | PreprocessorFactory* PPF; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 84 | const std::string OutDir; |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 85 | AnalyzerOptions Opts; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 82ec2e9 | 2009-07-31 00:34:52 +0000 | [diff] [blame^] | 87 | llvm::OwningPtr<PathDiagnosticClient> PD; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 88 | StoreManagerCreator CreateStoreMgr; |
| 89 | ConstraintManagerCreator CreateConstraintMgr; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 90 | |
| 91 | AnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
| 92 | PreprocessorFactory* ppf, |
| 93 | const LangOptions& lopts, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 94 | const std::string& outdir, |
| 95 | const AnalyzerOptions& opts) |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 96 | : LOpts(lopts), Diags(diags), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 97 | Ctx(0), PP(pp), PPF(ppf), |
Douglas Gregor | 17edea8 | 2009-07-30 16:10:26 +0000 | [diff] [blame] | 98 | OutDir(outdir), Opts(opts), PD(0) { |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 99 | DigestAnalyzerOptions(); |
| 100 | } |
| 101 | |
| 102 | void DigestAnalyzerOptions() { |
| 103 | // Create the PathDiagnosticClient. |
| 104 | if (!OutDir.empty()) { |
| 105 | switch (Opts.AnalysisDiagOpt) { |
| 106 | default: |
| 107 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ |
Ted Kremenek | 82ec2e9 | 2009-07-31 00:34:52 +0000 | [diff] [blame^] | 108 | case PD_##NAME: PD.reset(CREATEFN(OutDir, PP, PPF)); break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 109 | #include "clang/Frontend/Analyses.def" |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Create the analyzer component creators. |
| 114 | if (ManagerRegistry::StoreMgrCreator != 0) { |
| 115 | CreateStoreMgr = ManagerRegistry::StoreMgrCreator; |
| 116 | } |
| 117 | else { |
| 118 | switch (Opts.AnalysisStoreOpt) { |
| 119 | default: |
| 120 | assert(0 && "Unknown store manager."); |
| 121 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 122 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
| 123 | #include "clang/Frontend/Analyses.def" |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (ManagerRegistry::ConstraintMgrCreator != 0) |
| 128 | CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; |
| 129 | else { |
| 130 | switch (Opts.AnalysisConstraintsOpt) { |
| 131 | default: |
| 132 | assert(0 && "Unknown store manager."); |
| 133 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 134 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
| 135 | #include "clang/Frontend/Analyses.def" |
| 136 | } |
| 137 | } |
| 138 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 139 | |
| 140 | void addCodeAction(CodeAction action) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 141 | FunctionActions.push_back(action); |
| 142 | ObjCMethodActions.push_back(action); |
| 143 | } |
| 144 | |
| 145 | void addObjCImplementationAction(CodeAction action) { |
| 146 | ObjCImplementationActions.push_back(action); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 149 | void addTranslationUnitAction(CodeAction action) { |
| 150 | TranslationUnitActions.push_back(action); |
| 151 | } |
| 152 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 153 | virtual void Initialize(ASTContext &Context) { |
| 154 | Ctx = &Context; |
| 155 | } |
| 156 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 157 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 158 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 159 | HandleTopLevelSingleDecl(*I); |
| 160 | } |
| 161 | |
| 162 | void HandleTopLevelSingleDecl(Decl *D); |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 163 | virtual void HandleTranslationUnit(ASTContext &C); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 165 | void HandleCode(Decl* D, Stmt* Body, Actions& actions); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 170 | } // end anonymous namespace |
| 171 | |
| 172 | namespace llvm { |
| 173 | template <> struct FoldingSetTrait<CodeAction> { |
| 174 | static inline void Profile(CodeAction X, FoldingSetNodeID& ID) { |
| 175 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 176 | } |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | // AnalysisConsumer implementation. |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 184 | void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 185 | switch (D->getKind()) { |
| 186 | case Decl::Function: { |
| 187 | FunctionDecl* FD = cast<FunctionDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 188 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 189 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 190 | Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName()) |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 191 | break; |
| 192 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 193 | Stmt* Body = FD->getBody(); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 194 | if (Body) HandleCode(FD, Body, FunctionActions); |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | case Decl::ObjCMethod: { |
| 199 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 200 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 201 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 202 | Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString()) |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 203 | return; |
| 204 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 205 | Stmt* Body = MD->getBody(); |
| 206 | if (Body) HandleCode(MD, Body, ObjCMethodActions); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 215 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 217 | if(!TranslationUnitActions.empty()) { |
Ted Kremenek | 82ec2e9 | 2009-07-31 00:34:52 +0000 | [diff] [blame^] | 218 | AnalysisManager mgr(*Ctx, Diags, LOpts, PD.get(), |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 219 | CreateStoreMgr, CreateConstraintMgr, |
| 220 | Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot, |
| 221 | Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume, |
| 222 | Opts.TrimGraph); |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 223 | for (Actions::iterator I = TranslationUnitActions.begin(), |
| 224 | E = TranslationUnitActions.end(); I != E; ++I) |
| 225 | (*I)(mgr); |
| 226 | } |
| 227 | |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 228 | if (!ObjCImplementationActions.empty()) { |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 229 | TranslationUnitDecl *TUD = C.getTranslationUnitDecl(); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 230 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 231 | for (DeclContext::decl_iterator I = TUD->decls_begin(), |
| 232 | E = TUD->decls_end(); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 233 | I != E; ++I) |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 234 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 235 | HandleCode(ID, 0, ObjCImplementationActions); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 236 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 239 | void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 240 | |
| 241 | // Don't run the actions if an error has occured with parsing the file. |
| 242 | if (Diags.hasErrorOccurred()) |
| 243 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 245 | // Don't run the actions on declarations in header files unless |
| 246 | // otherwise specified. |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 247 | if (!Opts.AnalyzeAll && |
| 248 | !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 249 | return; |
| 250 | |
| 251 | // Create an AnalysisManager that will manage the state for analyzing |
| 252 | // this method/function. |
Ted Kremenek | 82ec2e9 | 2009-07-31 00:34:52 +0000 | [diff] [blame^] | 253 | AnalysisManager mgr(D, *Ctx, Diags, LOpts, PD.get(), |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 254 | CreateStoreMgr, CreateConstraintMgr, |
| 255 | Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot, |
| 256 | Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume, |
| 257 | Opts.TrimGraph); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 258 | |
| 259 | // Dispatch on the actions. |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 260 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 261 | (*I)(mgr); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | // Analyses |
| 266 | //===----------------------------------------------------------------------===// |
| 267 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 268 | static void ActionWarnDeadStores(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 269 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 270 | BugReporter BR(mgr); |
| 271 | CheckDeadStores(*L, BR); |
| 272 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 275 | static void ActionWarnUninitVals(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 276 | if (CFG* c = mgr.getCFG()) |
| 277 | CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic()); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 281 | static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf, |
| 282 | bool StandardWarnings = true) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 285 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 287 | // Display progress. |
| 288 | mgr.DisplayFunction(); |
| 289 | |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 290 | // Construct the analysis engine. |
| 291 | LiveVariables* L = mgr.getLiveVariables(); |
| 292 | if (!L) return; |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 294 | GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 295 | mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(), |
Zhongxing Xu | 22438a8 | 2008-11-27 01:55:08 +0000 | [diff] [blame] | 296 | mgr.getStoreManagerCreator(), |
| 297 | mgr.getConstraintManagerCreator()); |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 299 | Eng.setTransferFunctions(tf); |
| 300 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 301 | if (StandardWarnings) { |
| 302 | Eng.RegisterInternalChecks(); |
| 303 | RegisterAppleChecks(Eng); |
| 304 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 305 | |
| 306 | // Set the graph auditor. |
| 307 | llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor; |
| 308 | if (mgr.shouldVisualizeUbigraph()) { |
| 309 | Auditor.reset(CreateUbiViz()); |
| 310 | ExplodedNodeImpl::SetAuditor(Auditor.get()); |
| 311 | } |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 313 | // Execute the worklist algorithm. |
| 314 | Eng.ExecuteWorkList(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 316 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 317 | // created BugReporter. |
| 318 | ExplodedNodeImpl::SetAuditor(0); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 320 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 321 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 322 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 323 | |
| 324 | // Display warnings. |
| 325 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 328 | static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled, |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 329 | bool StandardWarnings) { |
| 330 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 331 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(), |
| 332 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 333 | mgr.getLangOptions()); |
| 334 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 335 | ActionGRExprEngine(mgr, TF, StandardWarnings); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 338 | static void ActionCheckerCFRef(AnalysisManager& mgr) { |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 339 | |
| 340 | switch (mgr.getLangOptions().getGCMode()) { |
| 341 | default: |
| 342 | assert (false && "Invalid GC mode."); |
| 343 | case LangOptions::NonGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 344 | ActionCheckerCFRefAux(mgr, false, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case LangOptions::GCOnly: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 348 | ActionCheckerCFRefAux(mgr, true, true); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 349 | break; |
| 350 | |
| 351 | case LangOptions::HybridGC: |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 352 | ActionCheckerCFRefAux(mgr, false, true); |
| 353 | ActionCheckerCFRefAux(mgr, true, false); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 358 | static void ActionDisplayLiveVariables(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 359 | if (LiveVariables* L = mgr.getLiveVariables()) { |
| 360 | mgr.DisplayFunction(); |
| 361 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 362 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 365 | static void ActionCFGDump(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 366 | if (CFG* c = mgr.getCFG()) { |
| 367 | mgr.DisplayFunction(); |
Zhongxing Xu | 7d49c71 | 2009-07-30 09:14:54 +0000 | [diff] [blame] | 368 | c->dump(mgr.getLangOptions()); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 369 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void ActionCFGView(AnalysisManager& mgr) { |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 373 | if (CFG* c = mgr.getCFG()) { |
| 374 | mgr.DisplayFunction(); |
Zhongxing Xu | 7d49c71 | 2009-07-30 09:14:54 +0000 | [diff] [blame] | 375 | c->viewCFG(mgr.getLangOptions()); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 376 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 379 | static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) { |
| 380 | BugReporter BR(mgr); |
| 381 | CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR); |
| 382 | } |
| 383 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 384 | static void ActionWarnObjCDealloc(AnalysisManager& mgr) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 385 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 386 | return; |
| 387 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 388 | BugReporter BR(mgr); |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 389 | |
| 390 | CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 391 | mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 394 | static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) { |
| 395 | BugReporter BR(mgr); |
| 396 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR); |
| 397 | } |
| 398 | |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 399 | static void ActionWarnObjCMethSigs(AnalysisManager& mgr) { |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 400 | BugReporter BR(mgr); |
| 401 | |
| 402 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), |
| 403 | BR); |
| 404 | } |
| 405 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 406 | //===----------------------------------------------------------------------===// |
| 407 | // AnalysisConsumer creation. |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 410 | ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 411 | PreprocessorFactory* ppf, |
| 412 | const LangOptions& lopts, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 413 | const std::string& OutDir, |
| 414 | const AnalyzerOptions& Opts) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 415 | |
| 416 | llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 417 | lopts, OutDir, |
| 418 | Opts)); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 419 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 420 | for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i) |
| 421 | switch (Opts.AnalysisList[i]) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 422 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 423 | case NAME:\ |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 424 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 425 | break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 426 | #include "clang/Frontend/Analyses.def" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 427 | default: break; |
| 428 | } |
Ted Kremenek | 2c4036e | 2009-05-07 19:02:53 +0000 | [diff] [blame] | 429 | |
| 430 | // Last, disable the effects of '-Werror' when using the AnalysisConsumer. |
| 431 | diags.setWarningsAsErrors(false); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 433 | return C.take(); |
| 434 | } |
| 435 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 436 | //===----------------------------------------------------------------------===// |
| 437 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 438 | //===----------------------------------------------------------------------===// |
| 439 | |
| 440 | namespace { |
| 441 | |
| 442 | class UbigraphViz : public ExplodedNodeImpl::Auditor { |
| 443 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 444 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 445 | unsigned Cntr; |
| 446 | |
| 447 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 448 | VMap M; |
| 449 | |
| 450 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 451 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 452 | llvm::sys::Path& filename); |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 453 | |
| 454 | ~UbigraphViz(); |
| 455 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 456 | virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst); |
| 457 | }; |
| 458 | |
| 459 | } // end anonymous namespace |
| 460 | |
| 461 | static ExplodedNodeImpl::Auditor* CreateUbiViz() { |
| 462 | std::string ErrMsg; |
| 463 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 464 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 465 | if (!ErrMsg.empty()) |
| 466 | return 0; |
| 467 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 468 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 469 | Filename.appendComponent("llvm_ubi"); |
| 470 | Filename.makeUnique(true,&ErrMsg); |
| 471 | |
| 472 | if (!ErrMsg.empty()) |
| 473 | return 0; |
| 474 | |
| 475 | llvm::cerr << "Writing '" << Filename << "'.\n"; |
| 476 | |
| 477 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 478 | std::string filename = Filename.toString(); |
Dan Gohman | 92db284 | 2009-07-15 17:32:18 +0000 | [diff] [blame] | 479 | Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, |
| 480 | /*Force=*/true, ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 481 | |
| 482 | if (!ErrMsg.empty()) |
| 483 | return 0; |
| 484 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 485 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) { |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 489 | |
| 490 | assert (Src != Dst && "Self-edges are not allowed."); |
| 491 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 492 | // Lookup the Src. If it is a new node, it's a root. |
| 493 | VMap::iterator SrcI= M.find(Src); |
| 494 | unsigned SrcID; |
| 495 | |
| 496 | if (SrcI == M.end()) { |
| 497 | M[Src] = SrcID = Cntr++; |
| 498 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 499 | } |
| 500 | else |
| 501 | SrcID = SrcI->second; |
| 502 | |
| 503 | // Lookup the Dst. |
| 504 | VMap::iterator DstI= M.find(Dst); |
| 505 | unsigned DstID; |
| 506 | |
| 507 | if (DstI == M.end()) { |
| 508 | M[Dst] = DstID = Cntr++; |
| 509 | *Out << "('vertex', " << DstID << ")\n"; |
| 510 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 511 | else { |
| 512 | // 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] | 513 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 514 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 515 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 516 | |
| 517 | // Add the edge. |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 518 | *Out << "('edge', " << SrcID << ", " << DstID |
| 519 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 522 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 523 | llvm::sys::Path& filename) |
| 524 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 525 | |
| 526 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 527 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 528 | " ('size', '1.5'))\n"; |
| 529 | } |
| 530 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 531 | UbigraphViz::~UbigraphViz() { |
| 532 | Out.reset(0); |
| 533 | llvm::cerr << "Running 'ubiviz' program... "; |
| 534 | std::string ErrMsg; |
| 535 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 536 | std::vector<const char*> args; |
| 537 | args.push_back(Ubiviz.c_str()); |
| 538 | args.push_back(Filename.c_str()); |
| 539 | args.push_back(0); |
| 540 | |
| 541 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 542 | llvm::cerr << "Error viewing graph: " << ErrMsg << "\n"; |
| 543 | } |
| 544 | |
| 545 | // Delete the directory. |
| 546 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 547 | } |