blob: 2031174cabdd1d188b9f05c0ea8edef4ba1cbe21 [file] [log] [blame]
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// "Meta" ASTConsumer for running different source analyses.
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedman0ec78fa2009-05-19 21:10:40 +000014#include "clang/Frontend/AnalysisConsumer.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
16#include "clang/Frontend/ManagerRegistry.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000017#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
20#include "llvm/Support/Compiler.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000021#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000022#include "clang/Analysis/CFG.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000023#include "clang/Analysis/Analyses/LiveVariables.h"
24#include "clang/Analysis/PathDiagnostic.h"
25#include "clang/Basic/SourceManager.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/AST/ParentMap.h"
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//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000044// 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//===----------------------------------------------------------------------===//
Ted Kremenekf7556062009-07-27 22:13:39 +000053// Special PathDiagnosticClients.
54//===----------------------------------------------------------------------===//
55
56static PathDiagnosticClient*
57CreatePlistHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
58 PreprocessorFactory* PPF) {
59 llvm::sys::Path F(prefix);
60 PathDiagnosticClientFactory *PF =
61 CreateHTMLDiagnosticClientFactory(F.getDirname(), PP, PPF);
62 return CreatePlistDiagnosticClient(prefix, PP, PPF, PF);
63}
64
65//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000066// AnalysisConsumer declaration.
67//===----------------------------------------------------------------------===//
68
69namespace {
70
71 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000072 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000073 Actions FunctionActions;
74 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000075 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +000076 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000077
78 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +000079 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000080 Diagnostic &Diags;
81 ASTContext* Ctx;
82 Preprocessor* PP;
83 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000084 const std::string OutDir;
Eli Friedmane71b85f2009-05-19 10:18:02 +000085 AnalyzerOptions Opts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000086 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000087
88 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
89 PreprocessorFactory* ppf,
90 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000091 const std::string& outdir,
92 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000093 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000094 Ctx(0), PP(pp), PPF(ppf),
Eli Friedmane71b85f2009-05-19 10:18:02 +000095 OutDir(outdir), Opts(opts) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +000096
97 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000098 FunctionActions.push_back(action);
99 ObjCMethodActions.push_back(action);
100 }
101
102 void addObjCImplementationAction(CodeAction action) {
103 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000104 }
105
Ted Kremenekdaac6342008-11-07 02:09:25 +0000106 void addTranslationUnitAction(CodeAction action) {
107 TranslationUnitActions.push_back(action);
108 }
109
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000110 virtual void Initialize(ASTContext &Context) {
111 Ctx = &Context;
112 }
113
Chris Lattner682bf922009-03-29 16:50:03 +0000114 virtual void HandleTopLevelDecl(DeclGroupRef D) {
115 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
116 HandleTopLevelSingleDecl(*I);
117 }
118
119 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000120 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000121
Ted Kremenek81922f02009-02-02 20:52:40 +0000122 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000123 };
124
125
Ted Kremenekc0959972008-07-02 21:24:01 +0000126 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000127 Decl* D; Stmt* Body;
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000128
129 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
130
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000131 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000132 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000133
134 llvm::OwningPtr<CFG> cfg;
135 llvm::OwningPtr<LiveVariables> liveness;
136 llvm::OwningPtr<ParentMap> PM;
137
Zhongxing Xu22438a82008-11-27 01:55:08 +0000138 // Configurable components creators.
139 StoreManagerCreator CreateStoreMgr;
140 ConstraintManagerCreator CreateConstraintMgr;
141
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000142 public:
Ted Kremenek491918e2009-01-23 20:52:26 +0000143 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
Chris Lattner678dc3b2009-03-28 04:05:05 +0000144 : D(d), Body(b), AScope(ScopeDecl), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000145 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000146 setManagerCreators();
147 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000148
Chris Lattner678dc3b2009-03-28 04:05:05 +0000149 AnalysisManager(AnalysisConsumer& c, bool displayProgress)
150 : D(0), Body(0), AScope(ScopeTU), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000151 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000152 setManagerCreators();
153 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000154
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000155 Decl* getCodeDecl() const {
156 assert (AScope == ScopeDecl);
157 return D;
158 }
159
160 Stmt* getBody() const {
161 assert (AScope == ScopeDecl);
162 return Body;
163 }
164
Zhongxing Xu22438a82008-11-27 01:55:08 +0000165 StoreManagerCreator getStoreManagerCreator() {
166 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000167 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000168
169 ConstraintManagerCreator getConstraintManagerCreator() {
170 return CreateConstraintMgr;
171 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000172
Ted Kremenek7032f462008-07-03 05:26:14 +0000173 virtual CFG* getCFG() {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000174 if (!cfg) cfg.reset(CFG::buildCFG(getBody(), &getContext()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000175 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000176 }
177
Ted Kremenekc0959972008-07-02 21:24:01 +0000178 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000179 if (!PM)
180 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000181 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000182 }
183
Ted Kremenekc0959972008-07-02 21:24:01 +0000184 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000185 return *C.Ctx;
186 }
187
Ted Kremenekc0959972008-07-02 21:24:01 +0000188 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000189 return getContext().getSourceManager();
190 }
191
Ted Kremenekc0959972008-07-02 21:24:01 +0000192 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000193 return C.Diags;
194 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000195
196 const LangOptions& getLangOptions() const {
197 return C.LOpts;
198 }
199
Ted Kremenekc0959972008-07-02 21:24:01 +0000200 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000201 if (C.PD.get() == 0 && !C.OutDir.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000202 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000203 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000204#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000205case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000206#include "clang/Frontend/Analyses.def"
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000207 }
208 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000209 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000210 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000211
Ted Kremenek7032f462008-07-03 05:26:14 +0000212 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000213 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000214 CFG* c = getCFG();
215 if (!c) return 0;
216
Ted Kremenekca9bab02008-12-09 00:17:51 +0000217 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000218 liveness->runOnCFG(*c);
219 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000220 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000221
Ted Kremenek7032f462008-07-03 05:26:14 +0000222 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000223 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000224
Eli Friedmane71b85f2009-05-19 10:18:02 +0000225 bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000226
Eli Friedmane71b85f2009-05-19 10:18:02 +0000227 bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000228
229 bool shouldVisualize() const {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000230 return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000231 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000232
Eli Friedmane71b85f2009-05-19 10:18:02 +0000233 bool shouldTrimGraph() const { return C.Opts.TrimGraph; }
234
235 bool shouldPurgeDead() const { return C.Opts.PurgeDead; }
236
237 bool shouldEagerlyAssume() const { return C.Opts.EagerlyAssume; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000238
Ted Kremenek34d77342008-07-02 16:49:11 +0000239 void DisplayFunction() {
240
241 if (DisplayedFunction)
242 return;
243
244 DisplayedFunction = true;
245
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000246 // FIXME: Is getCodeDecl() always a named decl?
247 if (isa<FunctionDecl>(getCodeDecl()) ||
248 isa<ObjCMethodDecl>(getCodeDecl())) {
249 NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
250 SourceManager &SM = getContext().getSourceManager();
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000251 llvm::cerr << "ANALYZE: "
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000252 << SM.getPresumedLoc(ND->getLocation()).getFilename()
253 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenek34d77342008-07-02 16:49:11 +0000254 }
255 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000256
257 private:
258 /// Set configurable analyzer components creators. First check if there are
259 /// components registered at runtime. Otherwise fall back to builtin
260 /// components.
261 void setManagerCreators() {
262 if (ManagerRegistry::StoreMgrCreator != 0) {
263 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
264 }
265 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000266 switch (C.Opts.AnalysisStoreOpt) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000267 default:
268 assert(0 && "Unknown store manager.");
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000269#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
270 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000271#include "clang/Frontend/Analyses.def"
Zhongxing Xu22438a82008-11-27 01:55:08 +0000272 }
273 }
274
275 if (ManagerRegistry::ConstraintMgrCreator != 0)
276 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000277 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000278 switch (C.Opts.AnalysisConstraintsOpt) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000279 default:
280 assert(0 && "Unknown store manager.");
281#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
282 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000283#include "clang/Frontend/Analyses.def"
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000284 }
285 }
286
Ted Kremenekc472d792009-01-23 20:06:20 +0000287
288 // Some DiagnosticClients should be created all the time instead of
289 // lazily. Create those now.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000290 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenekc472d792009-01-23 20:06:20 +0000291 default: break;
292#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
293case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000294#include "clang/Frontend/Analyses.def"
Ted Kremenekc472d792009-01-23 20:06:20 +0000295 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000296 }
297
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000298 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000299
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000300} // end anonymous namespace
301
302namespace llvm {
303 template <> struct FoldingSetTrait<CodeAction> {
304 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
305 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
306 }
307 };
308}
309
310//===----------------------------------------------------------------------===//
311// AnalysisConsumer implementation.
312//===----------------------------------------------------------------------===//
313
Chris Lattner682bf922009-03-29 16:50:03 +0000314void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000315 switch (D->getKind()) {
316 case Decl::Function: {
317 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000318
Eli Friedmane71b85f2009-05-19 10:18:02 +0000319 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
320 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000321 break;
322
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000323 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000324 if (Body) HandleCode(FD, Body, FunctionActions);
325 break;
326 }
327
328 case Decl::ObjCMethod: {
329 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000330
Eli Friedmane71b85f2009-05-19 10:18:02 +0000331 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
332 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000333 return;
334
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000335 Stmt* Body = MD->getBody();
336 if (Body) HandleCode(MD, Body, ObjCMethodActions);
337 break;
338 }
339
340 default:
341 break;
342 }
343}
344
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000345void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000346
Ted Kremenekdaac6342008-11-07 02:09:25 +0000347 if(!TranslationUnitActions.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000348 AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000349 for (Actions::iterator I = TranslationUnitActions.begin(),
350 E = TranslationUnitActions.end(); I != E; ++I)
351 (*I)(mgr);
352 }
353
Chris Lattnere9077872009-03-28 03:29:40 +0000354 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000355 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000356
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000357 for (DeclContext::decl_iterator I = TUD->decls_begin(),
358 E = TUD->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000359 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000360 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
361 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000362 }
Ted Kremenek4d53a532009-02-13 00:51:30 +0000363
364 // Delete the PathDiagnosticClient here just in case the AnalysisConsumer
365 // object doesn't get released. This will cause any side-effects in the
366 // destructor of the PathDiagnosticClient to get executed.
367 PD.reset();
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000368}
369
Ted Kremenek81922f02009-02-02 20:52:40 +0000370void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000371
372 // Don't run the actions if an error has occured with parsing the file.
373 if (Diags.hasErrorOccurred())
374 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000375
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000376 // Don't run the actions on declarations in header files unless
377 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000378 if (!Opts.AnalyzeAll &&
379 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000380 return;
381
382 // Create an AnalysisManager that will manage the state for analyzing
383 // this method/function.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000384 AnalysisManager mgr(*this, D, Body, Opts.AnalyzerDisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000385
386 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000387 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000388 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000389}
390
391//===----------------------------------------------------------------------===//
392// Analyses
393//===----------------------------------------------------------------------===//
394
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000395static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000396 if (LiveVariables* L = mgr.getLiveVariables()) {
397 BugReporter BR(mgr);
398 CheckDeadStores(*L, BR);
399 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000400}
401
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000402static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000403 if (CFG* c = mgr.getCFG())
404 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000405}
406
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000407
Ted Kremenek78d46242008-07-22 16:21:24 +0000408static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
409 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000410
Ted Kremenek7032f462008-07-03 05:26:14 +0000411
Ted Kremenekbc46f342008-07-02 16:35:50 +0000412 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000413
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000414 // Display progress.
415 mgr.DisplayFunction();
416
Ted Kremenek7032f462008-07-03 05:26:14 +0000417 // Construct the analysis engine.
418 LiveVariables* L = mgr.getLiveVariables();
419 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000420
Ted Kremenekcf118d42009-02-04 23:49:09 +0000421 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000422 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000423 mgr.getStoreManagerCreator(),
424 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000425
Ted Kremenekbc46f342008-07-02 16:35:50 +0000426 Eng.setTransferFunctions(tf);
427
Ted Kremenek78d46242008-07-22 16:21:24 +0000428 if (StandardWarnings) {
429 Eng.RegisterInternalChecks();
430 RegisterAppleChecks(Eng);
431 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000432
433 // Set the graph auditor.
434 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
435 if (mgr.shouldVisualizeUbigraph()) {
436 Auditor.reset(CreateUbiViz());
437 ExplodedNodeImpl::SetAuditor(Auditor.get());
438 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000439
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000440 // Execute the worklist algorithm.
441 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000442
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000443 // Release the auditor (if any) so that it doesn't monitor the graph
444 // created BugReporter.
445 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000446
Ted Kremenek34d77342008-07-02 16:49:11 +0000447 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000448 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000449 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000450
451 // Display warnings.
452 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000453}
454
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000455static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000456 bool StandardWarnings) {
457
Ted Kremenekbc46f342008-07-02 16:35:50 +0000458 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
459 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000460 mgr.getLangOptions());
461
Ted Kremenek78d46242008-07-22 16:21:24 +0000462 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000463}
464
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000465static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000466
467 switch (mgr.getLangOptions().getGCMode()) {
468 default:
469 assert (false && "Invalid GC mode.");
470 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000471 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000472 break;
473
474 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000475 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000476 break;
477
478 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000479 ActionCheckerCFRefAux(mgr, false, true);
480 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000481 break;
482 }
483}
484
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000485static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000486 if (LiveVariables* L = mgr.getLiveVariables()) {
487 mgr.DisplayFunction();
488 L->dumpBlockLiveness(mgr.getSourceManager());
489 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000490}
491
Ted Kremenek902141f2008-07-02 18:23:21 +0000492static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000493 if (CFG* c = mgr.getCFG()) {
494 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000495 LangOptions LO; // FIXME!
496 c->dump(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000497 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000498}
499
500static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000501 if (CFG* c = mgr.getCFG()) {
502 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000503 LangOptions LO; // FIXME!
504 c->viewCFG(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000505 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000506}
507
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000508static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
509 BugReporter BR(mgr);
510 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
511}
512
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000513static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000514 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
515 return;
516
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000517 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000518
519 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
520 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000521}
522
Ted Kremenek395aaf22008-07-23 00:45:26 +0000523static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
524 BugReporter BR(mgr);
525 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
526}
527
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000528static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000529 BugReporter BR(mgr);
530
531 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
532 BR);
533}
534
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000535//===----------------------------------------------------------------------===//
536// AnalysisConsumer creation.
537//===----------------------------------------------------------------------===//
538
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000539ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000540 PreprocessorFactory* ppf,
541 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000542 const std::string& OutDir,
543 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000544
545 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000546 lopts, OutDir,
547 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000548
Eli Friedmane71b85f2009-05-19 10:18:02 +0000549 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
550 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000551#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000552 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000553 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000554 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000555#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000556 default: break;
557 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000558
559 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
560 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000561
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000562 return C.take();
563}
564
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000565//===----------------------------------------------------------------------===//
566// Ubigraph Visualization. FIXME: Move to separate file.
567//===----------------------------------------------------------------------===//
568
569namespace {
570
571class UbigraphViz : public ExplodedNodeImpl::Auditor {
572 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000573 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000574 unsigned Cntr;
575
576 typedef llvm::DenseMap<void*,unsigned> VMap;
577 VMap M;
578
579public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000580 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000581 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000582
583 ~UbigraphViz();
584
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000585 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
586};
587
588} // end anonymous namespace
589
590static ExplodedNodeImpl::Auditor* CreateUbiViz() {
591 std::string ErrMsg;
592
Ted Kremenek710ad932008-08-28 03:54:51 +0000593 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000594 if (!ErrMsg.empty())
595 return 0;
596
Ted Kremenek710ad932008-08-28 03:54:51 +0000597 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000598 Filename.appendComponent("llvm_ubi");
599 Filename.makeUnique(true,&ErrMsg);
600
601 if (!ErrMsg.empty())
602 return 0;
603
604 llvm::cerr << "Writing '" << Filename << "'.\n";
605
606 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
607 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000608 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
609 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000610
611 if (!ErrMsg.empty())
612 return 0;
613
Ted Kremenek710ad932008-08-28 03:54:51 +0000614 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000615}
616
617void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000618
619 assert (Src != Dst && "Self-edges are not allowed.");
620
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000621 // Lookup the Src. If it is a new node, it's a root.
622 VMap::iterator SrcI= M.find(Src);
623 unsigned SrcID;
624
625 if (SrcI == M.end()) {
626 M[Src] = SrcID = Cntr++;
627 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
628 }
629 else
630 SrcID = SrcI->second;
631
632 // Lookup the Dst.
633 VMap::iterator DstI= M.find(Dst);
634 unsigned DstID;
635
636 if (DstI == M.end()) {
637 M[Dst] = DstID = Cntr++;
638 *Out << "('vertex', " << DstID << ")\n";
639 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000640 else {
641 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000642 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000643 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
644 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000645
646 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000647 *Out << "('edge', " << SrcID << ", " << DstID
648 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000649}
650
Ted Kremenek56b98712008-08-28 05:02:09 +0000651UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
652 llvm::sys::Path& filename)
653 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
654
655 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
656 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
657 " ('size', '1.5'))\n";
658}
659
Ted Kremenek710ad932008-08-28 03:54:51 +0000660UbigraphViz::~UbigraphViz() {
661 Out.reset(0);
662 llvm::cerr << "Running 'ubiviz' program... ";
663 std::string ErrMsg;
664 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
665 std::vector<const char*> args;
666 args.push_back(Ubiviz.c_str());
667 args.push_back(Filename.c_str());
668 args.push_back(0);
669
670 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
671 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
672 }
673
674 // Delete the directory.
675 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000676}