blob: 1ab36907450b7f64e5808a6772a8aef9e0512ae3 [file] [log] [blame]
Ted Kremenekf4381fd2008-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 Friedman0ec78fa2009-05-19 21:10:40 +000014#include "clang/Frontend/AnalysisConsumer.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
16#include "clang/Frontend/ManagerRegistry.h"
Ted Kremenekf4381fd2008-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 Kremenekf4381fd2008-07-02 00:03:09 +000021#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000022#include "clang/Analysis/CFG.h"
Ted Kremenekf4381fd2008-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 Xufda78322009-07-30 09:11:52 +000028#include "clang/Analysis/PathSensitive/AnalysisManager.h"
Ted Kremenekc0959972008-07-02 21:24:01 +000029#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekf4381fd2008-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 Xuff944a82008-12-22 01:52:37 +000034#include "llvm/Support/CommandLine.h"
Ted Kremenek34d77342008-07-02 16:49:11 +000035#include "llvm/Support/Streams.h"
Ted Kremenekf8ce6992008-08-27 22:31:43 +000036#include "llvm/Support/raw_ostream.h"
37#include "llvm/System/Path.h"
Ted Kremenek710ad932008-08-28 03:54:51 +000038#include "llvm/System/Program.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000039
Ted Kremenekf4381fd2008-07-02 00:03:09 +000040using namespace clang;
41
Ted Kremenekf8ce6992008-08-27 22:31:43 +000042static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000043
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000044//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000045// Basic type definitions.
46//===----------------------------------------------------------------------===//
47
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000048namespace {
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000049 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +000050} // end anonymous namespace
51
52//===----------------------------------------------------------------------===//
Ted Kremenekf7556062009-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 Kremenekf4381fd2008-07-02 00:03:09 +000066// AnalysisConsumer declaration.
67//===----------------------------------------------------------------------===//
68
69namespace {
70
71 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000072 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000073 Actions FunctionActions;
74 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000075 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +000076 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000077
78 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +000079 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000080 Diagnostic &Diags;
81 ASTContext* Ctx;
82 Preprocessor* PP;
83 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000084 const std::string OutDir;
Eli Friedmane71b85f2009-05-19 10:18:02 +000085 AnalyzerOptions Opts;
Zhongxing Xufda78322009-07-30 09:11:52 +000086
Zhongxing Xud07a0d02009-08-03 03:27:37 +000087
88 // PD is owned by AnalysisManager.
89 PathDiagnosticClient *PD;
90
Zhongxing Xufda78322009-07-30 09:11:52 +000091 StoreManagerCreator CreateStoreMgr;
92 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000093
Zhongxing Xuc471e7b2009-08-03 03:13:46 +000094 llvm::OwningPtr<AnalysisManager> Mgr;
95
Ted Kremenekf4381fd2008-07-02 00:03:09 +000096 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
97 PreprocessorFactory* ppf,
98 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000099 const std::string& outdir,
100 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000101 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000102 Ctx(0), PP(pp), PPF(ppf),
Douglas Gregor17edea82009-07-30 16:10:26 +0000103 OutDir(outdir), Opts(opts), PD(0) {
Zhongxing Xufda78322009-07-30 09:11:52 +0000104 DigestAnalyzerOptions();
105 }
106
107 void DigestAnalyzerOptions() {
108 // Create the PathDiagnosticClient.
109 if (!OutDir.empty()) {
110 switch (Opts.AnalysisDiagOpt) {
111 default:
112#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000113 case PD_##NAME: PD = CREATEFN(OutDir, PP, PPF); break;
Zhongxing Xufda78322009-07-30 09:11:52 +0000114#include "clang/Frontend/Analyses.def"
115 }
116 }
117
118 // Create the analyzer component creators.
119 if (ManagerRegistry::StoreMgrCreator != 0) {
120 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
121 }
122 else {
123 switch (Opts.AnalysisStoreOpt) {
124 default:
125 assert(0 && "Unknown store manager.");
126#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
127 case NAME##Model: CreateStoreMgr = CREATEFN; break;
128#include "clang/Frontend/Analyses.def"
129 }
130 }
131
132 if (ManagerRegistry::ConstraintMgrCreator != 0)
133 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
134 else {
135 switch (Opts.AnalysisConstraintsOpt) {
136 default:
137 assert(0 && "Unknown store manager.");
138#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
139 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
140#include "clang/Frontend/Analyses.def"
141 }
142 }
143 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000144
145 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000146 FunctionActions.push_back(action);
147 ObjCMethodActions.push_back(action);
148 }
149
150 void addObjCImplementationAction(CodeAction action) {
151 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000152 }
153
Ted Kremenekdaac6342008-11-07 02:09:25 +0000154 void addTranslationUnitAction(CodeAction action) {
155 TranslationUnitActions.push_back(action);
156 }
157
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000158 virtual void Initialize(ASTContext &Context) {
159 Ctx = &Context;
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000160 Mgr.reset(new AnalysisManager(*Ctx, Diags, LOpts, PD,
Zhongxing Xuc471e7b2009-08-03 03:13:46 +0000161 CreateStoreMgr, CreateConstraintMgr,
162 Opts.AnalyzerDisplayProgress,
163 Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
164 Opts.PurgeDead, Opts.EagerlyAssume,
165 Opts.TrimGraph));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000166 }
167
Chris Lattner682bf922009-03-29 16:50:03 +0000168 virtual void HandleTopLevelDecl(DeclGroupRef D) {
169 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
170 HandleTopLevelSingleDecl(*I);
171 }
172
173 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000174 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000175
Ted Kremenek81922f02009-02-02 20:52:40 +0000176 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000177 };
178
179
Zhongxing Xu22438a82008-11-27 01:55:08 +0000180
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000181} // end anonymous namespace
182
183namespace llvm {
184 template <> struct FoldingSetTrait<CodeAction> {
185 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
186 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
187 }
188 };
189}
190
191//===----------------------------------------------------------------------===//
192// AnalysisConsumer implementation.
193//===----------------------------------------------------------------------===//
194
Chris Lattner682bf922009-03-29 16:50:03 +0000195void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000196 switch (D->getKind()) {
197 case Decl::Function: {
198 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000199
Eli Friedmane71b85f2009-05-19 10:18:02 +0000200 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
201 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000202 break;
203
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000204 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000205 if (Body) HandleCode(FD, Body, FunctionActions);
206 break;
207 }
208
209 case Decl::ObjCMethod: {
210 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000211
Eli Friedmane71b85f2009-05-19 10:18:02 +0000212 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
213 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000214 return;
215
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000216 Stmt* Body = MD->getBody();
217 if (Body) HandleCode(MD, Body, ObjCMethodActions);
218 break;
219 }
220
221 default:
222 break;
223 }
224}
225
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000226void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdaac6342008-11-07 02:09:25 +0000227 if(!TranslationUnitActions.empty()) {
Ted Kremenekdaac6342008-11-07 02:09:25 +0000228 for (Actions::iterator I = TranslationUnitActions.begin(),
229 E = TranslationUnitActions.end(); I != E; ++I)
Zhongxing Xuc471e7b2009-08-03 03:13:46 +0000230 (*I)(*Mgr);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000231 }
232
Chris Lattnere9077872009-03-28 03:29:40 +0000233 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000234 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000235
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000236 for (DeclContext::decl_iterator I = TUD->decls_begin(),
237 E = TUD->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000238 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000239 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
240 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000241 }
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000242
Ted Kremenek690a7f42009-08-02 05:43:14 +0000243 // Explicitly destroy the PathDiagnosticClient. This will flush its output.
244 // FIXME: This should be replaced with something that doesn't rely on
245 // side-effects in PathDiagnosticClient's destructor.
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000246 Mgr.reset(NULL);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000247}
248
Ted Kremenek81922f02009-02-02 20:52:40 +0000249void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000250
251 // Don't run the actions if an error has occured with parsing the file.
252 if (Diags.hasErrorOccurred())
253 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000254
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000255 // Don't run the actions on declarations in header files unless
256 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000257 if (!Opts.AnalyzeAll &&
258 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000259 return;
260
Zhongxing Xuc471e7b2009-08-03 03:13:46 +0000261 Mgr->setContext(D);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000262
263 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000264 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Zhongxing Xuc471e7b2009-08-03 03:13:46 +0000265 (*I)(*Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000266}
267
268//===----------------------------------------------------------------------===//
269// Analyses
270//===----------------------------------------------------------------------===//
271
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000272static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000273 if (LiveVariables* L = mgr.getLiveVariables()) {
274 BugReporter BR(mgr);
275 CheckDeadStores(*L, BR);
276 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000277}
278
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000279static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000280 if (CFG* c = mgr.getCFG())
281 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000282}
283
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000284
Ted Kremenek78d46242008-07-22 16:21:24 +0000285static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
286 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000287
Ted Kremenek7032f462008-07-03 05:26:14 +0000288
Ted Kremenekbc46f342008-07-02 16:35:50 +0000289 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000290
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000291 // Display progress.
292 mgr.DisplayFunction();
293
Ted Kremenek7032f462008-07-03 05:26:14 +0000294 // Construct the analysis engine.
295 LiveVariables* L = mgr.getLiveVariables();
296 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000297
Ted Kremenekcf118d42009-02-04 23:49:09 +0000298 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000299 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000300 mgr.getStoreManagerCreator(),
301 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000302
Ted Kremenekbc46f342008-07-02 16:35:50 +0000303 Eng.setTransferFunctions(tf);
304
Ted Kremenek78d46242008-07-22 16:21:24 +0000305 if (StandardWarnings) {
306 Eng.RegisterInternalChecks();
307 RegisterAppleChecks(Eng);
308 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000309
310 // Set the graph auditor.
311 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
312 if (mgr.shouldVisualizeUbigraph()) {
313 Auditor.reset(CreateUbiViz());
314 ExplodedNodeImpl::SetAuditor(Auditor.get());
315 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000316
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000317 // Execute the worklist algorithm.
318 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000319
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000320 // Release the auditor (if any) so that it doesn't monitor the graph
321 // created BugReporter.
322 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000323
Ted Kremenek34d77342008-07-02 16:49:11 +0000324 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000325 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000326 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000327
328 // Display warnings.
329 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000330}
331
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000332static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000333 bool StandardWarnings) {
334
Ted Kremenekbc46f342008-07-02 16:35:50 +0000335 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
336 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000337 mgr.getLangOptions());
338
Ted Kremenek78d46242008-07-22 16:21:24 +0000339 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000340}
341
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000342static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000343
344 switch (mgr.getLangOptions().getGCMode()) {
345 default:
346 assert (false && "Invalid GC mode.");
347 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000348 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000349 break;
350
351 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000352 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000353 break;
354
355 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000356 ActionCheckerCFRefAux(mgr, false, true);
357 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000358 break;
359 }
360}
361
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000362static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000363 if (LiveVariables* L = mgr.getLiveVariables()) {
364 mgr.DisplayFunction();
365 L->dumpBlockLiveness(mgr.getSourceManager());
366 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000367}
368
Ted Kremenek902141f2008-07-02 18:23:21 +0000369static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000370 if (CFG* c = mgr.getCFG()) {
371 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000372 c->dump(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000373 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000374}
375
376static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000377 if (CFG* c = mgr.getCFG()) {
378 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000379 c->viewCFG(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000380 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000381}
382
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000383static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
384 BugReporter BR(mgr);
385 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
386}
387
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000388static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000389 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
390 return;
391
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000392 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000393
394 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
395 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000396}
397
Ted Kremenek395aaf22008-07-23 00:45:26 +0000398static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
399 BugReporter BR(mgr);
400 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
401}
402
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000403static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000404 BugReporter BR(mgr);
405
406 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
407 BR);
408}
409
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000410//===----------------------------------------------------------------------===//
411// AnalysisConsumer creation.
412//===----------------------------------------------------------------------===//
413
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000414ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000415 PreprocessorFactory* ppf,
416 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000417 const std::string& OutDir,
418 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000419
420 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000421 lopts, OutDir,
422 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000423
Eli Friedmane71b85f2009-05-19 10:18:02 +0000424 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
425 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000426#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000427 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000428 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000429 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000430#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000431 default: break;
432 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000433
434 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
435 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000436
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000437 return C.take();
438}
439
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000440//===----------------------------------------------------------------------===//
441// Ubigraph Visualization. FIXME: Move to separate file.
442//===----------------------------------------------------------------------===//
443
444namespace {
445
446class UbigraphViz : public ExplodedNodeImpl::Auditor {
447 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000448 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000449 unsigned Cntr;
450
451 typedef llvm::DenseMap<void*,unsigned> VMap;
452 VMap M;
453
454public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000455 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000456 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000457
458 ~UbigraphViz();
459
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000460 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
461};
462
463} // end anonymous namespace
464
465static ExplodedNodeImpl::Auditor* CreateUbiViz() {
466 std::string ErrMsg;
467
Ted Kremenek710ad932008-08-28 03:54:51 +0000468 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000469 if (!ErrMsg.empty())
470 return 0;
471
Ted Kremenek710ad932008-08-28 03:54:51 +0000472 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000473 Filename.appendComponent("llvm_ubi");
474 Filename.makeUnique(true,&ErrMsg);
475
476 if (!ErrMsg.empty())
477 return 0;
478
479 llvm::cerr << "Writing '" << Filename << "'.\n";
480
481 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
482 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000483 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
484 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000485
486 if (!ErrMsg.empty())
487 return 0;
488
Ted Kremenek710ad932008-08-28 03:54:51 +0000489 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000490}
491
492void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000493
494 assert (Src != Dst && "Self-edges are not allowed.");
495
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000496 // Lookup the Src. If it is a new node, it's a root.
497 VMap::iterator SrcI= M.find(Src);
498 unsigned SrcID;
499
500 if (SrcI == M.end()) {
501 M[Src] = SrcID = Cntr++;
502 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
503 }
504 else
505 SrcID = SrcI->second;
506
507 // Lookup the Dst.
508 VMap::iterator DstI= M.find(Dst);
509 unsigned DstID;
510
511 if (DstI == M.end()) {
512 M[Dst] = DstID = Cntr++;
513 *Out << "('vertex', " << DstID << ")\n";
514 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000515 else {
516 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000517 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000518 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
519 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000520
521 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000522 *Out << "('edge', " << SrcID << ", " << DstID
523 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000524}
525
Ted Kremenek56b98712008-08-28 05:02:09 +0000526UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
527 llvm::sys::Path& filename)
528 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
529
530 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
531 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
532 " ('size', '1.5'))\n";
533}
534
Ted Kremenek710ad932008-08-28 03:54:51 +0000535UbigraphViz::~UbigraphViz() {
536 Out.reset(0);
537 llvm::cerr << "Running 'ubiviz' program... ";
538 std::string ErrMsg;
539 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
540 std::vector<const char*> args;
541 args.push_back(Ubiviz.c_str());
542 args.push_back(Filename.c_str());
543 args.push_back(0);
544
545 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
546 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
547 }
548
549 // Delete the directory.
550 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000551}