blob: 6e23e16b51dfa5d7e27e949d414fe6aa9e2208e5 [file] [log] [blame]
Ted Kremenekf0413bf2008-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 Friedman5b5bd902009-05-19 21:10:40 +000014#include "clang/Frontend/AnalysisConsumer.h"
Ted Kremenekf0413bf2008-07-02 00:03:09 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/Decl.h"
Zhongxing Xu686b8452009-12-16 05:29:59 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekf0413bf2008-07-02 00:03:09 +000018#include "clang/AST/DeclObjC.h"
Ted Kremenekf0413bf2008-07-02 00:03:09 +000019#include "clang/AST/ParentMap.h"
Daniel Dunbarb5f20252009-11-05 02:41:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000021#include "clang/Analysis/Analyses/UninitializedValues.h"
Daniel Dunbarb5f20252009-11-05 02:41:58 +000022#include "clang/Analysis/CFG.h"
Ted Kremenek6296e092010-01-26 22:59:55 +000023#include "clang/Checker/Checkers/LocalCheckers.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000024#include "clang/Checker/ManagerRegistry.h"
Ted Kremenekfe0fc402010-01-25 17:10:22 +000025#include "clang/Checker/BugReporter/PathDiagnostic.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000026#include "clang/Checker/PathSensitive/AnalysisManager.h"
Ted Kremenekfe0fc402010-01-25 17:10:22 +000027#include "clang/Checker/BugReporter/BugReporter.h"
Ted Kremenekd6b87082010-01-25 04:41:41 +000028#include "clang/Checker/PathSensitive/GRExprEngine.h"
29#include "clang/Checker/PathSensitive/GRTransferFuncs.h"
Daniel Dunbarb5f20252009-11-05 02:41:58 +000030#include "clang/Basic/FileManager.h"
31#include "clang/Basic/SourceManager.h"
Daniel Dunbarb5f20252009-11-05 02:41:58 +000032#include "clang/Frontend/PathDiagnosticClients.h"
33#include "clang/Lex/Preprocessor.h"
Ted Kremenekd461d7a2008-08-27 22:31:43 +000034#include "llvm/Support/raw_ostream.h"
35#include "llvm/System/Path.h"
Ted Kremenek266ec3f2008-08-28 03:54:51 +000036#include "llvm/System/Program.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000037#include "llvm/ADT/OwningPtr.h"
Ted Kremenek0e7d2522008-07-03 04:29:21 +000038
Ted Kremenekf0413bf2008-07-02 00:03:09 +000039using namespace clang;
40
Zhongxing Xu20227f72009-08-06 01:32:16 +000041static ExplodedNode::Auditor* CreateUbiViz();
Zhongxing Xu5b0ae812008-12-22 01:52:37 +000042
Ted Kremenekb5351812009-02-17 04:27:41 +000043//===----------------------------------------------------------------------===//
Ted Kremenekf0413bf2008-07-02 00:03:09 +000044// Basic type definitions.
45//===----------------------------------------------------------------------===//
46
Ted Kremenekf0413bf2008-07-02 00:03:09 +000047//===----------------------------------------------------------------------===//
Ted Kremenek04ade6f2009-07-27 22:13:39 +000048// Special PathDiagnosticClients.
49//===----------------------------------------------------------------------===//
50
51static PathDiagnosticClient*
Daniel Dunbarb5f20252009-11-05 02:41:58 +000052CreatePlistHTMLDiagnosticClient(const std::string& prefix,
53 const Preprocessor &PP) {
Mike Stump11289f42009-09-09 15:08:12 +000054 llvm::sys::Path F(prefix);
Ted Kremenek5e860442009-11-05 02:09:23 +000055 PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP);
56 return CreatePlistDiagnosticClient(prefix, PP, PD);
Ted Kremenek04ade6f2009-07-27 22:13:39 +000057}
58
59//===----------------------------------------------------------------------===//
Ted Kremenekf0413bf2008-07-02 00:03:09 +000060// AnalysisConsumer declaration.
61//===----------------------------------------------------------------------===//
62
63namespace {
64
Zhongxing Xu685a1d82010-04-30 04:14:20 +000065class AnalysisConsumer : public ASTConsumer {
66public:
Douglas Gregor120f6a62009-11-17 06:14:37 +000067 typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D);
Ted Kremenek39df94b2010-02-14 19:08:51 +000068 typedef void (*TUAction)(AnalysisConsumer &C, AnalysisManager &M,
69 TranslationUnitDecl &TU);
70
Zhongxing Xu685a1d82010-04-30 04:14:20 +000071private:
Ted Kremenek55d59bf2009-11-11 06:28:42 +000072 typedef std::vector<CodeAction> Actions;
Ted Kremenek39df94b2010-02-14 19:08:51 +000073 typedef std::vector<TUAction> TUActions;
74
Ted Kremenek55d59bf2009-11-11 06:28:42 +000075 Actions FunctionActions;
76 Actions ObjCMethodActions;
77 Actions ObjCImplementationActions;
Zhongxing Xu685a1d82010-04-30 04:14:20 +000078 Actions CXXMethodActions;
79 TUActions TranslationUnitActions; // Remove this.
Mike Stump11289f42009-09-09 15:08:12 +000080
Ted Kremenek55d59bf2009-11-11 06:28:42 +000081public:
82 ASTContext* Ctx;
83 const Preprocessor &PP;
84 const std::string OutDir;
85 AnalyzerOptions Opts;
86 bool declDisplayed;
Zhongxing Xu4b03d492009-07-30 09:11:52 +000087
Zhongxing Xub9a45a42009-08-03 03:27:37 +000088
Ted Kremenek55d59bf2009-11-11 06:28:42 +000089 // PD is owned by AnalysisManager.
90 PathDiagnosticClient *PD;
Zhongxing Xub9a45a42009-08-03 03:27:37 +000091
Ted Kremenek55d59bf2009-11-11 06:28:42 +000092 StoreManagerCreator CreateStoreMgr;
93 ConstraintManagerCreator CreateConstraintMgr;
Ted Kremenekf0413bf2008-07-02 00:03:09 +000094
Ted Kremenek55d59bf2009-11-11 06:28:42 +000095 llvm::OwningPtr<AnalysisManager> Mgr;
Zhongxing Xubcbe44f2009-08-03 03:13:46 +000096
Ted Kremenek55d59bf2009-11-11 06:28:42 +000097 AnalysisConsumer(const Preprocessor& pp,
98 const std::string& outdir,
99 const AnalyzerOptions& opts)
100 : Ctx(0), PP(pp), OutDir(outdir),
101 Opts(opts), declDisplayed(false), PD(0) {
102 DigestAnalyzerOptions();
103 }
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000104
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000105 void DigestAnalyzerOptions() {
106 // Create the PathDiagnosticClient.
107 if (!OutDir.empty()) {
108 switch (Opts.AnalysisDiagOpt) {
109 default:
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000110#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000111 case PD_##NAME: PD = CREATEFN(OutDir, PP); break;
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000112#include "clang/Frontend/Analyses.def"
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000113 }
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000114 }
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000115
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000116 // Create the analyzer component creators.
117 if (ManagerRegistry::StoreMgrCreator != 0) {
118 CreateStoreMgr = ManagerRegistry::StoreMgrCreator;
119 }
120 else {
121 switch (Opts.AnalysisStoreOpt) {
122 default:
123 assert(0 && "Unknown store manager.");
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000124#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000125 case NAME##Model: CreateStoreMgr = CREATEFN; break;
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000126#include "clang/Frontend/Analyses.def"
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000127 }
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000128 }
Mike Stump11289f42009-09-09 15:08:12 +0000129
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000130 if (ManagerRegistry::ConstraintMgrCreator != 0)
131 CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator;
132 else {
133 switch (Opts.AnalysisConstraintsOpt) {
134 default:
135 assert(0 && "Unknown store manager.");
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000136#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000137 case NAME##Model: CreateConstraintMgr = CREATEFN; break;
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000138#include "clang/Frontend/Analyses.def"
Zhongxing Xu4b03d492009-07-30 09:11:52 +0000139 }
140 }
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000141 }
Ted Kremenek39df94b2010-02-14 19:08:51 +0000142
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000143 void DisplayFunction(const Decl *D) {
144 if (!Opts.AnalyzerDisplayProgress || declDisplayed)
145 return;
Ted Kremenek39df94b2010-02-14 19:08:51 +0000146
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000147 declDisplayed = true;
Ted Kremenek68189912009-12-07 22:06:12 +0000148 SourceManager &SM = Mgr->getASTContext().getSourceManager();
149 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
150 llvm::errs() << "ANALYZE: " << Loc.getFilename();
151
Ted Kremenek06ba78d2009-12-09 03:45:19 +0000152 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
153 const NamedDecl *ND = cast<NamedDecl>(D);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000154 llvm::errs() << ' ' << ND << '\n';
Ted Kremenek0e7d2522008-07-03 04:29:21 +0000155 }
Ted Kremenek06ba78d2009-12-09 03:45:19 +0000156 else if (isa<BlockDecl>(D)) {
Ted Kremenek68189912009-12-07 22:06:12 +0000157 llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
Ted Kremenek06ba78d2009-12-09 03:45:19 +0000158 << Loc.getColumn() << '\n';
Ted Kremenek68189912009-12-07 22:06:12 +0000159 }
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000162 void addCodeAction(CodeAction action) {
163 FunctionActions.push_back(action);
164 ObjCMethodActions.push_back(action);
Zhongxing Xu685a1d82010-04-30 04:14:20 +0000165 CXXMethodActions.push_back(action);
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000166 }
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek39df94b2010-02-14 19:08:51 +0000168 void addTranslationUnitAction(TUAction action) {
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000169 TranslationUnitActions.push_back(action);
170 }
Mike Stump11289f42009-09-09 15:08:12 +0000171
Zhongxing Xu685a1d82010-04-30 04:14:20 +0000172 void addObjCImplementationAction(CodeAction action) {
173 ObjCImplementationActions.push_back(action);
174 }
175
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000176 virtual void Initialize(ASTContext &Context) {
177 Ctx = &Context;
178 Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
179 PP.getLangOptions(), PD,
180 CreateStoreMgr, CreateConstraintMgr,
Zhongxing Xu7d4bc9a2010-04-13 06:44:31 +0000181 Opts.MaxNodes,
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000182 Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
183 Opts.PurgeDead, Opts.EagerlyAssume,
184 Opts.TrimGraph));
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000187 virtual void HandleTranslationUnit(ASTContext &C);
Zhongxing Xu685a1d82010-04-30 04:14:20 +0000188 void HandleCode(Decl *D, Stmt* Body, Actions& actions);
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000189};
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000190} // end anonymous namespace
191
192namespace llvm {
Douglas Gregor120f6a62009-11-17 06:14:37 +0000193 template <> struct FoldingSetTrait<AnalysisConsumer::CodeAction> {
Ted Kremenek39df94b2010-02-14 19:08:51 +0000194 static inline void Profile(AnalysisConsumer::CodeAction X,
Douglas Gregor120f6a62009-11-17 06:14:37 +0000195 FoldingSetNodeID& ID) {
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000196 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
197 }
Mike Stump11289f42009-09-09 15:08:12 +0000198 };
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000199}
200
201//===----------------------------------------------------------------------===//
202// AnalysisConsumer implementation.
203//===----------------------------------------------------------------------===//
204
Chris Lattnercf169832009-03-28 04:11:33 +0000205void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000206
Ted Kremenek39df94b2010-02-14 19:08:51 +0000207 TranslationUnitDecl *TU = C.getTranslationUnitDecl();
208
Zhongxing Xu685a1d82010-04-30 04:14:20 +0000209 for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end();
210 I != E; ++I) {
211 Decl *D = *I;
212
213 switch (D->getKind()) {
214 case Decl::CXXConstructor:
215 case Decl::CXXDestructor:
216 case Decl::CXXConversion:
217 case Decl::CXXMethod:
218 case Decl::Function: {
219 FunctionDecl* FD = cast<FunctionDecl>(D);
220
221 if (FD->isThisDeclarationADefinition()) {
222 if (!Opts.AnalyzeSpecificFunction.empty() &&
223 FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction)
224 break;
225 HandleCode(FD, FD->getBody(), FunctionActions);
226 }
227 break;
228 }
229
230 case Decl::ObjCMethod: {
231 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
232
233 if (MD->isThisDeclarationADefinition()) {
234 if (!Opts.AnalyzeSpecificFunction.empty() &&
235 Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString())
236 break;
237 HandleCode(MD, MD->getBody(), ObjCMethodActions);
238 }
239 break;
240 }
241
242 case Decl::ObjCImplementation: {
243 ObjCImplementationDecl* ID = cast<ObjCImplementationDecl>(*I);
244 HandleCode(ID, 0, ObjCImplementationActions);
245
246 for (ObjCImplementationDecl::method_iterator MI = ID->meth_begin(),
247 ME = ID->meth_end(); MI != ME; ++MI) {
248 if ((*MI)->isThisDeclarationADefinition()) {
249 if (!Opts.AnalyzeSpecificFunction.empty() &&
250 Opts.AnalyzeSpecificFunction != (*MI)->getSelector().getAsString())
251 break;
252 HandleCode(*MI, (*MI)->getBody(), ObjCMethodActions);
253 }
254 }
255 break;
256 }
257
258 default:
259 break;
260 }
261 }
262
Ted Kremenek39df94b2010-02-14 19:08:51 +0000263 for (TUActions::iterator I = TranslationUnitActions.begin(),
264 E = TranslationUnitActions.end(); I != E; ++I) {
265 (*I)(*this, *Mgr, *TU);
Ted Kremenek3391cb02008-11-07 02:09:25 +0000266 }
267
Ted Kremenek6ddbb4c2009-08-02 05:43:14 +0000268 // Explicitly destroy the PathDiagnosticClient. This will flush its output.
269 // FIXME: This should be replaced with something that doesn't rely on
Zhongxing Xu765f3e42009-12-15 09:32:42 +0000270 // side-effects in PathDiagnosticClient's destructor. This is required when
271 // used with option -disable-free.
Zhongxing Xub9a45a42009-08-03 03:27:37 +0000272 Mgr.reset(NULL);
Ted Kremenek0e7d2522008-07-03 04:29:21 +0000273}
274
Ted Kremenek68189912009-12-07 22:06:12 +0000275static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) {
276 if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
277 WL.push_back(BD);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000278
Ted Kremenek68189912009-12-07 22:06:12 +0000279 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
280 I!=E; ++I)
281 if (DeclContext *DC = dyn_cast<DeclContext>(*I))
282 FindBlocks(DC, WL);
283}
284
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000285void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) {
Mike Stump11289f42009-09-09 15:08:12 +0000286
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000287 // Don't run the actions if an error has occured with parsing the file.
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000288 if (PP.getDiagnostics().hasErrorOccurred())
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000289 return;
Ted Kremenekbb70c082009-02-02 20:52:40 +0000290
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000291 // Don't run the actions on declarations in header files unless
292 // otherwise specified.
Eli Friedman996ef2f2009-05-19 10:18:02 +0000293 if (!Opts.AnalyzeAll &&
294 !Ctx->getSourceManager().isFromMainFile(D->getLocation()))
Mike Stump11289f42009-09-09 15:08:12 +0000295 return;
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000296
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000297 // Clear the AnalysisManager of old AnalysisContexts.
298 Mgr->ClearContexts();
Ted Kremenek39df94b2010-02-14 19:08:51 +0000299
Mike Stump11289f42009-09-09 15:08:12 +0000300 // Dispatch on the actions.
Ted Kremenek68189912009-12-07 22:06:12 +0000301 llvm::SmallVector<Decl*, 10> WL;
302 WL.push_back(D);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000303
Ted Kremenek752ecd82009-12-12 01:04:14 +0000304 if (Body && Opts.AnalyzeNestedBlocks)
Ted Kremenek68189912009-12-07 22:06:12 +0000305 FindBlocks(cast<DeclContext>(D), WL);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000306
Zhongxing Xu96553942008-10-30 05:03:28 +0000307 for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
Ted Kremenek68189912009-12-07 22:06:12 +0000308 for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end();
309 WI != WE; ++WI)
310 (*I)(*this, *Mgr, *WI);
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000311}
312
313//===----------------------------------------------------------------------===//
314// Analyses
315//===----------------------------------------------------------------------===//
316
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000317static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr,
318 Decl *D) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000319 if (LiveVariables *L = mgr.getLiveVariables(D)) {
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000320 BugReporter BR(mgr);
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000321 CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR);
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000322 }
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000323}
324
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000325static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr,
326 Decl *D) {
327 if (CFG* c = mgr.getCFG(D)) {
Zhongxing Xu342950e2009-08-25 06:51:30 +0000328 CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000329 }
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000330}
331
Ted Kremenek8e631a02008-07-02 00:44:58 +0000332
Ted Kremenek916061f2009-11-25 21:55:23 +0000333static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr,
Ted Kremenek39df94b2010-02-14 19:08:51 +0000334 Decl *D,
Ted Kremenek82f7f9c2009-09-18 05:37:41 +0000335 GRTransferFuncs* tf) {
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenekbecec2c2008-07-02 16:35:50 +0000337 llvm::OwningPtr<GRTransferFuncs> TF(tf);
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000338
Ted Kremenek1e7f9882009-09-18 22:29:35 +0000339 // Construct the analysis engine. We first query for the LiveVariables
340 // information to see if the CFG is valid.
341 // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
342 if (!mgr.getLiveVariables(D))
Ted Kremenek39df94b2010-02-14 19:08:51 +0000343 return;
Ted Kremenekde8e7442010-01-05 00:15:18 +0000344 GRExprEngine Eng(mgr, TF.take());
Ted Kremenek39df94b2010-02-14 19:08:51 +0000345
Ted Kremenek4ef13f82009-11-13 18:46:29 +0000346 if (C.Opts.EnableExperimentalInternalChecks)
347 RegisterExperimentalInternalChecks(Eng);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000348
Ted Kremenek82f7f9c2009-09-18 05:37:41 +0000349 RegisterAppleChecks(Eng, *D);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000350
Ted Kremenekaedb7432009-11-13 01:15:47 +0000351 if (C.Opts.EnableExperimentalChecks)
352 RegisterExperimentalChecks(Eng);
Ted Kremenek39df94b2010-02-14 19:08:51 +0000353
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000354 // Set the graph auditor.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000355 llvm::OwningPtr<ExplodedNode::Auditor> Auditor;
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000356 if (mgr.shouldVisualizeUbigraph()) {
357 Auditor.reset(CreateUbiViz());
Zhongxing Xu20227f72009-08-06 01:32:16 +0000358 ExplodedNode::SetAuditor(Auditor.get());
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenek8e631a02008-07-02 00:44:58 +0000361 // Execute the worklist algorithm.
Zhongxing Xu7d4bc9a2010-04-13 06:44:31 +0000362 Eng.ExecuteWorkList(mgr.getStackFrame(D), mgr.getMaxNodes());
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000364 // Release the auditor (if any) so that it doesn't monitor the graph
365 // created BugReporter.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000366 ExplodedNode::SetAuditor(0);
Ted Kremenek39df18f2009-03-11 01:42:29 +0000367
Ted Kremenek71ac8322008-07-02 16:49:11 +0000368 // Visualize the exploded graph.
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000369 if (mgr.shouldVisualizeGraphviz())
Ted Kremenek71ac8322008-07-02 16:49:11 +0000370 Eng.ViewGraph(mgr.shouldTrimGraph());
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek39df18f2009-03-11 01:42:29 +0000372 // Display warnings.
373 Eng.getBugReporter().FlushReports();
Ted Kremenekbecec2c2008-07-02 16:35:50 +0000374}
375
Ted Kremenek2f2692f2010-02-05 02:06:54 +0000376static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr,
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000377 Decl *D, bool GCEnabled) {
Mike Stump11289f42009-09-09 15:08:12 +0000378
Zhongxing Xu342950e2009-08-25 06:51:30 +0000379 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(),
Ted Kremenekbecec2c2008-07-02 16:35:50 +0000380 GCEnabled,
Ted Kremenekbecec2c2008-07-02 16:35:50 +0000381 mgr.getLangOptions());
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000383 ActionGRExprEngine(C, mgr, D, TF);
Ted Kremenek8e631a02008-07-02 00:44:58 +0000384}
385
Ted Kremenek2f2692f2010-02-05 02:06:54 +0000386static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr,
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000387 Decl *D) {
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek8e631a02008-07-02 00:44:58 +0000389 switch (mgr.getLangOptions().getGCMode()) {
Mike Stump281d6d72010-01-20 02:03:14 +0000390 default:
391 assert (false && "Invalid GC mode.");
392 case LangOptions::NonGC:
Ted Kremenek2f2692f2010-02-05 02:06:54 +0000393 ActionObjCMemCheckerAux(C, mgr, D, false);
Mike Stump281d6d72010-01-20 02:03:14 +0000394 break;
Mike Stump11289f42009-09-09 15:08:12 +0000395
Mike Stump281d6d72010-01-20 02:03:14 +0000396 case LangOptions::GCOnly:
Ted Kremenek2f2692f2010-02-05 02:06:54 +0000397 ActionObjCMemCheckerAux(C, mgr, D, true);
Mike Stump281d6d72010-01-20 02:03:14 +0000398 break;
Mike Stump11289f42009-09-09 15:08:12 +0000399
Mike Stump281d6d72010-01-20 02:03:14 +0000400 case LangOptions::HybridGC:
Ted Kremenek2f2692f2010-02-05 02:06:54 +0000401 ActionObjCMemCheckerAux(C, mgr, D, false);
402 ActionObjCMemCheckerAux(C, mgr, D, true);
Mike Stump281d6d72010-01-20 02:03:14 +0000403 break;
Ted Kremenek8e631a02008-07-02 00:44:58 +0000404 }
405}
406
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000407static void ActionDisplayLiveVariables(AnalysisConsumer &C,
408 AnalysisManager& mgr, Decl *D) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000409 if (LiveVariables* L = mgr.getLiveVariables(D)) {
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000410 L->dumpBlockLiveness(mgr.getSourceManager());
411 }
Ted Kremenekbdbe6132008-07-02 18:11:29 +0000412}
413
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000414static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
415 if (CFG *cfg = mgr.getCFG(D)) {
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000416 cfg->dump(mgr.getLangOptions());
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000417 }
Ted Kremenekf992cfb2008-07-02 18:23:21 +0000418}
419
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000420static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
421 if (CFG *cfg = mgr.getCFG(D)) {
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000422 cfg->viewCFG(mgr.getLangOptions());
Ted Kremenek1d3c7972008-07-03 05:26:14 +0000423 }
Ted Kremenekf992cfb2008-07-02 18:23:21 +0000424}
425
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000426static void ActionSecuritySyntacticChecks(AnalysisConsumer &C,
427 AnalysisManager &mgr, Decl *D) {
Mike Stump11289f42009-09-09 15:08:12 +0000428 BugReporter BR(mgr);
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000429 CheckSecuritySyntaxOnly(D, BR);
Ted Kremenekc5b4c0e2009-07-23 01:07:19 +0000430}
431
Ted Kremenek184b3382010-02-14 02:45:18 +0000432static void ActionLLVMConventionChecker(AnalysisConsumer &C,
433 AnalysisManager &mgr,
Ted Kremenek988805c2010-02-14 19:09:05 +0000434 TranslationUnitDecl &TU) {
Ted Kremenek184b3382010-02-14 02:45:18 +0000435 BugReporter BR(mgr);
Ted Kremenek988805c2010-02-14 19:09:05 +0000436 CheckLLVMConventions(TU, BR);
Ted Kremenek184b3382010-02-14 02:45:18 +0000437}
438
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000439static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr,
440 Decl *D) {
Ted Kremenek98980282008-08-04 17:14:10 +0000441 if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
442 return;
Ted Kremenek0e7d2522008-07-03 04:29:21 +0000443 BugReporter BR(mgr);
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000444 CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
Ted Kremenek0e7d2522008-07-03 04:29:21 +0000445}
446
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000447static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr,
448 Decl *D) {
Ted Kremenek3b28f492008-07-23 00:45:26 +0000449 BugReporter BR(mgr);
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000450 CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
Ted Kremenek3b28f492008-07-23 00:45:26 +0000451}
452
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000453static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr,
454 Decl *D) {
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000455 BugReporter BR(mgr);
Zhongxing Xu7e3431b2009-09-10 05:44:00 +0000456 CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
Ted Kremenek3bfb3142008-07-11 22:40:47 +0000457}
458
Ted Kremenek55d59bf2009-11-11 06:28:42 +0000459static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr,
460 Decl *D) {
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +0000461 BugReporter BR(mgr);
462 CheckSizeofPointer(D, BR);
463}
464
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000465//===----------------------------------------------------------------------===//
466// AnalysisConsumer creation.
467//===----------------------------------------------------------------------===//
468
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000469ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp,
Eli Friedman996ef2f2009-05-19 10:18:02 +0000470 const std::string& OutDir,
471 const AnalyzerOptions& Opts) {
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000472 llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts));
Ted Kremenekb5351812009-02-17 04:27:41 +0000473
Eli Friedman996ef2f2009-05-19 10:18:02 +0000474 for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
475 switch (Opts.AnalysisList[i]) {
Ted Kremenekfd7efdf2008-07-15 00:46:02 +0000476#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
Mike Stump281d6d72010-01-20 02:03:14 +0000477 case NAME:\
478 C->add ## SCOPE ## Action(&Action ## NAME);\
479 break;
Eli Friedman58ed0f22009-05-19 21:16:18 +0000480#include "clang/Frontend/Analyses.def"
Mike Stump281d6d72010-01-20 02:03:14 +0000481 default: break;
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000482 }
Mike Stump11289f42009-09-09 15:08:12 +0000483
Ted Kremenek094bc312009-05-07 19:02:53 +0000484 // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
Daniel Dunbarb5f20252009-11-05 02:41:58 +0000485 pp.getDiagnostics().setWarningsAsErrors(false);
Ted Kremenekb5351812009-02-17 04:27:41 +0000486
Ted Kremenekf0413bf2008-07-02 00:03:09 +0000487 return C.take();
488}
489
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000490//===----------------------------------------------------------------------===//
491// Ubigraph Visualization. FIXME: Move to separate file.
492//===----------------------------------------------------------------------===//
493
494namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000495
Zhongxing Xu20227f72009-08-06 01:32:16 +0000496class UbigraphViz : public ExplodedNode::Auditor {
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000497 llvm::OwningPtr<llvm::raw_ostream> Out;
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000498 llvm::sys::Path Dir, Filename;
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000499 unsigned Cntr;
500
501 typedef llvm::DenseMap<void*,unsigned> VMap;
502 VMap M;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000504public:
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000505 UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
Ted Kremenek803e7e12008-08-28 05:02:09 +0000506 llvm::sys::Path& filename);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000508 ~UbigraphViz();
Mike Stump11289f42009-09-09 15:08:12 +0000509
510 virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst);
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000511};
Mike Stump11289f42009-09-09 15:08:12 +0000512
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000513} // end anonymous namespace
514
Zhongxing Xu20227f72009-08-06 01:32:16 +0000515static ExplodedNode::Auditor* CreateUbiViz() {
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000516 std::string ErrMsg;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000518 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000519 if (!ErrMsg.empty())
520 return 0;
521
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000522 llvm::sys::Path Filename = Dir;
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000523 Filename.appendComponent("llvm_ubi");
524 Filename.makeUnique(true,&ErrMsg);
525
526 if (!ErrMsg.empty())
527 return 0;
528
Chris Lattner3441b4f2009-08-23 22:45:33 +0000529 llvm::errs() << "Writing '" << Filename.str() << "'.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000531 llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
Dan Gohmana98e0e72009-08-25 15:36:09 +0000532 Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000533
534 if (!ErrMsg.empty())
535 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000537 return new UbigraphViz(Stream.take(), Dir, Filename);
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000538}
539
Zhongxing Xu20227f72009-08-06 01:32:16 +0000540void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) {
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenek2eb49f22008-08-28 18:34:41 +0000542 assert (Src != Dst && "Self-edges are not allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000543
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000544 // Lookup the Src. If it is a new node, it's a root.
545 VMap::iterator SrcI= M.find(Src);
546 unsigned SrcID;
Mike Stump11289f42009-09-09 15:08:12 +0000547
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000548 if (SrcI == M.end()) {
549 M[Src] = SrcID = Cntr++;
550 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
551 }
552 else
553 SrcID = SrcI->second;
Mike Stump11289f42009-09-09 15:08:12 +0000554
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000555 // Lookup the Dst.
556 VMap::iterator DstI= M.find(Dst);
557 unsigned DstID;
558
559 if (DstI == M.end()) {
560 M[Dst] = DstID = Cntr++;
561 *Out << "('vertex', " << DstID << ")\n";
562 }
Ted Kremenek803e7e12008-08-28 05:02:09 +0000563 else {
564 // We have hit DstID before. Change its style to reflect a cache hit.
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000565 DstID = DstI->second;
Ted Kremenek803e7e12008-08-28 05:02:09 +0000566 *Out << "('change_vertex_style', " << DstID << ", 1)\n";
567 }
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000568
569 // Add the edge.
Mike Stump11289f42009-09-09 15:08:12 +0000570 *Out << "('edge', " << SrcID << ", " << DstID
Ted Kremenekc8288b42008-08-27 22:46:55 +0000571 << ", ('arrow','true'), ('oriented', 'true'))\n";
Ted Kremenekd461d7a2008-08-27 22:31:43 +0000572}
573
Ted Kremenek803e7e12008-08-28 05:02:09 +0000574UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
575 llvm::sys::Path& filename)
576 : Out(out), Dir(dir), Filename(filename), Cntr(0) {
577
578 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
579 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
580 " ('size', '1.5'))\n";
581}
582
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000583UbigraphViz::~UbigraphViz() {
584 Out.reset(0);
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000585 llvm::errs() << "Running 'ubiviz' program... ";
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000586 std::string ErrMsg;
587 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
588 std::vector<const char*> args;
589 args.push_back(Ubiviz.c_str());
590 args.push_back(Filename.c_str());
591 args.push_back(0);
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000593 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
Benjamin Kramer89b422c2009-08-23 12:08:50 +0000594 llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Ted Kremenek266ec3f2008-08-28 03:54:51 +0000597 // Delete the directory.
Mike Stump11289f42009-09-09 15:08:12 +0000598 Dir.eraseFromDisk(true);
Daniel Dunbar9a0ea042008-08-29 03:45:59 +0000599}