blob: 5af6c0112a621b562d8932dc17e18dd60a711f5a [file] [log] [blame]
Ted Kremenek81ea7992008-07-02 00:03:09 +00001//===--- 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 Friedman7efefa52009-05-19 21:10:40 +000014#include "clang/Frontend/AnalysisConsumer.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
16#include "clang/Frontend/ManagerRegistry.h"
Ted Kremenek81ea7992008-07-02 00:03:09 +000017#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
20#include "llvm/Support/Compiler.h"
Ted Kremenek81ea7992008-07-02 00:03:09 +000021#include "llvm/ADT/OwningPtr.h"
Ted Kremenekba927fc2009-07-16 18:13:04 +000022#include "clang/Analysis/CFG.h"
Ted Kremenek81ea7992008-07-02 00:03:09 +000023#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 Xuacdd51e2009-07-30 09:11:52 +000028#include "clang/Analysis/PathSensitive/AnalysisManager.h"
Ted Kremenekba1c7ed2008-07-02 21:24:01 +000029#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek81ea7992008-07-02 00:03:09 +000030#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 Xue828d1c2008-12-22 01:52:37 +000034#include "llvm/Support/CommandLine.h"
Ted Kremeneked80d002008-07-02 16:49:11 +000035#include "llvm/Support/Streams.h"
Ted Kremenekcf262252008-08-27 22:31:43 +000036#include "llvm/Support/raw_ostream.h"
37#include "llvm/System/Path.h"
Ted Kremenek3926ce72008-08-28 03:54:51 +000038#include "llvm/System/Program.h"
Ted Kremenek724133b2008-07-03 04:29:21 +000039
Ted Kremenek81ea7992008-07-02 00:03:09 +000040using namespace clang;
41
Ted Kremenekcf262252008-08-27 22:31:43 +000042static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xue828d1c2008-12-22 01:52:37 +000043
Ted Kremenek99ec8f42009-02-17 04:27:41 +000044//===----------------------------------------------------------------------===//
Ted Kremenek81ea7992008-07-02 00:03:09 +000045// Basic type definitions.
46//===----------------------------------------------------------------------===//
47
Ted Kremenek8ce61b32008-07-14 23:41:13 +000048namespace {
Ted Kremenek8ce61b32008-07-14 23:41:13 +000049 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenek81ea7992008-07-02 00:03:09 +000050} // end anonymous namespace
51
52//===----------------------------------------------------------------------===//
Ted Kremenekd0f1a332009-07-27 22:13:39 +000053// Special PathDiagnosticClients.
54//===----------------------------------------------------------------------===//
55
56static PathDiagnosticClient*
57CreatePlistHTMLDiagnosticClient(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 Kremenek81ea7992008-07-02 00:03:09 +000066// AnalysisConsumer declaration.
67//===----------------------------------------------------------------------===//
68
69namespace {
70
71 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenek724133b2008-07-03 04:29:21 +000072 typedef std::vector<CodeAction> Actions;
Ted Kremenek81ea7992008-07-02 00:03:09 +000073 Actions FunctionActions;
74 Actions ObjCMethodActions;
Ted Kremenek724133b2008-07-03 04:29:21 +000075 Actions ObjCImplementationActions;
Ted Kremenekcf181702008-11-07 02:09:25 +000076 Actions TranslationUnitActions;
Ted Kremenek81ea7992008-07-02 00:03:09 +000077
78 public:
Ted Kremenekc3803992008-10-24 01:04:59 +000079 const LangOptions& LOpts;
Ted Kremenek81ea7992008-07-02 00:03:09 +000080 Diagnostic &Diags;
81 ASTContext* Ctx;
82 Preprocessor* PP;
83 PreprocessorFactory* PPF;
Ted Kremenek99ec8f42009-02-17 04:27:41 +000084 const std::string OutDir;
Eli Friedman78a57622009-05-19 10:18:02 +000085 AnalyzerOptions Opts;
Zhongxing Xuacdd51e2009-07-30 09:11:52 +000086
Ted Kremenek3f874752009-07-31 00:34:52 +000087 llvm::OwningPtr<PathDiagnosticClient> PD;
Zhongxing Xuacdd51e2009-07-30 09:11:52 +000088 StoreManagerCreator CreateStoreMgr;
89 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenek81ea7992008-07-02 00:03:09 +000090
Zhongxing Xu659e33c2009-08-03 03:13:46 +000091 llvm::OwningPtr<AnalysisManager> Mgr;
92
Ted Kremenek81ea7992008-07-02 00:03:09 +000093 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
94 PreprocessorFactory* ppf,
95 const LangOptions& lopts,
Eli Friedman78a57622009-05-19 10:18:02 +000096 const std::string& outdir,
97 const AnalyzerOptions& opts)
Ted Kremenek99ec8f42009-02-17 04:27:41 +000098 : LOpts(lopts), Diags(diags),
Ted Kremenek81ea7992008-07-02 00:03:09 +000099 Ctx(0), PP(pp), PPF(ppf),
Douglas Gregor2b8ea8c2009-07-30 16:10:26 +0000100 OutDir(outdir), Opts(opts), PD(0) {
Zhongxing Xuacdd51e2009-07-30 09:11:52 +0000101 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 Kremenek3f874752009-07-31 00:34:52 +0000110 case PD_##NAME: PD.reset(CREATEFN(OutDir, PP, PPF)); break;
Zhongxing Xuacdd51e2009-07-30 09:11:52 +0000111#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 Kremenek81ea7992008-07-02 00:03:09 +0000141
142 void addCodeAction(CodeAction action) {
Ted Kremenek724133b2008-07-03 04:29:21 +0000143 FunctionActions.push_back(action);
144 ObjCMethodActions.push_back(action);
145 }
146
147 void addObjCImplementationAction(CodeAction action) {
148 ObjCImplementationActions.push_back(action);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000149 }
150
Ted Kremenekcf181702008-11-07 02:09:25 +0000151 void addTranslationUnitAction(CodeAction action) {
152 TranslationUnitActions.push_back(action);
153 }
154
Ted Kremenek81ea7992008-07-02 00:03:09 +0000155 virtual void Initialize(ASTContext &Context) {
156 Ctx = &Context;
Zhongxing Xu659e33c2009-08-03 03:13:46 +0000157 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 Kremenek81ea7992008-07-02 00:03:09 +0000163 }
164
Chris Lattnera17991f2009-03-29 16:50:03 +0000165 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 Lattner2a594d02009-03-28 04:11:33 +0000171 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenek724133b2008-07-03 04:29:21 +0000172
Ted Kremenekb8da3452009-02-02 20:52:40 +0000173 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000174 };
175
176
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000177
Ted Kremenek81ea7992008-07-02 00:03:09 +0000178} // end anonymous namespace
179
180namespace 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 Lattnera17991f2009-03-29 16:50:03 +0000192void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000193 switch (D->getKind()) {
194 case Decl::Function: {
195 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000196
Eli Friedman78a57622009-05-19 10:18:02 +0000197 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
198 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000199 break;
200
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000201 Stmt* Body = FD->getBody();
Ted Kremenek81ea7992008-07-02 00:03:09 +0000202 if (Body) HandleCode(FD, Body, FunctionActions);
203 break;
204 }
205
206 case Decl::ObjCMethod: {
207 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000208
Eli Friedman78a57622009-05-19 10:18:02 +0000209 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
210 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000211 return;
212
Ted Kremenek81ea7992008-07-02 00:03:09 +0000213 Stmt* Body = MD->getBody();
214 if (Body) HandleCode(MD, Body, ObjCMethodActions);
215 break;
216 }
217
218 default:
219 break;
220 }
221}
222
Chris Lattner2a594d02009-03-28 04:11:33 +0000223void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenek724133b2008-07-03 04:29:21 +0000224
Ted Kremenekcf181702008-11-07 02:09:25 +0000225 if(!TranslationUnitActions.empty()) {
Ted Kremenekcf181702008-11-07 02:09:25 +0000226 for (Actions::iterator I = TranslationUnitActions.begin(),
227 E = TranslationUnitActions.end(); I != E; ++I)
Zhongxing Xu659e33c2009-08-03 03:13:46 +0000228 (*I)(*Mgr);
Ted Kremenekcf181702008-11-07 02:09:25 +0000229 }
230
Chris Lattner137e1312009-03-28 03:29:40 +0000231 if (!ObjCImplementationActions.empty()) {
Chris Lattner2a594d02009-03-28 04:11:33 +0000232 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattner137e1312009-03-28 03:29:40 +0000233
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000234 for (DeclContext::decl_iterator I = TUD->decls_begin(),
235 E = TUD->decls_end();
Chris Lattner137e1312009-03-28 03:29:40 +0000236 I != E; ++I)
Ted Kremenek14733c02009-02-13 00:51:30 +0000237 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
238 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattner137e1312009-03-28 03:29:40 +0000239 }
Ted Kremenek9059ce62009-08-02 05:43:14 +0000240
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 Kremenek724133b2008-07-03 04:29:21 +0000245}
246
Ted Kremenekb8da3452009-02-02 20:52:40 +0000247void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000248
249 // Don't run the actions if an error has occured with parsing the file.
250 if (Diags.hasErrorOccurred())
251 return;
Ted Kremenekb8da3452009-02-02 20:52:40 +0000252
Ted Kremenek81ea7992008-07-02 00:03:09 +0000253 // Don't run the actions on declarations in header files unless
254 // otherwise specified.
Eli Friedman78a57622009-05-19 10:18:02 +0000255 if (!Opts.AnalyzeAll &&
256 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenek81ea7992008-07-02 00:03:09 +0000257 return;
258
Zhongxing Xu659e33c2009-08-03 03:13:46 +0000259 Mgr->setContext(D);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000260
261 // Dispatch on the actions.
Zhongxing Xua89b4642008-10-30 05:03:28 +0000262 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Zhongxing Xu659e33c2009-08-03 03:13:46 +0000263 (*I)(*Mgr);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000264}
265
266//===----------------------------------------------------------------------===//
267// Analyses
268//===----------------------------------------------------------------------===//
269
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000270static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000271 if (LiveVariables* L = mgr.getLiveVariables()) {
272 BugReporter BR(mgr);
273 CheckDeadStores(*L, BR);
274 }
Ted Kremenek81ea7992008-07-02 00:03:09 +0000275}
276
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000277static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000278 if (CFG* c = mgr.getCFG())
279 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenek81ea7992008-07-02 00:03:09 +0000280}
281
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000282
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000283static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
284 bool StandardWarnings = true) {
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000285
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000286
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000287 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000288
Ted Kremenekbdf6f612008-11-24 20:53:32 +0000289 // Display progress.
290 mgr.DisplayFunction();
291
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000292 // Construct the analysis engine.
293 LiveVariables* L = mgr.getLiveVariables();
294 if (!L) return;
Ted Kremenekbdf6f612008-11-24 20:53:32 +0000295
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000296 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedman78a57622009-05-19 10:18:02 +0000297 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000298 mgr.getStoreManagerCreator(),
299 mgr.getConstraintManagerCreator());
Ted Kremenekc3803992008-10-24 01:04:59 +0000300
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000301 Eng.setTransferFunctions(tf);
302
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000303 if (StandardWarnings) {
304 Eng.RegisterInternalChecks();
305 RegisterAppleChecks(Eng);
306 }
Ted Kremenekcf262252008-08-27 22:31:43 +0000307
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 Kremenek9f20c7c2008-07-22 16:21:24 +0000314
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000315 // Execute the worklist algorithm.
316 Eng.ExecuteWorkList();
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000317
Ted Kremenekcf262252008-08-27 22:31:43 +0000318 // Release the auditor (if any) so that it doesn't monitor the graph
319 // created BugReporter.
320 ExplodedNodeImpl::SetAuditor(0);
Ted Kremeneka0de7a52009-03-11 01:42:29 +0000321
Ted Kremeneked80d002008-07-02 16:49:11 +0000322 // Visualize the exploded graph.
Ted Kremenekcf262252008-08-27 22:31:43 +0000323 if (mgr.shouldVisualizeGraphviz())
Ted Kremeneked80d002008-07-02 16:49:11 +0000324 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremeneka0de7a52009-03-11 01:42:29 +0000325
326 // Display warnings.
327 Eng.getBugReporter().FlushReports();
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000328}
329
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000330static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000331 bool StandardWarnings) {
332
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000333 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
334 GCEnabled,
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000335 mgr.getLangOptions());
336
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000337 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000338}
339
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000340static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000341
342 switch (mgr.getLangOptions().getGCMode()) {
343 default:
344 assert (false && "Invalid GC mode.");
345 case LangOptions::NonGC:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000346 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000347 break;
348
349 case LangOptions::GCOnly:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000350 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000351 break;
352
353 case LangOptions::HybridGC:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000354 ActionCheckerCFRefAux(mgr, false, true);
355 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000356 break;
357 }
358}
359
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000360static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000361 if (LiveVariables* L = mgr.getLiveVariables()) {
362 mgr.DisplayFunction();
363 L->dumpBlockLiveness(mgr.getSourceManager());
364 }
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000365}
366
Ted Kremeneke972d852008-07-02 18:23:21 +0000367static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000368 if (CFG* c = mgr.getCFG()) {
369 mgr.DisplayFunction();
Zhongxing Xudd8984b2009-07-30 09:14:54 +0000370 c->dump(mgr.getLangOptions());
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000371 }
Ted Kremeneke972d852008-07-02 18:23:21 +0000372}
373
374static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000375 if (CFG* c = mgr.getCFG()) {
376 mgr.DisplayFunction();
Zhongxing Xudd8984b2009-07-30 09:14:54 +0000377 c->viewCFG(mgr.getLangOptions());
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000378 }
Ted Kremeneke972d852008-07-02 18:23:21 +0000379}
380
Ted Kremenek076d84e2009-07-23 01:07:19 +0000381static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
382 BugReporter BR(mgr);
383 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
384}
385
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000386static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek991de6f2008-08-04 17:14:10 +0000387 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
388 return;
389
Ted Kremenek724133b2008-07-03 04:29:21 +0000390 BugReporter BR(mgr);
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000391
392 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
393 mgr.getLangOptions(), BR);
Ted Kremenek724133b2008-07-03 04:29:21 +0000394}
395
Ted Kremenek69ea7852008-07-23 00:45:26 +0000396static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
397 BugReporter BR(mgr);
398 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
399}
400
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000401static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenekd072b892008-07-11 22:40:47 +0000402 BugReporter BR(mgr);
403
404 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
405 BR);
406}
407
Ted Kremenek81ea7992008-07-02 00:03:09 +0000408//===----------------------------------------------------------------------===//
409// AnalysisConsumer creation.
410//===----------------------------------------------------------------------===//
411
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000412ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek81ea7992008-07-02 00:03:09 +0000413 PreprocessorFactory* ppf,
414 const LangOptions& lopts,
Eli Friedman78a57622009-05-19 10:18:02 +0000415 const std::string& OutDir,
416 const AnalyzerOptions& Opts) {
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000417
418 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedman78a57622009-05-19 10:18:02 +0000419 lopts, OutDir,
420 Opts));
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000421
Eli Friedman78a57622009-05-19 10:18:02 +0000422 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
423 switch (Opts.AnalysisList[i]) {
Ted Kremenekfbda0ef2008-07-15 00:46:02 +0000424#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000425 case NAME:\
Ted Kremenekfbda0ef2008-07-15 00:46:02 +0000426 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenek81ea7992008-07-02 00:03:09 +0000427 break;
Eli Friedman8c034072009-05-19 21:16:18 +0000428#include "clang/Frontend/Analyses.def"
Ted Kremenek81ea7992008-07-02 00:03:09 +0000429 default: break;
430 }
Ted Kremenekeb1dfa62009-05-07 19:02:53 +0000431
432 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
433 diags.setWarningsAsErrors(false);
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000434
Ted Kremenek81ea7992008-07-02 00:03:09 +0000435 return C.take();
436}
437
Ted Kremenekcf262252008-08-27 22:31:43 +0000438//===----------------------------------------------------------------------===//
439// Ubigraph Visualization. FIXME: Move to separate file.
440//===----------------------------------------------------------------------===//
441
442namespace {
443
444class UbigraphViz : public ExplodedNodeImpl::Auditor {
445 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek3926ce72008-08-28 03:54:51 +0000446 llvm::sys::Path Dir, Filename;
Ted Kremenekcf262252008-08-27 22:31:43 +0000447 unsigned Cntr;
448
449 typedef llvm::DenseMap<void*,unsigned> VMap;
450 VMap M;
451
452public:
Ted Kremenek3926ce72008-08-28 03:54:51 +0000453 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek2ea9cfe2008-08-28 05:02:09 +0000454 llvm::sys::Path& filename);
Ted Kremenek3926ce72008-08-28 03:54:51 +0000455
456 ~UbigraphViz();
457
Ted Kremenekcf262252008-08-27 22:31:43 +0000458 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
459};
460
461} // end anonymous namespace
462
463static ExplodedNodeImpl::Auditor* CreateUbiViz() {
464 std::string ErrMsg;
465
Ted Kremenek3926ce72008-08-28 03:54:51 +0000466 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekcf262252008-08-27 22:31:43 +0000467 if (!ErrMsg.empty())
468 return 0;
469
Ted Kremenek3926ce72008-08-28 03:54:51 +0000470 llvm::sys::Path Filename = Dir;
Ted Kremenekcf262252008-08-27 22:31:43 +0000471 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 Gohmancdb9c612009-07-15 17:32:18 +0000481 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
482 /*Force=*/true, ErrMsg));
Ted Kremenekcf262252008-08-27 22:31:43 +0000483
484 if (!ErrMsg.empty())
485 return 0;
486
Ted Kremenek3926ce72008-08-28 03:54:51 +0000487 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekcf262252008-08-27 22:31:43 +0000488}
489
490void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek33a9fdf2008-08-28 18:34:41 +0000491
492 assert (Src != Dst && "Self-edges are not allowed.");
493
Ted Kremenekcf262252008-08-27 22:31:43 +0000494 // 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 Kremenek2ea9cfe2008-08-28 05:02:09 +0000513 else {
514 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekcf262252008-08-27 22:31:43 +0000515 DstID = DstI->second;
Ted Kremenek2ea9cfe2008-08-28 05:02:09 +0000516 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
517 }
Ted Kremenekcf262252008-08-27 22:31:43 +0000518
519 // Add the edge.
Ted Kremenek9d5b7252008-08-27 22:46:55 +0000520 *Out << "('edge', " << SrcID << ", " << DstID
521 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekcf262252008-08-27 22:31:43 +0000522}
523
Ted Kremenek2ea9cfe2008-08-28 05:02:09 +0000524UbigraphViz::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 Kremenek3926ce72008-08-28 03:54:51 +0000533UbigraphViz::~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 Dunbar7d630a62008-08-29 03:45:59 +0000549}