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" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
| 16 | #include "clang/AST/Decl.h" |
Zhongxing Xu | 802be99 | 2009-12-16 05:29:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 9705309 | 2010-01-26 22:59:55 +0000 | [diff] [blame] | 23 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 24 | #include "clang/Checker/ManagerRegistry.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 25 | #include "clang/Checker/BugReporter/PathDiagnostic.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 26 | #include "clang/Checker/PathSensitive/AnalysisManager.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 27 | #include "clang/Checker/BugReporter/BugReporter.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 28 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 29 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 30 | #include "clang/Basic/FileManager.h" |
| 31 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/System/Path.h" |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 36 | #include "llvm/System/Program.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 39 | using namespace clang; |
| 40 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 41 | static ExplodedNode::Auditor* CreateUbiViz(); |
Zhongxing Xu | ff944a8 | 2008-12-22 01:52:37 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 44 | // Basic type definitions. |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 48 | // Special PathDiagnosticClients. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | static PathDiagnosticClient* |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 52 | CreatePlistHTMLDiagnosticClient(const std::string& prefix, |
| 53 | const Preprocessor &PP) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | llvm::sys::Path F(prefix); |
Ted Kremenek | b697a4e | 2009-11-05 02:09:23 +0000 | [diff] [blame] | 55 | PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP); |
| 56 | return CreatePlistDiagnosticClient(prefix, PP, PD); |
Ted Kremenek | f755606 | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 60 | // AnalysisConsumer declaration. |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| 63 | namespace { |
| 64 | |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 65 | class AnalysisConsumer : public ASTConsumer { |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 66 | public: |
| 67 | typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 68 | typedef void (*TUAction)(AnalysisConsumer &C, AnalysisManager &M, |
| 69 | TranslationUnitDecl &TU); |
| 70 | |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 71 | private: |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 72 | typedef std::vector<CodeAction> Actions; |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 73 | typedef std::vector<TUAction> TUActions; |
| 74 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 75 | Actions FunctionActions; |
| 76 | Actions ObjCMethodActions; |
| 77 | Actions ObjCImplementationActions; |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 78 | TUActions TranslationUnitActions; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 80 | public: |
| 81 | ASTContext* Ctx; |
| 82 | const Preprocessor &PP; |
| 83 | const std::string OutDir; |
| 84 | AnalyzerOptions Opts; |
| 85 | bool declDisplayed; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 86 | |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 88 | // PD is owned by AnalysisManager. |
| 89 | PathDiagnosticClient *PD; |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 91 | StoreManagerCreator CreateStoreMgr; |
| 92 | ConstraintManagerCreator CreateConstraintMgr; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 94 | llvm::OwningPtr<AnalysisManager> Mgr; |
Zhongxing Xu | c471e7b | 2009-08-03 03:13:46 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 96 | AnalysisConsumer(const Preprocessor& pp, |
| 97 | const std::string& outdir, |
| 98 | const AnalyzerOptions& opts) |
| 99 | : Ctx(0), PP(pp), OutDir(outdir), |
| 100 | Opts(opts), declDisplayed(false), PD(0) { |
| 101 | DigestAnalyzerOptions(); |
| 102 | } |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 103 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 104 | void DigestAnalyzerOptions() { |
| 105 | // Create the PathDiagnosticClient. |
| 106 | if (!OutDir.empty()) { |
| 107 | switch (Opts.AnalysisDiagOpt) { |
| 108 | default: |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 109 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 110 | case PD_##NAME: PD = CREATEFN(OutDir, PP); break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 111 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 112 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 113 | } |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 115 | // Create the analyzer component creators. |
| 116 | if (ManagerRegistry::StoreMgrCreator != 0) { |
| 117 | CreateStoreMgr = ManagerRegistry::StoreMgrCreator; |
| 118 | } |
| 119 | else { |
| 120 | switch (Opts.AnalysisStoreOpt) { |
| 121 | default: |
| 122 | assert(0 && "Unknown store manager."); |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 123 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 124 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 125 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 129 | if (ManagerRegistry::ConstraintMgrCreator != 0) |
| 130 | CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; |
| 131 | else { |
| 132 | switch (Opts.AnalysisConstraintsOpt) { |
| 133 | default: |
| 134 | assert(0 && "Unknown store manager."); |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 135 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 136 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 137 | #include "clang/Frontend/Analyses.def" |
Zhongxing Xu | fda7832 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 140 | } |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 142 | void DisplayFunction(const Decl *D) { |
| 143 | if (!Opts.AnalyzerDisplayProgress || declDisplayed) |
| 144 | return; |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 146 | declDisplayed = true; |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 147 | SourceManager &SM = Mgr->getASTContext().getSourceManager(); |
| 148 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
| 149 | llvm::errs() << "ANALYZE: " << Loc.getFilename(); |
| 150 | |
Ted Kremenek | 029a14a | 2009-12-09 03:45:19 +0000 | [diff] [blame] | 151 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 152 | const NamedDecl *ND = cast<NamedDecl>(D); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 153 | llvm::errs() << ' ' << ND << '\n'; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 154 | } |
Ted Kremenek | 029a14a | 2009-12-09 03:45:19 +0000 | [diff] [blame] | 155 | else if (isa<BlockDecl>(D)) { |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 156 | llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:" |
Ted Kremenek | 029a14a | 2009-12-09 03:45:19 +0000 | [diff] [blame] | 157 | << Loc.getColumn() << '\n'; |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 158 | } |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 161 | void addCodeAction(CodeAction action) { |
| 162 | FunctionActions.push_back(action); |
| 163 | ObjCMethodActions.push_back(action); |
| 164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 166 | void addObjCImplementationAction(CodeAction action) { |
| 167 | ObjCImplementationActions.push_back(action); |
| 168 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 170 | void addTranslationUnitAction(TUAction action) { |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 171 | TranslationUnitActions.push_back(action); |
| 172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 174 | virtual void Initialize(ASTContext &Context) { |
| 175 | Ctx = &Context; |
| 176 | Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(), |
| 177 | PP.getLangOptions(), PD, |
| 178 | CreateStoreMgr, CreateConstraintMgr, |
Zhongxing Xu | c09289d | 2010-04-13 06:44:31 +0000 | [diff] [blame] | 179 | Opts.MaxNodes, |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 180 | Opts.VisualizeEGDot, Opts.VisualizeEGUbi, |
| 181 | Opts.PurgeDead, Opts.EagerlyAssume, |
| 182 | Opts.TrimGraph)); |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 185 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 186 | declDisplayed = false; |
| 187 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 188 | HandleTopLevelSingleDecl(*I); |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 191 | void HandleTopLevelSingleDecl(Decl *D); |
| 192 | virtual void HandleTranslationUnit(ASTContext &C); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 194 | void HandleCode(Decl* D, Stmt* Body, Actions& actions); |
| 195 | }; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 196 | } // end anonymous namespace |
| 197 | |
| 198 | namespace llvm { |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 199 | template <> struct FoldingSetTrait<AnalysisConsumer::CodeAction> { |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 200 | static inline void Profile(AnalysisConsumer::CodeAction X, |
Douglas Gregor | 6490ae5 | 2009-11-17 06:14:37 +0000 | [diff] [blame] | 201 | FoldingSetNodeID& ID) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 202 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | }; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | // AnalysisConsumer implementation. |
| 209 | //===----------------------------------------------------------------------===// |
| 210 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) { |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 212 | switch (D->getKind()) { |
Ted Kremenek | 280cfd7 | 2010-02-14 02:44:52 +0000 | [diff] [blame] | 213 | case Decl::CXXConstructor: |
| 214 | case Decl::CXXDestructor: |
| 215 | case Decl::CXXConversion: |
| 216 | case Decl::CXXMethod: |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 217 | case Decl::Function: { |
| 218 | FunctionDecl* FD = cast<FunctionDecl>(D); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 219 | if (!Opts.AnalyzeSpecificFunction.empty() && |
Ted Kremenek | 280cfd7 | 2010-02-14 02:44:52 +0000 | [diff] [blame] | 220 | FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction) |
| 221 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 280cfd7 | 2010-02-14 02:44:52 +0000 | [diff] [blame] | 223 | if (Stmt *Body = FD->getBody()) |
| 224 | HandleCode(FD, Body, FunctionActions); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 228 | case Decl::ObjCMethod: { |
| 229 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | 280cfd7 | 2010-02-14 02:44:52 +0000 | [diff] [blame] | 231 | if (!Opts.AnalyzeSpecificFunction.empty() && |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 232 | Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString()) |
| 233 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 280cfd7 | 2010-02-14 02:44:52 +0000 | [diff] [blame] | 235 | if (Stmt* Body = MD->getBody()) |
| 236 | HandleCode(MD, Body, ObjCMethodActions); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
| 239 | |
| 240 | default: |
| 241 | break; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Chris Lattner | dacbc5d | 2009-03-28 04:11:33 +0000 | [diff] [blame] | 245 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 247 | TranslationUnitDecl *TU = C.getTranslationUnitDecl(); |
| 248 | |
| 249 | for (TUActions::iterator I = TranslationUnitActions.begin(), |
| 250 | E = TranslationUnitActions.end(); I != E; ++I) { |
| 251 | (*I)(*this, *Mgr, *TU); |
Ted Kremenek | daac634 | 2008-11-07 02:09:25 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 254 | if (!ObjCImplementationActions.empty()) { |
Ted Kremenek | 74e3c92 | 2009-09-26 04:15:09 +0000 | [diff] [blame] | 255 | for (DeclContext::decl_iterator I = TU->decls_begin(), |
| 256 | E = TU->decls_end(); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 257 | I != E; ++I) |
Ted Kremenek | 4d53a53 | 2009-02-13 00:51:30 +0000 | [diff] [blame] | 258 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 259 | HandleCode(ID, 0, ObjCImplementationActions); |
Chris Lattner | e907787 | 2009-03-28 03:29:40 +0000 | [diff] [blame] | 260 | } |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | 690a7f4 | 2009-08-02 05:43:14 +0000 | [diff] [blame] | 262 | // Explicitly destroy the PathDiagnosticClient. This will flush its output. |
| 263 | // FIXME: This should be replaced with something that doesn't rely on |
Zhongxing Xu | da17fd5 | 2009-12-15 09:32:42 +0000 | [diff] [blame] | 264 | // side-effects in PathDiagnosticClient's destructor. This is required when |
| 265 | // used with option -disable-free. |
Zhongxing Xu | d07a0d0 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 266 | Mgr.reset(NULL); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 269 | static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) { |
| 270 | if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 271 | WL.push_back(BD); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 272 | |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 273 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 274 | I!=E; ++I) |
| 275 | if (DeclContext *DC = dyn_cast<DeclContext>(*I)) |
| 276 | FindBlocks(DC, WL); |
| 277 | } |
| 278 | |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 279 | void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 281 | // Don't run the actions if an error has occured with parsing the file. |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 282 | if (PP.getDiagnostics().hasErrorOccurred()) |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 283 | return; |
Ted Kremenek | 81922f0 | 2009-02-02 20:52:40 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 285 | // Don't run the actions on declarations in header files unless |
| 286 | // otherwise specified. |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 287 | if (!Opts.AnalyzeAll && |
| 288 | !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | return; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 291 | // Clear the AnalysisManager of old AnalysisContexts. |
| 292 | Mgr->ClearContexts(); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 293 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | // Dispatch on the actions. |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 295 | llvm::SmallVector<Decl*, 10> WL; |
| 296 | WL.push_back(D); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 130d5ff | 2009-12-12 01:04:14 +0000 | [diff] [blame] | 298 | if (Body && Opts.AnalyzeNestedBlocks) |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 299 | FindBlocks(cast<DeclContext>(D), WL); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 300 | |
Zhongxing Xu | 3702af5 | 2008-10-30 05:03:28 +0000 | [diff] [blame] | 301 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
Ted Kremenek | fc57651 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 302 | for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); |
| 303 | WI != WE; ++WI) |
| 304 | (*I)(*this, *Mgr, *WI); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | // Analyses |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 311 | static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr, |
| 312 | Decl *D) { |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 313 | if (LiveVariables *L = mgr.getLiveVariables(D)) { |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 314 | C.DisplayFunction(D); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 315 | BugReporter BR(mgr); |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 316 | CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 317 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 320 | static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr, |
| 321 | Decl *D) { |
| 322 | if (CFG* c = mgr.getCFG(D)) { |
| 323 | C.DisplayFunction(D); |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 324 | CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic()); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 325 | } |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 763bd3b | 2009-11-25 21:55:23 +0000 | [diff] [blame] | 329 | static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr, |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 330 | Decl *D, |
Ted Kremenek | 6a19832 | 2009-09-18 05:37:41 +0000 | [diff] [blame] | 331 | GRTransferFuncs* tf) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 333 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 335 | // Display progress. |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 336 | C.DisplayFunction(D); |
Ted Kremenek | 8ffc8a5 | 2008-11-24 20:53:32 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 75d03cf | 2009-09-18 22:29:35 +0000 | [diff] [blame] | 338 | // Construct the analysis engine. We first query for the LiveVariables |
| 339 | // information to see if the CFG is valid. |
| 340 | // FIXME: Inter-procedural analysis will need to handle invalid CFGs. |
| 341 | if (!mgr.getLiveVariables(D)) |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 342 | return; |
| 343 | |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 344 | GRExprEngine Eng(mgr, TF.take()); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 8382cf5 | 2009-11-13 18:46:29 +0000 | [diff] [blame] | 346 | if (C.Opts.EnableExperimentalInternalChecks) |
| 347 | RegisterExperimentalInternalChecks(Eng); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 6a19832 | 2009-09-18 05:37:41 +0000 | [diff] [blame] | 349 | RegisterAppleChecks(Eng, *D); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | eb94113 | 2009-11-13 01:15:47 +0000 | [diff] [blame] | 351 | if (C.Opts.EnableExperimentalChecks) |
| 352 | RegisterExperimentalChecks(Eng); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 354 | // Set the graph auditor. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 355 | llvm::OwningPtr<ExplodedNode::Auditor> Auditor; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 356 | if (mgr.shouldVisualizeUbigraph()) { |
| 357 | Auditor.reset(CreateUbiViz()); |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 358 | ExplodedNode::SetAuditor(Auditor.get()); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 361 | // Execute the worklist algorithm. |
Zhongxing Xu | c09289d | 2010-04-13 06:44:31 +0000 | [diff] [blame] | 362 | Eng.ExecuteWorkList(mgr.getStackFrame(D), mgr.getMaxNodes()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 364 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 365 | // created BugReporter. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 366 | ExplodedNode::SetAuditor(0); |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 368 | // Visualize the exploded graph. |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 369 | if (mgr.shouldVisualizeGraphviz()) |
Ted Kremenek | 34d7734 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 370 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | 3df6421 | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 372 | // Display warnings. |
| 373 | Eng.getBugReporter().FlushReports(); |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 376 | static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr, |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 377 | Decl *D, bool GCEnabled) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Zhongxing Xu | 5032ffe | 2009-08-25 06:51:30 +0000 | [diff] [blame] | 379 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(), |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 380 | GCEnabled, |
Ted Kremenek | bc46f34 | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 381 | mgr.getLangOptions()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 383 | ActionGRExprEngine(C, mgr, D, TF); |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 386 | static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr, |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 387 | Decl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 389 | switch (mgr.getLangOptions().getGCMode()) { |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 390 | default: |
| 391 | assert (false && "Invalid GC mode."); |
| 392 | case LangOptions::NonGC: |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 393 | ActionObjCMemCheckerAux(C, mgr, D, false); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 394 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 396 | case LangOptions::GCOnly: |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 397 | ActionObjCMemCheckerAux(C, mgr, D, true); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 398 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 400 | case LangOptions::HybridGC: |
Ted Kremenek | 565e465 | 2010-02-05 02:06:54 +0000 | [diff] [blame] | 401 | ActionObjCMemCheckerAux(C, mgr, D, false); |
| 402 | ActionObjCMemCheckerAux(C, mgr, D, true); |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 403 | break; |
Ted Kremenek | b35a74a | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 407 | static void ActionDisplayLiveVariables(AnalysisConsumer &C, |
| 408 | AnalysisManager& mgr, Decl *D) { |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 409 | if (LiveVariables* L = mgr.getLiveVariables(D)) { |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 410 | C.DisplayFunction(D); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 411 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 412 | } |
Ted Kremenek | 235e031 | 2008-07-02 18:11:29 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 415 | static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { |
| 416 | if (CFG *cfg = mgr.getCFG(D)) { |
| 417 | C.DisplayFunction(D); |
| 418 | cfg->dump(mgr.getLangOptions()); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 419 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 422 | static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { |
| 423 | if (CFG *cfg = mgr.getCFG(D)) { |
| 424 | C.DisplayFunction(D); |
| 425 | cfg->viewCFG(mgr.getLangOptions()); |
Ted Kremenek | 7032f46 | 2008-07-03 05:26:14 +0000 | [diff] [blame] | 426 | } |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 429 | static void ActionSecuritySyntacticChecks(AnalysisConsumer &C, |
| 430 | AnalysisManager &mgr, Decl *D) { |
| 431 | C.DisplayFunction(D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | BugReporter BR(mgr); |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 433 | CheckSecuritySyntaxOnly(D, BR); |
Ted Kremenek | dbfb5f8 | 2009-07-23 01:07:19 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 436 | static void ActionLLVMConventionChecker(AnalysisConsumer &C, |
| 437 | AnalysisManager &mgr, |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 438 | TranslationUnitDecl &TU) { |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 439 | BugReporter BR(mgr); |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 440 | CheckLLVMConventions(TU, BR); |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 443 | static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr, |
| 444 | Decl *D) { |
Ted Kremenek | 4f4e7e4 | 2008-08-04 17:14:10 +0000 | [diff] [blame] | 445 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 446 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 448 | C.DisplayFunction(D); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 449 | BugReporter BR(mgr); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 450 | CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 453 | static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr, |
| 454 | Decl *D) { |
| 455 | C.DisplayFunction(D); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 456 | BugReporter BR(mgr); |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 457 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 460 | static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr, |
| 461 | Decl *D) { |
| 462 | C.DisplayFunction(D); |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 463 | BugReporter BR(mgr); |
Zhongxing Xu | b317f8f | 2009-09-10 05:44:00 +0000 | [diff] [blame] | 464 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR); |
Ted Kremenek | 0d8019e | 2008-07-11 22:40:47 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 467 | static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr, |
| 468 | Decl *D) { |
| 469 | C.DisplayFunction(D); |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 470 | BugReporter BR(mgr); |
| 471 | CheckSizeofPointer(D, BR); |
| 472 | } |
| 473 | |
Ted Kremenek | 1d9cbeb | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 474 | static void ActionInlineCall(AnalysisConsumer &C, AnalysisManager &mgr, |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 475 | TranslationUnitDecl &TU) { |
| 476 | |
| 477 | // Find the entry function definition (if any). |
| 478 | FunctionDecl *D = 0; |
| 479 | |
| 480 | // Must specify an entry function. |
| 481 | if (!C.Opts.AnalyzeSpecificFunction.empty()) { |
| 482 | for (DeclContext::decl_iterator I=TU.decls_begin(), E=TU.decls_end(); |
| 483 | I != E; ++I) { |
| 484 | if (FunctionDecl *fd = dyn_cast<FunctionDecl>(*I)) |
| 485 | if (fd->isThisDeclarationADefinition() && |
| 486 | fd->getNameAsString() == C.Opts.AnalyzeSpecificFunction) { |
| 487 | D = fd; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (!D) |
| 494 | return; |
| 495 | |
| 496 | |
| 497 | // FIXME: This is largely copy of ActionGRExprEngine. Needs cleanup. |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 498 | // Display progress. |
| 499 | C.DisplayFunction(D); |
| 500 | |
Ted Kremenek | 32a5808 | 2010-01-05 00:15:18 +0000 | [diff] [blame] | 501 | // FIXME: Make a fake transfer function. The GRTransferFunc interface |
| 502 | // eventually will be removed. |
| 503 | GRExprEngine Eng(mgr, new GRTransferFuncs()); |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 504 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 505 | if (C.Opts.EnableExperimentalInternalChecks) |
| 506 | RegisterExperimentalInternalChecks(Eng); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 507 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 508 | RegisterAppleChecks(Eng, *D); |
Ted Kremenek | f6eafcc | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 509 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 510 | if (C.Opts.EnableExperimentalChecks) |
| 511 | RegisterExperimentalChecks(Eng); |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 512 | |
Zhongxing Xu | 60fd3db | 2009-12-23 09:15:19 +0000 | [diff] [blame] | 513 | // Register call inliner as the last checker. |
| 514 | RegisterCallInliner(Eng); |
| 515 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 516 | // Execute the worklist algorithm. |
| 517 | Eng.ExecuteWorkList(mgr.getStackFrame(D)); |
| 518 | |
| 519 | // Visualize the exploded graph. |
| 520 | if (mgr.shouldVisualizeGraphviz()) |
| 521 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
| 522 | |
| 523 | // Display warnings. |
| 524 | Eng.getBugReporter().FlushReports(); |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 527 | //===----------------------------------------------------------------------===// |
| 528 | // AnalysisConsumer creation. |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 531 | ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp, |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 532 | const std::string& OutDir, |
| 533 | const AnalyzerOptions& Opts) { |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 534 | llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts)); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 535 | |
Eli Friedman | e71b85f | 2009-05-19 10:18:02 +0000 | [diff] [blame] | 536 | for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i) |
| 537 | switch (Opts.AnalysisList[i]) { |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 538 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 539 | case NAME:\ |
| 540 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
| 541 | break; |
Eli Friedman | 4df2c42 | 2009-05-19 21:16:18 +0000 | [diff] [blame] | 542 | #include "clang/Frontend/Analyses.def" |
Mike Stump | b716633 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 543 | default: break; |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 544 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 2c4036e | 2009-05-07 19:02:53 +0000 | [diff] [blame] | 546 | // Last, disable the effects of '-Werror' when using the AnalysisConsumer. |
Daniel Dunbar | efceabd | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 547 | pp.getDiagnostics().setWarningsAsErrors(false); |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 549 | return C.take(); |
| 550 | } |
| 551 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 552 | //===----------------------------------------------------------------------===// |
| 553 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 554 | //===----------------------------------------------------------------------===// |
| 555 | |
| 556 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 558 | class UbigraphViz : public ExplodedNode::Auditor { |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 559 | llvm::OwningPtr<llvm::raw_ostream> Out; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 560 | llvm::sys::Path Dir, Filename; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 561 | unsigned Cntr; |
| 562 | |
| 563 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 564 | VMap M; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 566 | public: |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 567 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 568 | llvm::sys::Path& filename); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 570 | ~UbigraphViz(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
| 572 | virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 573 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 575 | } // end anonymous namespace |
| 576 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 577 | static ExplodedNode::Auditor* CreateUbiViz() { |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 578 | std::string ErrMsg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 580 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 581 | if (!ErrMsg.empty()) |
| 582 | return 0; |
| 583 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 584 | llvm::sys::Path Filename = Dir; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 585 | Filename.appendComponent("llvm_ubi"); |
| 586 | Filename.makeUnique(true,&ErrMsg); |
| 587 | |
| 588 | if (!ErrMsg.empty()) |
| 589 | return 0; |
| 590 | |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 591 | llvm::errs() << "Writing '" << Filename.str() << "'.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 593 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 594 | Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 595 | |
| 596 | if (!ErrMsg.empty()) |
| 597 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 599 | return new UbigraphViz(Stream.take(), Dir, Filename); |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 602 | void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | |
Ted Kremenek | 45479c8 | 2008-08-28 18:34:41 +0000 | [diff] [blame] | 604 | assert (Src != Dst && "Self-edges are not allowed."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 606 | // Lookup the Src. If it is a new node, it's a root. |
| 607 | VMap::iterator SrcI= M.find(Src); |
| 608 | unsigned SrcID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 610 | if (SrcI == M.end()) { |
| 611 | M[Src] = SrcID = Cntr++; |
| 612 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 613 | } |
| 614 | else |
| 615 | SrcID = SrcI->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 617 | // Lookup the Dst. |
| 618 | VMap::iterator DstI= M.find(Dst); |
| 619 | unsigned DstID; |
| 620 | |
| 621 | if (DstI == M.end()) { |
| 622 | M[Dst] = DstID = Cntr++; |
| 623 | *Out << "('vertex', " << DstID << ")\n"; |
| 624 | } |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 625 | else { |
| 626 | // 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] | 627 | DstID = DstI->second; |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 628 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 629 | } |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 630 | |
| 631 | // Add the edge. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | *Out << "('edge', " << SrcID << ", " << DstID |
Ted Kremenek | d128932 | 2008-08-27 22:46:55 +0000 | [diff] [blame] | 633 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Ted Kremenek | 56b9871 | 2008-08-28 05:02:09 +0000 | [diff] [blame] | 636 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 637 | llvm::sys::Path& filename) |
| 638 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 639 | |
| 640 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 641 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 642 | " ('size', '1.5'))\n"; |
| 643 | } |
| 644 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 645 | UbigraphViz::~UbigraphViz() { |
| 646 | Out.reset(0); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 647 | llvm::errs() << "Running 'ubiviz' program... "; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 648 | std::string ErrMsg; |
| 649 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 650 | std::vector<const char*> args; |
| 651 | args.push_back(Ubiviz.c_str()); |
| 652 | args.push_back(Filename.c_str()); |
| 653 | args.push_back(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 655 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 656 | llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Ted Kremenek | 710ad93 | 2008-08-28 03:54:51 +0000 | [diff] [blame] | 659 | // Delete the directory. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | Dir.eraseFromDisk(true); |
Daniel Dunbar | 932680e | 2008-08-29 03:45:59 +0000 | [diff] [blame] | 661 | } |