blob: e8bd920d412a4e2a9eafa9fa28efae67f060cfa5 [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"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000018#include "clang/AST/ParentMap.h"
Daniel Dunbarefceabd2009-11-05 02:41:58 +000019#include "clang/Analysis/Analyses/LiveVariables.h"
20#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/CFG.h"
22#include "clang/Analysis/LocalCheckers.h"
Chandler Carruthf465e852009-11-11 19:10:59 +000023#include "clang/Analysis/ManagerRegistry.h"
Daniel Dunbarefceabd2009-11-05 02:41:58 +000024#include "clang/Analysis/PathDiagnostic.h"
Zhongxing Xufda78322009-07-30 09:11:52 +000025#include "clang/Analysis/PathSensitive/AnalysisManager.h"
Ted Kremenekc0959972008-07-02 21:24:01 +000026#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000027#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Daniel Dunbarefceabd2009-11-05 02:41:58 +000028#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
29#include "clang/Basic/FileManager.h"
30#include "clang/Basic/SourceManager.h"
Daniel Dunbarefceabd2009-11-05 02:41:58 +000031#include "clang/Frontend/PathDiagnosticClients.h"
32#include "clang/Lex/Preprocessor.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000033#include "llvm/Support/Compiler.h"
Ted Kremenekf8ce6992008-08-27 22:31:43 +000034#include "llvm/Support/raw_ostream.h"
35#include "llvm/System/Path.h"
Ted Kremenek710ad932008-08-28 03:54:51 +000036#include "llvm/System/Program.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000037#include "llvm/ADT/OwningPtr.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000038
Ted Kremenekf4381fd2008-07-02 00:03:09 +000039using namespace clang;
40
Zhongxing Xuc5619d92009-08-06 01:32:16 +000041static ExplodedNode::Auditor* CreateUbiViz();
Zhongxing Xuff944a82008-12-22 01:52:37 +000042
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +000043//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000044// Basic type definitions.
45//===----------------------------------------------------------------------===//
46
Mike Stump1eb44332009-09-09 15:08:12 +000047namespace {
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000048 class AnalysisConsumer;
49 typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D);
Ted Kremenekf4381fd2008-07-02 00:03:09 +000050} // end anonymous namespace
51
52//===----------------------------------------------------------------------===//
Ted Kremenekf7556062009-07-27 22:13:39 +000053// Special PathDiagnosticClients.
54//===----------------------------------------------------------------------===//
55
56static PathDiagnosticClient*
Daniel Dunbarefceabd2009-11-05 02:41:58 +000057CreatePlistHTMLDiagnosticClient(const std::string& prefix,
58 const Preprocessor &PP) {
Mike Stump1eb44332009-09-09 15:08:12 +000059 llvm::sys::Path F(prefix);
Ted Kremenekb697a4e2009-11-05 02:09:23 +000060 PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP);
61 return CreatePlistDiagnosticClient(prefix, PP, PD);
Ted Kremenekf7556062009-07-27 22:13:39 +000062}
63
64//===----------------------------------------------------------------------===//
Ted Kremenekf4381fd2008-07-02 00:03:09 +000065// AnalysisConsumer declaration.
66//===----------------------------------------------------------------------===//
67
68namespace {
69
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000070 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
71 typedef std::vector<CodeAction> Actions;
72 Actions FunctionActions;
73 Actions ObjCMethodActions;
74 Actions ObjCImplementationActions;
75 Actions TranslationUnitActions;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000077public:
78 ASTContext* Ctx;
79 const Preprocessor &PP;
80 const std::string OutDir;
81 AnalyzerOptions Opts;
82 bool declDisplayed;
Zhongxing Xufda78322009-07-30 09:11:52 +000083
Zhongxing Xud07a0d02009-08-03 03:27:37 +000084
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000085 // PD is owned by AnalysisManager.
86 PathDiagnosticClient *PD;
Zhongxing Xud07a0d02009-08-03 03:27:37 +000087
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000088 StoreManagerCreator CreateStoreMgr;
89 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenekf4381fd2008-07-02 00:03:09 +000090
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000091 llvm::OwningPtr<AnalysisManager> Mgr;
Zhongxing Xuc471e7b2009-08-03 03:13:46 +000092
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +000093 AnalysisConsumer(const Preprocessor& pp,
94 const std::string& outdir,
95 const AnalyzerOptions& opts)
96 : Ctx(0), PP(pp), OutDir(outdir),
97 Opts(opts), declDisplayed(false), PD(0) {
98 DigestAnalyzerOptions();
99 }
Zhongxing Xufda78322009-07-30 09:11:52 +0000100
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000101 void DigestAnalyzerOptions() {
102 // Create the PathDiagnosticClient.
103 if (!OutDir.empty()) {
104 switch (Opts.AnalysisDiagOpt) {
105 default:
Zhongxing Xufda78322009-07-30 09:11:52 +0000106#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000107 case PD_##NAME: PD = CREATEFN(OutDir, PP); break;
Zhongxing Xufda78322009-07-30 09:11:52 +0000108#include "clang/Frontend/Analyses.def"
Zhongxing Xufda78322009-07-30 09:11:52 +0000109 }
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000110 }
Zhongxing Xufda78322009-07-30 09:11:52 +0000111
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000112 // Create the analyzer component creators.
113 if (ManagerRegistry::StoreMgrCreator != 0) {
114 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
115 }
116 else {
117 switch (Opts.AnalysisStoreOpt) {
118 default:
119 assert(0 && "Unknown store manager.");
Zhongxing Xufda78322009-07-30 09:11:52 +0000120#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000121 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Zhongxing Xufda78322009-07-30 09:11:52 +0000122#include "clang/Frontend/Analyses.def"
Zhongxing Xufda78322009-07-30 09:11:52 +0000123 }
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000126 if (ManagerRegistry::ConstraintMgrCreator != 0)
127 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
128 else {
129 switch (Opts.AnalysisConstraintsOpt) {
130 default:
131 assert(0 && "Unknown store manager.");
Zhongxing Xufda78322009-07-30 09:11:52 +0000132#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000133 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
Zhongxing Xufda78322009-07-30 09:11:52 +0000134#include "clang/Frontend/Analyses.def"
Zhongxing Xufda78322009-07-30 09:11:52 +0000135 }
136 }
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000137 }
138
139 void DisplayFunction(const Decl *D) {
140 if (!Opts.AnalyzerDisplayProgress || declDisplayed)
141 return;
142
143 declDisplayed = true;
144 // FIXME: Is getCodeDecl() always a named decl?
145 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
146 const NamedDecl *ND = cast<NamedDecl>(D);
147 SourceManager &SM = Mgr->getASTContext().getSourceManager();
148 llvm::errs() << "ANALYZE: "
149 << SM.getPresumedLoc(ND->getLocation()).getFilename()
150 << ' ' << ND->getNameAsString() << '\n';
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000151 }
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000154 void addCodeAction(CodeAction action) {
155 FunctionActions.push_back(action);
156 ObjCMethodActions.push_back(action);
157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000159 void addObjCImplementationAction(CodeAction action) {
160 ObjCImplementationActions.push_back(action);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000163 void addTranslationUnitAction(CodeAction action) {
164 TranslationUnitActions.push_back(action);
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000167 virtual void Initialize(ASTContext &Context) {
168 Ctx = &Context;
169 Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
170 PP.getLangOptions(), PD,
171 CreateStoreMgr, CreateConstraintMgr,
172 Opts.AnalyzerDisplayProgress,
173 Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
174 Opts.PurgeDead, Opts.EagerlyAssume,
175 Opts.TrimGraph));
176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000178 virtual void HandleTopLevelDecl(DeclGroupRef D) {
179 declDisplayed = false;
180 for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I)
181 HandleTopLevelSingleDecl(*I);
182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000184 void HandleTopLevelSingleDecl(Decl *D);
185 virtual void HandleTranslationUnit(ASTContext &C);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000187 void HandleCode(Decl* D, Stmt* Body, Actions& actions);
188};
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000189} // end anonymous namespace
190
191namespace llvm {
192 template <> struct FoldingSetTrait<CodeAction> {
193 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
194 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196 };
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000197}
198
199//===----------------------------------------------------------------------===//
200// AnalysisConsumer implementation.
201//===----------------------------------------------------------------------===//
202
Mike Stump1eb44332009-09-09 15:08:12 +0000203void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) {
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000204 switch (D->getKind()) {
205 case Decl::Function: {
206 FunctionDecl* FD = cast<FunctionDecl>(D);
Ted Kremenek235e0312008-07-02 18:11:29 +0000207
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000208 if (!Opts.AnalyzeSpecificFunction.empty() &&
Eli Friedmane71b85f2009-05-19 10:18:02 +0000209 Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName())
Ted Kremenek235e0312008-07-02 18:11:29 +0000210 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000212 Stmt* Body = FD->getBody();
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000213 if (Body) HandleCode(FD, Body, FunctionActions);
214 break;
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000217 case Decl::ObjCMethod: {
218 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Eli Friedmane71b85f2009-05-19 10:18:02 +0000220 if (Opts.AnalyzeSpecificFunction.size() > 0 &&
221 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
Ted Kremenek235e0312008-07-02 18:11:29 +0000222 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000224 Stmt* Body = MD->getBody();
225 if (Body) HandleCode(MD, Body, ObjCMethodActions);
226 break;
227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000229 default:
230 break;
231 }
232}
233
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000234void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Ted Kremenek74e3c922009-09-26 04:15:09 +0000235
236 TranslationUnitDecl *TU = C.getTranslationUnitDecl();
237
238 if (!TranslationUnitActions.empty()) {
239 // Find the entry function definition (if any).
240 FunctionDecl *FD = 0;
241
242 if (!Opts.AnalyzeSpecificFunction.empty()) {
243 for (DeclContext::decl_iterator I=TU->decls_begin(), E=TU->decls_end();
244 I != E; ++I) {
245 if (FunctionDecl *fd = dyn_cast<FunctionDecl>(*I))
246 if (fd->isThisDeclarationADefinition() &&
247 fd->getNameAsString() == Opts.AnalyzeSpecificFunction) {
248 FD = fd;
249 break;
250 }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000251 }
Ted Kremenek74e3c922009-09-26 04:15:09 +0000252 }
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000253
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000254 for (Actions::iterator I = TranslationUnitActions.begin(),
Ted Kremenekdaac6342008-11-07 02:09:25 +0000255 E = TranslationUnitActions.end(); I != E; ++I)
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000256 (*I)(*this, *Mgr, FD);
Ted Kremenekdaac6342008-11-07 02:09:25 +0000257 }
258
Chris Lattnere9077872009-03-28 03:29:40 +0000259 if (!ObjCImplementationActions.empty()) {
Ted Kremenek74e3c922009-09-26 04:15:09 +0000260 for (DeclContext::decl_iterator I = TU->decls_begin(),
261 E = TU->decls_end();
Chris Lattnere9077872009-03-28 03:29:40 +0000262 I != E; ++I)
Ted Kremenek4d53a532009-02-13 00:51:30 +0000263 if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I))
264 HandleCode(ID, 0, ObjCImplementationActions);
Chris Lattnere9077872009-03-28 03:29:40 +0000265 }
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000266
Ted Kremenek690a7f42009-08-02 05:43:14 +0000267 // Explicitly destroy the PathDiagnosticClient. This will flush its output.
268 // FIXME: This should be replaced with something that doesn't rely on
269 // side-effects in PathDiagnosticClient's destructor.
Zhongxing Xud07a0d02009-08-03 03:27:37 +0000270 Mgr.reset(NULL);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000271}
272
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000273void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) {
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000275 // Don't run the actions if an error has occured with parsing the file.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000276 if (PP.getDiagnostics().hasErrorOccurred())
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000277 return;
Ted Kremenek81922f02009-02-02 20:52:40 +0000278
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000279 // Don't run the actions on declarations in header files unless
280 // otherwise specified.
Eli Friedmane71b85f2009-05-19 10:18:02 +0000281 if (!Opts.AnalyzeAll &&
282 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Mike Stump1eb44332009-09-09 15:08:12 +0000283 return;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000284
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000285 // Clear the AnalysisManager of old AnalysisContexts.
286 Mgr->ClearContexts();
287
Mike Stump1eb44332009-09-09 15:08:12 +0000288 // Dispatch on the actions.
Zhongxing Xu3702af52008-10-30 05:03:28 +0000289 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000290 (*I)(*this, *Mgr, D);
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000291}
292
293//===----------------------------------------------------------------------===//
294// Analyses
295//===----------------------------------------------------------------------===//
296
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000297static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr,
298 Decl *D) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000299 if (LiveVariables *L = mgr.getLiveVariables(D)) {
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000300 C.DisplayFunction(D);
Ted Kremenek7032f462008-07-03 05:26:14 +0000301 BugReporter BR(mgr);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000302 CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR);
Ted Kremenek7032f462008-07-03 05:26:14 +0000303 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000304}
305
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000306static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr,
307 Decl *D) {
308 if (CFG* c = mgr.getCFG(D)) {
309 C.DisplayFunction(D);
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000310 CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000311 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000312}
313
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000314
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000315static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D,
Ted Kremenek6a198322009-09-18 05:37:41 +0000316 GRTransferFuncs* tf) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenekbc46f342008-07-02 16:35:50 +0000318 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek7032f462008-07-03 05:26:14 +0000319
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000320 // Display progress.
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000321 C.DisplayFunction(D);
Ted Kremenek8ffc8a52008-11-24 20:53:32 +0000322
Ted Kremenek75d03cf2009-09-18 22:29:35 +0000323 // Construct the analysis engine. We first query for the LiveVariables
324 // information to see if the CFG is valid.
325 // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
326 if (!mgr.getLiveVariables(D))
327 return;
328
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000329 GRExprEngine Eng(mgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000330
Ted Kremenekbc46f342008-07-02 16:35:50 +0000331 Eng.setTransferFunctions(tf);
Ted Kremenek6a198322009-09-18 05:37:41 +0000332 Eng.RegisterInternalChecks(); // FIXME: Internal checks should just
333 // automatically register.
334 RegisterAppleChecks(Eng, *D);
Ted Kremenekeb941132009-11-13 01:15:47 +0000335
336 if (C.Opts.EnableExperimentalChecks)
337 RegisterExperimentalChecks(Eng);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000338
339 // Set the graph auditor.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000340 llvm::OwningPtr<ExplodedNode::Auditor> Auditor;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000341 if (mgr.shouldVisualizeUbigraph()) {
342 Auditor.reset(CreateUbiViz());
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000343 ExplodedNode::SetAuditor(Auditor.get());
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000346 // Execute the worklist algorithm.
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000347 Eng.ExecuteWorkList(mgr.getStackFrame(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000349 // Release the auditor (if any) so that it doesn't monitor the graph
350 // created BugReporter.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000351 ExplodedNode::SetAuditor(0);
Ted Kremenek3df64212009-03-11 01:42:29 +0000352
Ted Kremenek34d77342008-07-02 16:49:11 +0000353 // Visualize the exploded graph.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000354 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek34d77342008-07-02 16:49:11 +0000355 Eng.ViewGraph(mgr.shouldTrimGraph());
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek3df64212009-03-11 01:42:29 +0000357 // Display warnings.
358 Eng.getBugReporter().FlushReports();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000359}
360
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000361static void ActionCheckerCFRefAux(AnalysisConsumer &C, AnalysisManager& mgr,
362 Decl *D, bool GCEnabled) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Zhongxing Xu5032ffe2009-08-25 06:51:30 +0000364 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(),
Ted Kremenekbc46f342008-07-02 16:35:50 +0000365 GCEnabled,
Ted Kremenekbc46f342008-07-02 16:35:50 +0000366 mgr.getLangOptions());
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000368 ActionGRExprEngine(C, mgr, D, TF);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000369}
370
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000371static void ActionCheckerCFRef(AnalysisConsumer &C, AnalysisManager& mgr,
372 Decl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000374 switch (mgr.getLangOptions().getGCMode()) {
375 default:
376 assert (false && "Invalid GC mode.");
377 case LangOptions::NonGC:
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000378 ActionCheckerCFRefAux(C, mgr, D, false);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000379 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000381 case LangOptions::GCOnly:
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000382 ActionCheckerCFRefAux(C, mgr, D, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000383 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000385 case LangOptions::HybridGC:
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000386 ActionCheckerCFRefAux(C, mgr, D, false);
387 ActionCheckerCFRefAux(C, mgr, D, true);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000388 break;
389 }
390}
391
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000392static void ActionDisplayLiveVariables(AnalysisConsumer &C,
393 AnalysisManager& mgr, Decl *D) {
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000394 if (LiveVariables* L = mgr.getLiveVariables(D)) {
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000395 C.DisplayFunction(D);
Ted Kremenek7032f462008-07-03 05:26:14 +0000396 L->dumpBlockLiveness(mgr.getSourceManager());
397 }
Ted Kremenek235e0312008-07-02 18:11:29 +0000398}
399
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000400static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
401 if (CFG *cfg = mgr.getCFG(D)) {
402 C.DisplayFunction(D);
403 cfg->dump(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000404 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000405}
406
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000407static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
408 if (CFG *cfg = mgr.getCFG(D)) {
409 C.DisplayFunction(D);
410 cfg->viewCFG(mgr.getLangOptions());
Ted Kremenek7032f462008-07-03 05:26:14 +0000411 }
Ted Kremenek902141f2008-07-02 18:23:21 +0000412}
413
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000414static void ActionSecuritySyntacticChecks(AnalysisConsumer &C,
415 AnalysisManager &mgr, Decl *D) {
416 C.DisplayFunction(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000417 BugReporter BR(mgr);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000418 CheckSecuritySyntaxOnly(D, BR);
Ted Kremenekdbfb5f82009-07-23 01:07:19 +0000419}
420
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000421static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr,
422 Decl *D) {
Ted Kremenek4f4e7e42008-08-04 17:14:10 +0000423 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
424 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000426 C.DisplayFunction(D);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000427 BugReporter BR(mgr);
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000428 CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000429}
430
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000431static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr,
432 Decl *D) {
433 C.DisplayFunction(D);
Ted Kremenek395aaf22008-07-23 00:45:26 +0000434 BugReporter BR(mgr);
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000435 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
Ted Kremenek395aaf22008-07-23 00:45:26 +0000436}
437
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000438static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr,
439 Decl *D) {
440 C.DisplayFunction(D);
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000441 BugReporter BR(mgr);
Zhongxing Xub317f8f2009-09-10 05:44:00 +0000442 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
Ted Kremenek0d8019e2008-07-11 22:40:47 +0000443}
444
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000445static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr,
446 Decl *D) {
447 C.DisplayFunction(D);
Zhongxing Xu28a109f2009-11-08 13:10:34 +0000448 BugReporter BR(mgr);
449 CheckSizeofPointer(D, BR);
450}
451
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000452static void ActionInlineCall(AnalysisConsumer &C, AnalysisManager &mgr,
453 Decl *D) {
Zhongxing Xu66847a22009-09-11 04:13:42 +0000454 if (!D)
455 return;
456
Ted Kremenek1d9cbeb2009-11-11 06:28:42 +0000457 C.DisplayFunction(D);
Zhongxing Xu66847a22009-09-11 04:13:42 +0000458 llvm::OwningPtr<GRTransferFuncs> TF(CreateCallInliner(mgr.getASTContext()));
459
460 // Construct the analysis engine.
461 GRExprEngine Eng(mgr);
462
463 Eng.setTransferFunctions(TF.get());
464
465 Eng.RegisterInternalChecks();
466 RegisterAppleChecks(Eng, *D);
467
468 // Execute the worklist algorithm.
469 Eng.ExecuteWorkList(mgr.getStackFrame(D));
470
471 // Visualize the exploded graph.
472 if (mgr.shouldVisualizeGraphviz())
473 Eng.ViewGraph(mgr.shouldTrimGraph());
474}
475
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000476//===----------------------------------------------------------------------===//
477// AnalysisConsumer creation.
478//===----------------------------------------------------------------------===//
479
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000480ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp,
Eli Friedmane71b85f2009-05-19 10:18:02 +0000481 const std::string& OutDir,
482 const AnalyzerOptions& Opts) {
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000483 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts));
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000484
Eli Friedmane71b85f2009-05-19 10:18:02 +0000485 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
486 switch (Opts.AnalysisList[i]) {
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000487#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Ted Kremenekfb9a48c2008-07-14 23:41:13 +0000488 case NAME:\
Ted Kremenekf7f3c202008-07-15 00:46:02 +0000489 C->add ## SCOPE ## Action(&Action ## NAME);\
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000490 break;
Eli Friedman4df2c422009-05-19 21:16:18 +0000491#include "clang/Frontend/Analyses.def"
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000492 default: break;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Ted Kremenek2c4036e2009-05-07 19:02:53 +0000495 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
Daniel Dunbarefceabd2009-11-05 02:41:58 +0000496 pp.getDiagnostics().setWarningsAsErrors(false);
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000497
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000498 return C.take();
499}
500
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000501//===----------------------------------------------------------------------===//
502// Ubigraph Visualization. FIXME: Move to separate file.
503//===----------------------------------------------------------------------===//
504
505namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000507class UbigraphViz : public ExplodedNode::Auditor {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000508 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek710ad932008-08-28 03:54:51 +0000509 llvm::sys::Path Dir, Filename;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000510 unsigned Cntr;
511
512 typedef llvm::DenseMap<void*,unsigned> VMap;
513 VMap M;
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000515public:
Ted Kremenek710ad932008-08-28 03:54:51 +0000516 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek56b98712008-08-28 05:02:09 +0000517 llvm::sys::Path& filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Ted Kremenek710ad932008-08-28 03:54:51 +0000519 ~UbigraphViz();
Mike Stump1eb44332009-09-09 15:08:12 +0000520
521 virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000522};
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000524} // end anonymous namespace
525
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000526static ExplodedNode::Auditor* CreateUbiViz() {
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000527 std::string ErrMsg;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Ted Kremenek710ad932008-08-28 03:54:51 +0000529 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000530 if (!ErrMsg.empty())
531 return 0;
532
Ted Kremenek710ad932008-08-28 03:54:51 +0000533 llvm::sys::Path Filename = Dir;
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000534 Filename.appendComponent("llvm_ubi");
535 Filename.makeUnique(true,&ErrMsg);
536
537 if (!ErrMsg.empty())
538 return 0;
539
Chris Lattnerd57a7ef2009-08-23 22:45:33 +0000540 llvm::errs() << "Writing '" << Filename.str() << "'.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000542 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
Dan Gohmanb044c472009-08-25 15:36:09 +0000543 Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000544
545 if (!ErrMsg.empty())
546 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek710ad932008-08-28 03:54:51 +0000548 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000549}
550
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000551void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) {
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek45479c82008-08-28 18:34:41 +0000553 assert (Src != Dst && "Self-edges are not allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000555 // Lookup the Src. If it is a new node, it's a root.
556 VMap::iterator SrcI= M.find(Src);
557 unsigned SrcID;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000559 if (SrcI == M.end()) {
560 M[Src] = SrcID = Cntr++;
561 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
562 }
563 else
564 SrcID = SrcI->second;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000566 // Lookup the Dst.
567 VMap::iterator DstI= M.find(Dst);
568 unsigned DstID;
569
570 if (DstI == M.end()) {
571 M[Dst] = DstID = Cntr++;
572 *Out << "('vertex', " << DstID << ")\n";
573 }
Ted Kremenek56b98712008-08-28 05:02:09 +0000574 else {
575 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000576 DstID = DstI->second;
Ted Kremenek56b98712008-08-28 05:02:09 +0000577 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
578 }
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000579
580 // Add the edge.
Mike Stump1eb44332009-09-09 15:08:12 +0000581 *Out << "('edge', " << SrcID << ", " << DstID
Ted Kremenekd1289322008-08-27 22:46:55 +0000582 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekf8ce6992008-08-27 22:31:43 +0000583}
584
Ted Kremenek56b98712008-08-28 05:02:09 +0000585UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
586 llvm::sys::Path& filename)
587 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
588
589 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
590 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
591 " ('size', '1.5'))\n";
592}
593
Ted Kremenek710ad932008-08-28 03:54:51 +0000594UbigraphViz::~UbigraphViz() {
595 Out.reset(0);
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000596 llvm::errs() << "Running 'ubiviz' program... ";
Ted Kremenek710ad932008-08-28 03:54:51 +0000597 std::string ErrMsg;
598 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
599 std::vector<const char*> args;
600 args.push_back(Ubiviz.c_str());
601 args.push_back(Filename.c_str());
602 args.push_back(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenek710ad932008-08-28 03:54:51 +0000604 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +0000605 llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
Ted Kremenek710ad932008-08-28 03:54:51 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Ted Kremenek710ad932008-08-28 03:54:51 +0000608 // Delete the directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000609 Dir.eraseFromDisk(true);
Daniel Dunbar932680e2008-08-29 03:45:59 +0000610}