blob: 9c3274309c083884f172c785cf8d0349ea9388a2 [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
87 // PD is owned by AnalysisManager.
88 PathDiagnosticClient *PD;
89 StoreManagerCreator CreateStoreMgr;
90 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000091
92 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
93 PreprocessorFactory* ppf,
94 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000095 const std::string& outdir,
96 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000097 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000098 Ctx(0), PP(pp), PPF(ppf),
Zhongxing Xufda78322009-07-30 09:11:52 +000099 OutDir(outdir), Opts(opts) {
100 DigestAnalyzerOptions();
101 }
102
103 void DigestAnalyzerOptions() {
104 // Create the PathDiagnosticClient.
105 if (!OutDir.empty()) {
106 switch (Opts.AnalysisDiagOpt) {
107 default:
108#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
109 case PD_##NAME: PD = CREATEFN(OutDir, PP, PPF); break;
110#include "clang/Frontend/Analyses.def"
111 }
112 }
113
114 // Create the analyzer component creators.
115 if (ManagerRegistry::StoreMgrCreator != 0) {
116 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
117 }
118 else {
119 switch (Opts.AnalysisStoreOpt) {
120 default:
121 assert(0 && "Unknown store manager.");
122#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
123 case NAME##Model: CreateStoreMgr = CREATEFN; break;
124#include "clang/Frontend/Analyses.def"
125 }
126 }
127
128 if (ManagerRegistry::ConstraintMgrCreator != 0)
129 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
130 else {
131 switch (Opts.AnalysisConstraintsOpt) {
132 default:
133 assert(0 && "Unknown store manager.");
134#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
135 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
136#include "clang/Frontend/Analyses.def"
137 }
138 }
139 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000140
141 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000142 FunctionActions.push_back(action);
143 ObjCMethodActions.push_back(action);
144 }
145
146 void addObjCImplementationAction(CodeAction action) {
147 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000148 }
149
Ted Kremenekdaac6342008-11-07 02:09:25 +0000150 void addTranslationUnitAction(CodeAction action) {
151 TranslationUnitActions.push_back(action);
152 }
153
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000154 virtual void Initialize(ASTContext &Context) {
155 Ctx = &Context;
156 }
157
Chris Lattner682bf922009-03-29 16:50:03 +0000158 virtual void HandleTopLevelDecl(DeclGroupRef D) {
159 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
160 HandleTopLevelSingleDecl(*I);
161 }
162
163 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000164 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000165
Ted Kremenek81922f02009-02-02 20:52:40 +0000166 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000167 };
168
169
Zhongxing Xu22438a82008-11-27 01:55:08 +0000170
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000171} // end anonymous namespace
172
173namespace llvm {
174 template <> struct FoldingSetTrait<CodeAction> {
175 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
176 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
177 }
178 };
179}
180
181//===----------------------------------------------------------------------===//
182// AnalysisConsumer implementation.
183//===----------------------------------------------------------------------===//
184
Chris Lattner682bf922009-03-29 16:50:03 +0000185void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000186 switch (D->getKind()) {
187 case Decl::Function: {
188 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000189
Eli Friedmane71b85f2009-05-19 10:18:02 +0000190 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
191 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000192 break;
193
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000194 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000195 if (Body) HandleCode(FD, Body, FunctionActions);
196 break;
197 }
198
199 case Decl::ObjCMethod: {
200 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000201
Eli Friedmane71b85f2009-05-19 10:18:02 +0000202 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
203 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000204 return;
205
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000206 Stmt* Body = MD->getBody();
207 if (Body) HandleCode(MD, Body, ObjCMethodActions);
208 break;
209 }
210
211 default:
212 break;
213 }
214}
215
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000216void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000217
Ted Kremenekdaac6342008-11-07 02:09:25 +0000218 if(!TranslationUnitActions.empty()) {
Zhongxing Xufda78322009-07-30 09:11:52 +0000219 AnalysisManager mgr(*Ctx, Diags, LOpts, PD,
220 CreateStoreMgr, CreateConstraintMgr,
221 Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot,
222 Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume,
223 Opts.TrimGraph);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000224 for (Actions::iterator I = TranslationUnitActions.begin(),
225 E = TranslationUnitActions.end(); I != E; ++I)
226 (*I)(mgr);
227 }
228
Chris Lattnere9077872009-03-28 03:29:40 +0000229 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000230 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000231
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000232 for (DeclContext::decl_iterator I = TUD->decls_begin(),
233 E = TUD->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000234 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000235 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
236 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000237 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000238}
239
Ted Kremenek81922f02009-02-02 20:52:40 +0000240void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000241
242 // Don't run the actions if an error has occured with parsing the file.
243 if (Diags.hasErrorOccurred())
244 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000245
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000246 // Don't run the actions on declarations in header files unless
247 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000248 if (!Opts.AnalyzeAll &&
249 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000250 return;
251
252 // Create an AnalysisManager that will manage the state for analyzing
253 // this method/function.
Zhongxing Xufda78322009-07-30 09:11:52 +0000254 AnalysisManager mgr(D, *Ctx, Diags, LOpts, PD,
255 CreateStoreMgr, CreateConstraintMgr,
256 Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot,
257 Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume,
258 Opts.TrimGraph);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000259
260 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000261 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000262 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000263}
264
265//===----------------------------------------------------------------------===//
266// Analyses
267//===----------------------------------------------------------------------===//
268
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000269static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000270 if (LiveVariables* L = mgr.getLiveVariables()) {
271 BugReporter BR(mgr);
272 CheckDeadStores(*L, BR);
273 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000274}
275
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000276static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000277 if (CFG* c = mgr.getCFG())
278 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000279}
280
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000281
Ted Kremenek78d46242008-07-22 16:21:24 +0000282static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
283 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000284
Ted Kremenek7032f462008-07-03 05:26:14 +0000285
Ted Kremenekbc46f342008-07-02 16:35:50 +0000286 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000287
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000288 // Display progress.
289 mgr.DisplayFunction();
290
Ted Kremenek7032f462008-07-03 05:26:14 +0000291 // Construct the analysis engine.
292 LiveVariables* L = mgr.getLiveVariables();
293 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000294
Ted Kremenekcf118d42009-02-04 23:49:09 +0000295 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000296 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000297 mgr.getStoreManagerCreator(),
298 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000299
Ted Kremenekbc46f342008-07-02 16:35:50 +0000300 Eng.setTransferFunctions(tf);
301
Ted Kremenek78d46242008-07-22 16:21:24 +0000302 if (StandardWarnings) {
303 Eng.RegisterInternalChecks();
304 RegisterAppleChecks(Eng);
305 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000306
307 // Set the graph auditor.
308 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
309 if (mgr.shouldVisualizeUbigraph()) {
310 Auditor.reset(CreateUbiViz());
311 ExplodedNodeImpl::SetAuditor(Auditor.get());
312 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000313
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000314 // Execute the worklist algorithm.
315 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000316
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000317 // Release the auditor (if any) so that it doesn't monitor the graph
318 // created BugReporter.
319 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000320
Ted Kremenek34d77342008-07-02 16:49:11 +0000321 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000322 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000323 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000324
325 // Display warnings.
326 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000327}
328
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000329static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000330 bool StandardWarnings) {
331
Ted Kremenekbc46f342008-07-02 16:35:50 +0000332 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
333 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000334 mgr.getLangOptions());
335
Ted Kremenek78d46242008-07-22 16:21:24 +0000336 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000337}
338
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000339static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000340
341 switch (mgr.getLangOptions().getGCMode()) {
342 default:
343 assert (false && "Invalid GC mode.");
344 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000345 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000346 break;
347
348 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000349 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000350 break;
351
352 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000353 ActionCheckerCFRefAux(mgr, false, true);
354 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000355 break;
356 }
357}
358
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000359static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000360 if (LiveVariables* L = mgr.getLiveVariables()) {
361 mgr.DisplayFunction();
362 L->dumpBlockLiveness(mgr.getSourceManager());
363 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000364}
365
Ted Kremenek902141f2008-07-02 18:23:21 +0000366static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000367 if (CFG* c = mgr.getCFG()) {
368 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000369 c->dump(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000370 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000371}
372
373static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000374 if (CFG* c = mgr.getCFG()) {
375 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000376 c->viewCFG(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000377 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000378}
379
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000380static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
381 BugReporter BR(mgr);
382 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
383}
384
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000385static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000386 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
387 return;
388
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000389 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000390
391 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
392 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000393}
394
Ted Kremenek395aaf22008-07-23 00:45:26 +0000395static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
396 BugReporter BR(mgr);
397 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
398}
399
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000400static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000401 BugReporter BR(mgr);
402
403 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
404 BR);
405}
406
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000407//===----------------------------------------------------------------------===//
408// AnalysisConsumer creation.
409//===----------------------------------------------------------------------===//
410
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000411ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000412 PreprocessorFactory* ppf,
413 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000414 const std::string& OutDir,
415 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000416
417 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000418 lopts, OutDir,
419 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000420
Eli Friedmane71b85f2009-05-19 10:18:02 +0000421 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
422 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000423#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000424 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000425 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000426 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000427#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000428 default: break;
429 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000430
431 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
432 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000433
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000434 return C.take();
435}
436
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000437//===----------------------------------------------------------------------===//
438// Ubigraph Visualization. FIXME: Move to separate file.
439//===----------------------------------------------------------------------===//
440
441namespace {
442
443class UbigraphViz : public ExplodedNodeImpl::Auditor {
444 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000445 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000446 unsigned Cntr;
447
448 typedef llvm::DenseMap<void*,unsigned> VMap;
449 VMap M;
450
451public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000452 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000453 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000454
455 ~UbigraphViz();
456
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000457 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
458};
459
460} // end anonymous namespace
461
462static ExplodedNodeImpl::Auditor* CreateUbiViz() {
463 std::string ErrMsg;
464
Ted Kremenek710ad932008-08-28 03:54:51 +0000465 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000466 if (!ErrMsg.empty())
467 return 0;
468
Ted Kremenek710ad932008-08-28 03:54:51 +0000469 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000470 Filename.appendComponent("llvm_ubi");
471 Filename.makeUnique(true,&ErrMsg);
472
473 if (!ErrMsg.empty())
474 return 0;
475
476 llvm::cerr << "Writing '" << Filename << "'.\n";
477
478 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
479 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000480 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
481 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000482
483 if (!ErrMsg.empty())
484 return 0;
485
Ted Kremenek710ad932008-08-28 03:54:51 +0000486 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000487}
488
489void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000490
491 assert (Src != Dst && "Self-edges are not allowed.");
492
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000493 // Lookup the Src. If it is a new node, it's a root.
494 VMap::iterator SrcI= M.find(Src);
495 unsigned SrcID;
496
497 if (SrcI == M.end()) {
498 M[Src] = SrcID = Cntr++;
499 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
500 }
501 else
502 SrcID = SrcI->second;
503
504 // Lookup the Dst.
505 VMap::iterator DstI= M.find(Dst);
506 unsigned DstID;
507
508 if (DstI == M.end()) {
509 M[Dst] = DstID = Cntr++;
510 *Out << "('vertex', " << DstID << ")\n";
511 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000512 else {
513 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000514 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000515 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
516 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000517
518 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000519 *Out << "('edge', " << SrcID << ", " << DstID
520 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000521}
522
Ted Kremenek56b98712008-08-28 05:02:09 +0000523UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
524 llvm::sys::Path& filename)
525 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
526
527 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
528 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
529 " ('size', '1.5'))\n";
530}
531
Ted Kremenek710ad932008-08-28 03:54:51 +0000532UbigraphViz::~UbigraphViz() {
533 Out.reset(0);
534 llvm::cerr << "Running 'ubiviz' program... ";
535 std::string ErrMsg;
536 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
537 std::vector<const char*> args;
538 args.push_back(Ubiviz.c_str());
539 args.push_back(Filename.c_str());
540 args.push_back(0);
541
542 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
543 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
544 }
545
546 // Delete the directory.
547 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000548}