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