blob: 5f338c8af53dd8cd2704c998d62e5c6505dc37b4 [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
14#include "ASTConsumers.h"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000016#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclObjC.h"
19#include "llvm/Support/Compiler.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000020#include "llvm/ADT/OwningPtr.h"
21#include "clang/AST/CFG.h"
22#include "clang/Analysis/Analyses/LiveVariables.h"
23#include "clang/Analysis/PathDiagnostic.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
26#include "clang/AST/ParentMap.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000027#include "clang/AST/TranslationUnit.h"
Ted Kremenekc0959972008-07-02 21:24:01 +000028#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000029#include "clang/Analysis/Analyses/LiveVariables.h"
30#include "clang/Analysis/LocalCheckers.h"
31#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
32#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek34d77342008-07-02 16:49:11 +000033#include "llvm/Support/Streams.h"
Ted Kremenekf8ce6992008-08-27 22:31:43 +000034#include "llvm/Support/raw_ostream.h"
35#include "llvm/System/Path.h"
Ted Kremenek710ad932008-08-28 03:54:51 +000036#include "llvm/System/Program.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000037#include <vector>
38
Ted Kremenekf4381fd2008-07-02 00:03:09 +000039using namespace clang;
40
Ted Kremenekf8ce6992008-08-27 22:31:43 +000041static ExplodedNodeImpl::Auditor* CreateUbiViz();
Ted Kremenekf4381fd2008-07-02 00:03:09 +000042
43//===----------------------------------------------------------------------===//
44// Basic type definitions.
45//===----------------------------------------------------------------------===//
46
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000047namespace {
Ted Kremenekf4381fd2008-07-02 00:03:09 +000048 class AnalysisManager;
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//===----------------------------------------------------------------------===//
53// AnalysisConsumer declaration.
54//===----------------------------------------------------------------------===//
55
56namespace {
57
58 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000059 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000060 Actions FunctionActions;
61 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000062 Actions ObjCImplementationActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000063
64 public:
Ted Kremenekf8ce6992008-08-27 22:31:43 +000065 const bool VisGraphviz;
66 const bool VisUbigraph;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000067 const bool TrimGraph;
Ted Kremenek95c7b002008-10-24 01:04:59 +000068 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000069 Diagnostic &Diags;
70 ASTContext* Ctx;
71 Preprocessor* PP;
72 PreprocessorFactory* PPF;
73 const std::string HTMLDir;
74 const std::string FName;
75 llvm::OwningPtr<PathDiagnosticClient> PD;
76 bool AnalyzeAll;
Ted Kremenek95c7b002008-10-24 01:04:59 +000077 AnalysisStores SM;
Ted Kremenek4fc82c82008-11-03 23:18:07 +000078 AnalysisDiagClients DC;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000079
80 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
81 PreprocessorFactory* ppf,
82 const LangOptions& lopts,
83 const std::string& fname,
84 const std::string& htmldir,
Ted Kremenek4fc82c82008-11-03 23:18:07 +000085 AnalysisStores sm, AnalysisDiagClients dc,
Ted Kremenekf8ce6992008-08-27 22:31:43 +000086 bool visgraphviz, bool visubi, bool trim, bool analyzeAll)
87 : VisGraphviz(visgraphviz), VisUbigraph(visubi), TrimGraph(trim),
88 LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000089 Ctx(0), PP(pp), PPF(ppf),
90 HTMLDir(htmldir),
91 FName(fname),
Ted Kremenek4fc82c82008-11-03 23:18:07 +000092 AnalyzeAll(analyzeAll), SM(sm), DC(dc) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +000093
94 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000095 FunctionActions.push_back(action);
96 ObjCMethodActions.push_back(action);
97 }
98
99 void addObjCImplementationAction(CodeAction action) {
100 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000101 }
102
103 virtual void Initialize(ASTContext &Context) {
104 Ctx = &Context;
105 }
106
107 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000108 virtual void HandleTranslationUnit(TranslationUnit &TU);
109
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000110 void HandleCode(Decl* D, Stmt* Body, Actions actions);
111 };
112
113
Ted Kremenekc0959972008-07-02 21:24:01 +0000114 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000115 Decl* D;
116 Stmt* Body;
117 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000118 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000119
120 llvm::OwningPtr<CFG> cfg;
121 llvm::OwningPtr<LiveVariables> liveness;
122 llvm::OwningPtr<ParentMap> PM;
123
124 public:
125 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b)
Ted Kremenek34d77342008-07-02 16:49:11 +0000126 : D(d), Body(b), C(c), DisplayedFunction(false) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000127
128
129 Decl* getCodeDecl() const { return D; }
130 Stmt* getBody() const { return Body; }
131
Ted Kremenek95c7b002008-10-24 01:04:59 +0000132 GRStateManager::StoreManagerCreator getStoreManagerCreator() {
133 switch (C.SM) {
134 default:
135#define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\
136case NAME##Model: return Create##NAME##Manager;
137#include "Analyses.def"
138 }
139 };
140
Ted Kremenek7032f462008-07-03 05:26:14 +0000141 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000142 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000143 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000144 }
145
Ted Kremenekc0959972008-07-02 21:24:01 +0000146 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000147 if (!PM)
148 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000149 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000150 }
151
Ted Kremenekc0959972008-07-02 21:24:01 +0000152 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000153 return *C.Ctx;
154 }
155
Ted Kremenekc0959972008-07-02 21:24:01 +0000156 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000157 return getContext().getSourceManager();
158 }
159
Ted Kremenekc0959972008-07-02 21:24:01 +0000160 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000161 return C.Diags;
162 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000163
164 const LangOptions& getLangOptions() const {
165 return C.LOpts;
166 }
167
Ted Kremenekc0959972008-07-02 21:24:01 +0000168 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000169 if (C.PD.get() == 0 && !C.HTMLDir.empty()) {
170 switch (C.DC) {
171 default:
172#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)\
173case PD_##NAME: C.PD.reset(CREATEFN(C.HTMLDir, C.PP, C.PPF)); break;
174#include "Analyses.def"
175 }
176 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000177 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000178 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000179
Ted Kremenek7032f462008-07-03 05:26:14 +0000180 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000181 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000182 CFG* c = getCFG();
183 if (!c) return 0;
184
185 liveness.reset(new LiveVariables(*c));
186 liveness->runOnCFG(*c);
187 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000188 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000189
Ted Kremenek7032f462008-07-03 05:26:14 +0000190 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000191 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000192
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000193 bool shouldVisualizeGraphviz() const {
194 return C.VisGraphviz;
195 }
196
197 bool shouldVisualizeUbigraph() const {
198 return C.VisUbigraph;
199 }
200
Ted Kremenek34d77342008-07-02 16:49:11 +0000201 bool shouldVisualize() const {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000202 return C.VisGraphviz || C.VisUbigraph;
Ted Kremenek34d77342008-07-02 16:49:11 +0000203 }
204
205 bool shouldTrimGraph() const {
206 return C.TrimGraph;
207 }
208
209 void DisplayFunction() {
210
211 if (DisplayedFunction)
212 return;
213
214 DisplayedFunction = true;
215
216 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
Ted Kremenek628a42e2008-09-04 00:02:50 +0000217 llvm::cout << "ANALYZE: "
Ted Kremenek34d77342008-07-02 16:49:11 +0000218 << getContext().getSourceManager().getSourceName(FD->getLocation())
219 << ' '
220 << FD->getIdentifier()->getName()
221 << '\n';
222 }
223 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
Ted Kremenek628a42e2008-09-04 00:02:50 +0000224 llvm::cout << "ANALYZE (ObjC Method): "
Ted Kremenek34d77342008-07-02 16:49:11 +0000225 << getContext().getSourceManager().getSourceName(MD->getLocation())
226 << " '"
227 << MD->getSelector().getName() << "'\n";
228 }
229 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000230 };
231
232} // end anonymous namespace
233
234namespace llvm {
235 template <> struct FoldingSetTrait<CodeAction> {
236 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
237 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
238 }
239 };
240}
241
242//===----------------------------------------------------------------------===//
243// AnalysisConsumer implementation.
244//===----------------------------------------------------------------------===//
245
246void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
247 switch (D->getKind()) {
248 case Decl::Function: {
249 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000250
251 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
252 break;
253
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000254 Stmt* Body = FD->getBody();
255 if (Body) HandleCode(FD, Body, FunctionActions);
256 break;
257 }
258
259 case Decl::ObjCMethod: {
260 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000261
262 if (FName.size() > 0 && FName != MD->getSelector().getName())
263 return;
264
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000265 Stmt* Body = MD->getBody();
266 if (Body) HandleCode(MD, Body, ObjCMethodActions);
267 break;
268 }
269
270 default:
271 break;
272 }
273}
274
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000275void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) {
276
277 if (ObjCImplementationActions.empty())
278 return;
279
280 for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) {
281
282 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
283 HandleCode(ID, 0, ObjCImplementationActions);
284 }
285}
286
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000287void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
288
289 // Don't run the actions if an error has occured with parsing the file.
290 if (Diags.hasErrorOccurred())
291 return;
292
293 SourceLocation Loc = D->getLocation();
294
295 // Only run actions on declarations defined in actual source.
296 if (!Loc.isFileID())
297 return;
298
299 // Don't run the actions on declarations in header files unless
300 // otherwise specified.
301 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
302 return;
303
304 // Create an AnalysisManager that will manage the state for analyzing
305 // this method/function.
306 AnalysisManager mgr(*this, D, Body);
307
308 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000309 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000310 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000311}
312
313//===----------------------------------------------------------------------===//
314// Analyses
315//===----------------------------------------------------------------------===//
316
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000317static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000318 if (LiveVariables* L = mgr.getLiveVariables()) {
319 BugReporter BR(mgr);
320 CheckDeadStores(*L, BR);
321 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000322}
323
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000324static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000325 if (CFG* c = mgr.getCFG())
326 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000327}
328
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000329
Ted Kremenek78d46242008-07-22 16:21:24 +0000330static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
331 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000332
Ted Kremenek7032f462008-07-03 05:26:14 +0000333
Ted Kremenekbc46f342008-07-02 16:35:50 +0000334 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000335
336 // Construct the analysis engine.
337 LiveVariables* L = mgr.getLiveVariables();
338 if (!L) return;
Ted Kremenekbc46f342008-07-02 16:35:50 +0000339
Ted Kremenek34d77342008-07-02 16:49:11 +0000340 // Display progress.
Ted Kremenek45479c82008-08-28 18:34:41 +0000341 mgr.DisplayFunction();
Ted Kremenek34d77342008-07-02 16:49:11 +0000342
Ted Kremenek95c7b002008-10-24 01:04:59 +0000343 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L,
344 mgr.getStoreManagerCreator());
345
Ted Kremenekbc46f342008-07-02 16:35:50 +0000346 Eng.setTransferFunctions(tf);
347
Ted Kremenek78d46242008-07-22 16:21:24 +0000348 if (StandardWarnings) {
349 Eng.RegisterInternalChecks();
350 RegisterAppleChecks(Eng);
351 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000352
353 // Set the graph auditor.
354 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
355 if (mgr.shouldVisualizeUbigraph()) {
356 Auditor.reset(CreateUbiViz());
357 ExplodedNodeImpl::SetAuditor(Auditor.get());
358 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000359
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000360 // Execute the worklist algorithm.
361 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000362
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000363 // Release the auditor (if any) so that it doesn't monitor the graph
364 // created BugReporter.
365 ExplodedNodeImpl::SetAuditor(0);
366
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000367 // Display warnings.
Ted Kremenekc0959972008-07-02 21:24:01 +0000368 Eng.EmitWarnings(mgr);
Ted Kremenek34d77342008-07-02 16:49:11 +0000369
370 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000371 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000372 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000373}
374
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000375static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000376 bool StandardWarnings) {
377
Ted Kremenekbc46f342008-07-02 16:35:50 +0000378 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
379 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000380 mgr.getLangOptions());
381
Ted Kremenek78d46242008-07-22 16:21:24 +0000382 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000383}
384
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000385static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000386
387 switch (mgr.getLangOptions().getGCMode()) {
388 default:
389 assert (false && "Invalid GC mode.");
390 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000391 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000392 break;
393
394 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000395 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000396 break;
397
398 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000399 ActionCheckerCFRefAux(mgr, false, true);
400 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000401 break;
402 }
403}
404
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000405static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000406 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
407}
408
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000409static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000410 if (LiveVariables* L = mgr.getLiveVariables()) {
411 mgr.DisplayFunction();
412 L->dumpBlockLiveness(mgr.getSourceManager());
413 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000414}
415
Ted Kremenek902141f2008-07-02 18:23:21 +0000416static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000417 if (CFG* c = mgr.getCFG()) {
418 mgr.DisplayFunction();
419 c->dump();
420 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000421}
422
423static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000424 if (CFG* c = mgr.getCFG()) {
425 mgr.DisplayFunction();
426 c->viewCFG();
427 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000428}
429
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000430static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000431 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
432 return;
433
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000434 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000435
436 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
437 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000438}
439
Ted Kremenek395aaf22008-07-23 00:45:26 +0000440static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
441 BugReporter BR(mgr);
442 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
443}
444
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000445static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000446 BugReporter BR(mgr);
447
448 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
449 BR);
450}
451
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000452//===----------------------------------------------------------------------===//
453// AnalysisConsumer creation.
454//===----------------------------------------------------------------------===//
455
456ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000457 AnalysisStores SM,
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000458 AnalysisDiagClients DC,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000459 Diagnostic &diags, Preprocessor* pp,
460 PreprocessorFactory* ppf,
461 const LangOptions& lopts,
462 const std::string& fname,
463 const std::string& htmldir,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000464 bool VisGraphviz, bool VisUbi,
465 bool trim,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000466 bool analyzeAll) {
467
468 llvm::OwningPtr<AnalysisConsumer>
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000469 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM, DC,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000470 VisGraphviz, VisUbi, trim, analyzeAll));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000471
472 for ( ; Beg != End ; ++Beg)
473 switch (*Beg) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000474#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000475 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000476 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000477 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000478#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000479 default: break;
480 }
481
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000482 return C.take();
483}
484
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000485//===----------------------------------------------------------------------===//
486// Ubigraph Visualization. FIXME: Move to separate file.
487//===----------------------------------------------------------------------===//
488
489namespace {
490
491class UbigraphViz : public ExplodedNodeImpl::Auditor {
492 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000493 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000494 unsigned Cntr;
495
496 typedef llvm::DenseMap<void*,unsigned> VMap;
497 VMap M;
498
499public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000500 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000501 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000502
503 ~UbigraphViz();
504
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000505 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
506};
507
508} // end anonymous namespace
509
510static ExplodedNodeImpl::Auditor* CreateUbiViz() {
511 std::string ErrMsg;
512
Ted Kremenek710ad932008-08-28 03:54:51 +0000513 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000514 if (!ErrMsg.empty())
515 return 0;
516
Ted Kremenek710ad932008-08-28 03:54:51 +0000517 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000518 Filename.appendComponent("llvm_ubi");
519 Filename.makeUnique(true,&ErrMsg);
520
521 if (!ErrMsg.empty())
522 return 0;
523
524 llvm::cerr << "Writing '" << Filename << "'.\n";
525
526 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
527 std::string filename = Filename.toString();
528 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), ErrMsg));
529
530 if (!ErrMsg.empty())
531 return 0;
532
Ted Kremenek710ad932008-08-28 03:54:51 +0000533 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000534}
535
536void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000537
538 assert (Src != Dst && "Self-edges are not allowed.");
539
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000540 // Lookup the Src. If it is a new node, it's a root.
541 VMap::iterator SrcI= M.find(Src);
542 unsigned SrcID;
543
544 if (SrcI == M.end()) {
545 M[Src] = SrcID = Cntr++;
546 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
547 }
548 else
549 SrcID = SrcI->second;
550
551 // Lookup the Dst.
552 VMap::iterator DstI= M.find(Dst);
553 unsigned DstID;
554
555 if (DstI == M.end()) {
556 M[Dst] = DstID = Cntr++;
557 *Out << "('vertex', " << DstID << ")\n";
558 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000559 else {
560 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000561 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000562 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
563 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000564
565 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000566 *Out << "('edge', " << SrcID << ", " << DstID
567 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000568}
569
Ted Kremenek56b98712008-08-28 05:02:09 +0000570UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
571 llvm::sys::Path& filename)
572 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
573
574 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
575 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
576 " ('size', '1.5'))\n";
577}
578
Ted Kremenek710ad932008-08-28 03:54:51 +0000579UbigraphViz::~UbigraphViz() {
580 Out.reset(0);
581 llvm::cerr << "Running 'ubiviz' program... ";
582 std::string ErrMsg;
583 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
584 std::vector<const char*> args;
585 args.push_back(Ubiviz.c_str());
586 args.push_back(Filename.c_str());
587 args.push_back(0);
588
589 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
590 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
591 }
592
593 // Delete the directory.
594 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000595}