blob: d631c69749a47d8cc232a4abfcdbb86f07759325 [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"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000028#include "clang/Analysis/PathSensitive/AnalysisContext.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
Ted Kremenekf4381fd2008-07-02 00:03:09 +000040using namespace clang;
41
Ted Kremenekf8ce6992008-08-27 22:31:43 +000042static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000043
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000044//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000045// Basic type definitions.
46//===----------------------------------------------------------------------===//
47
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000048namespace {
Ted Kremenekf4381fd2008-07-02 00:03:09 +000049 class AnalysisManager;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000050 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +000051} // end anonymous namespace
52
53//===----------------------------------------------------------------------===//
Ted Kremenekf7556062009-07-27 22:13:39 +000054// Special PathDiagnosticClients.
55//===----------------------------------------------------------------------===//
56
57static PathDiagnosticClient*
58CreatePlistHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP,
59 PreprocessorFactory* PPF) {
60 llvm::sys::Path F(prefix);
61 PathDiagnosticClientFactory *PF =
62 CreateHTMLDiagnosticClientFactory(F.getDirname(), PP, PPF);
63 return CreatePlistDiagnosticClient(prefix, PP, PPF, PF);
64}
65
66//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000067// AnalysisConsumer declaration.
68//===----------------------------------------------------------------------===//
69
70namespace {
71
72 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000073 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000074 Actions FunctionActions;
75 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000076 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +000077 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000078
79 public:
Ted Kremenek95c7b002008-10-24 01:04:59 +000080 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000081 Diagnostic &Diags;
82 ASTContext* Ctx;
83 Preprocessor* PP;
84 PreprocessorFactory* PPF;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000085 const std::string OutDir;
Eli Friedmane71b85f2009-05-19 10:18:02 +000086 AnalyzerOptions Opts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000087 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000088
89 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
90 PreprocessorFactory* ppf,
91 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +000092 const std::string& outdir,
93 const AnalyzerOptions& opts)
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000094 : LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000095 Ctx(0), PP(pp), PPF(ppf),
Eli Friedmane71b85f2009-05-19 10:18:02 +000096 OutDir(outdir), Opts(opts) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +000097
98 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000099 FunctionActions.push_back(action);
100 ObjCMethodActions.push_back(action);
101 }
102
103 void addObjCImplementationAction(CodeAction action) {
104 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000105 }
106
Ted Kremenekdaac6342008-11-07 02:09:25 +0000107 void addTranslationUnitAction(CodeAction action) {
108 TranslationUnitActions.push_back(action);
109 }
110
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000111 virtual void Initialize(ASTContext &Context) {
112 Ctx = &Context;
113 }
114
Chris Lattner682bf922009-03-29 16:50:03 +0000115 virtual void HandleTopLevelDecl(DeclGroupRef D) {
116 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
117 HandleTopLevelSingleDecl(*I);
118 }
119
120 void HandleTopLevelSingleDecl(Decl *D);
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000121 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000122
Ted Kremenek81922f02009-02-02 20:52:40 +0000123 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000124 };
125
126
Ted Kremenekc0959972008-07-02 21:24:01 +0000127 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000128 AnalysisContextManager ContextMgr;
129 AnalysisContext *CurrentContext;
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000130
131 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
132
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000133 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000134 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000135
Zhongxing Xu22438a82008-11-27 01:55:08 +0000136 // Configurable components creators.
137 StoreManagerCreator CreateStoreMgr;
138 ConstraintManagerCreator CreateConstraintMgr;
139
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000140 public:
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000141 AnalysisManager(AnalysisConsumer& c, Decl* d, bool displayProgress)
142 : AScope(ScopeDecl), C(c), DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000143 setManagerCreators();
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000144 CurrentContext = ContextMgr.getContext(d);
Zhongxing Xu22438a82008-11-27 01:55:08 +0000145 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000146
Chris Lattner678dc3b2009-03-28 04:05:05 +0000147 AnalysisManager(AnalysisConsumer& c, bool displayProgress)
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000148 : AScope(ScopeTU), C(c), DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000149 setManagerCreators();
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000150 CurrentContext = 0;
Zhongxing Xu22438a82008-11-27 01:55:08 +0000151 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000152
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000153 Decl* getCodeDecl() const {
154 assert (AScope == ScopeDecl);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000155 return CurrentContext->getDecl();
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000156 }
157
158 Stmt* getBody() const {
159 assert (AScope == ScopeDecl);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000160 return CurrentContext->getBody();
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000161 }
162
Zhongxing Xu22438a82008-11-27 01:55:08 +0000163 StoreManagerCreator getStoreManagerCreator() {
164 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000165 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000166
167 ConstraintManagerCreator getConstraintManagerCreator() {
168 return CreateConstraintMgr;
169 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000170
Ted Kremenek7032f462008-07-03 05:26:14 +0000171 virtual CFG* getCFG() {
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000172 return CurrentContext->getCFG();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000173 }
174
Ted Kremenekc0959972008-07-02 21:24:01 +0000175 virtual ParentMap& getParentMap() {
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000176 return CurrentContext->getParentMap();
177 }
178
179 virtual LiveVariables* getLiveVariables() {
180 return CurrentContext->getLiveVariables();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000181 }
182
Ted Kremenekc0959972008-07-02 21:24:01 +0000183 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000184 return *C.Ctx;
185 }
186
Ted Kremenekc0959972008-07-02 21:24:01 +0000187 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000188 return getContext().getSourceManager();
189 }
190
Ted Kremenekc0959972008-07-02 21:24:01 +0000191 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000192 return C.Diags;
193 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000194
195 const LangOptions& getLangOptions() const {
196 return C.LOpts;
197 }
198
Ted Kremenekc0959972008-07-02 21:24:01 +0000199 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000200 if (C.PD.get() == 0 && !C.OutDir.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000201 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000202 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000203#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000204case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000205#include "clang/Frontend/Analyses.def"
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000206 }
207 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000208 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000209 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000210
Eli Friedmane71b85f2009-05-19 10:18:02 +0000211 bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000212
Eli Friedmane71b85f2009-05-19 10:18:02 +0000213 bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000214
215 bool shouldVisualize() const {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000216 return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000217 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000218
Eli Friedmane71b85f2009-05-19 10:18:02 +0000219 bool shouldTrimGraph() const { return C.Opts.TrimGraph; }
220
221 bool shouldPurgeDead() const { return C.Opts.PurgeDead; }
222
223 bool shouldEagerlyAssume() const { return C.Opts.EagerlyAssume; }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000224
Ted Kremenek34d77342008-07-02 16:49:11 +0000225 void DisplayFunction() {
226
227 if (DisplayedFunction)
228 return;
229
230 DisplayedFunction = true;
231
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000232 // FIXME: Is getCodeDecl() always a named decl?
233 if (isa<FunctionDecl>(getCodeDecl()) ||
234 isa<ObjCMethodDecl>(getCodeDecl())) {
235 NamedDecl *ND = cast<NamedDecl>(getCodeDecl());
236 SourceManager &SM = getContext().getSourceManager();
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000237 llvm::cerr << "ANALYZE: "
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000238 << SM.getPresumedLoc(ND->getLocation()).getFilename()
239 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenek34d77342008-07-02 16:49:11 +0000240 }
241 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000242
243 private:
244 /// Set configurable analyzer components creators. First check if there are
245 /// components registered at runtime. Otherwise fall back to builtin
246 /// components.
247 void setManagerCreators() {
248 if (ManagerRegistry::StoreMgrCreator != 0) {
249 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
250 }
251 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000252 switch (C.Opts.AnalysisStoreOpt) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000253 default:
254 assert(0 && "Unknown store manager.");
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000255#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
256 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000257#include "clang/Frontend/Analyses.def"
Zhongxing Xu22438a82008-11-27 01:55:08 +0000258 }
259 }
260
261 if (ManagerRegistry::ConstraintMgrCreator != 0)
262 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000263 else {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000264 switch (C.Opts.AnalysisConstraintsOpt) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000265 default:
266 assert(0 && "Unknown store manager.");
267#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
268 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000269#include "clang/Frontend/Analyses.def"
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000270 }
271 }
272
Ted Kremenekc472d792009-01-23 20:06:20 +0000273
274 // Some DiagnosticClients should be created all the time instead of
275 // lazily. Create those now.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000276 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenekc472d792009-01-23 20:06:20 +0000277 default: break;
278#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
279case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000280#include "clang/Frontend/Analyses.def"
Ted Kremenekc472d792009-01-23 20:06:20 +0000281 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000282 }
283
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000284 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000285
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000286} // end anonymous namespace
287
288namespace llvm {
289 template <> struct FoldingSetTrait<CodeAction> {
290 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
291 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
292 }
293 };
294}
295
296//===----------------------------------------------------------------------===//
297// AnalysisConsumer implementation.
298//===----------------------------------------------------------------------===//
299
Chris Lattner682bf922009-03-29 16:50:03 +0000300void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000301 switch (D->getKind()) {
302 case Decl::Function: {
303 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000304
Eli Friedmane71b85f2009-05-19 10:18:02 +0000305 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
306 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000307 break;
308
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000309 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000310 if (Body) HandleCode(FD, Body, FunctionActions);
311 break;
312 }
313
314 case Decl::ObjCMethod: {
315 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000316
Eli Friedmane71b85f2009-05-19 10:18:02 +0000317 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
318 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000319 return;
320
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000321 Stmt* Body = MD->getBody();
322 if (Body) HandleCode(MD, Body, ObjCMethodActions);
323 break;
324 }
325
326 default:
327 break;
328 }
329}
330
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000331void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000332
Ted Kremenekdaac6342008-11-07 02:09:25 +0000333 if(!TranslationUnitActions.empty()) {
Eli Friedmane71b85f2009-05-19 10:18:02 +0000334 AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000335 for (Actions::iterator I = TranslationUnitActions.begin(),
336 E = TranslationUnitActions.end(); I != E; ++I)
337 (*I)(mgr);
338 }
339
Chris Lattnere9077872009-03-28 03:29:40 +0000340 if (!ObjCImplementationActions.empty()) {
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000341 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattnere9077872009-03-28 03:29:40 +0000342
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000343 for (DeclContext::decl_iterator I = TUD->decls_begin(),
344 E = TUD->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000345 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000346 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
347 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000348 }
Ted Kremenek4d53a532009-02-13 00:51:30 +0000349
350 // Delete the PathDiagnosticClient here just in case the AnalysisConsumer
351 // object doesn't get released. This will cause any side-effects in the
352 // destructor of the PathDiagnosticClient to get executed.
353 PD.reset();
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000354}
355
Ted Kremenek81922f02009-02-02 20:52:40 +0000356void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000357
358 // Don't run the actions if an error has occured with parsing the file.
359 if (Diags.hasErrorOccurred())
360 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000361
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000362 // Don't run the actions on declarations in header files unless
363 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000364 if (!Opts.AnalyzeAll &&
365 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000366 return;
367
368 // Create an AnalysisManager that will manage the state for analyzing
369 // this method/function.
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000370 AnalysisManager mgr(*this, D, Opts.AnalyzerDisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000371
372 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000373 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000374 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000375}
376
377//===----------------------------------------------------------------------===//
378// Analyses
379//===----------------------------------------------------------------------===//
380
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000381static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000382 if (LiveVariables* L = mgr.getLiveVariables()) {
383 BugReporter BR(mgr);
384 CheckDeadStores(*L, BR);
385 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000386}
387
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000388static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000389 if (CFG* c = mgr.getCFG())
390 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000391}
392
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000393
Ted Kremenek78d46242008-07-22 16:21:24 +0000394static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
395 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000396
Ted Kremenek7032f462008-07-03 05:26:14 +0000397
Ted Kremenekbc46f342008-07-02 16:35:50 +0000398 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000399
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000400 // Display progress.
401 mgr.DisplayFunction();
402
Ted Kremenek7032f462008-07-03 05:26:14 +0000403 // Construct the analysis engine.
404 LiveVariables* L = mgr.getLiveVariables();
405 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000406
Ted Kremenekcf118d42009-02-04 23:49:09 +0000407 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000408 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu22438a82008-11-27 01:55:08 +0000409 mgr.getStoreManagerCreator(),
410 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000411
Ted Kremenekbc46f342008-07-02 16:35:50 +0000412 Eng.setTransferFunctions(tf);
413
Ted Kremenek78d46242008-07-22 16:21:24 +0000414 if (StandardWarnings) {
415 Eng.RegisterInternalChecks();
416 RegisterAppleChecks(Eng);
417 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000418
419 // Set the graph auditor.
420 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
421 if (mgr.shouldVisualizeUbigraph()) {
422 Auditor.reset(CreateUbiViz());
423 ExplodedNodeImpl::SetAuditor(Auditor.get());
424 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000425
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000426 // Execute the worklist algorithm.
427 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000428
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000429 // Release the auditor (if any) so that it doesn't monitor the graph
430 // created BugReporter.
431 ExplodedNodeImpl::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000432
Ted Kremenek34d77342008-07-02 16:49:11 +0000433 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000434 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000435 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenek3df64212009-03-11 01:42:29 +0000436
437 // Display warnings.
438 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000439}
440
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000441static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000442 bool StandardWarnings) {
443
Ted Kremenekbc46f342008-07-02 16:35:50 +0000444 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
445 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000446 mgr.getLangOptions());
447
Ted Kremenek78d46242008-07-22 16:21:24 +0000448 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000449}
450
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000451static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000452
453 switch (mgr.getLangOptions().getGCMode()) {
454 default:
455 assert (false && "Invalid GC mode.");
456 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000457 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000458 break;
459
460 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000461 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000462 break;
463
464 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000465 ActionCheckerCFRefAux(mgr, false, true);
466 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000467 break;
468 }
469}
470
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000471static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000472 if (LiveVariables* L = mgr.getLiveVariables()) {
473 mgr.DisplayFunction();
474 L->dumpBlockLiveness(mgr.getSourceManager());
475 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000476}
477
Ted Kremenek902141f2008-07-02 18:23:21 +0000478static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000479 if (CFG* c = mgr.getCFG()) {
480 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000481 LangOptions LO; // FIXME!
482 c->dump(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000483 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000484}
485
486static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000487 if (CFG* c = mgr.getCFG()) {
488 mgr.DisplayFunction();
Chris Lattnere4f21422009-06-30 01:26:17 +0000489 LangOptions LO; // FIXME!
490 c->viewCFG(LO);
Ted Kremenek7032f462008-07-03 05:26:14 +0000491 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000492}
493
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000494static void ActionSecuritySyntacticChecks(AnalysisManager &mgr) {
495 BugReporter BR(mgr);
496 CheckSecuritySyntaxOnly(mgr.getCodeDecl(), BR);
497}
498
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000499static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000500 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
501 return;
502
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000503 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000504
505 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
506 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000507}
508
Ted Kremenek395aaf22008-07-23 00:45:26 +0000509static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
510 BugReporter BR(mgr);
511 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
512}
513
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000514static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000515 BugReporter BR(mgr);
516
517 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
518 BR);
519}
520
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000521//===----------------------------------------------------------------------===//
522// AnalysisConsumer creation.
523//===----------------------------------------------------------------------===//
524
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000525ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000526 PreprocessorFactory* ppf,
527 const LangOptions& lopts,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000528 const std::string& OutDir,
529 const AnalyzerOptions& Opts) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000530
531 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000532 lopts, OutDir,
533 Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000534
Eli Friedmane71b85f2009-05-19 10:18:02 +0000535 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
536 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000537#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000538 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000539 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000540 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000541#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000542 default: break;
543 }
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000544
545 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
546 diags.setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000547
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000548 return C.take();
549}
550
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000551//===----------------------------------------------------------------------===//
552// Ubigraph Visualization. FIXME: Move to separate file.
553//===----------------------------------------------------------------------===//
554
555namespace {
556
557class UbigraphViz : public ExplodedNodeImpl::Auditor {
558 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000559 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000560 unsigned Cntr;
561
562 typedef llvm::DenseMap<void*,unsigned> VMap;
563 VMap M;
564
565public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000566 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000567 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000568
569 ~UbigraphViz();
570
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000571 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
572};
573
574} // end anonymous namespace
575
576static ExplodedNodeImpl::Auditor* CreateUbiViz() {
577 std::string ErrMsg;
578
Ted Kremenek710ad932008-08-28 03:54:51 +0000579 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000580 if (!ErrMsg.empty())
581 return 0;
582
Ted Kremenek710ad932008-08-28 03:54:51 +0000583 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000584 Filename.appendComponent("llvm_ubi");
585 Filename.makeUnique(true,&ErrMsg);
586
587 if (!ErrMsg.empty())
588 return 0;
589
590 llvm::cerr << "Writing '" << Filename << "'.\n";
591
592 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
593 std::string filename = Filename.toString();
Dan Gohman92db2842009-07-15 17:32:18 +0000594 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false,
595 /*Force=*/true, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000596
597 if (!ErrMsg.empty())
598 return 0;
599
Ted Kremenek710ad932008-08-28 03:54:51 +0000600 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000601}
602
603void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000604
605 assert (Src != Dst && "Self-edges are not allowed.");
606
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000607 // Lookup the Src. If it is a new node, it's a root.
608 VMap::iterator SrcI= M.find(Src);
609 unsigned SrcID;
610
611 if (SrcI == M.end()) {
612 M[Src] = SrcID = Cntr++;
613 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
614 }
615 else
616 SrcID = SrcI->second;
617
618 // Lookup the Dst.
619 VMap::iterator DstI= M.find(Dst);
620 unsigned DstID;
621
622 if (DstI == M.end()) {
623 M[Dst] = DstID = Cntr++;
624 *Out << "('vertex', " << DstID << ")\n";
625 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000626 else {
627 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000628 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000629 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
630 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000631
632 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000633 *Out << "('edge', " << SrcID << ", " << DstID
634 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000635}
636
Ted Kremenek56b98712008-08-28 05:02:09 +0000637UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
638 llvm::sys::Path& filename)
639 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
640
641 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
642 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
643 " ('size', '1.5'))\n";
644}
645
Ted Kremenek710ad932008-08-28 03:54:51 +0000646UbigraphViz::~UbigraphViz() {
647 Out.reset(0);
648 llvm::cerr << "Running 'ubiviz' program... ";
649 std::string ErrMsg;
650 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
651 std::vector<const char*> args;
652 args.push_back(Ubiviz.c_str());
653 args.push_back(Filename.c_str());
654 args.push_back(0);
655
656 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
657 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
658 }
659
660 // Delete the directory.
661 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000662}