blob: 44d79be11c1e62516ac424a39a73c0e35a9a9303 [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 Kremenek690a7f42009-08-02 05:43:14 +0000237
238 // Explicitly destroy the PathDiagnosticClient. This will flush its output.
239 // FIXME: This should be replaced with something that doesn't rely on
240 // side-effects in PathDiagnosticClient's destructor.
241 PD.reset(NULL);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000242}
243
Ted Kremenek81922f02009-02-02 20:52:40 +0000244void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000245
246 // Don't run the actions if an error has occured with parsing the file.
247 if (Diags.hasErrorOccurred())
248 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000249
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000250 // Don't run the actions on declarations in header files unless
251 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000252 if (!Opts.AnalyzeAll &&
253 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000254 return;
255
256 // Create an AnalysisManager that will manage the state for analyzing
257 // this method/function.
Ted Kremenek82ec2e92009-07-31 00:34:52 +0000258 AnalysisManager mgr(D, *Ctx, Diags, LOpts, PD.get(),
Zhongxing Xufda78322009-07-30 09:11:52 +0000259 CreateStoreMgr, CreateConstraintMgr,
260 Opts.AnalyzerDisplayProgress, Opts.VisualizeEGDot,
261 Opts.VisualizeEGUbi, Opts.PurgeDead, Opts.EagerlyAssume,
262 Opts.TrimGraph);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000263
264 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000265 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000266 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000267}
268
269//===----------------------------------------------------------------------===//
270// Analyses
271//===----------------------------------------------------------------------===//
272
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000273static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000274 if (LiveVariables* L = mgr.getLiveVariables()) {
275 BugReporter BR(mgr);
276 CheckDeadStores(*L, BR);
277 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000278}
279
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000280static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000281 if (CFG* c = mgr.getCFG())
282 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000283}
284
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000285
Ted Kremenek78d46242008-07-22 16:21:24 +0000286static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
287 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000288
Ted Kremenek7032f462008-07-03 05:26:14 +0000289
Ted Kremenekbc46f342008-07-02 16:35:50 +0000290 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000291
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000292 // Display progress.
293 mgr.DisplayFunction();
294
Ted Kremenek7032f462008-07-03 05:26:14 +0000295 // Construct the analysis engine.
296 LiveVariables* L = mgr.getLiveVariables();
297 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000298
Ted Kremenekcf118d42009-02-04 23:49:09 +0000299 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000300 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000301 mgr.getStoreManagerCreator(),
302 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000303
Ted Kremenekbc46f342008-07-02 16:35:50 +0000304 Eng.setTransferFunctions(tf);
305
Ted Kremenek78d46242008-07-22 16:21:24 +0000306 if (StandardWarnings) {
307 Eng.RegisterInternalChecks();
308 RegisterAppleChecks(Eng);
309 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000310
311 // Set the graph auditor.
312 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
313 if (mgr.shouldVisualizeUbigraph()) {
314 Auditor.reset(CreateUbiViz());
315 ExplodedNodeImpl::SetAuditor(Auditor.get());
316 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000317
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000318 // Execute the worklist algorithm.
319 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000320
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000321 // Release the auditor (if any) so that it doesn't monitor the graph
322 // created BugReporter.
323 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000324
Ted Kremenek34d77342008-07-02 16:49:11 +0000325 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000326 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000327 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000328
329 // Display warnings.
330 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000331}
332
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000333static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000334 bool StandardWarnings) {
335
Ted Kremenekbc46f342008-07-02 16:35:50 +0000336 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
337 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000338 mgr.getLangOptions());
339
Ted Kremenek78d46242008-07-22 16:21:24 +0000340 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000341}
342
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000343static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000344
345 switch (mgr.getLangOptions().getGCMode()) {
346 default:
347 assert (false && "Invalid GC mode.");
348 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000349 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000350 break;
351
352 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000353 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000354 break;
355
356 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000357 ActionCheckerCFRefAux(mgr, false, true);
358 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000359 break;
360 }
361}
362
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000363static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000364 if (LiveVariables* L = mgr.getLiveVariables()) {
365 mgr.DisplayFunction();
366 L->dumpBlockLiveness(mgr.getSourceManager());
367 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000368}
369
Ted Kremenek902141f2008-07-02 18:23:21 +0000370static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000371 if (CFG* c = mgr.getCFG()) {
372 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000373 c->dump(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000374 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000375}
376
377static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000378 if (CFG* c = mgr.getCFG()) {
379 mgr.DisplayFunction();
Zhongxing Xu7d49c712009-07-30 09:14:54 +0000380 c->viewCFG(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000381 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000382}
383
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000384static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
385 BugReporter BR(mgr);
386 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
387}
388
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000389static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000390 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
391 return;
392
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000393 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000394
395 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
396 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000397}
398
Ted Kremenek395aaf22008-07-23 00:45:26 +0000399static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
400 BugReporter BR(mgr);
401 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
402}
403
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000404static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000405 BugReporter BR(mgr);
406
407 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
408 BR);
409}
410
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000411//===----------------------------------------------------------------------===//
412// AnalysisConsumer creation.
413//===----------------------------------------------------------------------===//
414
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000415ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000416 PreprocessorFactory* ppf,
417 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000418 const std::string& OutDir,
419 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000420
421 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000422 lopts, OutDir,
423 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000424
Eli Friedmane71b85f2009-05-19 10:18:02 +0000425 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
426 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000427#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000428 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000429 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000430 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000431#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000432 default: break;
433 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000434
435 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
436 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000437
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000438 return C.take();
439}
440
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000441//===----------------------------------------------------------------------===//
442// Ubigraph Visualization. FIXME: Move to separate file.
443//===----------------------------------------------------------------------===//
444
445namespace {
446
447class UbigraphViz : public ExplodedNodeImpl::Auditor {
448 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000449 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000450 unsigned Cntr;
451
452 typedef llvm::DenseMap<void*,unsigned> VMap;
453 VMap M;
454
455public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000456 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000457 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000458
459 ~UbigraphViz();
460
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000461 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
462};
463
464} // end anonymous namespace
465
466static ExplodedNodeImpl::Auditor* CreateUbiViz() {
467 std::string ErrMsg;
468
Ted Kremenek710ad932008-08-28 03:54:51 +0000469 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000470 if (!ErrMsg.empty())
471 return 0;
472
Ted Kremenek710ad932008-08-28 03:54:51 +0000473 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000474 Filename.appendComponent("llvm_ubi");
475 Filename.makeUnique(true,&ErrMsg);
476
477 if (!ErrMsg.empty())
478 return 0;
479
480 llvm::cerr << "Writing '" << Filename << "'.\n";
481
482 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
483 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000484 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
485 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000486
487 if (!ErrMsg.empty())
488 return 0;
489
Ted Kremenek710ad932008-08-28 03:54:51 +0000490 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000491}
492
493void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000494
495 assert (Src != Dst && "Self-edges are not allowed.");
496
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000497 // Lookup the Src. If it is a new node, it's a root.
498 VMap::iterator SrcI= M.find(Src);
499 unsigned SrcID;
500
501 if (SrcI == M.end()) {
502 M[Src] = SrcID = Cntr++;
503 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
504 }
505 else
506 SrcID = SrcI->second;
507
508 // Lookup the Dst.
509 VMap::iterator DstI= M.find(Dst);
510 unsigned DstID;
511
512 if (DstI == M.end()) {
513 M[Dst] = DstID = Cntr++;
514 *Out << "('vertex', " << DstID << ")\n";
515 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000516 else {
517 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000518 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000519 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
520 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000521
522 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000523 *Out << "('edge', " << SrcID << ", " << DstID
524 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000525}
526
Ted Kremenek56b98712008-08-28 05:02:09 +0000527UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
528 llvm::sys::Path& filename)
529 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
530
531 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
532 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
533 " ('size', '1.5'))\n";
534}
535
Ted Kremenek710ad932008-08-28 03:54:51 +0000536UbigraphViz::~UbigraphViz() {
537 Out.reset(0);
538 llvm::cerr << "Running 'ubiviz' program... ";
539 std::string ErrMsg;
540 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
541 std::vector<const char*> args;
542 args.push_back(Ubiviz.c_str());
543 args.push_back(Filename.c_str());
544 args.push_back(0);
545
546 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
547 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
548 }
549
550 // Delete the directory.
551 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000552}