blob: 3bdd0cacd7a135e23bab869b1aaf9377fb2d3b4f [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"
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//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
53// AnalysisConsumer declaration.
54//===----------------------------------------------------------------------===//
55
56namespace {
57
58 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000059 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000060 Actions FunctionActions;
61 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000062 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +000063 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000064
65 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +000066 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000067 Diagnostic &Diags;
68 ASTContext* Ctx;
69 Preprocessor* PP;
70 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000071 const std::string OutDir;
Eli Friedmane71b85f2009-05-19 10:18:02 +000072 AnalyzerOptions Opts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000073 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000074
75 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
76 PreprocessorFactory* ppf,
77 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000078 const std::string& outdir,
79 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000080 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000081 Ctx(0), PP(pp), PPF(ppf),
Eli Friedmane71b85f2009-05-19 10:18:02 +000082 OutDir(outdir), Opts(opts) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +000083
84 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000085 FunctionActions.push_back(action);
86 ObjCMethodActions.push_back(action);
87 }
88
89 void addObjCImplementationAction(CodeAction action) {
90 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +000091 }
92
Ted Kremenekdaac6342008-11-07 02:09:25 +000093 void addTranslationUnitAction(CodeAction action) {
94 TranslationUnitActions.push_back(action);
95 }
96
Ted Kremenekf4381fd2008-07-02 00:03:09 +000097 virtual void Initialize(ASTContext &Context) {
98 Ctx = &Context;
99 }
100
Chris Lattner682bf922009-03-29 16:50:03 +0000101 virtual void HandleTopLevelDecl(DeclGroupRef D) {
102 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
103 HandleTopLevelSingleDecl(*I);
104 }
105
106 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000107 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000108
Ted Kremenek81922f02009-02-02 20:52:40 +0000109 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000110 };
111
112
Ted Kremenekc0959972008-07-02 21:24:01 +0000113 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000114 Decl* D; Stmt* Body;
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000115
116 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
117
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000118 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000119 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000120
121 llvm::OwningPtr<CFG> cfg;
122 llvm::OwningPtr<LiveVariables> liveness;
123 llvm::OwningPtr<ParentMap> PM;
124
Zhongxing Xu22438a82008-11-27 01:55:08 +0000125 // Configurable components creators.
126 StoreManagerCreator CreateStoreMgr;
127 ConstraintManagerCreator CreateConstraintMgr;
128
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000129 public:
Ted Kremenek491918e2009-01-23 20:52:26 +0000130 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
Chris Lattner678dc3b2009-03-28 04:05:05 +0000131 : D(d), Body(b), AScope(ScopeDecl), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000132 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000133 setManagerCreators();
134 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000135
Chris Lattner678dc3b2009-03-28 04:05:05 +0000136 AnalysisManager(AnalysisConsumer& c, bool displayProgress)
137 : D(0), Body(0), AScope(ScopeTU), C(c),
Ted Kremenek491918e2009-01-23 20:52:26 +0000138 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000139 setManagerCreators();
140 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000141
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000142 Decl* getCodeDecl() const {
143 assert (AScope == ScopeDecl);
144 return D;
145 }
146
147 Stmt* getBody() const {
148 assert (AScope == ScopeDecl);
149 return Body;
150 }
151
Zhongxing Xu22438a82008-11-27 01:55:08 +0000152 StoreManagerCreator getStoreManagerCreator() {
153 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000154 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000155
156 ConstraintManagerCreator getConstraintManagerCreator() {
157 return CreateConstraintMgr;
158 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000159
Ted Kremenek7032f462008-07-03 05:26:14 +0000160 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000161 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000162 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000163 }
164
Ted Kremenekc0959972008-07-02 21:24:01 +0000165 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000166 if (!PM)
167 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000168 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000169 }
170
Ted Kremenekc0959972008-07-02 21:24:01 +0000171 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000172 return *C.Ctx;
173 }
174
Ted Kremenekc0959972008-07-02 21:24:01 +0000175 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000176 return getContext().getSourceManager();
177 }
178
Ted Kremenekc0959972008-07-02 21:24:01 +0000179 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000180 return C.Diags;
181 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000182
183 const LangOptions& getLangOptions() const {
184 return C.LOpts;
185 }
186
Ted Kremenekc0959972008-07-02 21:24:01 +0000187 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000188 if (C.PD.get() == 0 && !C.OutDir.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000189 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000190 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000191#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000192case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000193#include "clang/Frontend/Analyses.def"
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000194 }
195 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000196 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000197 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000198
Ted Kremenek7032f462008-07-03 05:26:14 +0000199 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000200 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000201 CFG* c = getCFG();
202 if (!c) return 0;
203
Ted Kremenekca9bab02008-12-09 00:17:51 +0000204 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000205 liveness->runOnCFG(*c);
206 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000207 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000208
Ted Kremenek7032f462008-07-03 05:26:14 +0000209 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000210 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000211
Eli Friedmane71b85f2009-05-19 10:18:02 +0000212 bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000213
Eli Friedmane71b85f2009-05-19 10:18:02 +0000214 bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000215
216 bool shouldVisualize() const {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000217 return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000218 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000219
Eli Friedmane71b85f2009-05-19 10:18:02 +0000220 bool shouldTrimGraph() const { return C.Opts.TrimGraph; }
221
222 bool shouldPurgeDead() const { return C.Opts.PurgeDead; }
223
224 bool shouldEagerlyAssume() const { return C.Opts.EagerlyAssume; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000225
Ted Kremenek34d77342008-07-02 16:49:11 +0000226 void DisplayFunction() {
227
228 if (DisplayedFunction)
229 return;
230
231 DisplayedFunction = true;
232
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000233 // FIXME: Is getCodeDecl() always a named decl?
234 if (isa<FunctionDecl>(getCodeDecl()) ||
235 isa<ObjCMethodDecl>(getCodeDecl())) {
236 NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
237 SourceManager &SM = getContext().getSourceManager();
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000238 llvm::cerr << "ANALYZE: "
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000239 << SM.getPresumedLoc(ND->getLocation()).getFilename()
240 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenek34d77342008-07-02 16:49:11 +0000241 }
242 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000243
244 private:
245 /// Set configurable analyzer components creators. First check if there are
246 /// components registered at runtime. Otherwise fall back to builtin
247 /// components.
248 void setManagerCreators() {
249 if (ManagerRegistry::StoreMgrCreator != 0) {
250 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
251 }
252 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000253 switch (C.Opts.AnalysisStoreOpt) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000254 default:
255 assert(0 && "Unknown store manager.");
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000256#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
257 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000258#include "clang/Frontend/Analyses.def"
Zhongxing Xu22438a82008-11-27 01:55:08 +0000259 }
260 }
261
262 if (ManagerRegistry::ConstraintMgrCreator != 0)
263 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000264 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000265 switch (C.Opts.AnalysisConstraintsOpt) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000266 default:
267 assert(0 && "Unknown store manager.");
268#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
269 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000270#include "clang/Frontend/Analyses.def"
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000271 }
272 }
273
Ted Kremenekc472d792009-01-23 20:06:20 +0000274
275 // Some DiagnosticClients should be created all the time instead of
276 // lazily. Create those now.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000277 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenekc472d792009-01-23 20:06:20 +0000278 default: break;
279#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
280case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000281#include "clang/Frontend/Analyses.def"
Ted Kremenekc472d792009-01-23 20:06:20 +0000282 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000283 }
284
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000285 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000286
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000287} // end anonymous namespace
288
289namespace llvm {
290 template <> struct FoldingSetTrait<CodeAction> {
291 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
292 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
293 }
294 };
295}
296
297//===----------------------------------------------------------------------===//
298// AnalysisConsumer implementation.
299//===----------------------------------------------------------------------===//
300
Chris Lattner682bf922009-03-29 16:50:03 +0000301void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000302 switch (D->getKind()) {
303 case Decl::Function: {
304 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000305
Eli Friedmane71b85f2009-05-19 10:18:02 +0000306 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
307 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000308 break;
309
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000310 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000311 if (Body) HandleCode(FD, Body, FunctionActions);
312 break;
313 }
314
315 case Decl::ObjCMethod: {
316 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000317
Eli Friedmane71b85f2009-05-19 10:18:02 +0000318 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
319 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000320 return;
321
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000322 Stmt* Body = MD->getBody();
323 if (Body) HandleCode(MD, Body, ObjCMethodActions);
324 break;
325 }
326
327 default:
328 break;
329 }
330}
331
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000332void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000333
Ted Kremenekdaac6342008-11-07 02:09:25 +0000334 if(!TranslationUnitActions.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000335 AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000336 for (Actions::iterator I = TranslationUnitActions.begin(),
337 E = TranslationUnitActions.end(); I != E; ++I)
338 (*I)(mgr);
339 }
340
Chris Lattnere9077872009-03-28 03:29:40 +0000341 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000342 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000343
Douglas Gregor6ab35242009-04-09 21:40:53 +0000344 for (DeclContext::decl_iterator I = TUD->decls_begin(C),
345 E = TUD->decls_end(C);
Chris Lattnere9077872009-03-28 03:29:40 +0000346 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000347 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
348 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000349 }
Ted Kremenek4d53a532009-02-13 00:51:30 +0000350
351 // Delete the PathDiagnosticClient here just in case the AnalysisConsumer
352 // object doesn't get released. This will cause any side-effects in the
353 // destructor of the PathDiagnosticClient to get executed.
354 PD.reset();
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000355}
356
Ted Kremenek81922f02009-02-02 20:52:40 +0000357void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000358
359 // Don't run the actions if an error has occured with parsing the file.
360 if (Diags.hasErrorOccurred())
361 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000362
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000363 // Don't run the actions on declarations in header files unless
364 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000365 if (!Opts.AnalyzeAll &&
366 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000367 return;
368
369 // Create an AnalysisManager that will manage the state for analyzing
370 // this method/function.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000371 AnalysisManager mgr(*this, D, Body, Opts.AnalyzerDisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000372
373 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000374 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000375 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000376}
377
378//===----------------------------------------------------------------------===//
379// Analyses
380//===----------------------------------------------------------------------===//
381
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000382static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000383 if (LiveVariables* L = mgr.getLiveVariables()) {
384 BugReporter BR(mgr);
385 CheckDeadStores(*L, BR);
386 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000387}
388
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000389static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000390 if (CFG* c = mgr.getCFG())
391 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000392}
393
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000394
Ted Kremenek78d46242008-07-22 16:21:24 +0000395static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
396 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000397
Ted Kremenek7032f462008-07-03 05:26:14 +0000398
Ted Kremenekbc46f342008-07-02 16:35:50 +0000399 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000400
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000401 // Display progress.
402 mgr.DisplayFunction();
403
Ted Kremenek7032f462008-07-03 05:26:14 +0000404 // Construct the analysis engine.
405 LiveVariables* L = mgr.getLiveVariables();
406 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000407
Ted Kremenekcf118d42009-02-04 23:49:09 +0000408 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000409 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000410 mgr.getStoreManagerCreator(),
411 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000412
Ted Kremenekbc46f342008-07-02 16:35:50 +0000413 Eng.setTransferFunctions(tf);
414
Ted Kremenek78d46242008-07-22 16:21:24 +0000415 if (StandardWarnings) {
416 Eng.RegisterInternalChecks();
417 RegisterAppleChecks(Eng);
418 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000419
420 // Set the graph auditor.
421 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
422 if (mgr.shouldVisualizeUbigraph()) {
423 Auditor.reset(CreateUbiViz());
424 ExplodedNodeImpl::SetAuditor(Auditor.get());
425 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000426
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000427 // Execute the worklist algorithm.
428 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000429
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000430 // Release the auditor (if any) so that it doesn't monitor the graph
431 // created BugReporter.
432 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000433
Ted Kremenek34d77342008-07-02 16:49:11 +0000434 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000435 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000436 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000437
438 // Display warnings.
439 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000440}
441
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000442static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000443 bool StandardWarnings) {
444
Ted Kremenekbc46f342008-07-02 16:35:50 +0000445 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
446 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000447 mgr.getLangOptions());
448
Ted Kremenek78d46242008-07-22 16:21:24 +0000449 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000450}
451
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000452static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000453
454 switch (mgr.getLangOptions().getGCMode()) {
455 default:
456 assert (false && "Invalid GC mode.");
457 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000458 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000459 break;
460
461 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000462 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000463 break;
464
465 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000466 ActionCheckerCFRefAux(mgr, false, true);
467 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000468 break;
469 }
470}
471
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000472static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000473 if (LiveVariables* L = mgr.getLiveVariables()) {
474 mgr.DisplayFunction();
475 L->dumpBlockLiveness(mgr.getSourceManager());
476 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000477}
478
Ted Kremenek902141f2008-07-02 18:23:21 +0000479static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000480 if (CFG* c = mgr.getCFG()) {
481 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000482 LangOptions LO; // FIXME!
483 c->dump(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000484 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000485}
486
487static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000488 if (CFG* c = mgr.getCFG()) {
489 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000490 LangOptions LO; // FIXME!
491 c->viewCFG(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000492 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000493}
494
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000495static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000496 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
497 return;
498
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000499 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000500
501 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
502 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000503}
504
Ted Kremenek395aaf22008-07-23 00:45:26 +0000505static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
506 BugReporter BR(mgr);
507 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
508}
509
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000510static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000511 BugReporter BR(mgr);
512
513 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
514 BR);
515}
516
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000517//===----------------------------------------------------------------------===//
518// AnalysisConsumer creation.
519//===----------------------------------------------------------------------===//
520
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000521ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000522 PreprocessorFactory* ppf,
523 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000524 const std::string& OutDir,
525 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000526
527 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000528 lopts, OutDir,
529 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000530
Eli Friedmane71b85f2009-05-19 10:18:02 +0000531 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
532 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000533#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000534 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000535 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000536 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000537#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000538 default: break;
539 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000540
541 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
542 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000543
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000544 return C.take();
545}
546
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000547//===----------------------------------------------------------------------===//
548// Ubigraph Visualization. FIXME: Move to separate file.
549//===----------------------------------------------------------------------===//
550
551namespace {
552
553class UbigraphViz : public ExplodedNodeImpl::Auditor {
554 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000555 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000556 unsigned Cntr;
557
558 typedef llvm::DenseMap<void*,unsigned> VMap;
559 VMap M;
560
561public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000562 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000563 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000564
565 ~UbigraphViz();
566
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000567 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
568};
569
570} // end anonymous namespace
571
572static ExplodedNodeImpl::Auditor* CreateUbiViz() {
573 std::string ErrMsg;
574
Ted Kremenek710ad932008-08-28 03:54:51 +0000575 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000576 if (!ErrMsg.empty())
577 return 0;
578
Ted Kremenek710ad932008-08-28 03:54:51 +0000579 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000580 Filename.appendComponent("llvm_ubi");
581 Filename.makeUnique(true,&ErrMsg);
582
583 if (!ErrMsg.empty())
584 return 0;
585
586 llvm::cerr << "Writing '" << Filename << "'.\n";
587
588 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
589 std::string filename = Filename.toString();
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000590 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000591
592 if (!ErrMsg.empty())
593 return 0;
594
Ted Kremenek710ad932008-08-28 03:54:51 +0000595 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000596}
597
598void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000599
600 assert (Src != Dst && "Self-edges are not allowed.");
601
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000602 // Lookup the Src. If it is a new node, it's a root.
603 VMap::iterator SrcI= M.find(Src);
604 unsigned SrcID;
605
606 if (SrcI == M.end()) {
607 M[Src] = SrcID = Cntr++;
608 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
609 }
610 else
611 SrcID = SrcI->second;
612
613 // Lookup the Dst.
614 VMap::iterator DstI= M.find(Dst);
615 unsigned DstID;
616
617 if (DstI == M.end()) {
618 M[Dst] = DstID = Cntr++;
619 *Out << "('vertex', " << DstID << ")\n";
620 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000621 else {
622 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000623 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000624 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
625 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000626
627 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000628 *Out << "('edge', " << SrcID << ", " << DstID
629 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000630}
631
Ted Kremenek56b98712008-08-28 05:02:09 +0000632UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
633 llvm::sys::Path& filename)
634 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
635
636 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
637 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
638 " ('size', '1.5'))\n";
639}
640
Ted Kremenek710ad932008-08-28 03:54:51 +0000641UbigraphViz::~UbigraphViz() {
642 Out.reset(0);
643 llvm::cerr << "Running 'ubiviz' program... ";
644 std::string ErrMsg;
645 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
646 std::vector<const char*> args;
647 args.push_back(Ubiviz.c_str());
648 args.push_back(Filename.c_str());
649 args.push_back(0);
650
651 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
652 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
653 }
654
655 // Delete the directory.
656 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000657}