blob: 1ada515c10e017b802b1d2c20d855b7b0293f73e [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 Kremenekf4381fd2008-07-02 00:03:09 +000089
90 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
91 PreprocessorFactory* ppf,
92 const LangOptions& lopts,
93 const std::string& fname,
94 const std::string& htmldir,
Ted Kremenek4fc82c82008-11-03 23:18:07 +000095 AnalysisStores sm, AnalysisDiagClients dc,
Zhongxing Xuff944a82008-12-22 01:52:37 +000096 bool visgraphviz, bool visubi, bool trim, bool analyzeAll)
Ted Kremenekf8ce6992008-08-27 22:31:43 +000097 : VisGraphviz(visgraphviz), VisUbigraph(visubi), TrimGraph(trim),
98 LOpts(lopts), Diags(diags),
Ted Kremenekf4381fd2008-07-02 00:03:09 +000099 Ctx(0), PP(pp), PPF(ppf),
100 HTMLDir(htmldir),
101 FName(fname),
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000102 AnalyzeAll(analyzeAll), SM(sm), DC(dc) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000103
104 void addCodeAction(CodeAction action) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000105 FunctionActions.push_back(action);
106 ObjCMethodActions.push_back(action);
107 }
108
109 void addObjCImplementationAction(CodeAction action) {
110 ObjCImplementationActions.push_back(action);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000111 }
112
Ted Kremenekdaac6342008-11-07 02:09:25 +0000113 void addTranslationUnitAction(CodeAction action) {
114 TranslationUnitActions.push_back(action);
115 }
116
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000117 virtual void Initialize(ASTContext &Context) {
118 Ctx = &Context;
119 }
120
121 virtual void HandleTopLevelDecl(Decl *D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000122 virtual void HandleTranslationUnit(TranslationUnit &TU);
123
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000124 void HandleCode(Decl* D, Stmt* Body, Actions actions);
125 };
126
127
Ted Kremenekc0959972008-07-02 21:24:01 +0000128 class VISIBILITY_HIDDEN AnalysisManager : public BugReporterData {
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000129 Decl* D; Stmt* Body;
130 TranslationUnit* TU;
131
132 enum AnalysisScope { ScopeTU, ScopeDecl } AScope;
133
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000134 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000135 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000136
137 llvm::OwningPtr<CFG> cfg;
138 llvm::OwningPtr<LiveVariables> liveness;
139 llvm::OwningPtr<ParentMap> PM;
140
Zhongxing Xu22438a82008-11-27 01:55:08 +0000141 // Configurable components creators.
142 StoreManagerCreator CreateStoreMgr;
143 ConstraintManagerCreator CreateConstraintMgr;
144
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000145 public:
146 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b)
Zhongxing Xuff944a82008-12-22 01:52:37 +0000147 : D(d), Body(b), TU(0), AScope(ScopeDecl), C(c), DisplayedFunction(false){
Zhongxing Xu22438a82008-11-27 01:55:08 +0000148 setManagerCreators();
149 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000150
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000151 AnalysisManager(AnalysisConsumer& c, TranslationUnit* tu)
Zhongxing Xuff944a82008-12-22 01:52:37 +0000152 : D(0), Body(0), TU(tu), AScope(ScopeTU), C(c), DisplayedFunction(false) {
Zhongxing Xu22438a82008-11-27 01:55:08 +0000153 setManagerCreators();
154 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000155
Ted Kremenekf304ddc2008-11-05 19:05:06 +0000156 Decl* getCodeDecl() const {
157 assert (AScope == ScopeDecl);
158 return D;
159 }
160
161 Stmt* getBody() const {
162 assert (AScope == ScopeDecl);
163 return Body;
164 }
165
166 TranslationUnit* getTranslationUnit() const {
167 assert (AScope == ScopeTU);
168 return TU;
169 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000170
Zhongxing Xu22438a82008-11-27 01:55:08 +0000171 StoreManagerCreator getStoreManagerCreator() {
172 return CreateStoreMgr;
Ted Kremenek95c7b002008-10-24 01:04:59 +0000173 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000174
175 ConstraintManagerCreator getConstraintManagerCreator() {
176 return CreateConstraintMgr;
177 }
Ted Kremenek95c7b002008-10-24 01:04:59 +0000178
Ted Kremenek7032f462008-07-03 05:26:14 +0000179 virtual CFG* getCFG() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000180 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
Ted Kremenek7032f462008-07-03 05:26:14 +0000181 return cfg.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000182 }
183
Ted Kremenekc0959972008-07-02 21:24:01 +0000184 virtual ParentMap& getParentMap() {
Ted Kremeneke2075582008-07-02 23:16:33 +0000185 if (!PM)
186 PM.reset(new ParentMap(getBody()));
Ted Kremenekc0959972008-07-02 21:24:01 +0000187 return *PM.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000188 }
189
Ted Kremenekc0959972008-07-02 21:24:01 +0000190 virtual ASTContext& getContext() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000191 return *C.Ctx;
192 }
193
Ted Kremenekc0959972008-07-02 21:24:01 +0000194 virtual SourceManager& getSourceManager() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000195 return getContext().getSourceManager();
196 }
197
Ted Kremenekc0959972008-07-02 21:24:01 +0000198 virtual Diagnostic& getDiagnostic() {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000199 return C.Diags;
200 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000201
202 const LangOptions& getLangOptions() const {
203 return C.LOpts;
204 }
205
Ted Kremenekc0959972008-07-02 21:24:01 +0000206 virtual PathDiagnosticClient* getPathDiagnosticClient() {
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000207 if (C.PD.get() == 0 && !C.HTMLDir.empty()) {
208 switch (C.DC) {
209 default:
210#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)\
211case PD_##NAME: C.PD.reset(CREATEFN(C.HTMLDir, C.PP, C.PPF)); break;
212#include "Analyses.def"
213 }
214 }
Ted Kremeneke2075582008-07-02 23:16:33 +0000215 return C.PD.get();
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000216 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000217
Ted Kremenek7032f462008-07-03 05:26:14 +0000218 virtual LiveVariables* getLiveVariables() {
Ted Kremenek235e0312008-07-02 18:11:29 +0000219 if (!liveness) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000220 CFG* c = getCFG();
221 if (!c) return 0;
222
Ted Kremenekca9bab02008-12-09 00:17:51 +0000223 liveness.reset(new LiveVariables(getContext(), *c));
Ted Kremenek7032f462008-07-03 05:26:14 +0000224 liveness->runOnCFG(*c);
225 liveness->runOnAllBlocks(*c, 0, true);
Ted Kremenek235e0312008-07-02 18:11:29 +0000226 }
Ted Kremenekc0959972008-07-02 21:24:01 +0000227
Ted Kremenek7032f462008-07-03 05:26:14 +0000228 return liveness.get();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000229 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000230
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000231 bool shouldVisualizeGraphviz() const {
232 return C.VisGraphviz;
233 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000234
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000235 bool shouldVisualizeUbigraph() const {
236 return C.VisUbigraph;
237 }
238
Ted Kremenek34d77342008-07-02 16:49:11 +0000239 bool shouldVisualize() const {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000240 return C.VisGraphviz || C.VisUbigraph;
Ted Kremenek34d77342008-07-02 16:49:11 +0000241 }
242
243 bool shouldTrimGraph() const {
244 return C.TrimGraph;
245 }
246
247 void DisplayFunction() {
248
249 if (DisplayedFunction)
250 return;
251
252 DisplayedFunction = true;
253
254 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000255 llvm::cerr << "ANALYZE: "
Ted Kremenek34d77342008-07-02 16:49:11 +0000256 << getContext().getSourceManager().getSourceName(FD->getLocation())
257 << ' '
258 << FD->getIdentifier()->getName()
259 << '\n';
260 }
261 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
Ted Kremeneka88fcef2008-11-20 16:14:48 +0000262 llvm::cerr << "ANALYZE (ObjC Method): "
Ted Kremenek34d77342008-07-02 16:49:11 +0000263 << getContext().getSourceManager().getSourceName(MD->getLocation())
264 << " '"
Chris Lattner077bf5e2008-11-24 03:33:13 +0000265 << MD->getSelector().getAsString() << "'\n";
Ted Kremenek34d77342008-07-02 16:49:11 +0000266 }
267 }
Zhongxing Xu22438a82008-11-27 01:55:08 +0000268
269 private:
270 /// Set configurable analyzer components creators. First check if there are
271 /// components registered at runtime. Otherwise fall back to builtin
272 /// components.
273 void setManagerCreators() {
274 if (ManagerRegistry::StoreMgrCreator != 0) {
275 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
276 }
277 else {
278 switch (C.SM) {
279 default:
280 assert(0 && "Unknown store manager.");
281#define ANALYSIS_STORE(NAME, CMDFLAG, DESC) \
282 case NAME##Model: CreateStoreMgr = Create##NAME##Manager; break;
283#include "Analyses.def"
284 }
285 }
286
287 if (ManagerRegistry::ConstraintMgrCreator != 0)
288 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
289 else
290 CreateConstraintMgr = CreateBasicConstraintManager;
291 }
292
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000293 };
Zhongxing Xu22438a82008-11-27 01:55:08 +0000294
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000295} // end anonymous namespace
296
297namespace llvm {
298 template <> struct FoldingSetTrait<CodeAction> {
299 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
300 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
301 }
302 };
303}
304
305//===----------------------------------------------------------------------===//
306// AnalysisConsumer implementation.
307//===----------------------------------------------------------------------===//
308
309void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
310 switch (D->getKind()) {
311 case Decl::Function: {
312 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000313
314 if (FName.size() > 0 && FName != FD->getIdentifier()->getName())
315 break;
316
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000317 Stmt* Body = FD->getBody();
318 if (Body) HandleCode(FD, Body, FunctionActions);
319 break;
320 }
321
322 case Decl::ObjCMethod: {
323 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000324
Chris Lattner077bf5e2008-11-24 03:33:13 +0000325 if (FName.size() > 0 && FName != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000326 return;
327
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000328 Stmt* Body = MD->getBody();
329 if (Body) HandleCode(MD, Body, ObjCMethodActions);
330 break;
331 }
332
333 default:
334 break;
335 }
336}
337
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000338void AnalysisConsumer::HandleTranslationUnit(TranslationUnit& TU) {
339
Ted Kremenekdaac6342008-11-07 02:09:25 +0000340 if(!TranslationUnitActions.empty()) {
341 AnalysisManager mgr(*this, &TU);
342 for (Actions::iterator I = TranslationUnitActions.begin(),
343 E = TranslationUnitActions.end(); I != E; ++I)
344 (*I)(mgr);
345 }
346
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000347 if (ObjCImplementationActions.empty())
348 return;
349
350 for (TranslationUnit::iterator I = TU.begin(), E = TU.end(); I!=E; ++I) {
351
352 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
353 HandleCode(ID, 0, ObjCImplementationActions);
354 }
355}
356
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000357void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
358
359 // Don't run the actions if an error has occured with parsing the file.
360 if (Diags.hasErrorOccurred())
361 return;
362
363 SourceLocation Loc = D->getLocation();
364
365 // Only run actions on declarations defined in actual source.
366 if (!Loc.isFileID())
367 return;
368
369 // Don't run the actions on declarations in header files unless
370 // otherwise specified.
371 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
372 return;
373
374 // Create an AnalysisManager that will manage the state for analyzing
375 // this method/function.
376 AnalysisManager mgr(*this, D, Body);
377
378 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000379 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000380 (*I)(mgr);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000381}
382
383//===----------------------------------------------------------------------===//
384// Analyses
385//===----------------------------------------------------------------------===//
386
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000387static void ActionWarnDeadStores(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000388 if (LiveVariables* L = mgr.getLiveVariables()) {
389 BugReporter BR(mgr);
390 CheckDeadStores(*L, BR);
391 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000392}
393
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000394static void ActionWarnUninitVals(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000395 if (CFG* c = mgr.getCFG())
396 CheckUninitializedValues(*c, mgr.getContext(), mgr.getDiagnostic());
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000397}
398
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000399
Ted Kremenek78d46242008-07-22 16:21:24 +0000400static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf,
401 bool StandardWarnings = true) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000402
Ted Kremenek7032f462008-07-03 05:26:14 +0000403
Ted Kremenekbc46f342008-07-02 16:35:50 +0000404 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000405
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000406 // Display progress.
407 mgr.DisplayFunction();
408
Ted Kremenek7032f462008-07-03 05:26:14 +0000409 // Construct the analysis engine.
410 LiveVariables* L = mgr.getLiveVariables();
411 if (!L) return;
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000412
Ted Kremenek95c7b002008-10-24 01:04:59 +0000413 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext(), *L,
Zhongxing Xuff944a82008-12-22 01:52:37 +0000414 PurgeDead,
Zhongxing Xu22438a82008-11-27 01:55:08 +0000415 mgr.getStoreManagerCreator(),
416 mgr.getConstraintManagerCreator());
Ted Kremenek95c7b002008-10-24 01:04:59 +0000417
Ted Kremenekbc46f342008-07-02 16:35:50 +0000418 Eng.setTransferFunctions(tf);
419
Ted Kremenek78d46242008-07-22 16:21:24 +0000420 if (StandardWarnings) {
421 Eng.RegisterInternalChecks();
422 RegisterAppleChecks(Eng);
423 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000424
425 // Set the graph auditor.
426 llvm::OwningPtr<ExplodedNodeImpl::Auditor> Auditor;
427 if (mgr.shouldVisualizeUbigraph()) {
428 Auditor.reset(CreateUbiViz());
429 ExplodedNodeImpl::SetAuditor(Auditor.get());
430 }
Ted Kremenek78d46242008-07-22 16:21:24 +0000431
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000432 // Execute the worklist algorithm.
433 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000434
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000435 // Release the auditor (if any) so that it doesn't monitor the graph
436 // created BugReporter.
437 ExplodedNodeImpl::SetAuditor(0);
438
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000439 // Display warnings.
Ted Kremenekc0959972008-07-02 21:24:01 +0000440 Eng.EmitWarnings(mgr);
Ted Kremenek34d77342008-07-02 16:49:11 +0000441
442 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000443 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000444 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000445}
446
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000447static void ActionCheckerCFRefAux(AnalysisManager& mgr, bool GCEnabled,
Ted Kremenek78d46242008-07-22 16:21:24 +0000448 bool StandardWarnings) {
449
Ted Kremenekbc46f342008-07-02 16:35:50 +0000450 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
451 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000452 mgr.getLangOptions());
453
Ted Kremenek78d46242008-07-22 16:21:24 +0000454 ActionGRExprEngine(mgr, TF, StandardWarnings);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000455}
456
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000457static void ActionCheckerCFRef(AnalysisManager& mgr) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000458
459 switch (mgr.getLangOptions().getGCMode()) {
460 default:
461 assert (false && "Invalid GC mode.");
462 case LangOptions::NonGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000463 ActionCheckerCFRefAux(mgr, false, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000464 break;
465
466 case LangOptions::GCOnly:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000467 ActionCheckerCFRefAux(mgr, true, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000468 break;
469
470 case LangOptions::HybridGC:
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000471 ActionCheckerCFRefAux(mgr, false, true);
472 ActionCheckerCFRefAux(mgr, true, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000473 break;
474 }
475}
476
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000477static void ActionCheckerSimple(AnalysisManager& mgr) {
Ted Kremenekbc46f342008-07-02 16:35:50 +0000478 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
479}
480
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000481static void ActionDisplayLiveVariables(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000482 if (LiveVariables* L = mgr.getLiveVariables()) {
483 mgr.DisplayFunction();
484 L->dumpBlockLiveness(mgr.getSourceManager());
485 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000486}
487
Ted Kremenek902141f2008-07-02 18:23:21 +0000488static void ActionCFGDump(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000489 if (CFG* c = mgr.getCFG()) {
490 mgr.DisplayFunction();
491 c->dump();
492 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000493}
494
495static void ActionCFGView(AnalysisManager& mgr) {
Ted Kremenek7032f462008-07-03 05:26:14 +0000496 if (CFG* c = mgr.getCFG()) {
497 mgr.DisplayFunction();
498 c->viewCFG();
499 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000500}
501
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000502static void ActionWarnObjCDealloc(AnalysisManager& mgr) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000503 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
504 return;
505
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000506 BugReporter BR(mgr);
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000507
508 CheckObjCDealloc(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
509 mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000510}
511
Ted Kremenek395aaf22008-07-23 00:45:26 +0000512static void ActionWarnObjCUnusedIvars(AnalysisManager& mgr) {
513 BugReporter BR(mgr);
514 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(mgr.getCodeDecl()), BR);
515}
516
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000517static void ActionWarnObjCMethSigs(AnalysisManager& mgr) {
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000518 BugReporter BR(mgr);
519
520 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(mgr.getCodeDecl()),
521 BR);
522}
523
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000524//===----------------------------------------------------------------------===//
525// AnalysisConsumer creation.
526//===----------------------------------------------------------------------===//
527
528ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
Ted Kremenek95c7b002008-10-24 01:04:59 +0000529 AnalysisStores SM,
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000530 AnalysisDiagClients DC,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000531 Diagnostic &diags, Preprocessor* pp,
532 PreprocessorFactory* ppf,
533 const LangOptions& lopts,
534 const std::string& fname,
535 const std::string& htmldir,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000536 bool VisGraphviz, bool VisUbi,
537 bool trim,
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000538 bool analyzeAll) {
539
540 llvm::OwningPtr<AnalysisConsumer>
Ted Kremenek4fc82c82008-11-03 23:18:07 +0000541 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir, SM, DC,
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000542 VisGraphviz, VisUbi, trim, analyzeAll));
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000543
544 for ( ; Beg != End ; ++Beg)
545 switch (*Beg) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000546#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000547 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000548 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000549 break;
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000550#include "Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000551 default: break;
552 }
553
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000554 return C.take();
555}
556
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000557//===----------------------------------------------------------------------===//
558// Ubigraph Visualization. FIXME: Move to separate file.
559//===----------------------------------------------------------------------===//
560
561namespace {
562
563class UbigraphViz : public ExplodedNodeImpl::Auditor {
564 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000565 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000566 unsigned Cntr;
567
568 typedef llvm::DenseMap<void*,unsigned> VMap;
569 VMap M;
570
571public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000572 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000573 llvm::sys::Path& filename);
Ted Kremenek710ad932008-08-28 03:54:51 +0000574
575 ~UbigraphViz();
576
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000577 virtual void AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst);
578};
579
580} // end anonymous namespace
581
582static ExplodedNodeImpl::Auditor* CreateUbiViz() {
583 std::string ErrMsg;
584
Ted Kremenek710ad932008-08-28 03:54:51 +0000585 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000586 if (!ErrMsg.empty())
587 return 0;
588
Ted Kremenek710ad932008-08-28 03:54:51 +0000589 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000590 Filename.appendComponent("llvm_ubi");
591 Filename.makeUnique(true,&ErrMsg);
592
593 if (!ErrMsg.empty())
594 return 0;
595
596 llvm::cerr << "Writing '" << Filename << "'.\n";
597
598 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
599 std::string filename = Filename.toString();
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000600 Stream.reset(new llvm::raw_fd_ostream(filename.c_str(), false, ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000601
602 if (!ErrMsg.empty())
603 return 0;
604
Ted Kremenek710ad932008-08-28 03:54:51 +0000605 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000606}
607
608void UbigraphViz::AddEdge(ExplodedNodeImpl* Src, ExplodedNodeImpl* Dst) {
Ted Kremenek45479c82008-08-28 18:34:41 +0000609
610 assert (Src != Dst && "Self-edges are not allowed.");
611
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000612 // Lookup the Src. If it is a new node, it's a root.
613 VMap::iterator SrcI= M.find(Src);
614 unsigned SrcID;
615
616 if (SrcI == M.end()) {
617 M[Src] = SrcID = Cntr++;
618 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
619 }
620 else
621 SrcID = SrcI->second;
622
623 // Lookup the Dst.
624 VMap::iterator DstI= M.find(Dst);
625 unsigned DstID;
626
627 if (DstI == M.end()) {
628 M[Dst] = DstID = Cntr++;
629 *Out << "('vertex', " << DstID << ")\n";
630 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000631 else {
632 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000633 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000634 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
635 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000636
637 // Add the edge.
Ted Kremenekd1289322008-08-27 22:46:55 +0000638 *Out << "('edge', " << SrcID << ", " << DstID
639 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000640}
641
Ted Kremenek56b98712008-08-28 05:02:09 +0000642UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
643 llvm::sys::Path& filename)
644 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
645
646 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
647 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
648 " ('size', '1.5'))\n";
649}
650
Ted Kremenek710ad932008-08-28 03:54:51 +0000651UbigraphViz::~UbigraphViz() {
652 Out.reset(0);
653 llvm::cerr << "Running 'ubiviz' program... ";
654 std::string ErrMsg;
655 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
656 std::vector<const char*> args;
657 args.push_back(Ubiviz.c_str());
658 args.push_back(Filename.c_str());
659 args.push_back(0);
660
661 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
662 llvm::cerr << "Error viewing graph: " << ErrMsg << "\n";
663 }
664
665 // Delete the directory.
666 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000667}