blob: aa78337ae00135cd364db0dd97f716784d8ee370 [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 Kremenekf4381fd2008-07-02 00:03:09 +000078
79 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
80 PreprocessorFactory* ppf,
81 const LangOptions& lopts,
82 const std::string& fname,
83 const std::string& htmldir,
Ted Kremenek95c7b002008-10-24 01:04:59 +000084 AnalysisStores sm,
Ted Kremenekf8ce6992008-08-27 22:31:43 +000085 bool visgraphviz, bool visubi, bool trim, bool analyzeAll)
86 : VisGraphviz(visgraphviz), VisUbigraph(visubi), TrimGraph(trim),
87 LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000088 Ctx(0), PP(pp), PPF(ppf),
89 HTMLDir(htmldir),
90 FName(fname),
Ted Kremenek95c7b002008-10-24 01:04:59 +000091 AnalyzeAll(analyzeAll), SM(sm) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +000092
93 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000094 FunctionActions.push_back(action);
95 ObjCMethodActions.push_back(action);
96 }
97
98 void addObjCImplementationAction(CodeAction action) {
99 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000100 }
101
102 virtual void Initialize(ASTContext &Context) {
103 Ctx = &Context;
104 }
105
106 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000107 virtual void HandleTranslationUnit(TranslationUnit &TU);
108
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000109 void HandleCode(Decl* D, Stmt* Body, Actions actions);
110 };
111
112
Ted Kremenekc0959972008-07-02 21:24:01 +0000113 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000114 Decl* D;
115 Stmt* Body;
116 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000117 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000118
119 llvm::OwningPtr<CFG> cfg;
120 llvm::OwningPtr<LiveVariables> liveness;
121 llvm::OwningPtr<ParentMap> PM;
122
123 public:
124 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b)
Ted Kremenek34d77342008-07-02 16:49:11 +0000125 : D(d), Body(b), C(c), DisplayedFunction(false) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000126
127
128 Decl* getCodeDecl() const { return D; }
129 Stmt* getBody() const { return Body; }
130
Ted Kremenek95c7b002008-10-24 01:04:59 +0000131 GRStateManager::StoreManagerCreator getStoreManagerCreator() {
132 switch (C.SM) {
133 default:
134#define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\
135case NAME##Model: return Create##NAME##Manager;
136#include "Analyses.def"
137 }
138 };
139
Ted Kremenek7032f462008-07-03 05:26:14 +0000140 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000141 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000142 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000143 }
144
Ted Kremenekc0959972008-07-02 21:24:01 +0000145 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000146 if (!PM)
147 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000148 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000149 }
150
Ted Kremenekc0959972008-07-02 21:24:01 +0000151 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000152 return *C.Ctx;
153 }
154
Ted Kremenekc0959972008-07-02 21:24:01 +0000155 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000156 return getContext().getSourceManager();
157 }
158
Ted Kremenekc0959972008-07-02 21:24:01 +0000159 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000160 return C.Diags;
161 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000162
163 const LangOptions& getLangOptions() const {
164 return C.LOpts;
165 }
166
Ted Kremenekc0959972008-07-02 21:24:01 +0000167 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000168 if (C.PD.get() == 0 && !C.HTMLDir.empty())
169 C.PD.reset(CreateHTMLDiagnosticClient(C.HTMLDir, C.PP, C.PPF));
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000170
Ted Kremeneke2075582008-07-02 23:16:33 +0000171 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000172 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000173
Ted Kremenek7032f462008-07-03 05:26:14 +0000174 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000175 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000176 CFG* c = getCFG();
177 if (!c) return 0;
178
179 liveness.reset(new LiveVariables(*c));
180 liveness->runOnCFG(*c);
181 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000182 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000183
Ted Kremenek7032f462008-07-03 05:26:14 +0000184 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000185 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000186
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000187 bool shouldVisualizeGraphviz() const {
188 return C.VisGraphviz;
189 }
190
191 bool shouldVisualizeUbigraph() const {
192 return C.VisUbigraph;
193 }
194
Ted Kremenek34d77342008-07-02 16:49:11 +0000195 bool shouldVisualize() const {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000196 return C.VisGraphviz || C.VisUbigraph;
Ted Kremenek34d77342008-07-02 16:49:11 +0000197 }
198
199 bool shouldTrimGraph() const {
200 return C.TrimGraph;
201 }
202
203 void DisplayFunction() {
204
205 if (DisplayedFunction)
206 return;
207
208 DisplayedFunction = true;
209
210 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
Ted Kremenek628a42e2008-09-04 00:02:50 +0000211 llvm::cout << "ANALYZE: "
Ted Kremenek34d77342008-07-02 16:49:11 +0000212 << getContext().getSourceManager().getSourceName(FD->getLocation())
213 << ' '
214 << FD->getIdentifier()->getName()
215 << '\n';
216 }
217 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
Ted Kremenek628a42e2008-09-04 00:02:50 +0000218 llvm::cout << "ANALYZE (ObjC Method): "
Ted Kremenek34d77342008-07-02 16:49:11 +0000219 << getContext().getSourceManager().getSourceName(MD->getLocation())
220 << " '"
221 << MD->getSelector().getName() << "'\n";
222 }
223 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000224 };
225
226} // end anonymous namespace
227
228namespace llvm {
229 template <> struct FoldingSetTrait<CodeAction> {
230 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
231 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
232 }
233 };
234}
235
236//===----------------------------------------------------------------------===//
237// AnalysisConsumer implementation.
238//===----------------------------------------------------------------------===//
239
240void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
241 switch (D->getKind()) {
242 case Decl::Function: {
243 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000244
245 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
246 break;
247
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000248 Stmt* Body = FD->getBody();
249 if (Body) HandleCode(FD, Body, FunctionActions);
250 break;
251 }
252
253 case Decl::ObjCMethod: {
254 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000255
256 if (FName.size() > 0 && FName != MD->getSelector().getName())
257 return;
258
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000259 Stmt* Body = MD->getBody();
260 if (Body) HandleCode(MD, Body, ObjCMethodActions);
261 break;
262 }
263
264 default:
265 break;
266 }
267}
268
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000269void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) {
270
271 if (ObjCImplementationActions.empty())
272 return;
273
274 for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) {
275
276 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
277 HandleCode(ID, 0, ObjCImplementationActions);
278 }
279}
280
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000281void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
282
283 // Don't run the actions if an error has occured with parsing the file.
284 if (Diags.hasErrorOccurred())
285 return;
286
287 SourceLocation Loc = D->getLocation();
288
289 // Only run actions on declarations defined in actual source.
290 if (!Loc.isFileID())
291 return;
292
293 // Don't run the actions on declarations in header files unless
294 // otherwise specified.
295 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
296 return;
297
298 // Create an AnalysisManager that will manage the state for analyzing
299 // this method/function.
300 AnalysisManager mgr(*this, D, Body);
301
302 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000303 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000304 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000305}
306
307//===----------------------------------------------------------------------===//
308// Analyses
309//===----------------------------------------------------------------------===//
310
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000311static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000312 if (LiveVariables* L = mgr.getLiveVariables()) {
313 BugReporter BR(mgr);
314 CheckDeadStores(*L, BR);
315 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000316}
317
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000318static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000319 if (CFG* c = mgr.getCFG())
320 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000321}
322
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000323
Ted Kremenek78d46242008-07-22 16:21:24 +0000324static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
325 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000326
Ted Kremenek7032f462008-07-03 05:26:14 +0000327
Ted Kremenekbc46f342008-07-02 16:35:50 +0000328 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000329
330 // Construct the analysis engine.
331 LiveVariables* L = mgr.getLiveVariables();
332 if (!L) return;
Ted Kremenekbc46f342008-07-02 16:35:50 +0000333
Ted Kremenek34d77342008-07-02 16:49:11 +0000334 // Display progress.
Ted Kremenek45479c82008-08-28 18:34:41 +0000335 mgr.DisplayFunction();
Ted Kremenek34d77342008-07-02 16:49:11 +0000336
Ted Kremenek95c7b002008-10-24 01:04:59 +0000337 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L,
338 mgr.getStoreManagerCreator());
339
Ted Kremenekbc46f342008-07-02 16:35:50 +0000340 Eng.setTransferFunctions(tf);
341
Ted Kremenek78d46242008-07-22 16:21:24 +0000342 if (StandardWarnings) {
343 Eng.RegisterInternalChecks();
344 RegisterAppleChecks(Eng);
345 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000346
347 // Set the graph auditor.
348 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
349 if (mgr.shouldVisualizeUbigraph()) {
350 Auditor.reset(CreateUbiViz());
351 ExplodedNodeImpl::SetAuditor(Auditor.get());
352 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000353
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000354 // Execute the worklist algorithm.
355 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000356
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000357 // Release the auditor (if any) so that it doesn't monitor the graph
358 // created BugReporter.
359 ExplodedNodeImpl::SetAuditor(0);
360
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000361 // Display warnings.
Ted Kremenekc0959972008-07-02 21:24:01 +0000362 Eng.EmitWarnings(mgr);
Ted Kremenek34d77342008-07-02 16:49:11 +0000363
364 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000365 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000366 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000367}
368
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000369static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000370 bool StandardWarnings) {
371
Ted Kremenekbc46f342008-07-02 16:35:50 +0000372 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
373 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000374 mgr.getLangOptions());
375
Ted Kremenek78d46242008-07-22 16:21:24 +0000376 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000377}
378
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000379static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000380
381 switch (mgr.getLangOptions().getGCMode()) {
382 default:
383 assert (false && "Invalid GC mode.");
384 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000385 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000386 break;
387
388 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000389 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000390 break;
391
392 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000393 ActionCheckerCFRefAux(mgr, false, true);
394 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000395 break;
396 }
397}
398
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000399static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000400 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
401}
402
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000403static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000404 if (LiveVariables* L = mgr.getLiveVariables()) {
405 mgr.DisplayFunction();
406 L->dumpBlockLiveness(mgr.getSourceManager());
407 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000408}
409
Ted Kremenek902141f2008-07-02 18:23:21 +0000410static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000411 if (CFG* c = mgr.getCFG()) {
412 mgr.DisplayFunction();
413 c->dump();
414 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000415}
416
417static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000418 if (CFG* c = mgr.getCFG()) {
419 mgr.DisplayFunction();
420 c->viewCFG();
421 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000422}
423
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000424static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000425 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
426 return;
427
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000428 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000429
430 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
431 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000432}
433
Ted Kremenek395aaf22008-07-23 00:45:26 +0000434static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
435 BugReporter BR(mgr);
436 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
437}
438
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000439static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000440 BugReporter BR(mgr);
441
442 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
443 BR);
444}
445
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000446//===----------------------------------------------------------------------===//
447// AnalysisConsumer creation.
448//===----------------------------------------------------------------------===//
449
450ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000451 AnalysisStores SM,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000452 Diagnostic &diags, Preprocessor* pp,
453 PreprocessorFactory* ppf,
454 const LangOptions& lopts,
455 const std::string& fname,
456 const std::string& htmldir,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000457 bool VisGraphviz, bool VisUbi,
458 bool trim,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000459 bool analyzeAll) {
460
461 llvm::OwningPtr<AnalysisConsumer>
Ted Kremenek95c7b002008-10-24 01:04:59 +0000462 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000463 VisGraphviz, VisUbi, trim, analyzeAll));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000464
465 for ( ; Beg != End ; ++Beg)
466 switch (*Beg) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000467#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000468 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000469 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000470 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000471#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000472 default: break;
473 }
474
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000475 return C.take();
476}
477
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000478//===----------------------------------------------------------------------===//
479// Ubigraph Visualization. FIXME: Move to separate file.
480//===----------------------------------------------------------------------===//
481
482namespace {
483
484class UbigraphViz : public ExplodedNodeImpl::Auditor {
485 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000486 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000487 unsigned Cntr;
488
489 typedef llvm::DenseMap<void*,unsigned> VMap;
490 VMap M;
491
492public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000493 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000494 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000495
496 ~UbigraphViz();
497
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000498 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
499};
500
501} // end anonymous namespace
502
503static ExplodedNodeImpl::Auditor* CreateUbiViz() {
504 std::string ErrMsg;
505
Ted Kremenek710ad932008-08-28 03:54:51 +0000506 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000507 if (!ErrMsg.empty())
508 return 0;
509
Ted Kremenek710ad932008-08-28 03:54:51 +0000510 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000511 Filename.appendComponent("llvm_ubi");
512 Filename.makeUnique(true,&ErrMsg);
513
514 if (!ErrMsg.empty())
515 return 0;
516
517 llvm::cerr << "Writing '" << Filename << "'.\n";
518
519 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
520 std::string filename = Filename.toString();
521 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), ErrMsg));
522
523 if (!ErrMsg.empty())
524 return 0;
525
Ted Kremenek710ad932008-08-28 03:54:51 +0000526 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000527}
528
529void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000530
531 assert (Src != Dst && "Self-edges are not allowed.");
532
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000533 // Lookup the Src. If it is a new node, it's a root.
534 VMap::iterator SrcI= M.find(Src);
535 unsigned SrcID;
536
537 if (SrcI == M.end()) {
538 M[Src] = SrcID = Cntr++;
539 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
540 }
541 else
542 SrcID = SrcI->second;
543
544 // Lookup the Dst.
545 VMap::iterator DstI= M.find(Dst);
546 unsigned DstID;
547
548 if (DstI == M.end()) {
549 M[Dst] = DstID = Cntr++;
550 *Out << "('vertex', " << DstID << ")\n";
551 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000552 else {
553 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000554 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000555 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
556 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000557
558 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000559 *Out << "('edge', " << SrcID << ", " << DstID
560 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000561}
562
Ted Kremenek56b98712008-08-28 05:02:09 +0000563UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
564 llvm::sys::Path& filename)
565 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
566
567 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
568 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
569 " ('size', '1.5'))\n";
570}
571
Ted Kremenek710ad932008-08-28 03:54:51 +0000572UbigraphViz::~UbigraphViz() {
573 Out.reset(0);
574 llvm::cerr << "Running 'ubiviz' program... ";
575 std::string ErrMsg;
576 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
577 std::vector<const char*> args;
578 args.push_back(Ubiviz.c_str());
579 args.push_back(Filename.c_str());
580 args.push_back(0);
581
582 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
583 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
584 }
585
586 // Delete the directory.
587 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000588}