blob: 4dab9c7b4bc10741ea8a9415dc5076e716a6a846 [file] [log] [blame]
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// "Meta" ASTConsumer for running different source analyses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ASTConsumers.h"
Ted Kremenekad99dbf2008-11-03 22:31:48 +000015#include "clang/Driver/PathDiagnosticClients.h"
Zhongxing Xu22438a82008-11-27 01:55:08 +000016#include "clang/Driver/ManagerRegistry.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000017#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
20#include "llvm/Support/Compiler.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000021#include "llvm/ADT/OwningPtr.h"
22#include "clang/AST/CFG.h"
23#include "clang/Analysis/Analyses/LiveVariables.h"
24#include "clang/Analysis/PathDiagnostic.h"
25#include "clang/Basic/SourceManager.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/AST/ParentMap.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000028#include "clang/AST/TranslationUnit.h"
Ted Kremenekc0959972008-07-02 21:24:01 +000029#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000030#include "clang/Analysis/Analyses/LiveVariables.h"
31#include "clang/Analysis/LocalCheckers.h"
32#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
33#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Zhongxing Xuff944a82008-12-22 01:52:37 +000034#include "llvm/Support/CommandLine.h"
Ted Kremenek34d77342008-07-02 16:49:11 +000035#include "llvm/Support/Streams.h"
Ted Kremenekf8ce6992008-08-27 22:31:43 +000036#include "llvm/Support/raw_ostream.h"
37#include "llvm/System/Path.h"
Ted Kremenek710ad932008-08-28 03:54:51 +000038#include "llvm/System/Program.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000039#include <vector>
40
Ted Kremenekf4381fd2008-07-02 00:03:09 +000041using namespace clang;
42
Ted Kremenekf8ce6992008-08-27 22:31:43 +000043static ExplodedNodeImpl::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000044
45// Analyzer options.
46static llvm::cl::opt<bool>
47PurgeDead("analyzer-purge-dead",
48 llvm::cl::init(true),
49 llvm::cl::desc("Remove dead symbols, bindings, and constraints before"
50 " processing a statement."));
Ted Kremenekf4381fd2008-07-02 00:03:09 +000051
52//===----------------------------------------------------------------------===//
53// Basic type definitions.
54//===----------------------------------------------------------------------===//
55
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000056namespace {
Ted Kremenekf4381fd2008-07-02 00:03:09 +000057 class AnalysisManager;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +000058 typedef void (*CodeAction)(AnalysisManager& Mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +000059} // end anonymous namespace
60
61//===----------------------------------------------------------------------===//
62// AnalysisConsumer declaration.
63//===----------------------------------------------------------------------===//
64
65namespace {
66
67 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000068 typedef std::vector<CodeAction> Actions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000069 Actions FunctionActions;
70 Actions ObjCMethodActions;
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000071 Actions ObjCImplementationActions;
Ted Kremenekdaac6342008-11-07 02:09:25 +000072 Actions TranslationUnitActions;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000073
74 public:
Ted Kremenekf8ce6992008-08-27 22:31:43 +000075 const bool VisGraphviz;
76 const bool VisUbigraph;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000077 const bool TrimGraph;
Ted Kremenek95c7b002008-10-24 01:04:59 +000078 const LangOptions& LOpts;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000079 Diagnostic &Diags;
80 ASTContext* Ctx;
81 Preprocessor* PP;
82 PreprocessorFactory* PPF;
83 const std::string HTMLDir;
84 const std::string FName;
85 llvm::OwningPtr<PathDiagnosticClient> PD;
86 bool AnalyzeAll;
Ted Kremenek95c7b002008-10-24 01:04:59 +000087 AnalysisStores SM;
Ted Kremenek4fc82c82008-11-03 23:18:07 +000088 AnalysisDiagClients DC;
Ted Kremenek491918e2009-01-23 20:52:26 +000089 const bool DisplayProgress;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000090
91 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
92 PreprocessorFactory* ppf,
93 const LangOptions& lopts,
94 const std::string& fname,
95 const std::string& htmldir,
Ted Kremenek4fc82c82008-11-03 23:18:07 +000096 AnalysisStores sm, AnalysisDiagClients dc,
Ted Kremenek491918e2009-01-23 20:52:26 +000097 bool visgraphviz, bool visubi, bool trim, bool analyzeAll,
98 bool displayProgress)
Ted Kremenekf8ce6992008-08-27 22:31:43 +000099 : VisGraphviz(visgraphviz), VisUbigraph(visubi), TrimGraph(trim),
100 LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000101 Ctx(0), PP(pp), PPF(ppf),
102 HTMLDir(htmldir),
103 FName(fname),
Ted Kremenek491918e2009-01-23 20:52:26 +0000104 AnalyzeAll(analyzeAll), SM(sm), DC(dc),
105 DisplayProgress(displayProgress) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000106
107 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000108 FunctionActions.push_back(action);
109 ObjCMethodActions.push_back(action);
110 }
111
112 void addObjCImplementationAction(CodeAction action) {
113 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000114 }
115
Ted Kremenekdaac6342008-11-07 02:09:25 +0000116 void addTranslationUnitAction(CodeAction action) {
117 TranslationUnitActions.push_back(action);
118 }
119
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000120 virtual void Initialize(ASTContext &Context) {
121 Ctx = &Context;
122 }
123
124 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000125 virtual void HandleTranslationUnit(TranslationUnit &TU);
126
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000127 void HandleCode(Decl* D, Stmt* Body, Actions actions);
128 };
129
130
Ted Kremenekc0959972008-07-02 21:24:01 +0000131 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000132 Decl* D; Stmt* Body;
133 TranslationUnit* TU;
134
135 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
136
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000137 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000138 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000139
140 llvm::OwningPtr<CFG> cfg;
141 llvm::OwningPtr<LiveVariables> liveness;
142 llvm::OwningPtr<ParentMap> PM;
143
Zhongxing Xu22438a82008-11-27 01:55:08 +0000144 // Configurable components creators.
145 StoreManagerCreator CreateStoreMgr;
146 ConstraintManagerCreator CreateConstraintMgr;
147
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000148 public:
Ted Kremenek491918e2009-01-23 20:52:26 +0000149 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b, bool displayProgress)
150 : D(d), Body(b), TU(0), AScope(ScopeDecl), C(c),
151 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000152 setManagerCreators();
153 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000154
Ted Kremenek491918e2009-01-23 20:52:26 +0000155 AnalysisManager(AnalysisConsumer& c, TranslationUnit* tu,
156 bool displayProgress)
157 : D(0), Body(0), TU(tu), AScope(ScopeTU), C(c),
158 DisplayedFunction(!displayProgress) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000159 setManagerCreators();
160 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000161
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000162 Decl* getCodeDecl() const {
163 assert (AScope == ScopeDecl);
164 return D;
165 }
166
167 Stmt* getBody() const {
168 assert (AScope == ScopeDecl);
169 return Body;
170 }
171
172 TranslationUnit* getTranslationUnit() const {
173 assert (AScope == ScopeTU);
174 return TU;
175 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000176
Zhongxing Xu22438a82008-11-27 01:55:08 +0000177 StoreManagerCreator getStoreManagerCreator() {
178 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000179 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000180
181 ConstraintManagerCreator getConstraintManagerCreator() {
182 return CreateConstraintMgr;
183 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000184
Ted Kremenek7032f462008-07-03 05:26:14 +0000185 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000186 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000187 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000188 }
189
Ted Kremenekc0959972008-07-02 21:24:01 +0000190 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000191 if (!PM)
192 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000193 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000194 }
195
Ted Kremenekc0959972008-07-02 21:24:01 +0000196 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000197 return *C.Ctx;
198 }
199
Ted Kremenekc0959972008-07-02 21:24:01 +0000200 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000201 return getContext().getSourceManager();
202 }
203
Ted Kremenekc0959972008-07-02 21:24:01 +0000204 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000205 return C.Diags;
206 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000207
208 const LangOptions& getLangOptions() const {
209 return C.LOpts;
210 }
211
Ted Kremenekc0959972008-07-02 21:24:01 +0000212 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000213 if (C.PD.get() == 0 && !C.HTMLDir.empty()) {
214 switch (C.DC) {
215 default:
Ted Kremenekc472d792009-01-23 20:06:20 +0000216#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000217case PD_##NAME: C.PD.reset(CREATEFN(C.HTMLDir, C.PP, C.PPF)); break;
218#include "Analyses.def"
219 }
220 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000221 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000222 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000223
Ted Kremenek7032f462008-07-03 05:26:14 +0000224 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000225 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000226 CFG* c = getCFG();
227 if (!c) return 0;
228
Ted Kremenekca9bab02008-12-09 00:17:51 +0000229 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000230 liveness->runOnCFG(*c);
231 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000232 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000233
Ted Kremenek7032f462008-07-03 05:26:14 +0000234 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000235 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000236
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000237 bool shouldVisualizeGraphviz() const {
238 return C.VisGraphviz;
239 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000240
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000241 bool shouldVisualizeUbigraph() const {
242 return C.VisUbigraph;
243 }
244
Ted Kremenek34d77342008-07-02 16:49:11 +0000245 bool shouldVisualize() const {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000246 return C.VisGraphviz || C.VisUbigraph;
Ted Kremenek34d77342008-07-02 16:49:11 +0000247 }
248
249 bool shouldTrimGraph() const {
250 return C.TrimGraph;
251 }
252
253 void DisplayFunction() {
254
255 if (DisplayedFunction)
256 return;
257
258 DisplayedFunction = true;
259
260 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000261 llvm::cerr << "ANALYZE: "
Ted Kremenek34d77342008-07-02 16:49:11 +0000262 << getContext().getSourceManager().getSourceName(FD->getLocation())
263 << ' '
264 << FD->getIdentifier()->getName()
265 << '\n';
266 }
267 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000268 llvm::cerr << "ANALYZE (ObjC Method): "
Ted Kremenek34d77342008-07-02 16:49:11 +0000269 << getContext().getSourceManager().getSourceName(MD->getLocation())
270 << " '"
Chris Lattner077bf5e2008-11-24 03:33:13 +0000271 << MD->getSelector().getAsString() << "'\n";
Ted Kremenek34d77342008-07-02 16:49:11 +0000272 }
273 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000274
275 private:
276 /// Set configurable analyzer components creators. First check if there are
277 /// components registered at runtime. Otherwise fall back to builtin
278 /// components.
279 void setManagerCreators() {
280 if (ManagerRegistry::StoreMgrCreator != 0) {
281 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
282 }
283 else {
284 switch (C.SM) {
285 default:
286 assert(0 && "Unknown store manager.");
287#define ANALYSIS_STORE(NAME, CMDFLAG, DESC) \
288 case NAME##Model: CreateStoreMgr = Create##NAME##Manager; break;
289#include "Analyses.def"
290 }
291 }
292
293 if (ManagerRegistry::ConstraintMgrCreator != 0)
294 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
295 else
296 CreateConstraintMgr = CreateBasicConstraintManager;
Ted Kremenekc472d792009-01-23 20:06:20 +0000297
298 // Some DiagnosticClients should be created all the time instead of
299 // lazily. Create those now.
300 switch (C.DC) {
301 default: break;
302#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE)\
303case PD_##NAME: if (AUTOCREATE) getPathDiagnosticClient(); break;
304#include "Analyses.def"
305 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000306 }
307
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000308 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000309
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000310} // end anonymous namespace
311
312namespace llvm {
313 template <> struct FoldingSetTrait<CodeAction> {
314 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
315 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
316 }
317 };
318}
319
320//===----------------------------------------------------------------------===//
321// AnalysisConsumer implementation.
322//===----------------------------------------------------------------------===//
323
324void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
325 switch (D->getKind()) {
326 case Decl::Function: {
327 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000328
329 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
330 break;
331
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000332 Stmt* Body = FD->getBody();
333 if (Body) HandleCode(FD, Body, FunctionActions);
334 break;
335 }
336
337 case Decl::ObjCMethod: {
338 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000339
Chris Lattner077bf5e2008-11-24 03:33:13 +0000340 if (FName.size() > 0 && FName != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000341 return;
342
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000343 Stmt* Body = MD->getBody();
344 if (Body) HandleCode(MD, Body, ObjCMethodActions);
345 break;
346 }
347
348 default:
349 break;
350 }
351}
352
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000353void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) {
354
Ted Kremenekdaac6342008-11-07 02:09:25 +0000355 if(!TranslationUnitActions.empty()) {
Ted Kremenek491918e2009-01-23 20:52:26 +0000356 AnalysisManager mgr(*this, &TU, DisplayProgress);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000357 for (Actions::iterator I = TranslationUnitActions.begin(),
358 E = TranslationUnitActions.end(); I != E; ++I)
359 (*I)(mgr);
360 }
361
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000362 if (ObjCImplementationActions.empty())
363 return;
364
365 for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) {
366
367 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
368 HandleCode(ID, 0, ObjCImplementationActions);
369 }
370}
371
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000372void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
373
374 // Don't run the actions if an error has occured with parsing the file.
375 if (Diags.hasErrorOccurred())
376 return;
377
378 SourceLocation Loc = D->getLocation();
379
380 // Only run actions on declarations defined in actual source.
381 if (!Loc.isFileID())
382 return;
383
384 // Don't run the actions on declarations in header files unless
385 // otherwise specified.
386 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
387 return;
388
389 // Create an AnalysisManager that will manage the state for analyzing
390 // this method/function.
Ted Kremenek491918e2009-01-23 20:52:26 +0000391 AnalysisManager mgr(*this, D, Body, DisplayProgress);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000392
393 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000394 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000395 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000396}
397
398//===----------------------------------------------------------------------===//
399// Analyses
400//===----------------------------------------------------------------------===//
401
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000402static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000403 if (LiveVariables* L = mgr.getLiveVariables()) {
404 BugReporter BR(mgr);
405 CheckDeadStores(*L, BR);
406 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000407}
408
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000409static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000410 if (CFG* c = mgr.getCFG())
411 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000412}
413
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000414
Ted Kremenek78d46242008-07-22 16:21:24 +0000415static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
416 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000417
Ted Kremenek7032f462008-07-03 05:26:14 +0000418
Ted Kremenekbc46f342008-07-02 16:35:50 +0000419 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000420
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000421 // Display progress.
422 mgr.DisplayFunction();
423
Ted Kremenek7032f462008-07-03 05:26:14 +0000424 // Construct the analysis engine.
425 LiveVariables* L = mgr.getLiveVariables();
426 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000427
Ted Kremenek95c7b002008-10-24 01:04:59 +0000428 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L,
Zhongxing Xuff944a82008-12-22 01:52:37 +0000429 PurgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000430 mgr.getStoreManagerCreator(),
431 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000432
Ted Kremenekbc46f342008-07-02 16:35:50 +0000433 Eng.setTransferFunctions(tf);
434
Ted Kremenek78d46242008-07-22 16:21:24 +0000435 if (StandardWarnings) {
436 Eng.RegisterInternalChecks();
437 RegisterAppleChecks(Eng);
438 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000439
440 // Set the graph auditor.
441 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
442 if (mgr.shouldVisualizeUbigraph()) {
443 Auditor.reset(CreateUbiViz());
444 ExplodedNodeImpl::SetAuditor(Auditor.get());
445 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000446
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000447 // Execute the worklist algorithm.
448 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000449
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000450 // Release the auditor (if any) so that it doesn't monitor the graph
451 // created BugReporter.
452 ExplodedNodeImpl::SetAuditor(0);
453
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000454 // Display warnings.
Ted Kremenekc0959972008-07-02 21:24:01 +0000455 Eng.EmitWarnings(mgr);
Ted Kremenek34d77342008-07-02 16:49:11 +0000456
457 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000458 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000459 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000460}
461
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000462static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000463 bool StandardWarnings) {
464
Ted Kremenekbc46f342008-07-02 16:35:50 +0000465 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
466 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000467 mgr.getLangOptions());
468
Ted Kremenek78d46242008-07-22 16:21:24 +0000469 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000470}
471
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000472static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000473
474 switch (mgr.getLangOptions().getGCMode()) {
475 default:
476 assert (false && "Invalid GC mode.");
477 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000478 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000479 break;
480
481 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000482 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000483 break;
484
485 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000486 ActionCheckerCFRefAux(mgr, false, true);
487 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000488 break;
489 }
490}
491
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000492static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000493 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
494}
495
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000496static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000497 if (LiveVariables* L = mgr.getLiveVariables()) {
498 mgr.DisplayFunction();
499 L->dumpBlockLiveness(mgr.getSourceManager());
500 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000501}
502
Ted Kremenek902141f2008-07-02 18:23:21 +0000503static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000504 if (CFG* c = mgr.getCFG()) {
505 mgr.DisplayFunction();
506 c->dump();
507 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000508}
509
510static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000511 if (CFG* c = mgr.getCFG()) {
512 mgr.DisplayFunction();
513 c->viewCFG();
514 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000515}
516
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000517static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000518 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
519 return;
520
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000521 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000522
523 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
524 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000525}
526
Ted Kremenek395aaf22008-07-23 00:45:26 +0000527static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
528 BugReporter BR(mgr);
529 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
530}
531
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000532static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000533 BugReporter BR(mgr);
534
535 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
536 BR);
537}
538
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000539//===----------------------------------------------------------------------===//
540// AnalysisConsumer creation.
541//===----------------------------------------------------------------------===//
542
543ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000544 AnalysisStores SM,
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000545 AnalysisDiagClients DC,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000546 Diagnostic &diags, Preprocessor* pp,
547 PreprocessorFactory* ppf,
548 const LangOptions& lopts,
549 const std::string& fname,
550 const std::string& htmldir,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000551 bool VisGraphviz, bool VisUbi,
552 bool trim,
Ted Kremenek491918e2009-01-23 20:52:26 +0000553 bool analyzeAll,
554 bool displayProgress) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000555
556 llvm::OwningPtr<AnalysisConsumer>
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000557 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM, DC,
Ted Kremenek491918e2009-01-23 20:52:26 +0000558 VisGraphviz, VisUbi, trim, analyzeAll,
559 displayProgress));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000560
561 for ( ; Beg != End ; ++Beg)
562 switch (*Beg) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000563#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000564 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000565 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000566 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000567#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000568 default: break;
569 }
570
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000571 return C.take();
572}
573
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000574//===----------------------------------------------------------------------===//
575// Ubigraph Visualization. FIXME: Move to separate file.
576//===----------------------------------------------------------------------===//
577
578namespace {
579
580class UbigraphViz : public ExplodedNodeImpl::Auditor {
581 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000582 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000583 unsigned Cntr;
584
585 typedef llvm::DenseMap<void*,unsigned> VMap;
586 VMap M;
587
588public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000589 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000590 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000591
592 ~UbigraphViz();
593
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000594 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
595};
596
597} // end anonymous namespace
598
599static ExplodedNodeImpl::Auditor* CreateUbiViz() {
600 std::string ErrMsg;
601
Ted Kremenek710ad932008-08-28 03:54:51 +0000602 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000603 if (!ErrMsg.empty())
604 return 0;
605
Ted Kremenek710ad932008-08-28 03:54:51 +0000606 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000607 Filename.appendComponent("llvm_ubi");
608 Filename.makeUnique(true,&ErrMsg);
609
610 if (!ErrMsg.empty())
611 return 0;
612
613 llvm::cerr << "Writing '" << Filename << "'.\n";
614
615 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
616 std::string filename = Filename.toString();
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000617 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000618
619 if (!ErrMsg.empty())
620 return 0;
621
Ted Kremenek710ad932008-08-28 03:54:51 +0000622 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000623}
624
625void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000626
627 assert (Src != Dst && "Self-edges are not allowed.");
628
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000629 // Lookup the Src. If it is a new node, it's a root.
630 VMap::iterator SrcI= M.find(Src);
631 unsigned SrcID;
632
633 if (SrcI == M.end()) {
634 M[Src] = SrcID = Cntr++;
635 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
636 }
637 else
638 SrcID = SrcI->second;
639
640 // Lookup the Dst.
641 VMap::iterator DstI= M.find(Dst);
642 unsigned DstID;
643
644 if (DstI == M.end()) {
645 M[Dst] = DstID = Cntr++;
646 *Out << "('vertex', " << DstID << ")\n";
647 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000648 else {
649 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000650 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000651 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
652 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000653
654 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000655 *Out << "('edge', " << SrcID << ", " << DstID
656 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000657}
658
Ted Kremenek56b98712008-08-28 05:02:09 +0000659UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
660 llvm::sys::Path& filename)
661 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
662
663 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
664 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
665 " ('size', '1.5'))\n";
666}
667
Ted Kremenek710ad932008-08-28 03:54:51 +0000668UbigraphViz::~UbigraphViz() {
669 Out.reset(0);
670 llvm::cerr << "Running 'ubiviz' program... ";
671 std::string ErrMsg;
672 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
673 std::vector<const char*> args;
674 args.push_back(Ubiviz.c_str());
675 args.push_back(Filename.c_str());
676 args.push_back(0);
677
678 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
679 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
680 }
681
682 // Delete the directory.
683 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000684}