blob: fd74f7dcd1790dcdd7e87f3ea1ac5a6ded2ab612 [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"
Zhongxing Xu22438a82008-11-27 01:55:08 +000016#include "clang/Driver/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 Kremenekdb09a4d2008-07-03 04:29:21 +000028#include "clang/AST/TranslationUnit.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#include <vector>
40
Ted Kremenekf4381fd2008-07-02 00:03:09 +000041using namespace clang;
42
Ted Kremenekf8ce6992008-08-27 22:31:43 +000043static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000044
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000045//===----------------------------------------------------------------------===//
46// Analyzer Options: available analyses.
47//===----------------------------------------------------------------------===//
48
49/// Analysis - Set of available source code analyses.
50enum Analyses {
51#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE) NAME,
52#include "Analyses.def"
53NumAnalyses
54};
55
56static llvm::cl::list<Analyses>
57AnalysisList(llvm::cl::desc("Source Code Analysis - Checks and Analyses"),
58llvm::cl::values(
59#define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\
60clEnumValN(NAME, CMDFLAG, DESC),
61#include "Analyses.def"
62clEnumValEnd));
63
64//===----------------------------------------------------------------------===//
65// Analyzer Options: store model.
66//===----------------------------------------------------------------------===//
67
68/// AnalysisStores - Set of available analysis store models.
69enum AnalysisStores {
70#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
71#include "Analyses.def"
72NumStores
73};
74
75static llvm::cl::opt<AnalysisStores>
76AnalysisStoreOpt("analyzer-store",
77 llvm::cl::desc("Source Code Analysis - Abstract Memory Store Models"),
78 llvm::cl::init(BasicStoreModel),
79 llvm::cl::values(
80#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATFN)\
81clEnumValN(NAME##Model, CMDFLAG, DESC),
82#include "Analyses.def"
83clEnumValEnd));
84
85//===----------------------------------------------------------------------===//
86// Analyzer Options: constraint engines.
87//===----------------------------------------------------------------------===//
88
89/// AnalysisConstraints - Set of available constraint models.
90enum AnalysisConstraints {
91#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN) NAME##Model,
92#include "Analyses.def"
93NumConstraints
94};
95
96static llvm::cl::opt<AnalysisConstraints>
97AnalysisConstraintsOpt("analyzer-constraints",
98 llvm::cl::desc("Source Code Analysis - Symbolic Constraint Engines"),
Ted Kremenek9f4ecb32009-02-20 21:49:22 +000099 llvm::cl::init(RangeConstraintsModel),
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000100 llvm::cl::values(
101#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATFN)\
102clEnumValN(NAME##Model, CMDFLAG, DESC),
103#include "Analyses.def"
104clEnumValEnd));
105
106//===----------------------------------------------------------------------===//
107// Analyzer Options: diagnostic clients.
108//===----------------------------------------------------------------------===//
109
110/// AnalysisDiagClients - Set of available diagnostic clients for rendering
111/// analysis results.
112enum AnalysisDiagClients {
113#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREAT) PD_##NAME,
114#include "Analyses.def"
115NUM_ANALYSIS_DIAG_CLIENTS
116};
117
118static llvm::cl::opt<AnalysisDiagClients>
119AnalysisDiagOpt("analyzer-output",
120 llvm::cl::desc("Source Code Analysis - Output Options"),
121 llvm::cl::init(PD_HTML),
122 llvm::cl::values(
123#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\
124clEnumValN(PD_##NAME, CMDFLAG, DESC),
125#include "Analyses.def"
126clEnumValEnd));
127
128//===----------------------------------------------------------------------===//
129// Misc. fun options.
130//===----------------------------------------------------------------------===//
131
132static llvm::cl::opt<bool>
133VisualizeEGDot("analyzer-viz-egraph-graphviz",
134 llvm::cl::desc("Display exploded graph using GraphViz"));
135
136static llvm::cl::opt<bool>
137VisualizeEGUbi("analyzer-viz-egraph-ubigraph",
138 llvm::cl::desc("Display exploded graph using Ubigraph"));
139
140static llvm::cl::opt<bool>
141AnalyzeAll("analyzer-opt-analyze-headers",
142 llvm::cl::desc("Force the static analyzer to analyze "
143 "functions defined in header files"));
144
145static llvm::cl::opt<bool>
146AnalyzerDisplayProgress("analyzer-display-progress",
147 llvm::cl::desc("Emit verbose output about the analyzer's progress."));
148
Zhongxing Xuff944a82008-12-22 01:52:37 +0000149static llvm::cl::opt<bool>
150PurgeDead("analyzer-purge-dead",
151 llvm::cl::init(true),
152 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
153 " processing a statement."));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000154
155static llvm::cl::opt<std::string>
156AnalyzeSpecificFunction("analyze-function",
157 llvm::cl::desc("Run analysis on specific function"));
158
Ted Kremenek45021952009-02-14 17:08:39 +0000159static llvm::cl::opt<bool>
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000160TrimGraph("trim-egraph",
161 llvm::cl::desc("Only show error-related paths in the analysis graph"));
162
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000163//===----------------------------------------------------------------------===//
164// Basic type definitions.
165//===----------------------------------------------------------------------===//
166
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000167namespace {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000168 class AnalysisManager;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000169 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000170} // end anonymous namespace
171
172//===----------------------------------------------------------------------===//
173// AnalysisConsumer declaration.
174//===----------------------------------------------------------------------===//
175
176namespace {
177
178 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000179 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000180 Actions FunctionActions;
181 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000182 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +0000183 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000184
185 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +0000186 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000187 Diagnostic &Diags;
188 ASTContext* Ctx;
189 Preprocessor* PP;
190 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000191 const std::string OutDir;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000192 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000193
194 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
195 PreprocessorFactory* ppf,
196 const LangOptions& lopts,
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000197 const std::string& outdir)
198 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000199 Ctx(0), PP(pp), PPF(ppf),
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000200 OutDir(outdir) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000201
202 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000203 FunctionActions.push_back(action);
204 ObjCMethodActions.push_back(action);
205 }
206
207 void addObjCImplementationAction(CodeAction action) {
208 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000209 }
210
Ted Kremenekdaac6342008-11-07 02:09:25 +0000211 void addTranslationUnitAction(CodeAction action) {
212 TranslationUnitActions.push_back(action);
213 }
214
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000215 virtual void Initialize(ASTContext &Context) {
216 Ctx = &Context;
217 }
218
219 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000220 virtual void HandleTranslationUnit(TranslationUnit &TU);
221
Ted Kremenek81922f02009-02-02 20:52:40 +0000222 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000223 };
224
225
Ted Kremenekc0959972008-07-02 21:24:01 +0000226 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000227 Decl* D; Stmt* Body;
228 TranslationUnit* TU;
229
230 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
231
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000232 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000233 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000234
235 llvm::OwningPtr<CFG> cfg;
236 llvm::OwningPtr<LiveVariables> liveness;
237 llvm::OwningPtr<ParentMap> PM;
238
Zhongxing Xu22438a82008-11-27 01:55:08 +0000239 // Configurable components creators.
240 StoreManagerCreator CreateStoreMgr;
241 ConstraintManagerCreator CreateConstraintMgr;
242
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000243 public:
Ted Kremenek491918e2009-01-23 20:52:26 +0000244 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
245 : D(d), Body(b), TU(0), AScope(ScopeDecl), C(c),
246 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000247 setManagerCreators();
248 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000249
Ted Kremenek491918e2009-01-23 20:52:26 +0000250 AnalysisManager(AnalysisConsumer& c, TranslationUnit* tu,
251 bool displayProgress)
252 : D(0), Body(0), TU(tu), AScope(ScopeTU), C(c),
253 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000254 setManagerCreators();
255 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000256
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000257 Decl* getCodeDecl() const {
258 assert (AScope == ScopeDecl);
259 return D;
260 }
261
262 Stmt* getBody() const {
263 assert (AScope == ScopeDecl);
264 return Body;
265 }
266
267 TranslationUnit* getTranslationUnit() const {
268 assert (AScope == ScopeTU);
269 return TU;
270 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000271
Zhongxing Xu22438a82008-11-27 01:55:08 +0000272 StoreManagerCreator getStoreManagerCreator() {
273 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000274 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000275
276 ConstraintManagerCreator getConstraintManagerCreator() {
277 return CreateConstraintMgr;
278 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000279
Ted Kremenek7032f462008-07-03 05:26:14 +0000280 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000281 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000282 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000283 }
284
Ted Kremenekc0959972008-07-02 21:24:01 +0000285 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000286 if (!PM)
287 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000288 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000289 }
290
Ted Kremenekc0959972008-07-02 21:24:01 +0000291 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000292 return *C.Ctx;
293 }
294
Ted Kremenekc0959972008-07-02 21:24:01 +0000295 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000296 return getContext().getSourceManager();
297 }
298
Ted Kremenekc0959972008-07-02 21:24:01 +0000299 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000300 return C.Diags;
301 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000302
303 const LangOptions& getLangOptions() const {
304 return C.LOpts;
305 }
306
Ted Kremenekc0959972008-07-02 21:24:01 +0000307 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000308 if (C.PD.get() == 0 && !C.OutDir.empty()) {
309 switch (AnalysisDiagOpt) {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000310 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000311#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000312case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000313#include "Analyses.def"
314 }
315 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000316 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000317 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000318
Ted Kremenek7032f462008-07-03 05:26:14 +0000319 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000320 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000321 CFG* c = getCFG();
322 if (!c) return 0;
323
Ted Kremenekca9bab02008-12-09 00:17:51 +0000324 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000325 liveness->runOnCFG(*c);
326 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000327 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000328
Ted Kremenek7032f462008-07-03 05:26:14 +0000329 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000330 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000331
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000332 bool shouldVisualizeGraphviz() const { return VisualizeEGDot; }
333
334 bool shouldVisualizeUbigraph() const { return VisualizeEGUbi; }
335
336 bool shouldVisualize() const {
337 return VisualizeEGDot || VisualizeEGUbi;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000338 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000339
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000340 bool shouldTrimGraph() const { return TrimGraph; }
341
Ted Kremenek34d77342008-07-02 16:49:11 +0000342 void DisplayFunction() {
343
344 if (DisplayedFunction)
345 return;
346
347 DisplayedFunction = true;
348
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000349 // FIXME: Is getCodeDecl() always a named decl?
350 if (isa<FunctionDecl>(getCodeDecl()) ||
351 isa<ObjCMethodDecl>(getCodeDecl())) {
352 NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
353 SourceManager &SM = getContext().getSourceManager();
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000354 llvm::cerr << "ANALYZE: "
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000355 << SM.getPresumedLoc(ND->getLocation()).getFilename()
356 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenek34d77342008-07-02 16:49:11 +0000357 }
358 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000359
360 private:
361 /// Set configurable analyzer components creators. First check if there are
362 /// components registered at runtime. Otherwise fall back to builtin
363 /// components.
364 void setManagerCreators() {
365 if (ManagerRegistry::StoreMgrCreator != 0) {
366 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
367 }
368 else {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000369 switch (AnalysisStoreOpt) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000370 default:
371 assert(0 && "Unknown store manager.");
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000372#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
373 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Zhongxing Xu22438a82008-11-27 01:55:08 +0000374#include "Analyses.def"
375 }
376 }
377
378 if (ManagerRegistry::ConstraintMgrCreator != 0)
379 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000380 else {
381 switch (AnalysisConstraintsOpt) {
382 default:
383 assert(0 && "Unknown store manager.");
384#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
385 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
386#include "Analyses.def"
387 }
388 }
389
Ted Kremenekc472d792009-01-23 20:06:20 +0000390
391 // Some DiagnosticClients should be created all the time instead of
392 // lazily. Create those now.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000393 switch (AnalysisDiagOpt) {
Ted Kremenekc472d792009-01-23 20:06:20 +0000394 default: break;
395#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
396case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
397#include "Analyses.def"
398 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000399 }
400
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000401 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000402
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000403} // end anonymous namespace
404
405namespace llvm {
406 template <> struct FoldingSetTrait<CodeAction> {
407 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
408 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
409 }
410 };
411}
412
413//===----------------------------------------------------------------------===//
414// AnalysisConsumer implementation.
415//===----------------------------------------------------------------------===//
416
417void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
418 switch (D->getKind()) {
419 case Decl::Function: {
420 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000421
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000422 if (AnalyzeSpecificFunction.size() > 0 &&
423 AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000424 break;
425
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000426 Stmt* Body = FD->getBody();
427 if (Body) HandleCode(FD, Body, FunctionActions);
428 break;
429 }
430
431 case Decl::ObjCMethod: {
432 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000433
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000434 if (AnalyzeSpecificFunction.size() > 0 &&
435 AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000436 return;
437
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000438 Stmt* Body = MD->getBody();
439 if (Body) HandleCode(MD, Body, ObjCMethodActions);
440 break;
441 }
442
443 default:
444 break;
445 }
446}
447
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000448void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) {
449
Ted Kremenekdaac6342008-11-07 02:09:25 +0000450 if(!TranslationUnitActions.empty()) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000451 AnalysisManager mgr(*this, &TU, AnalyzerDisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000452 for (Actions::iterator I = TranslationUnitActions.begin(),
453 E = TranslationUnitActions.end(); I != E; ++I)
454 (*I)(mgr);
455 }
456
Ted Kremenek4d53a532009-02-13 00:51:30 +0000457 if (!ObjCImplementationActions.empty())
458 for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I)
459 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
460 HandleCode(ID, 0, ObjCImplementationActions);
461
462 // Delete the PathDiagnosticClient here just in case the AnalysisConsumer
463 // object doesn't get released. This will cause any side-effects in the
464 // destructor of the PathDiagnosticClient to get executed.
465 PD.reset();
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000466}
467
Ted Kremenek81922f02009-02-02 20:52:40 +0000468void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000469
470 // Don't run the actions if an error has occured with parsing the file.
471 if (Diags.hasErrorOccurred())
472 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000473
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000474 // Don't run the actions on declarations in header files unless
475 // otherwise specified.
Ted Kremenek81922f02009-02-02 20:52:40 +0000476 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000477 return;
478
479 // Create an AnalysisManager that will manage the state for analyzing
480 // this method/function.
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000481 AnalysisManager mgr(*this, D, Body, AnalyzerDisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000482
483 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000484 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000485 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000486}
487
488//===----------------------------------------------------------------------===//
489// Analyses
490//===----------------------------------------------------------------------===//
491
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000492static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000493 if (LiveVariables* L = mgr.getLiveVariables()) {
494 BugReporter BR(mgr);
495 CheckDeadStores(*L, BR);
496 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000497}
498
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000499static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000500 if (CFG* c = mgr.getCFG())
501 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000502}
503
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000504
Ted Kremenek78d46242008-07-22 16:21:24 +0000505static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
506 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000507
Ted Kremenek7032f462008-07-03 05:26:14 +0000508
Ted Kremenekbc46f342008-07-02 16:35:50 +0000509 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000510
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000511 // Display progress.
512 mgr.DisplayFunction();
513
Ted Kremenek7032f462008-07-03 05:26:14 +0000514 // Construct the analysis engine.
515 LiveVariables* L = mgr.getLiveVariables();
516 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000517
Ted Kremenekcf118d42009-02-04 23:49:09 +0000518 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Zhongxing Xuff944a82008-12-22 01:52:37 +0000519 PurgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000520 mgr.getStoreManagerCreator(),
521 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000522
Ted Kremenekbc46f342008-07-02 16:35:50 +0000523 Eng.setTransferFunctions(tf);
524
Ted Kremenek78d46242008-07-22 16:21:24 +0000525 if (StandardWarnings) {
526 Eng.RegisterInternalChecks();
527 RegisterAppleChecks(Eng);
528 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000529
530 // Set the graph auditor.
531 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
532 if (mgr.shouldVisualizeUbigraph()) {
533 Auditor.reset(CreateUbiViz());
534 ExplodedNodeImpl::SetAuditor(Auditor.get());
535 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000536
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000537 // Execute the worklist algorithm.
538 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000539
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000540 // Release the auditor (if any) so that it doesn't monitor the graph
541 // created BugReporter.
542 ExplodedNodeImpl::SetAuditor(0);
543
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000544 // Display warnings.
Ted Kremenekcf118d42009-02-04 23:49:09 +0000545 Eng.getBugReporter().FlushReports();
Ted Kremenek34d77342008-07-02 16:49:11 +0000546
547 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000548 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000549 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000550}
551
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000552static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000553 bool StandardWarnings) {
554
Ted Kremenekbc46f342008-07-02 16:35:50 +0000555 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
556 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000557 mgr.getLangOptions());
558
Ted Kremenek78d46242008-07-22 16:21:24 +0000559 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000560}
561
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000562static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000563
564 switch (mgr.getLangOptions().getGCMode()) {
565 default:
566 assert (false && "Invalid GC mode.");
567 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000568 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000569 break;
570
571 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000572 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000573 break;
574
575 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000576 ActionCheckerCFRefAux(mgr, false, true);
577 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000578 break;
579 }
580}
581
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000582static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000583 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
584}
585
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000586static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000587 if (LiveVariables* L = mgr.getLiveVariables()) {
588 mgr.DisplayFunction();
589 L->dumpBlockLiveness(mgr.getSourceManager());
590 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000591}
592
Ted Kremenek902141f2008-07-02 18:23:21 +0000593static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000594 if (CFG* c = mgr.getCFG()) {
595 mgr.DisplayFunction();
596 c->dump();
597 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000598}
599
600static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000601 if (CFG* c = mgr.getCFG()) {
602 mgr.DisplayFunction();
603 c->viewCFG();
604 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000605}
606
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000607static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000608 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
609 return;
610
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000611 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000612
613 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
614 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000615}
616
Ted Kremenek395aaf22008-07-23 00:45:26 +0000617static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
618 BugReporter BR(mgr);
619 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
620}
621
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000622static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000623 BugReporter BR(mgr);
624
625 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
626 BR);
627}
628
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000629//===----------------------------------------------------------------------===//
630// AnalysisConsumer creation.
631//===----------------------------------------------------------------------===//
632
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000633ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000634 PreprocessorFactory* ppf,
635 const LangOptions& lopts,
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000636 const std::string& OutDir) {
637
638 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
639 lopts, OutDir));
640
641 for (unsigned i = 0; i < AnalysisList.size(); ++i)
642 switch (AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000643#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000644 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000645 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000646 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000647#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000648 default: break;
649 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000650
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000651 return C.take();
652}
653
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000654//===----------------------------------------------------------------------===//
655// Ubigraph Visualization. FIXME: Move to separate file.
656//===----------------------------------------------------------------------===//
657
658namespace {
659
660class UbigraphViz : public ExplodedNodeImpl::Auditor {
661 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000662 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000663 unsigned Cntr;
664
665 typedef llvm::DenseMap<void*,unsigned> VMap;
666 VMap M;
667
668public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000669 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000670 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000671
672 ~UbigraphViz();
673
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000674 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
675};
676
677} // end anonymous namespace
678
679static ExplodedNodeImpl::Auditor* CreateUbiViz() {
680 std::string ErrMsg;
681
Ted Kremenek710ad932008-08-28 03:54:51 +0000682 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000683 if (!ErrMsg.empty())
684 return 0;
685
Ted Kremenek710ad932008-08-28 03:54:51 +0000686 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000687 Filename.appendComponent("llvm_ubi");
688 Filename.makeUnique(true,&ErrMsg);
689
690 if (!ErrMsg.empty())
691 return 0;
692
693 llvm::cerr << "Writing '" << Filename << "'.\n";
694
695 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
696 std::string filename = Filename.toString();
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000697 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000698
699 if (!ErrMsg.empty())
700 return 0;
701
Ted Kremenek710ad932008-08-28 03:54:51 +0000702 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000703}
704
705void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000706
707 assert (Src != Dst && "Self-edges are not allowed.");
708
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000709 // Lookup the Src. If it is a new node, it's a root.
710 VMap::iterator SrcI= M.find(Src);
711 unsigned SrcID;
712
713 if (SrcI == M.end()) {
714 M[Src] = SrcID = Cntr++;
715 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
716 }
717 else
718 SrcID = SrcI->second;
719
720 // Lookup the Dst.
721 VMap::iterator DstI= M.find(Dst);
722 unsigned DstID;
723
724 if (DstI == M.end()) {
725 M[Dst] = DstID = Cntr++;
726 *Out << "('vertex', " << DstID << ")\n";
727 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000728 else {
729 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000730 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000731 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
732 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000733
734 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000735 *Out << "('edge', " << SrcID << ", " << DstID
736 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000737}
738
Ted Kremenek56b98712008-08-28 05:02:09 +0000739UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
740 llvm::sys::Path& filename)
741 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
742
743 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
744 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
745 " ('size', '1.5'))\n";
746}
747
Ted Kremenek710ad932008-08-28 03:54:51 +0000748UbigraphViz::~UbigraphViz() {
749 Out.reset(0);
750 llvm::cerr << "Running 'ubiviz' program... ";
751 std::string ErrMsg;
752 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
753 std::vector<const char*> args;
754 args.push_back(Ubiviz.c_str());
755 args.push_back(Filename.c_str());
756 args.push_back(0);
757
758 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
759 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
760 }
761
762 // Delete the directory.
763 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000764}