blob: 2363ead9c19767810cd747f79f48a84a203202af [file] [log] [blame]
Ted Kremenek81ea7992008-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 Friedman7efefa52009-05-19 21:10:40 +000014#include "clang/Frontend/AnalysisConsumer.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000015#include "clang/Frontend/PathDiagnosticClients.h"
16#include "clang/Frontend/ManagerRegistry.h"
Ted Kremenek81ea7992008-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 Kremenek81ea7992008-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 Kremenekba1c7ed2008-07-02 21:24:01 +000028#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek81ea7992008-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 Xue828d1c2008-12-22 01:52:37 +000033#include "llvm/Support/CommandLine.h"
Ted Kremeneked80d002008-07-02 16:49:11 +000034#include "llvm/Support/Streams.h"
Ted Kremenekcf262252008-08-27 22:31:43 +000035#include "llvm/Support/raw_ostream.h"
36#include "llvm/System/Path.h"
Ted Kremenek3926ce72008-08-28 03:54:51 +000037#include "llvm/System/Program.h"
Ted Kremenek724133b2008-07-03 04:29:21 +000038
Ted Kremenek81ea7992008-07-02 00:03:09 +000039using namespace clang;
40
Ted Kremenekcf262252008-08-27 22:31:43 +000041static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xue828d1c2008-12-22 01:52:37 +000042
Ted Kremenek99ec8f42009-02-17 04:27:41 +000043//===----------------------------------------------------------------------===//
Ted Kremenek81ea7992008-07-02 00:03:09 +000044// Basic type definitions.
45//===----------------------------------------------------------------------===//
46
Ted Kremenek8ce61b32008-07-14 23:41:13 +000047namespace {
Ted Kremenek81ea7992008-07-02 00:03:09 +000048 class AnalysisManager;
Ted Kremenek8ce61b32008-07-14 23:41:13 +000049 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenek81ea7992008-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 Kremenek724133b2008-07-03 04:29:21 +000059 typedef std::vector<CodeAction> Actions;
Ted Kremenek81ea7992008-07-02 00:03:09 +000060 Actions FunctionActions;
61 Actions ObjCMethodActions;
Ted Kremenek724133b2008-07-03 04:29:21 +000062 Actions ObjCImplementationActions;
Ted Kremenekcf181702008-11-07 02:09:25 +000063 Actions TranslationUnitActions;
Ted Kremenek81ea7992008-07-02 00:03:09 +000064
65 public:
Ted Kremenekc3803992008-10-24 01:04:59 +000066 const LangOptions& LOpts;
Ted Kremenek81ea7992008-07-02 00:03:09 +000067 Diagnostic &Diags;
68 ASTContext* Ctx;
69 Preprocessor* PP;
70 PreprocessorFactory* PPF;
Ted Kremenek99ec8f42009-02-17 04:27:41 +000071 const std::string OutDir;
Eli Friedman78a57622009-05-19 10:18:02 +000072 AnalyzerOptions Opts;
Ted Kremenek81ea7992008-07-02 00:03:09 +000073 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenek81ea7992008-07-02 00:03:09 +000074
75 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
76 PreprocessorFactory* ppf,
77 const LangOptions& lopts,
Eli Friedman78a57622009-05-19 10:18:02 +000078 const std::string& outdir,
79 const AnalyzerOptions& opts)
Ted Kremenek99ec8f42009-02-17 04:27:41 +000080 : LOpts(lopts), Diags(diags),
Ted Kremenek81ea7992008-07-02 00:03:09 +000081 Ctx(0), PP(pp), PPF(ppf),
Eli Friedman78a57622009-05-19 10:18:02 +000082 OutDir(outdir), Opts(opts) {}
Ted Kremenek81ea7992008-07-02 00:03:09 +000083
84 void addCodeAction(CodeAction action) {
Ted Kremenek724133b2008-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 Kremenek81ea7992008-07-02 00:03:09 +000091 }
92
Ted Kremenekcf181702008-11-07 02:09:25 +000093 void addTranslationUnitAction(CodeAction action) {
94 TranslationUnitActions.push_back(action);
95 }
96
Ted Kremenek81ea7992008-07-02 00:03:09 +000097 virtual void Initialize(ASTContext &Context) {
98 Ctx = &Context;
99 }
100
Chris Lattnera17991f2009-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 Lattner2a594d02009-03-28 04:11:33 +0000107 virtual void HandleTranslationUnit(ASTContext &C);
Ted Kremenek724133b2008-07-03 04:29:21 +0000108
Ted Kremenekb8da3452009-02-02 20:52:40 +0000109 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000110 };
111
112
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000113 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenek959b72b2008-11-05 19:05:06 +0000114 Decl* D; Stmt* Body;
Ted Kremenek959b72b2008-11-05 19:05:06 +0000115
116 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
117
Ted Kremenek81ea7992008-07-02 00:03:09 +0000118 AnalysisConsumer& C;
Ted Kremeneked80d002008-07-02 16:49:11 +0000119 bool DisplayedFunction;
Ted Kremenek81ea7992008-07-02 00:03:09 +0000120
121 llvm::OwningPtr<CFG> cfg;
122 llvm::OwningPtr<LiveVariables> liveness;
123 llvm::OwningPtr<ParentMap> PM;
124
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000125 // Configurable components creators.
126 StoreManagerCreator CreateStoreMgr;
127 ConstraintManagerCreator CreateConstraintMgr;
128
Ted Kremenek81ea7992008-07-02 00:03:09 +0000129 public:
Ted Kremenek60feb282009-01-23 20:52:26 +0000130 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
Chris Lattner53934e72009-03-28 04:05:05 +0000131 : D(d), Body(b), AScope(ScopeDecl), C(c),
Ted Kremenek60feb282009-01-23 20:52:26 +0000132 DisplayedFunction(!displayProgress) {
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000133 setManagerCreators();
134 }
Ted Kremenek81ea7992008-07-02 00:03:09 +0000135
Chris Lattner53934e72009-03-28 04:05:05 +0000136 AnalysisManager(AnalysisConsumer& c, bool displayProgress)
137 : D(0), Body(0), AScope(ScopeTU), C(c),
Ted Kremenek60feb282009-01-23 20:52:26 +0000138 DisplayedFunction(!displayProgress) {
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000139 setManagerCreators();
140 }
Ted Kremenek81ea7992008-07-02 00:03:09 +0000141
Ted Kremenek959b72b2008-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 Xu0e77b732008-11-27 01:55:08 +0000152 StoreManagerCreator getStoreManagerCreator() {
153 return CreateStoreMgr;
Ted Kremenekc3803992008-10-24 01:04:59 +0000154 };
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000155
156 ConstraintManagerCreator getConstraintManagerCreator() {
157 return CreateConstraintMgr;
158 }
Ted Kremenekc3803992008-10-24 01:04:59 +0000159
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000160 virtual CFG* getCFG() {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000161 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000162 return cfg.get();
Ted Kremenek81ea7992008-07-02 00:03:09 +0000163 }
164
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000165 virtual ParentMap& getParentMap() {
Ted Kremenekeb9627b2008-07-02 23:16:33 +0000166 if (!PM)
167 PM.reset(new ParentMap(getBody()));
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000168 return *PM.get();
Ted Kremenek81ea7992008-07-02 00:03:09 +0000169 }
170
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000171 virtual ASTContext& getContext() {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000172 return *C.Ctx;
173 }
174
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000175 virtual SourceManager& getSourceManager() {
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000176 return getContext().getSourceManager();
177 }
178
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000179 virtual Diagnostic& getDiagnostic() {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000180 return C.Diags;
181 }
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000182
183 const LangOptions& getLangOptions() const {
184 return C.LOpts;
185 }
186
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000187 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000188 if (C.PD.get() == 0 && !C.OutDir.empty()) {
Eli Friedman78a57622009-05-19 10:18:02 +0000189 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremeneka3f825e2008-11-03 23:18:07 +0000190 default:
Ted Kremenekdd485cb2009-01-23 20:06:20 +0000191#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000192case PD_##NAME: C.PD.reset(CREATEFN(C.OutDir, C.PP, C.PPF)); break;
Eli Friedman8c034072009-05-19 21:16:18 +0000193#include "clang/Frontend/Analyses.def"
Ted Kremeneka3f825e2008-11-03 23:18:07 +0000194 }
195 }
Ted Kremenekeb9627b2008-07-02 23:16:33 +0000196 return C.PD.get();
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000197 }
Ted Kremenek81ea7992008-07-02 00:03:09 +0000198
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000199 virtual LiveVariables* getLiveVariables() {
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000200 if (!liveness) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000201 CFG* c = getCFG();
202 if (!c) return 0;
203
Ted Kremenek04061812008-12-09 00:17:51 +0000204 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000205 liveness->runOnCFG(*c);
206 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000207 }
Ted Kremenekba1c7ed2008-07-02 21:24:01 +0000208
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000209 return liveness.get();
Ted Kremenek81ea7992008-07-02 00:03:09 +0000210 }
Ted Kremeneked80d002008-07-02 16:49:11 +0000211
Eli Friedman78a57622009-05-19 10:18:02 +0000212 bool shouldVisualizeGraphviz() const { return C.Opts.VisualizeEGDot; }
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000213
Eli Friedman78a57622009-05-19 10:18:02 +0000214 bool shouldVisualizeUbigraph() const { return C.Opts.VisualizeEGUbi; }
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000215
216 bool shouldVisualize() const {
Eli Friedman78a57622009-05-19 10:18:02 +0000217 return C.Opts.VisualizeEGDot || C.Opts.VisualizeEGUbi;
Ted Kremenekcf262252008-08-27 22:31:43 +0000218 }
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000219
Eli Friedman78a57622009-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 Kremenek99ec8f42009-02-17 04:27:41 +0000225
Ted Kremeneked80d002008-07-02 16:49:11 +0000226 void DisplayFunction() {
227
228 if (DisplayedFunction)
229 return;
230
231 DisplayedFunction = true;
232
Chris Lattner836774b2009-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 Kremenek7c142142008-11-20 16:14:48 +0000238 llvm::cerr << "ANALYZE: "
Chris Lattner836774b2009-01-27 07:57:44 +0000239 << SM.getPresumedLoc(ND->getLocation()).getFilename()
240 << ' ' << ND->getNameAsString() << '\n';
Ted Kremeneked80d002008-07-02 16:49:11 +0000241 }
242 }
Zhongxing Xu0e77b732008-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 Friedman78a57622009-05-19 10:18:02 +0000253 switch (C.Opts.AnalysisStoreOpt) {
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000254 default:
255 assert(0 && "Unknown store manager.");
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000256#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
257 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Eli Friedman8c034072009-05-19 21:16:18 +0000258#include "clang/Frontend/Analyses.def"
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000259 }
260 }
261
262 if (ManagerRegistry::ConstraintMgrCreator != 0)
263 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000264 else {
Eli Friedman78a57622009-05-19 10:18:02 +0000265 switch (C.Opts.AnalysisConstraintsOpt) {
Ted Kremenek99ec8f42009-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 Friedman8c034072009-05-19 21:16:18 +0000270#include "clang/Frontend/Analyses.def"
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000271 }
272 }
273
Ted Kremenekdd485cb2009-01-23 20:06:20 +0000274
275 // Some DiagnosticClients should be created all the time instead of
276 // lazily. Create those now.
Eli Friedman78a57622009-05-19 10:18:02 +0000277 switch (C.Opts.AnalysisDiagOpt) {
Ted Kremenekdd485cb2009-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 Friedman8c034072009-05-19 21:16:18 +0000281#include "clang/Frontend/Analyses.def"
Ted Kremenekdd485cb2009-01-23 20:06:20 +0000282 }
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000283 }
284
Ted Kremenek81ea7992008-07-02 00:03:09 +0000285 };
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000286
Ted Kremenek81ea7992008-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 Lattnera17991f2009-03-29 16:50:03 +0000301void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenek81ea7992008-07-02 00:03:09 +0000302 switch (D->getKind()) {
303 case Decl::Function: {
304 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000305
Eli Friedman78a57622009-05-19 10:18:02 +0000306 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
307 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000308 break;
309
Douglas Gregore3241e92009-04-18 00:02:19 +0000310 Stmt* Body = FD->getBody(*Ctx);
Ted Kremenek81ea7992008-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 Kremenekd899b0f2008-07-02 18:11:29 +0000317
Eli Friedman78a57622009-05-19 10:18:02 +0000318 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
319 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000320 return;
321
Ted Kremenek81ea7992008-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 Lattner2a594d02009-03-28 04:11:33 +0000332void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenek724133b2008-07-03 04:29:21 +0000333
Ted Kremenekcf181702008-11-07 02:09:25 +0000334 if(!TranslationUnitActions.empty()) {
Eli Friedman78a57622009-05-19 10:18:02 +0000335 AnalysisManager mgr(*this, Opts.AnalyzerDisplayProgress);
Ted Kremenekcf181702008-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 Lattner137e1312009-03-28 03:29:40 +0000341 if (!ObjCImplementationActions.empty()) {
Chris Lattner2a594d02009-03-28 04:11:33 +0000342 TranslationUnitDecl *TUD = C.getTranslationUnitDecl();
Chris Lattner137e1312009-03-28 03:29:40 +0000343
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000344 for (DeclContext::decl_iterator I = TUD->decls_begin(C),
345 E = TUD->decls_end(C);
Chris Lattner137e1312009-03-28 03:29:40 +0000346 I != E; ++I)
Ted Kremenek14733c02009-02-13 00:51:30 +0000347 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
348 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattner137e1312009-03-28 03:29:40 +0000349 }
Ted Kremenek14733c02009-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 Kremenek724133b2008-07-03 04:29:21 +0000355}
356
Ted Kremenekb8da3452009-02-02 20:52:40 +0000357void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions& actions) {
Ted Kremenek81ea7992008-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 Kremenekb8da3452009-02-02 20:52:40 +0000362
Ted Kremenek81ea7992008-07-02 00:03:09 +0000363 // Don't run the actions on declarations in header files unless
364 // otherwise specified.
Eli Friedman78a57622009-05-19 10:18:02 +0000365 if (!Opts.AnalyzeAll &&
366 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Ted Kremenek81ea7992008-07-02 00:03:09 +0000367 return;
368
369 // Create an AnalysisManager that will manage the state for analyzing
370 // this method/function.
Eli Friedman78a57622009-05-19 10:18:02 +0000371 AnalysisManager mgr(*this, D, Body, Opts.AnalyzerDisplayProgress);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000372
373 // Dispatch on the actions.
Zhongxing Xua89b4642008-10-30 05:03:28 +0000374 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenek724133b2008-07-03 04:29:21 +0000375 (*I)(mgr);
Ted Kremenek81ea7992008-07-02 00:03:09 +0000376}
377
378//===----------------------------------------------------------------------===//
379// Analyses
380//===----------------------------------------------------------------------===//
381
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000382static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000383 if (LiveVariables* L = mgr.getLiveVariables()) {
384 BugReporter BR(mgr);
385 CheckDeadStores(*L, BR);
386 }
Ted Kremenek81ea7992008-07-02 00:03:09 +0000387}
388
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000389static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000390 if (CFG* c = mgr.getCFG())
391 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenek81ea7992008-07-02 00:03:09 +0000392}
393
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000394
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000395static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
396 bool StandardWarnings = true) {
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000397
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000398
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000399 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000400
Ted Kremenekbdf6f612008-11-24 20:53:32 +0000401 // Display progress.
402 mgr.DisplayFunction();
403
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000404 // Construct the analysis engine.
405 LiveVariables* L = mgr.getLiveVariables();
406 if (!L) return;
Ted Kremenekbdf6f612008-11-24 20:53:32 +0000407
Ted Kremenekbf6babf2009-02-04 23:49:09 +0000408 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L, mgr,
Eli Friedman78a57622009-05-19 10:18:02 +0000409 mgr.shouldPurgeDead(), mgr.shouldEagerlyAssume(),
Zhongxing Xu0e77b732008-11-27 01:55:08 +0000410 mgr.getStoreManagerCreator(),
411 mgr.getConstraintManagerCreator());
Ted Kremenekc3803992008-10-24 01:04:59 +0000412
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000413 Eng.setTransferFunctions(tf);
414
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000415 if (StandardWarnings) {
416 Eng.RegisterInternalChecks();
417 RegisterAppleChecks(Eng);
418 }
Ted Kremenekcf262252008-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 Kremenek9f20c7c2008-07-22 16:21:24 +0000426
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000427 // Execute the worklist algorithm.
428 Eng.ExecuteWorkList();
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000429
Ted Kremenekcf262252008-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 Kremeneka0de7a52009-03-11 01:42:29 +0000433
Ted Kremeneked80d002008-07-02 16:49:11 +0000434 // Visualize the exploded graph.
Ted Kremenekcf262252008-08-27 22:31:43 +0000435 if (mgr.shouldVisualizeGraphviz())
Ted Kremeneked80d002008-07-02 16:49:11 +0000436 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremeneka0de7a52009-03-11 01:42:29 +0000437
438 // Display warnings.
439 Eng.getBugReporter().FlushReports();
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000440}
441
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000442static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000443 bool StandardWarnings) {
444
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000445 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
446 GCEnabled,
Ted Kremenekc8a5fd42008-07-02 16:35:50 +0000447 mgr.getLangOptions());
448
Ted Kremenek9f20c7c2008-07-22 16:21:24 +0000449 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000450}
451
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000452static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000453
454 switch (mgr.getLangOptions().getGCMode()) {
455 default:
456 assert (false && "Invalid GC mode.");
457 case LangOptions::NonGC:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000458 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000459 break;
460
461 case LangOptions::GCOnly:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000462 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000463 break;
464
465 case LangOptions::HybridGC:
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000466 ActionCheckerCFRefAux(mgr, false, true);
467 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenek1c6cd212008-07-02 00:44:58 +0000468 break;
469 }
470}
471
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000472static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000473 if (LiveVariables* L = mgr.getLiveVariables()) {
474 mgr.DisplayFunction();
475 L->dumpBlockLiveness(mgr.getSourceManager());
476 }
Ted Kremenekd899b0f2008-07-02 18:11:29 +0000477}
478
Ted Kremeneke972d852008-07-02 18:23:21 +0000479static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000480 if (CFG* c = mgr.getCFG()) {
481 mgr.DisplayFunction();
Chris Lattner7099c782009-06-30 01:26:17 +0000482 LangOptions LO; // FIXME!
483 c->dump(LO);
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000484 }
Ted Kremeneke972d852008-07-02 18:23:21 +0000485}
486
487static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000488 if (CFG* c = mgr.getCFG()) {
489 mgr.DisplayFunction();
Chris Lattner7099c782009-06-30 01:26:17 +0000490 LangOptions LO; // FIXME!
491 c->viewCFG(LO);
Ted Kremenekc2a9eae2008-07-03 05:26:14 +0000492 }
Ted Kremeneke972d852008-07-02 18:23:21 +0000493}
494
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000495static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek991de6f2008-08-04 17:14:10 +0000496 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
497 return;
498
Ted Kremenek724133b2008-07-03 04:29:21 +0000499 BugReporter BR(mgr);
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000500
501 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
502 mgr.getLangOptions(), BR);
Ted Kremenek724133b2008-07-03 04:29:21 +0000503}
504
Ted Kremenek69ea7852008-07-23 00:45:26 +0000505static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
506 BugReporter BR(mgr);
507 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
508}
509
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000510static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenekd072b892008-07-11 22:40:47 +0000511 BugReporter BR(mgr);
512
513 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
514 BR);
515}
516
Ted Kremenek81ea7992008-07-02 00:03:09 +0000517//===----------------------------------------------------------------------===//
518// AnalysisConsumer creation.
519//===----------------------------------------------------------------------===//
520
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000521ASTConsumer* clang::CreateAnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
Ted Kremenek81ea7992008-07-02 00:03:09 +0000522 PreprocessorFactory* ppf,
523 const LangOptions& lopts,
Eli Friedman78a57622009-05-19 10:18:02 +0000524 const std::string& OutDir,
525 const AnalyzerOptions& Opts) {
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000526
527 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(diags, pp, ppf,
Eli Friedman78a57622009-05-19 10:18:02 +0000528 lopts, OutDir,
529 Opts));
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000530
Eli Friedman78a57622009-05-19 10:18:02 +0000531 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
532 switch (Opts.AnalysisList[i]) {
Ted Kremenekfbda0ef2008-07-15 00:46:02 +0000533#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenek8ce61b32008-07-14 23:41:13 +0000534 case NAME:\
Ted Kremenekfbda0ef2008-07-15 00:46:02 +0000535 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenek81ea7992008-07-02 00:03:09 +0000536 break;
Eli Friedman8c034072009-05-19 21:16:18 +0000537#include "clang/Frontend/Analyses.def"
Ted Kremenek81ea7992008-07-02 00:03:09 +0000538 default: break;
539 }
Ted Kremenekeb1dfa62009-05-07 19:02:53 +0000540
541 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
542 diags.setWarningsAsErrors(false);
Ted Kremenek99ec8f42009-02-17 04:27:41 +0000543
Ted Kremenek81ea7992008-07-02 00:03:09 +0000544 return C.take();
545}
546
Ted Kremenekcf262252008-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 Kremenek3926ce72008-08-28 03:54:51 +0000555 llvm::sys::Path Dir, Filename;
Ted Kremenekcf262252008-08-27 22:31:43 +0000556 unsigned Cntr;
557
558 typedef llvm::DenseMap<void*,unsigned> VMap;
559 VMap M;
560
561public:
Ted Kremenek3926ce72008-08-28 03:54:51 +0000562 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek2ea9cfe2008-08-28 05:02:09 +0000563 llvm::sys::Path& filename);
Ted Kremenek3926ce72008-08-28 03:54:51 +0000564
565 ~UbigraphViz();
566
Ted Kremenekcf262252008-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 Kremenek3926ce72008-08-28 03:54:51 +0000575 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekcf262252008-08-27 22:31:43 +0000576 if (!ErrMsg.empty())
577 return 0;
578
Ted Kremenek3926ce72008-08-28 03:54:51 +0000579 llvm::sys::Path Filename = Dir;
Ted Kremenekcf262252008-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 Dunbar8fc9ba62008-11-13 05:09:21 +0000590 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekcf262252008-08-27 22:31:43 +0000591
592 if (!ErrMsg.empty())
593 return 0;
594
Ted Kremenek3926ce72008-08-28 03:54:51 +0000595 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekcf262252008-08-27 22:31:43 +0000596}
597
598void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek33a9fdf2008-08-28 18:34:41 +0000599
600 assert (Src != Dst && "Self-edges are not allowed.");
601
Ted Kremenekcf262252008-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 Kremenek2ea9cfe2008-08-28 05:02:09 +0000621 else {
622 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekcf262252008-08-27 22:31:43 +0000623 DstID = DstI->second;
Ted Kremenek2ea9cfe2008-08-28 05:02:09 +0000624 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
625 }
Ted Kremenekcf262252008-08-27 22:31:43 +0000626
627 // Add the edge.
Ted Kremenek9d5b7252008-08-27 22:46:55 +0000628 *Out << "('edge', " << SrcID << ", " << DstID
629 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekcf262252008-08-27 22:31:43 +0000630}
631
Ted Kremenek2ea9cfe2008-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 Kremenek3926ce72008-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 Dunbar7d630a62008-08-29 03:45:59 +0000657}