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