blob: ed7ea5e8e7c696eb65f85ec98307fdaaa4020417 [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
Ted Kremenek82ec2e92009-07-31 00:34:52 +000087 llvm::OwningPtr<PathDiagnosticClient> PD;
Zhongxing Xufda78322009-07-30 09:11:52 +000088 StoreManagerCreator CreateStoreMgr;
89 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000090
91 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
92 PreprocessorFactory* ppf,
93 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000094 const std::string& outdir,
95 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000096 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000097 Ctx(0), PP(pp), PPF(ppf),
Douglas Gregor17edea82009-07-30 16:10:26 +000098 OutDir(outdir), Opts(opts), PD(0) {
Zhongxing Xufda78322009-07-30 09:11:52 +000099 DigestAnalyzerOptions();
100 }
101
102 void DigestAnalyzerOptions() {
103 // Create the PathDiagnosticClient.
104 if (!OutDir.empty()) {
105 switch (Opts.AnalysisDiagOpt) {
106 default:
107#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
Ted Kremenek82ec2e92009-07-31 00:34:52 +0000108 case PD_##NAME: PD.reset(CREATEFN(OutDir, PP, PPF)); break;
Zhongxing Xufda78322009-07-30 09:11:52 +0000109#include "clang/Frontend/Analyses.def"
110 }
111 }
112
113 // Create the analyzer component creators.
114 if (ManagerRegistry::StoreMgrCreator != 0) {
115 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
116 }
117 else {
118 switch (Opts.AnalysisStoreOpt) {
119 default:
120 assert(0 && "Unknown store manager.");
121#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
122 case NAME##Model: CreateStoreMgr = CREATEFN; break;
123#include "clang/Frontend/Analyses.def"
124 }
125 }
126
127 if (ManagerRegistry::ConstraintMgrCreator != 0)
128 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
129 else {
130 switch (Opts.AnalysisConstraintsOpt) {
131 default:
132 assert(0 && "Unknown store manager.");
133#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
134 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
135#include "clang/Frontend/Analyses.def"
136 }
137 }
138 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000139
140 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000141 FunctionActions.push_back(action);
142 ObjCMethodActions.push_back(action);
143 }
144
145 void addObjCImplementationAction(CodeAction action) {
146 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000147 }
148
Ted Kremenekdaac6342008-11-07 02:09:25 +0000149 void addTranslationUnitAction(CodeAction action) {
150 TranslationUnitActions.push_back(action);
151 }
152
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000153 virtual void Initialize(ASTContext &Context) {
154 Ctx = &Context;
155 }
156
Chris Lattner682bf922009-03-29 16:50:03 +0000157 virtual void HandleTopLevelDecl(DeclGroupRef D) {
158 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
159 HandleTopLevelSingleDecl(*I);
160 }
161
162 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000163 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000164
Ted Kremenek81922f02009-02-02 20:52:40 +0000165 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000166 };
167
168
Zhongxing Xu22438a82008-11-27 01:55:08 +0000169
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000170} // end anonymous namespace
171
172namespace llvm {
173 template <> struct FoldingSetTrait<CodeAction> {
174 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
175 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
176 }
177 };
178}
179
180//===----------------------------------------------------------------------===//
181// AnalysisConsumer implementation.
182//===----------------------------------------------------------------------===//
183
Chris Lattner682bf922009-03-29 16:50:03 +0000184void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000185 switch (D->getKind()) {
186 case Decl::Function: {
187 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000188
Eli Friedmane71b85f2009-05-19 10:18:02 +0000189 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
190 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000191 break;
192
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000193 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000194 if (Body) HandleCode(FD, Body, FunctionActions);
195 break;
196 }
197
198 case Decl::ObjCMethod: {
199 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000200
Eli Friedmane71b85f2009-05-19 10:18:02 +0000201 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
202 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000203 return;
204
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000205 Stmt* Body = MD->getBody();
206 if (Body) HandleCode(MD, Body, ObjCMethodActions);
207 break;
208 }
209
210 default:
211 break;
212 }
213}
214
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000215void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000216
Ted Kremenekdaac6342008-11-07 02:09:25 +0000217 if(!TranslationUnitActions.empty()) {
Ted Kremenek82ec2e92009-07-31 00:34:52 +0000218 AnalysisManager mgr(*Ctx, Diags, LOpts, PD.get(),
Zhongxing Xufda78322009-07-30 09:11:52 +0000219 CreateStoreMgr, CreateConstraintMgr,
220 Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot,
221 Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume,
222 Opts.TrimGraph);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000223 for (Actions::iterator I = TranslationUnitActions.begin(),
224 E = TranslationUnitActions.end(); I != E; ++I)
225 (*I)(mgr);
226 }
227
Chris Lattnere9077872009-03-28 03:29:40 +0000228 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000229 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000230
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000231 for (DeclContext::decl_iterator I = TUD->decls_begin(),
232 E = TUD->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000233 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000234 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
235 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000236 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000237}
238
Ted Kremenek81922f02009-02-02 20:52:40 +0000239void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000240
241 // Don't run the actions if an error has occured with parsing the file.
242 if (Diags.hasErrorOccurred())
243 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000244
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000245 // Don't run the actions on declarations in header files unless
246 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000247 if (!Opts.AnalyzeAll &&
248 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000249 return;
250
251 // Create an AnalysisManager that will manage the state for analyzing
252 // this method/function.
Ted Kremenek82ec2e92009-07-31 00:34:52 +0000253 AnalysisManager mgr(D, *Ctx, Diags, LOpts, PD.get(),
Zhongxing Xufda78322009-07-30 09:11:52 +0000254 CreateStoreMgr, CreateConstraintMgr,
255 Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot,
256 Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume,
257 Opts.TrimGraph);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000258
259 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000260 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000261 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000262}
263
264//===----------------------------------------------------------------------===//
265// Analyses
266//===----------------------------------------------------------------------===//
267
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000268static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000269 if (LiveVariables* L = mgr.getLiveVariables()) {
270 BugReporter BR(mgr);
271 CheckDeadStores(*L, BR);
272 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000273}
274
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000275static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000276 if (CFG* c = mgr.getCFG())
277 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000278}
279
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000280
Ted Kremenek78d46242008-07-22 16:21:24 +0000281static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
282 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000283
Ted Kremenek7032f462008-07-03 05:26:14 +0000284
Ted Kremenekbc46f342008-07-02 16:35:50 +0000285 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000286
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000287 // Display progress.
288 mgr.DisplayFunction();
289
Ted Kremenek7032f462008-07-03 05:26:14 +0000290 // Construct the analysis engine.
291 LiveVariables* L = mgr.getLiveVariables();
292 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000293
Ted Kremenekcf118d42009-02-04 23:49:09 +0000294 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000295 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000296 mgr.getStoreManagerCreator(),
297 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000298
Ted Kremenekbc46f342008-07-02 16:35:50 +0000299 Eng.setTransferFunctions(tf);
300
Ted Kremenek78d46242008-07-22 16:21:24 +0000301 if (StandardWarnings) {
302 Eng.RegisterInternalChecks();
303 RegisterAppleChecks(Eng);
304 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000305
306 // Set the graph auditor.
307 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
308 if (mgr.shouldVisualizeUbigraph()) {
309 Auditor.reset(CreateUbiViz());
310 ExplodedNodeImpl::SetAuditor(Auditor.get());
311 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000312
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000313 // Execute the worklist algorithm.
314 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000315
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000316 // Release the auditor (if any) so that it doesn't monitor the graph
317 // created BugReporter.
318 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000319
Ted Kremenek34d77342008-07-02 16:49:11 +0000320 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000321 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000322 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000323
324 // Display warnings.
325 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000326}
327
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000328static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000329 bool StandardWarnings) {
330
Ted Kremenekbc46f342008-07-02 16:35:50 +0000331 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
332 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000333 mgr.getLangOptions());
334
Ted Kremenek78d46242008-07-22 16:21:24 +0000335 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000336}
337
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000338static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000339
340 switch (mgr.getLangOptions().getGCMode()) {
341 default:
342 assert (false && "Invalid GC mode.");
343 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000344 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000345 break;
346
347 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000348 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000349 break;
350
351 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000352 ActionCheckerCFRefAux(mgr, false, true);
353 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000354 break;
355 }
356}
357
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000358static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000359 if (LiveVariables* L = mgr.getLiveVariables()) {
360 mgr.DisplayFunction();
361 L->dumpBlockLiveness(mgr.getSourceManager());
362 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000363}
364
Ted Kremenek902141f2008-07-02 18:23:21 +0000365static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000366 if (CFG* c = mgr.getCFG()) {
367 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000368 c->dump(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000369 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000370}
371
372static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000373 if (CFG* c = mgr.getCFG()) {
374 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000375 c->viewCFG(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000376 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000377}
378
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000379static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
380 BugReporter BR(mgr);
381 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
382}
383
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000384static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000385 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
386 return;
387
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000388 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000389
390 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
391 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000392}
393
Ted Kremenek395aaf22008-07-23 00:45:26 +0000394static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
395 BugReporter BR(mgr);
396 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
397}
398
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000399static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000400 BugReporter BR(mgr);
401
402 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
403 BR);
404}
405
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000406//===----------------------------------------------------------------------===//
407// AnalysisConsumer creation.
408//===----------------------------------------------------------------------===//
409
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000410ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000411 PreprocessorFactory* ppf,
412 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000413 const std::string& OutDir,
414 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000415
416 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000417 lopts, OutDir,
418 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000419
Eli Friedmane71b85f2009-05-19 10:18:02 +0000420 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
421 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000422#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000423 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000424 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000425 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000426#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000427 default: break;
428 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000429
430 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
431 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000432
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000433 return C.take();
434}
435
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000436//===----------------------------------------------------------------------===//
437// Ubigraph Visualization. FIXME: Move to separate file.
438//===----------------------------------------------------------------------===//
439
440namespace {
441
442class UbigraphViz : public ExplodedNodeImpl::Auditor {
443 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000444 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000445 unsigned Cntr;
446
447 typedef llvm::DenseMap<void*,unsigned> VMap;
448 VMap M;
449
450public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000451 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000452 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000453
454 ~UbigraphViz();
455
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000456 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
457};
458
459} // end anonymous namespace
460
461static ExplodedNodeImpl::Auditor* CreateUbiViz() {
462 std::string ErrMsg;
463
Ted Kremenek710ad932008-08-28 03:54:51 +0000464 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000465 if (!ErrMsg.empty())
466 return 0;
467
Ted Kremenek710ad932008-08-28 03:54:51 +0000468 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000469 Filename.appendComponent("llvm_ubi");
470 Filename.makeUnique(true,&ErrMsg);
471
472 if (!ErrMsg.empty())
473 return 0;
474
475 llvm::cerr << "Writing '" << Filename << "'.\n";
476
477 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
478 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000479 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
480 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000481
482 if (!ErrMsg.empty())
483 return 0;
484
Ted Kremenek710ad932008-08-28 03:54:51 +0000485 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000486}
487
488void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000489
490 assert (Src != Dst && "Self-edges are not allowed.");
491
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000492 // Lookup the Src. If it is a new node, it's a root.
493 VMap::iterator SrcI= M.find(Src);
494 unsigned SrcID;
495
496 if (SrcI == M.end()) {
497 M[Src] = SrcID = Cntr++;
498 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
499 }
500 else
501 SrcID = SrcI->second;
502
503 // Lookup the Dst.
504 VMap::iterator DstI= M.find(Dst);
505 unsigned DstID;
506
507 if (DstI == M.end()) {
508 M[Dst] = DstID = Cntr++;
509 *Out << "('vertex', " << DstID << ")\n";
510 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000511 else {
512 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000513 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000514 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
515 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000516
517 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000518 *Out << "('edge', " << SrcID << ", " << DstID
519 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000520}
521
Ted Kremenek56b98712008-08-28 05:02:09 +0000522UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
523 llvm::sys::Path& filename)
524 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
525
526 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
527 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
528 " ('size', '1.5'))\n";
529}
530
Ted Kremenek710ad932008-08-28 03:54:51 +0000531UbigraphViz::~UbigraphViz() {
532 Out.reset(0);
533 llvm::cerr << "Running 'ubiviz' program... ";
534 std::string ErrMsg;
535 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
536 std::vector<const char*> args;
537 args.push_back(Ubiviz.c_str());
538 args.push_back(Filename.c_str());
539 args.push_back(0);
540
541 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
542 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
543 }
544
545 // Delete the directory.
546 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000547}