blob: 366f3844f537f24cf936f7ebbe64367e8ff1839c [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"
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"
22#include "clang/AST/CFG.h"
23#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"
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"
Zhongxing Xuff944a82008-12-22 01:52:37 +000033#include "llvm/Support/CommandLine.h"
Ted Kremenek34d77342008-07-02 16:49:11 +000034#include "llvm/Support/Streams.h"
Ted Kremenekf8ce6992008-08-27 22:31:43 +000035#include "llvm/Support/raw_ostream.h"
36#include "llvm/System/Path.h"
Ted Kremenek710ad932008-08-28 03:54:51 +000037#include "llvm/System/Program.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000038
Ted Kremenekf4381fd2008-07-02 00:03:09 +000039using namespace clang;
40
Ted Kremenekf8ce6992008-08-27 22:31:43 +000041static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000042
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000043//===----------------------------------------------------------------------===//
44// Analyzer Options: available analyses.
45//===----------------------------------------------------------------------===//
46
47/// Analysis - Set of available source code analyses.
48enum Analyses {
49#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE) NAME,
50#include "Analyses.def"
51NumAnalyses
52};
53
54static llvm::cl::list<Analyses>
55AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
56llvm::cl::values(
57#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
58clEnumValN(NAME, CMDFLAG, DESC),
59#include "Analyses.def"
60clEnumValEnd));
61
62//===----------------------------------------------------------------------===//
63// Analyzer Options: store model.
64//===----------------------------------------------------------------------===//
65
66/// AnalysisStores - Set of available analysis store models.
67enum AnalysisStores {
68#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
69#include "Analyses.def"
70NumStores
71};
72
73static llvm::cl::opt<AnalysisStores>
74AnalysisStoreOpt("analyzer-store",
75 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
76 llvm::cl::init(BasicStoreModel),
77 llvm::cl::values(
78#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
79clEnumValN(NAME##Model, CMDFLAG, DESC),
80#include "Analyses.def"
81clEnumValEnd));
82
83//===----------------------------------------------------------------------===//
84// Analyzer Options: constraint engines.
85//===----------------------------------------------------------------------===//
86
87/// AnalysisConstraints - Set of available constraint models.
88enum AnalysisConstraints {
89#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
90#include "Analyses.def"
91NumConstraints
92};
93
94static llvm::cl::opt<AnalysisConstraints>
95AnalysisConstraintsOpt("analyzer-constraints",
96 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
Ted Kremenek9f4ecb32009-02-20 21:49:22 +000097 llvm::cl::init(RangeConstraintsModel),
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000098 llvm::cl::values(
99#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
100clEnumValN(NAME##Model, CMDFLAG, DESC),
101#include "Analyses.def"
102clEnumValEnd));
103
104//===----------------------------------------------------------------------===//
105// Analyzer Options: diagnostic clients.
106//===----------------------------------------------------------------------===//
107
108/// AnalysisDiagClients - Set of available diagnostic clients for rendering
109/// analysis results.
110enum AnalysisDiagClients {
111#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREAT) PD_##NAME,
112#include "Analyses.def"
113NUM_ANALYSIS_DIAG_CLIENTS
114};
115
116static llvm::cl::opt<AnalysisDiagClients>
117AnalysisDiagOpt("analyzer-output",
118 llvm::cl::desc("Source Code Analysis - Output Options"),
119 llvm::cl::init(PD_HTML),
120 llvm::cl::values(
121#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
122clEnumValN(PD_##NAME, CMDFLAG, DESC),
123#include "Analyses.def"
124clEnumValEnd));
125
126//===----------------------------------------------------------------------===//
127// Misc. fun options.
128//===----------------------------------------------------------------------===//
129
130static llvm::cl::opt<bool>
131VisualizeEGDot("analyzer-viz-egraph-graphviz",
132 llvm::cl::desc("Display exploded graph using GraphViz"));
133
134static llvm::cl::opt<bool>
135VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
136 llvm::cl::desc("Display exploded graph using Ubigraph"));
137
138static llvm::cl::opt<bool>
139AnalyzeAll("analyzer-opt-analyze-headers",
140 llvm::cl::desc("Force the static analyzer to analyze "
141 "functions defined in header files"));
142
143static llvm::cl::opt<bool>
144AnalyzerDisplayProgress("analyzer-display-progress",
145 llvm::cl::desc("Emit verbose output about the analyzer's progress."));
146
Zhongxing Xuff944a82008-12-22 01:52:37 +0000147static llvm::cl::opt<bool>
148PurgeDead("analyzer-purge-dead",
149 llvm::cl::init(true),
150 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
151 " processing a statement."));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000152
Ted Kremenek48af2a92009-02-25 22:32:02 +0000153static llvm::cl::opt<bool>
154EagerlyAssume("analyzer-eagerly-assume",
155 llvm::cl::init(false),
156 llvm::cl::desc("Eagerly assume the truth/falseness of some "
157 "symbolic constraints."));
158
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000159static llvm::cl::opt<std::string>
160AnalyzeSpecificFunction("analyze-function",
161 llvm::cl::desc("Run analysis on specific function"));
162
Ted Kremenek45021952009-02-14 17:08:39 +0000163static llvm::cl::opt<bool>
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000164TrimGraph("trim-egraph",
165 llvm::cl::desc("Only show error-related paths in the analysis graph"));
166
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000167//===----------------------------------------------------------------------===//
168// Basic type definitions.
169//===----------------------------------------------------------------------===//
170
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000171namespace {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000172 class AnalysisManager;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000173 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000174} // end anonymous namespace
175
176//===----------------------------------------------------------------------===//
177// AnalysisConsumer declaration.
178//===----------------------------------------------------------------------===//
179
180namespace {
181
182 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000183 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000184 Actions FunctionActions;
185 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000186 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +0000187 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000188
189 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +0000190 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000191 Diagnostic &Diags;
192 ASTContext* Ctx;
193 Preprocessor* PP;
194 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000195 const std::string OutDir;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000196 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000197
198 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
199 PreprocessorFactory* ppf,
200 const LangOptions& lopts,
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000201 const std::string& outdir)
202 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000203 Ctx(0), PP(pp), PPF(ppf),
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000204 OutDir(outdir) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000205
206 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000207 FunctionActions.push_back(action);
208 ObjCMethodActions.push_back(action);
209 }
210
211 void addObjCImplementationAction(CodeAction action) {
212 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000213 }
214
Ted Kremenekdaac6342008-11-07 02:09:25 +0000215 void addTranslationUnitAction(CodeAction action) {
216 TranslationUnitActions.push_back(action);
217 }
218
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000219 virtual void Initialize(ASTContext &Context) {
220 Ctx = &Context;
221 }
222
223 virtual void HandleTopLevelDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000224 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000225
Ted Kremenek81922f02009-02-02 20:52:40 +0000226 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000227 };
228
229
Ted Kremenekc0959972008-07-02 21:24:01 +0000230 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000231 Decl* D; Stmt* Body;
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000232
233 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
234
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000235 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000236 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000237
238 llvm::OwningPtr<CFG> cfg;
239 llvm::OwningPtr<LiveVariables> liveness;
240 llvm::OwningPtr<ParentMap> PM;
241
Zhongxing Xu22438a82008-11-27 01:55:08 +0000242 // Configurable components creators.
243 StoreManagerCreator CreateStoreMgr;
244 ConstraintManagerCreator CreateConstraintMgr;
245
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000246 public:
Ted Kremenek491918e2009-01-23 20:52:26 +0000247 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
Chris Lattner678dc3b2009-03-28 04:05:05 +0000248 : D(d), Body(b), AScope(ScopeDecl), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000249 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000250 setManagerCreators();
251 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000252
Chris Lattner678dc3b2009-03-28 04:05:05 +0000253 AnalysisManager(AnalysisConsumer& c, bool displayProgress)
254 : D(0), Body(0), AScope(ScopeTU), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000255 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000256 setManagerCreators();
257 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000258
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000259 Decl* getCodeDecl() const {
260 assert (AScope == ScopeDecl);
261 return D;
262 }
263
264 Stmt* getBody() const {
265 assert (AScope == ScopeDecl);
266 return Body;
267 }
268
Zhongxing Xu22438a82008-11-27 01:55:08 +0000269 StoreManagerCreator getStoreManagerCreator() {
270 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000271 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000272
273 ConstraintManagerCreator getConstraintManagerCreator() {
274 return CreateConstraintMgr;
275 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000276
Ted Kremenek7032f462008-07-03 05:26:14 +0000277 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000278 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000279 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000280 }
281
Ted Kremenekc0959972008-07-02 21:24:01 +0000282 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000283 if (!PM)
284 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000285 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000286 }
287
Ted Kremenekc0959972008-07-02 21:24:01 +0000288 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000289 return *C.Ctx;
290 }
291
Ted Kremenekc0959972008-07-02 21:24:01 +0000292 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000293 return getContext().getSourceManager();
294 }
295
Ted Kremenekc0959972008-07-02 21:24:01 +0000296 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000297 return C.Diags;
298 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000299
300 const LangOptions& getLangOptions() const {
301 return C.LOpts;
302 }
303
Ted Kremenekc0959972008-07-02 21:24:01 +0000304 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000305 if (C.PD.get() == 0 && !C.OutDir.empty()) {
306 switch (AnalysisDiagOpt) {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000307 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000308#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000309case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000310#include "Analyses.def"
311 }
312 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000313 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000314 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000315
Ted Kremenek7032f462008-07-03 05:26:14 +0000316 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000317 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000318 CFG* c = getCFG();
319 if (!c) return 0;
320
Ted Kremenekca9bab02008-12-09 00:17:51 +0000321 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000322 liveness->runOnCFG(*c);
323 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000324 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000325
Ted Kremenek7032f462008-07-03 05:26:14 +0000326 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000327 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000328
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000329 bool shouldVisualizeGraphviz() const { return VisualizeEGDot; }
330
331 bool shouldVisualizeUbigraph() const { return VisualizeEGUbi; }
332
333 bool shouldVisualize() const {
334 return VisualizeEGDot || VisualizeEGUbi;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000335 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000336
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000337 bool shouldTrimGraph() const { return TrimGraph; }
338
Ted Kremenek34d77342008-07-02 16:49:11 +0000339 void DisplayFunction() {
340
341 if (DisplayedFunction)
342 return;
343
344 DisplayedFunction = true;
345
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000346 // FIXME: Is getCodeDecl() always a named decl?
347 if (isa<FunctionDecl>(getCodeDecl()) ||
348 isa<ObjCMethodDecl>(getCodeDecl())) {
349 NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
350 SourceManager &SM = getContext().getSourceManager();
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000351 llvm::cerr << "ANALYZE: "
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000352 << SM.getPresumedLoc(ND->getLocation()).getFilename()
353 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenek34d77342008-07-02 16:49:11 +0000354 }
355 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000356
357 private:
358 /// Set configurable analyzer components creators. First check if there are
359 /// components registered at runtime. Otherwise fall back to builtin
360 /// components.
361 void setManagerCreators() {
362 if (ManagerRegistry::StoreMgrCreator != 0) {
363 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
364 }
365 else {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000366 switch (AnalysisStoreOpt) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000367 default:
368 assert(0 && "Unknown store manager.");
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000369#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
370 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Zhongxing Xu22438a82008-11-27 01:55:08 +0000371#include "Analyses.def"
372 }
373 }
374
375 if (ManagerRegistry::ConstraintMgrCreator != 0)
376 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000377 else {
378 switch (AnalysisConstraintsOpt) {
379 default:
380 assert(0 && "Unknown store manager.");
381#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
382 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
383#include "Analyses.def"
384 }
385 }
386
Ted Kremenekc472d792009-01-23 20:06:20 +0000387
388 // Some DiagnosticClients should be created all the time instead of
389 // lazily. Create those now.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000390 switch (AnalysisDiagOpt) {
Ted Kremenekc472d792009-01-23 20:06:20 +0000391 default: break;
392#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
393case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
394#include "Analyses.def"
395 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000396 }
397
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000398 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000399
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000400} // end anonymous namespace
401
402namespace llvm {
403 template <> struct FoldingSetTrait<CodeAction> {
404 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
405 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
406 }
407 };
408}
409
410//===----------------------------------------------------------------------===//
411// AnalysisConsumer implementation.
412//===----------------------------------------------------------------------===//
413
414void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
415 switch (D->getKind()) {
416 case Decl::Function: {
417 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000418
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000419 if (AnalyzeSpecificFunction.size() > 0 &&
420 AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000421 break;
422
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000423 Stmt* Body = FD->getBody();
424 if (Body) HandleCode(FD, Body, FunctionActions);
425 break;
426 }
427
428 case Decl::ObjCMethod: {
429 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000430
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000431 if (AnalyzeSpecificFunction.size() > 0 &&
432 AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000433 return;
434
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000435 Stmt* Body = MD->getBody();
436 if (Body) HandleCode(MD, Body, ObjCMethodActions);
437 break;
438 }
439
440 default:
441 break;
442 }
443}
444
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000445void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000446
Ted Kremenekdaac6342008-11-07 02:09:25 +0000447 if(!TranslationUnitActions.empty()) {
Chris Lattner678dc3b2009-03-28 04:05:05 +0000448 AnalysisManager mgr(*this, AnalyzerDisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000449 for (Actions::iterator I = TranslationUnitActions.begin(),
450 E = TranslationUnitActions.end(); I != E; ++I)
451 (*I)(mgr);
452 }
453
Chris Lattnere9077872009-03-28 03:29:40 +0000454 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000455 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000456
457 for (DeclContext::decl_iterator I = TUD->decls_begin(),E = TUD->decls_end();
458 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000459 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
460 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000461 }
Ted Kremenek4d53a532009-02-13 00:51:30 +0000462
463 // Delete the PathDiagnosticClient here just in case the AnalysisConsumer
464 // object doesn't get released. This will cause any side-effects in the
465 // destructor of the PathDiagnosticClient to get executed.
466 PD.reset();
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000467}
468
Ted Kremenek81922f02009-02-02 20:52:40 +0000469void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000470
471 // Don't run the actions if an error has occured with parsing the file.
472 if (Diags.hasErrorOccurred())
473 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000474
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000475 // Don't run the actions on declarations in header files unless
476 // otherwise specified.
Ted Kremenek81922f02009-02-02 20:52:40 +0000477 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000478 return;
479
480 // Create an AnalysisManager that will manage the state for analyzing
481 // this method/function.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000482 AnalysisManager mgr(*this, D, Body, AnalyzerDisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000483
484 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000485 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000486 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000487}
488
489//===----------------------------------------------------------------------===//
490// Analyses
491//===----------------------------------------------------------------------===//
492
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000493static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000494 if (LiveVariables* L = mgr.getLiveVariables()) {
495 BugReporter BR(mgr);
496 CheckDeadStores(*L, BR);
497 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000498}
499
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000500static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000501 if (CFG* c = mgr.getCFG())
502 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000503}
504
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000505
Ted Kremenek78d46242008-07-22 16:21:24 +0000506static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
507 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000508
Ted Kremenek7032f462008-07-03 05:26:14 +0000509
Ted Kremenekbc46f342008-07-02 16:35:50 +0000510 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000511
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000512 // Display progress.
513 mgr.DisplayFunction();
514
Ted Kremenek7032f462008-07-03 05:26:14 +0000515 // Construct the analysis engine.
516 LiveVariables* L = mgr.getLiveVariables();
517 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000518
Ted Kremenekcf118d42009-02-04 23:49:09 +0000519 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Ted Kremenek48af2a92009-02-25 22:32:02 +0000520 PurgeDead, EagerlyAssume,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000521 mgr.getStoreManagerCreator(),
522 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000523
Ted Kremenekbc46f342008-07-02 16:35:50 +0000524 Eng.setTransferFunctions(tf);
525
Ted Kremenek78d46242008-07-22 16:21:24 +0000526 if (StandardWarnings) {
527 Eng.RegisterInternalChecks();
528 RegisterAppleChecks(Eng);
529 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000530
531 // Set the graph auditor.
532 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
533 if (mgr.shouldVisualizeUbigraph()) {
534 Auditor.reset(CreateUbiViz());
535 ExplodedNodeImpl::SetAuditor(Auditor.get());
536 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000537
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000538 // Execute the worklist algorithm.
539 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000540
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000541 // Release the auditor (if any) so that it doesn't monitor the graph
542 // created BugReporter.
543 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000544
Ted Kremenek34d77342008-07-02 16:49:11 +0000545 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000546 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000547 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000548
549 // Display warnings.
550 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000551}
552
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000553static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000554 bool StandardWarnings) {
555
Ted Kremenekbc46f342008-07-02 16:35:50 +0000556 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
557 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000558 mgr.getLangOptions());
559
Ted Kremenek78d46242008-07-22 16:21:24 +0000560 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000561}
562
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000563static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000564
565 switch (mgr.getLangOptions().getGCMode()) {
566 default:
567 assert (false && "Invalid GC mode.");
568 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000569 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000570 break;
571
572 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000573 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000574 break;
575
576 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000577 ActionCheckerCFRefAux(mgr, false, true);
578 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000579 break;
580 }
581}
582
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000583static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000584 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
585}
586
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000587static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000588 if (LiveVariables* L = mgr.getLiveVariables()) {
589 mgr.DisplayFunction();
590 L->dumpBlockLiveness(mgr.getSourceManager());
591 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000592}
593
Ted Kremenek902141f2008-07-02 18:23:21 +0000594static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000595 if (CFG* c = mgr.getCFG()) {
596 mgr.DisplayFunction();
597 c->dump();
598 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000599}
600
601static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000602 if (CFG* c = mgr.getCFG()) {
603 mgr.DisplayFunction();
604 c->viewCFG();
605 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000606}
607
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000608static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000609 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
610 return;
611
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000612 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000613
614 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
615 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000616}
617
Ted Kremenek395aaf22008-07-23 00:45:26 +0000618static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
619 BugReporter BR(mgr);
620 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
621}
622
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000623static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000624 BugReporter BR(mgr);
625
626 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
627 BR);
628}
629
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000630//===----------------------------------------------------------------------===//
631// AnalysisConsumer creation.
632//===----------------------------------------------------------------------===//
633
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000634ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000635 PreprocessorFactory* ppf,
636 const LangOptions& lopts,
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000637 const std::string& OutDir) {
638
639 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
640 lopts, OutDir));
641
642 for (unsigned i = 0; i < AnalysisList.size(); ++i)
643 switch (AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000644#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000645 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000646 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000647 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000648#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000649 default: break;
650 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000651
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000652 return C.take();
653}
654
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000655//===----------------------------------------------------------------------===//
656// Ubigraph Visualization. FIXME: Move to separate file.
657//===----------------------------------------------------------------------===//
658
659namespace {
660
661class UbigraphViz : public ExplodedNodeImpl::Auditor {
662 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000663 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000664 unsigned Cntr;
665
666 typedef llvm::DenseMap<void*,unsigned> VMap;
667 VMap M;
668
669public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000670 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000671 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000672
673 ~UbigraphViz();
674
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000675 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
676};
677
678} // end anonymous namespace
679
680static ExplodedNodeImpl::Auditor* CreateUbiViz() {
681 std::string ErrMsg;
682
Ted Kremenek710ad932008-08-28 03:54:51 +0000683 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000684 if (!ErrMsg.empty())
685 return 0;
686
Ted Kremenek710ad932008-08-28 03:54:51 +0000687 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000688 Filename.appendComponent("llvm_ubi");
689 Filename.makeUnique(true,&ErrMsg);
690
691 if (!ErrMsg.empty())
692 return 0;
693
694 llvm::cerr << "Writing '" << Filename << "'.\n";
695
696 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
697 std::string filename = Filename.toString();
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000698 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000699
700 if (!ErrMsg.empty())
701 return 0;
702
Ted Kremenek710ad932008-08-28 03:54:51 +0000703 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000704}
705
706void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000707
708 assert (Src != Dst && "Self-edges are not allowed.");
709
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000710 // Lookup the Src. If it is a new node, it's a root.
711 VMap::iterator SrcI= M.find(Src);
712 unsigned SrcID;
713
714 if (SrcI == M.end()) {
715 M[Src] = SrcID = Cntr++;
716 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
717 }
718 else
719 SrcID = SrcI->second;
720
721 // Lookup the Dst.
722 VMap::iterator DstI= M.find(Dst);
723 unsigned DstID;
724
725 if (DstI == M.end()) {
726 M[Dst] = DstID = Cntr++;
727 *Out << "('vertex', " << DstID << ")\n";
728 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000729 else {
730 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000731 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000732 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
733 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000734
735 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000736 *Out << "('edge', " << SrcID << ", " << DstID
737 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000738}
739
Ted Kremenek56b98712008-08-28 05:02:09 +0000740UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
741 llvm::sys::Path& filename)
742 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
743
744 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
745 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
746 " ('size', '1.5'))\n";
747}
748
Ted Kremenek710ad932008-08-28 03:54:51 +0000749UbigraphViz::~UbigraphViz() {
750 Out.reset(0);
751 llvm::cerr << "Running 'ubiviz' program... ";
752 std::string ErrMsg;
753 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
754 std::vector<const char*> args;
755 args.push_back(Ubiviz.c_str());
756 args.push_back(Filename.c_str());
757 args.push_back(0);
758
759 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
760 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
761 }
762
763 // Delete the directory.
764 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000765}