blob: 94f59f196ee71f21e281da5afc9300201e60e311 [file] [log] [blame]
Ted Kremeneka0a5ca12011-03-15 03:17:07 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenekb749a6d2011-01-15 02:58:47 +00002//
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// This file implements uninitialized values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Richard Smith130b8d42012-07-13 23:33:44 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000016#include "clang/AST/Decl.h"
Jordan Rosea7f94ce2013-05-15 23:22:55 +000017#include "clang/AST/StmtVisitor.h"
Artyom Skrobov27720762014-09-23 08:34:41 +000018#include "clang/Analysis/Analyses/PostOrderCFGView.h"
Ted Kremeneka0a5ca12011-03-15 03:17:07 +000019#include "clang/Analysis/Analyses/UninitializedValues.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/Analysis/AnalysisContext.h"
21#include "clang/Analysis/CFG.h"
Ted Kremenekedf22ed2012-09-13 00:21:35 +000022#include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/Optional.h"
25#include "llvm/ADT/PackedVector.h"
26#include "llvm/ADT/SmallBitVector.h"
27#include "llvm/ADT/SmallVector.h"
Argyrios Kyrtzidis981a9612012-03-01 19:45:56 +000028#include "llvm/Support/SaveAndRestore.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000029#include <utility>
Ted Kremenekb749a6d2011-01-15 02:58:47 +000030
31using namespace clang;
32
Richard Smith130b8d42012-07-13 23:33:44 +000033#define DEBUG_LOGGING 0
34
Ted Kremenek93a31382011-01-27 02:29:34 +000035static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
Ted Kremenekc15a4e42011-03-17 03:06:11 +000036 if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
Richard Smithd88b44d2014-06-11 00:31:00 +000037 !vd->isExceptionVariable() && !vd->isInitCapture() &&
Ted Kremenekc15a4e42011-03-17 03:06:11 +000038 vd->getDeclContext() == dc) {
39 QualType ty = vd->getType();
40 return ty->isScalarType() || ty->isVectorType();
41 }
42 return false;
Ted Kremenekcab479f2011-01-18 04:53:25 +000043}
44
Ted Kremenekb749a6d2011-01-15 02:58:47 +000045//------------------------------------------------------------------------====//
Ted Kremeneka895fe92011-03-15 04:57:27 +000046// DeclToIndex: a mapping from Decls we track to value indices.
Ted Kremenekb749a6d2011-01-15 02:58:47 +000047//====------------------------------------------------------------------------//
48
49namespace {
Ted Kremeneka895fe92011-03-15 04:57:27 +000050class DeclToIndex {
Ted Kremenekb749a6d2011-01-15 02:58:47 +000051 llvm::DenseMap<const VarDecl *, unsigned> map;
52public:
Ted Kremeneka895fe92011-03-15 04:57:27 +000053 DeclToIndex() {}
Ted Kremenekb749a6d2011-01-15 02:58:47 +000054
55 /// Compute the actual mapping from declarations to bits.
56 void computeMap(const DeclContext &dc);
57
58 /// Return the number of declarations in the map.
59 unsigned size() const { return map.size(); }
60
61 /// Returns the bit vector index for a given declaration.
David Blaikie05785d12013-02-20 22:23:23 +000062 Optional<unsigned> getValueIndex(const VarDecl *d) const;
Ted Kremenekb749a6d2011-01-15 02:58:47 +000063};
64}
65
Ted Kremeneka895fe92011-03-15 04:57:27 +000066void DeclToIndex::computeMap(const DeclContext &dc) {
Ted Kremenekb749a6d2011-01-15 02:58:47 +000067 unsigned count = 0;
68 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
69 E(dc.decls_end());
70 for ( ; I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +000071 const VarDecl *vd = *I;
Ted Kremenek93a31382011-01-27 02:29:34 +000072 if (isTrackedVar(vd, &dc))
Ted Kremenekb749a6d2011-01-15 02:58:47 +000073 map[vd] = count++;
74 }
75}
76
David Blaikie05785d12013-02-20 22:23:23 +000077Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
Ted Kremenek03325c42011-03-29 01:40:00 +000078 llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
Ted Kremenekb749a6d2011-01-15 02:58:47 +000079 if (I == map.end())
David Blaikie7a30dc52013-02-21 01:47:18 +000080 return None;
Ted Kremenekb749a6d2011-01-15 02:58:47 +000081 return I->second;
82}
83
84//------------------------------------------------------------------------====//
85// CFGBlockValues: dataflow values for CFG blocks.
86//====------------------------------------------------------------------------//
87
Ted Kremenekc8c4e5f2011-03-15 04:57:38 +000088// These values are defined in such a way that a merge can be done using
89// a bitwise OR.
90enum Value { Unknown = 0x0, /* 00 */
91 Initialized = 0x1, /* 01 */
92 Uninitialized = 0x2, /* 10 */
93 MayUninitialized = 0x3 /* 11 */ };
94
95static bool isUninitialized(const Value v) {
96 return v >= Uninitialized;
97}
98static bool isAlwaysUninit(const Value v) {
99 return v == Uninitialized;
100}
Ted Kremenekd3def382011-03-15 04:57:29 +0000101
Benjamin Kramer8aef5962011-03-26 12:38:21 +0000102namespace {
Ted Kremenek9b15c962011-03-15 04:57:32 +0000103
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000104typedef llvm::PackedVector<Value, 2, llvm::SmallBitVector> ValueVector;
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000105
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000106class CFGBlockValues {
107 const CFG &cfg;
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000108 SmallVector<ValueVector, 8> vals;
Ted Kremeneka895fe92011-03-15 04:57:27 +0000109 ValueVector scratch;
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000110 DeclToIndex declToIndex;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000111public:
112 CFGBlockValues(const CFG &cfg);
Ted Kremenek6080d322012-07-19 04:59:05 +0000113
Ted Kremenek37881932011-04-04 23:29:12 +0000114 unsigned getNumEntries() const { return declToIndex.size(); }
115
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000116 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek6080d322012-07-19 04:59:05 +0000117 ValueVector &getValueVector(const CFGBlock *block) {
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000118 return vals[block->getBlockID()];
Ted Kremenek6080d322012-07-19 04:59:05 +0000119 }
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000120
Richard Smithb721e302012-07-02 23:23:04 +0000121 void setAllScratchValues(Value V);
Ted Kremeneka895fe92011-03-15 04:57:27 +0000122 void mergeIntoScratch(ValueVector const &source, bool isFirst);
123 bool updateValueVectorWithScratch(const CFGBlock *block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000124
125 bool hasNoDeclarations() const {
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000126 return declToIndex.size() == 0;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000127 }
Ted Kremenek417d5662011-08-20 01:15:28 +0000128
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000129 void resetScratch();
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000130
Ted Kremeneka895fe92011-03-15 04:57:27 +0000131 ValueVector::reference operator[](const VarDecl *vd);
Richard Smith4323bf82012-05-25 02:17:09 +0000132
133 Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
134 const VarDecl *vd) {
David Blaikie05785d12013-02-20 22:23:23 +0000135 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
Richard Smith4323bf82012-05-25 02:17:09 +0000136 assert(idx.hasValue());
Ted Kremenek6080d322012-07-19 04:59:05 +0000137 return getValueVector(block)[idx.getValue()];
Richard Smith4323bf82012-05-25 02:17:09 +0000138 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000139};
Benjamin Kramer8aef5962011-03-26 12:38:21 +0000140} // end anonymous namespace
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000141
Ted Kremenek6080d322012-07-19 04:59:05 +0000142CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000143
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000144void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000145 declToIndex.computeMap(dc);
Ted Kremenek6080d322012-07-19 04:59:05 +0000146 unsigned decls = declToIndex.size();
147 scratch.resize(decls);
148 unsigned n = cfg.getNumBlockIDs();
149 if (!n)
150 return;
151 vals.resize(n);
152 for (unsigned i = 0; i < n; ++i)
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000153 vals[i].resize(decls);
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000154}
155
Richard Smith130b8d42012-07-13 23:33:44 +0000156#if DEBUG_LOGGING
Ted Kremeneka895fe92011-03-15 04:57:27 +0000157static void printVector(const CFGBlock *block, ValueVector &bv,
Ted Kremenekba357292011-02-01 17:43:18 +0000158 unsigned num) {
Ted Kremenekba357292011-02-01 17:43:18 +0000159 llvm::errs() << block->getBlockID() << " :";
160 for (unsigned i = 0; i < bv.size(); ++i) {
161 llvm::errs() << ' ' << bv[i];
162 }
163 llvm::errs() << " : " << num << '\n';
164}
165#endif
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000166
Richard Smithb721e302012-07-02 23:23:04 +0000167void CFGBlockValues::setAllScratchValues(Value V) {
168 for (unsigned I = 0, E = scratch.size(); I != E; ++I)
169 scratch[I] = V;
170}
171
Ted Kremenekf8fd4d42011-10-07 00:42:48 +0000172void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
173 bool isFirst) {
174 if (isFirst)
175 scratch = source;
176 else
177 scratch |= source;
178}
179
Ted Kremeneka895fe92011-03-15 04:57:27 +0000180bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
Ted Kremenek6080d322012-07-19 04:59:05 +0000181 ValueVector &dst = getValueVector(block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000182 bool changed = (dst != scratch);
183 if (changed)
184 dst = scratch;
Richard Smith130b8d42012-07-13 23:33:44 +0000185#if DEBUG_LOGGING
Ted Kremenekba357292011-02-01 17:43:18 +0000186 printVector(block, scratch, 0);
187#endif
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000188 return changed;
189}
190
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000191void CFGBlockValues::resetScratch() {
192 scratch.reset();
193}
194
Ted Kremeneka895fe92011-03-15 04:57:27 +0000195ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
David Blaikie05785d12013-02-20 22:23:23 +0000196 const Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000197 assert(idx.hasValue());
198 return scratch[idx.getValue()];
199}
200
201//------------------------------------------------------------------------====//
Artyom Skrobov27720762014-09-23 08:34:41 +0000202// Worklist: worklist for dataflow analysis.
203//====------------------------------------------------------------------------//
204
205namespace {
206class DataflowWorklist {
207 PostOrderCFGView::iterator PO_I, PO_E;
208 SmallVector<const CFGBlock *, 20> worklist;
209 llvm::BitVector enqueuedBlocks;
210public:
211 DataflowWorklist(const CFG &cfg, PostOrderCFGView &view)
212 : PO_I(view.begin()), PO_E(view.end()),
213 enqueuedBlocks(cfg.getNumBlockIDs(), true) {
214 // Treat the first block as already analyzed.
215 if (PO_I != PO_E) {
216 assert(*PO_I == &cfg.getEntry());
217 enqueuedBlocks[(*PO_I)->getBlockID()] = false;
218 ++PO_I;
219 }
220 }
221
222 void enqueueSuccessors(const CFGBlock *block);
223 const CFGBlock *dequeue();
224};
225}
226
227void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
228 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
229 E = block->succ_end(); I != E; ++I) {
230 const CFGBlock *Successor = *I;
231 if (!Successor || enqueuedBlocks[Successor->getBlockID()])
232 continue;
233 worklist.push_back(Successor);
234 enqueuedBlocks[Successor->getBlockID()] = true;
235 }
236}
237
238const CFGBlock *DataflowWorklist::dequeue() {
239 const CFGBlock *B = nullptr;
240
241 // First dequeue from the worklist. This can represent
242 // updates along backedges that we want propagated as quickly as possible.
243 if (!worklist.empty())
244 B = worklist.pop_back_val();
245
246 // Next dequeue from the initial reverse post order. This is the
247 // theoretical ideal in the presence of no back edges.
248 else if (PO_I != PO_E) {
249 B = *PO_I;
250 ++PO_I;
251 }
252 else {
253 return nullptr;
254 }
255
256 assert(enqueuedBlocks[B->getBlockID()] == true);
257 enqueuedBlocks[B->getBlockID()] = false;
258 return B;
259}
260
261//------------------------------------------------------------------------====//
Richard Smith6376d1f2012-07-17 00:06:14 +0000262// Classification of DeclRefExprs as use or initialization.
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000263//====------------------------------------------------------------------------//
264
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000265namespace {
266class FindVarResult {
267 const VarDecl *vd;
268 const DeclRefExpr *dr;
269public:
Richard Smith6376d1f2012-07-17 00:06:14 +0000270 FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
271
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000272 const DeclRefExpr *getDeclRefExpr() const { return dr; }
273 const VarDecl *getDecl() const { return vd; }
274};
Richard Smith6376d1f2012-07-17 00:06:14 +0000275
276static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
277 while (Ex) {
278 Ex = Ex->IgnoreParenNoopCasts(C);
279 if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
280 if (CE->getCastKind() == CK_LValueBitCast) {
281 Ex = CE->getSubExpr();
282 continue;
283 }
284 }
285 break;
286 }
287 return Ex;
288}
289
290/// If E is an expression comprising a reference to a single variable, find that
291/// variable.
292static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
293 if (const DeclRefExpr *DRE =
294 dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
295 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
296 if (isTrackedVar(VD, DC))
297 return FindVarResult(VD, DRE);
Craig Topper25542942014-05-20 04:30:07 +0000298 return FindVarResult(nullptr, nullptr);
Richard Smith6376d1f2012-07-17 00:06:14 +0000299}
300
301/// \brief Classify each DeclRefExpr as an initialization or a use. Any
302/// DeclRefExpr which isn't explicitly classified will be assumed to have
303/// escaped the analysis and will be treated as an initialization.
304class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
305public:
306 enum Class {
307 Init,
308 Use,
309 SelfInit,
310 Ignore
311 };
312
313private:
314 const DeclContext *DC;
315 llvm::DenseMap<const DeclRefExpr*, Class> Classification;
316
317 bool isTrackedVar(const VarDecl *VD) const {
318 return ::isTrackedVar(VD, DC);
319 }
320
321 void classify(const Expr *E, Class C);
322
323public:
324 ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
325
326 void VisitDeclStmt(DeclStmt *DS);
327 void VisitUnaryOperator(UnaryOperator *UO);
328 void VisitBinaryOperator(BinaryOperator *BO);
329 void VisitCallExpr(CallExpr *CE);
330 void VisitCastExpr(CastExpr *CE);
331
332 void operator()(Stmt *S) { Visit(S); }
333
334 Class get(const DeclRefExpr *DRE) const {
335 llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
336 = Classification.find(DRE);
337 if (I != Classification.end())
338 return I->second;
339
340 const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
341 if (!VD || !isTrackedVar(VD))
342 return Ignore;
343
344 return Init;
345 }
346};
347}
348
349static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
350 if (Expr *Init = VD->getInit()) {
351 const DeclRefExpr *DRE
352 = dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
353 if (DRE && DRE->getDecl() == VD)
354 return DRE;
355 }
Craig Topper25542942014-05-20 04:30:07 +0000356 return nullptr;
Richard Smith6376d1f2012-07-17 00:06:14 +0000357}
358
359void ClassifyRefs::classify(const Expr *E, Class C) {
Ted Kremenek7ba78c62013-01-19 00:25:06 +0000360 // The result of a ?: could also be an lvalue.
361 E = E->IgnoreParens();
362 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
Richard Trieuabf6ec42014-08-27 22:15:10 +0000363 classify(CO->getTrueExpr(), C);
Ted Kremenek7ba78c62013-01-19 00:25:06 +0000364 classify(CO->getFalseExpr(), C);
365 return;
366 }
367
Richard Trieuabf6ec42014-08-27 22:15:10 +0000368 if (const BinaryConditionalOperator *BCO =
369 dyn_cast<BinaryConditionalOperator>(E)) {
370 classify(BCO->getFalseExpr(), C);
371 return;
372 }
373
374 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
375 classify(OVE->getSourceExpr(), C);
376 return;
377 }
378
379 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
380 if (BO->getOpcode() == BO_Comma)
381 classify(BO->getRHS(), C);
382 return;
383 }
384
Richard Smith6376d1f2012-07-17 00:06:14 +0000385 FindVarResult Var = findVar(E, DC);
386 if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
387 Classification[DRE] = std::max(Classification[DRE], C);
388}
389
390void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000391 for (auto *DI : DS->decls()) {
392 VarDecl *VD = dyn_cast<VarDecl>(DI);
Richard Smith6376d1f2012-07-17 00:06:14 +0000393 if (VD && isTrackedVar(VD))
394 if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
395 Classification[DRE] = SelfInit;
396 }
397}
398
399void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
400 // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
401 // is not a compound-assignment, we will treat it as initializing the variable
402 // when TransferFunctions visits it. A compound-assignment does not affect
403 // whether a variable is uninitialized, and there's no point counting it as a
404 // use.
Richard Smithb21dd022012-07-17 01:27:33 +0000405 if (BO->isCompoundAssignmentOp())
406 classify(BO->getLHS(), Use);
407 else if (BO->getOpcode() == BO_Assign)
Richard Smith6376d1f2012-07-17 00:06:14 +0000408 classify(BO->getLHS(), Ignore);
409}
410
411void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
412 // Increment and decrement are uses despite there being no lvalue-to-rvalue
413 // conversion.
414 if (UO->isIncrementDecrementOp())
415 classify(UO->getSubExpr(), Use);
416}
417
418void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
Richard Trieu11fd0792014-08-26 04:30:55 +0000419 // Classify arguments to std::move as used.
420 if (CE->getNumArgs() == 1) {
421 if (FunctionDecl *FD = CE->getDirectCallee()) {
422 if (FD->getIdentifier() && FD->getIdentifier()->isStr("move")) {
423 classify(CE->getArg(0), Use);
424 return;
425 }
426 }
427 }
428
Richard Smith6376d1f2012-07-17 00:06:14 +0000429 // If a value is passed by const reference to a function, we should not assume
430 // that it is initialized by the call, and we conservatively do not assume
431 // that it is used.
432 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
433 I != E; ++I)
434 if ((*I)->getType().isConstQualified() && (*I)->isGLValue())
435 classify(*I, Ignore);
436}
437
438void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
439 if (CE->getCastKind() == CK_LValueToRValue)
440 classify(CE->getSubExpr(), Use);
441 else if (CStyleCastExpr *CSE = dyn_cast<CStyleCastExpr>(CE)) {
442 if (CSE->getType()->isVoidType()) {
443 // Squelch any detected load of an uninitialized value if
444 // we cast it to void.
445 // e.g. (void) x;
446 classify(CSE->getSubExpr(), Ignore);
447 }
448 }
449}
450
451//------------------------------------------------------------------------====//
452// Transfer function for uninitialized values analysis.
453//====------------------------------------------------------------------------//
454
455namespace {
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000456class TransferFunctions : public StmtVisitor<TransferFunctions> {
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000457 CFGBlockValues &vals;
458 const CFG &cfg;
Richard Smith4323bf82012-05-25 02:17:09 +0000459 const CFGBlock *block;
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000460 AnalysisDeclContext &ac;
Richard Smith6376d1f2012-07-17 00:06:14 +0000461 const ClassifyRefs &classification;
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000462 ObjCNoReturn objCNoRet;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000463 UninitVariablesHandler &handler;
Richard Smith6376d1f2012-07-17 00:06:14 +0000464
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000465public:
466 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Richard Smith4323bf82012-05-25 02:17:09 +0000467 const CFGBlock *block, AnalysisDeclContext &ac,
Richard Smith6376d1f2012-07-17 00:06:14 +0000468 const ClassifyRefs &classification,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000469 UninitVariablesHandler &handler)
Richard Smith6376d1f2012-07-17 00:06:14 +0000470 : vals(vals), cfg(cfg), block(block), ac(ac),
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000471 classification(classification), objCNoRet(ac.getASTContext()),
472 handler(handler) {}
Richard Smith6376d1f2012-07-17 00:06:14 +0000473
Richard Smith3d31e8b2012-05-24 23:45:35 +0000474 void reportUse(const Expr *ex, const VarDecl *vd);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000475
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000476 void VisitBinaryOperator(BinaryOperator *bo);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000477 void VisitBlockExpr(BlockExpr *be);
Richard Smithb721e302012-07-02 23:23:04 +0000478 void VisitCallExpr(CallExpr *ce);
Ted Kremenekb63931e2011-01-18 21:18:58 +0000479 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000480 void VisitDeclStmt(DeclStmt *ds);
481 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
482 void VisitObjCMessageExpr(ObjCMessageExpr *ME);
Richard Smith4323bf82012-05-25 02:17:09 +0000483
Ted Kremenek93a31382011-01-27 02:29:34 +0000484 bool isTrackedVar(const VarDecl *vd) {
485 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
486 }
Richard Smith4323bf82012-05-25 02:17:09 +0000487
Richard Smith6376d1f2012-07-17 00:06:14 +0000488 FindVarResult findVar(const Expr *ex) {
489 return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
490 }
491
Richard Smith4323bf82012-05-25 02:17:09 +0000492 UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
493 UninitUse Use(ex, isAlwaysUninit(v));
494
495 assert(isUninitialized(v));
496 if (Use.getKind() == UninitUse::Always)
497 return Use;
498
499 // If an edge which leads unconditionally to this use did not initialize
500 // the variable, we can say something stronger than 'may be uninitialized':
501 // we can say 'either it's used uninitialized or you have dead code'.
502 //
503 // We track the number of successors of a node which have been visited, and
504 // visit a node once we have visited all of its successors. Only edges where
505 // the variable might still be uninitialized are followed. Since a variable
506 // can't transfer from being initialized to being uninitialized, this will
507 // trace out the subgraph which inevitably leads to the use and does not
508 // initialize the variable. We do not want to skip past loops, since their
509 // non-termination might be correlated with the initialization condition.
510 //
511 // For example:
512 //
513 // void f(bool a, bool b) {
514 // block1: int n;
515 // if (a) {
516 // block2: if (b)
517 // block3: n = 1;
518 // block4: } else if (b) {
519 // block5: while (!a) {
520 // block6: do_work(&a);
521 // n = 2;
522 // }
523 // }
524 // block7: if (a)
525 // block8: g();
526 // block9: return n;
527 // }
528 //
529 // Starting from the maybe-uninitialized use in block 9:
530 // * Block 7 is not visited because we have only visited one of its two
531 // successors.
532 // * Block 8 is visited because we've visited its only successor.
533 // From block 8:
534 // * Block 7 is visited because we've now visited both of its successors.
535 // From block 7:
536 // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
537 // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
538 // * Block 3 is not visited because it initializes 'n'.
539 // Now the algorithm terminates, having visited blocks 7 and 8, and having
540 // found the frontier is blocks 2, 4, and 5.
541 //
542 // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
543 // and 4), so we report that any time either of those edges is taken (in
544 // each case when 'b == false'), 'n' is used uninitialized.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000545 SmallVector<const CFGBlock*, 32> Queue;
546 SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
Richard Smith4323bf82012-05-25 02:17:09 +0000547 Queue.push_back(block);
548 // Specify that we've already visited all successors of the starting block.
549 // This has the dual purpose of ensuring we never add it to the queue, and
550 // of marking it as not being a candidate element of the frontier.
551 SuccsVisited[block->getBlockID()] = block->succ_size();
552 while (!Queue.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000553 const CFGBlock *B = Queue.pop_back_val();
Richard Smithba8071e2013-09-12 18:49:10 +0000554
555 // If the use is always reached from the entry block, make a note of that.
556 if (B == &cfg.getEntry())
557 Use.setUninitAfterCall();
558
Richard Smith4323bf82012-05-25 02:17:09 +0000559 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
560 I != E; ++I) {
561 const CFGBlock *Pred = *I;
Ted Kremenek4b6fee62014-02-27 00:24:00 +0000562 if (!Pred)
563 continue;
564
Richard Smithba8071e2013-09-12 18:49:10 +0000565 Value AtPredExit = vals.getValue(Pred, B, vd);
566 if (AtPredExit == Initialized)
Richard Smith4323bf82012-05-25 02:17:09 +0000567 // This block initializes the variable.
568 continue;
Richard Smithba8071e2013-09-12 18:49:10 +0000569 if (AtPredExit == MayUninitialized &&
Craig Topper25542942014-05-20 04:30:07 +0000570 vals.getValue(B, nullptr, vd) == Uninitialized) {
Richard Smithba8071e2013-09-12 18:49:10 +0000571 // This block declares the variable (uninitialized), and is reachable
572 // from a block that initializes the variable. We can't guarantee to
573 // give an earlier location for the diagnostic (and it appears that
574 // this code is intended to be reachable) so give a diagnostic here
575 // and go no further down this path.
576 Use.setUninitAfterDecl();
577 continue;
578 }
Richard Smith4323bf82012-05-25 02:17:09 +0000579
Richard Smith130b8d42012-07-13 23:33:44 +0000580 unsigned &SV = SuccsVisited[Pred->getBlockID()];
581 if (!SV) {
582 // When visiting the first successor of a block, mark all NULL
583 // successors as having been visited.
584 for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
585 SE = Pred->succ_end();
586 SI != SE; ++SI)
587 if (!*SI)
588 ++SV;
589 }
590
591 if (++SV == Pred->succ_size())
Richard Smith4323bf82012-05-25 02:17:09 +0000592 // All paths from this block lead to the use and don't initialize the
593 // variable.
594 Queue.push_back(Pred);
595 }
596 }
597
598 // Scan the frontier, looking for blocks where the variable was
599 // uninitialized.
600 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
601 const CFGBlock *Block = *BI;
602 unsigned BlockID = Block->getBlockID();
603 const Stmt *Term = Block->getTerminator();
604 if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
605 Term) {
606 // This block inevitably leads to the use. If we have an edge from here
607 // to a post-dominator block, and the variable is uninitialized on that
608 // edge, we have found a bug.
609 for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
610 E = Block->succ_end(); I != E; ++I) {
611 const CFGBlock *Succ = *I;
612 if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
613 vals.getValue(Block, Succ, vd) == Uninitialized) {
614 // Switch cases are a special case: report the label to the caller
615 // as the 'terminator', not the switch statement itself. Suppress
616 // situations where no label matched: we can't be sure that's
617 // possible.
618 if (isa<SwitchStmt>(Term)) {
619 const Stmt *Label = Succ->getLabel();
620 if (!Label || !isa<SwitchCase>(Label))
621 // Might not be possible.
622 continue;
623 UninitUse::Branch Branch;
624 Branch.Terminator = Label;
625 Branch.Output = 0; // Ignored.
626 Use.addUninitBranch(Branch);
627 } else {
628 UninitUse::Branch Branch;
629 Branch.Terminator = Term;
630 Branch.Output = I - Block->succ_begin();
631 Use.addUninitBranch(Branch);
632 }
633 }
634 }
635 }
636 }
637
638 return Use;
639 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000640};
641}
642
Richard Smith3d31e8b2012-05-24 23:45:35 +0000643void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
Richard Smith3d31e8b2012-05-24 23:45:35 +0000644 Value v = vals[vd];
645 if (isUninitialized(v))
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000646 handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000647}
648
Richard Smith6376d1f2012-07-17 00:06:14 +0000649void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
Ted Kremenek4058d872011-01-27 02:01:31 +0000650 // This represents an initialization of the 'element' value.
Richard Smith6376d1f2012-07-17 00:06:14 +0000651 if (DeclStmt *DS = dyn_cast<DeclStmt>(FS->getElement())) {
652 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
653 if (isTrackedVar(VD))
654 vals[VD] = Initialized;
Ted Kremenek4058d872011-01-27 02:01:31 +0000655 }
Ted Kremenek4058d872011-01-27 02:01:31 +0000656}
657
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000658void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
Ted Kremenek77361762011-03-31 22:32:41 +0000659 const BlockDecl *bd = be->getBlockDecl();
Aaron Ballman9371dd22014-03-14 18:34:04 +0000660 for (const auto &I : bd->captures()) {
661 const VarDecl *vd = I.getVariable();
Ted Kremenek77361762011-03-31 22:32:41 +0000662 if (!isTrackedVar(vd))
663 continue;
Aaron Ballman9371dd22014-03-14 18:34:04 +0000664 if (I.isByRef()) {
Ted Kremenek77361762011-03-31 22:32:41 +0000665 vals[vd] = Initialized;
666 continue;
667 }
Richard Smith3d31e8b2012-05-24 23:45:35 +0000668 reportUse(be, vd);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000669 }
670}
671
Richard Smithb721e302012-07-02 23:23:04 +0000672void TransferFunctions::VisitCallExpr(CallExpr *ce) {
Ted Kremenek7979ccf2012-09-12 05:53:43 +0000673 if (Decl *Callee = ce->getCalleeDecl()) {
674 if (Callee->hasAttr<ReturnsTwiceAttr>()) {
675 // After a call to a function like setjmp or vfork, any variable which is
676 // initialized anywhere within this function may now be initialized. For
677 // now, just assume such a call initializes all variables. FIXME: Only
678 // mark variables as initialized if they have an initializer which is
679 // reachable from here.
680 vals.setAllScratchValues(Initialized);
681 }
682 else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
683 // Functions labeled like "analyzer_noreturn" are often used to denote
684 // "panic" functions that in special debug situations can still return,
685 // but for the most part should not be treated as returning. This is a
686 // useful annotation borrowed from the static analyzer that is useful for
687 // suppressing branch-specific false positives when we call one of these
688 // functions but keep pretending the path continues (when in reality the
689 // user doesn't care).
690 vals.setAllScratchValues(Unknown);
691 }
692 }
Richard Smithb721e302012-07-02 23:23:04 +0000693}
694
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000695void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
Richard Smith6376d1f2012-07-17 00:06:14 +0000696 switch (classification.get(dr)) {
697 case ClassifyRefs::Ignore:
698 break;
699 case ClassifyRefs::Use:
700 reportUse(dr, cast<VarDecl>(dr->getDecl()));
701 break;
702 case ClassifyRefs::Init:
703 vals[cast<VarDecl>(dr->getDecl())] = Initialized;
704 break;
705 case ClassifyRefs::SelfInit:
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000706 handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
Richard Smith6376d1f2012-07-17 00:06:14 +0000707 break;
708 }
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000709}
710
Richard Smith6376d1f2012-07-17 00:06:14 +0000711void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
712 if (BO->getOpcode() == BO_Assign) {
713 FindVarResult Var = findVar(BO->getLHS());
714 if (const VarDecl *VD = Var.getDecl())
715 vals[VD] = Initialized;
716 }
717}
718
719void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000720 for (auto *DI : DS->decls()) {
721 VarDecl *VD = dyn_cast<VarDecl>(DI);
Richard Smith6376d1f2012-07-17 00:06:14 +0000722 if (VD && isTrackedVar(VD)) {
723 if (getSelfInitExpr(VD)) {
724 // If the initializer consists solely of a reference to itself, we
725 // explicitly mark the variable as uninitialized. This allows code
726 // like the following:
727 //
728 // int x = x;
729 //
730 // to deliberately leave a variable uninitialized. Different analysis
731 // clients can detect this pattern and adjust their reporting
732 // appropriately, but we need to continue to analyze subsequent uses
733 // of the variable.
734 vals[VD] = Uninitialized;
735 } else if (VD->getInit()) {
736 // Treat the new variable as initialized.
737 vals[VD] = Initialized;
738 } else {
739 // No initializer: the variable is now uninitialized. This matters
740 // for cases like:
741 // while (...) {
742 // int n;
743 // use(n);
744 // n = 0;
745 // }
746 // FIXME: Mark the variable as uninitialized whenever its scope is
747 // left, since its scope could be re-entered by a jump over the
748 // declaration.
749 vals[VD] = Uninitialized;
Ted Kremenekb63931e2011-01-18 21:18:58 +0000750 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000751 }
752 }
753}
754
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000755void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
756 // If the Objective-C message expression is an implicit no-return that
757 // is not modeled in the CFG, set the tracked dataflow values to Unknown.
758 if (objCNoRet.isImplicitNoReturn(ME)) {
759 vals.setAllScratchValues(Unknown);
760 }
761}
762
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000763//------------------------------------------------------------------------====//
764// High-level "driver" logic for uninitialized values analysis.
765//====------------------------------------------------------------------------//
766
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000767static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000768 AnalysisDeclContext &ac, CFGBlockValues &vals,
Richard Smith6376d1f2012-07-17 00:06:14 +0000769 const ClassifyRefs &classification,
Ted Kremenek352a7082011-04-04 20:30:58 +0000770 llvm::BitVector &wasAnalyzed,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000771 UninitVariablesHandler &handler) {
Ted Kremenek352a7082011-04-04 20:30:58 +0000772 wasAnalyzed[block->getBlockID()] = true;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000773 vals.resetScratch();
Ted Kremenek6080d322012-07-19 04:59:05 +0000774 // Merge in values of predecessor blocks.
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000775 bool isFirst = true;
776 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
777 E = block->pred_end(); I != E; ++I) {
Ted Kremenekaed46772011-09-02 19:39:26 +0000778 const CFGBlock *pred = *I;
Ted Kremenek4b6fee62014-02-27 00:24:00 +0000779 if (!pred)
780 continue;
Ted Kremenekaed46772011-09-02 19:39:26 +0000781 if (wasAnalyzed[pred->getBlockID()]) {
Ted Kremenek6080d322012-07-19 04:59:05 +0000782 vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
Ted Kremenekaed46772011-09-02 19:39:26 +0000783 isFirst = false;
784 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000785 }
786 // Apply the transfer function.
Richard Smith6376d1f2012-07-17 00:06:14 +0000787 TransferFunctions tf(vals, cfg, block, ac, classification, handler);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000788 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
789 I != E; ++I) {
David Blaikie00be69a2013-02-23 00:29:34 +0000790 if (Optional<CFGStmt> cs = I->getAs<CFGStmt>())
791 tf.Visit(const_cast<Stmt*>(cs->getStmt()));
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000792 }
Ted Kremeneka895fe92011-03-15 04:57:27 +0000793 return vals.updateValueVectorWithScratch(block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000794}
795
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000796/// PruneBlocksHandler is a special UninitVariablesHandler that is used
797/// to detect when a CFGBlock has any *potential* use of an uninitialized
798/// variable. It is mainly used to prune out work during the final
799/// reporting pass.
800namespace {
801struct PruneBlocksHandler : public UninitVariablesHandler {
802 PruneBlocksHandler(unsigned numBlocks)
803 : hadUse(numBlocks, false), hadAnyUse(false),
804 currentBlock(0) {}
805
806 virtual ~PruneBlocksHandler() {}
807
808 /// Records if a CFGBlock had a potential use of an uninitialized variable.
809 llvm::BitVector hadUse;
810
811 /// Records if any CFGBlock had a potential use of an uninitialized variable.
812 bool hadAnyUse;
813
814 /// The current block to scribble use information.
815 unsigned currentBlock;
816
Craig Topperb45acb82014-03-14 06:02:07 +0000817 void handleUseOfUninitVariable(const VarDecl *vd,
818 const UninitUse &use) override {
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000819 hadUse[currentBlock] = true;
820 hadAnyUse = true;
821 }
822
823 /// Called when the uninitialized variable analysis detects the
824 /// idiom 'int x = x'. All other uses of 'x' within the initializer
825 /// are handled by handleUseOfUninitVariable.
Craig Topperb45acb82014-03-14 06:02:07 +0000826 void handleSelfInit(const VarDecl *vd) override {
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000827 hadUse[currentBlock] = true;
828 hadAnyUse = true;
829 }
830};
831}
832
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000833void clang::runUninitializedVariablesAnalysis(
834 const DeclContext &dc,
835 const CFG &cfg,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000836 AnalysisDeclContext &ac,
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000837 UninitVariablesHandler &handler,
838 UninitVariablesAnalysisStats &stats) {
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000839 CFGBlockValues vals(cfg);
840 vals.computeSetOfDeclarations(dc);
841 if (vals.hasNoDeclarations())
842 return;
Ted Kremenek37881932011-04-04 23:29:12 +0000843
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000844 stats.NumVariablesAnalyzed = vals.getNumEntries();
845
Richard Smith6376d1f2012-07-17 00:06:14 +0000846 // Precompute which expressions are uses and which are initializations.
847 ClassifyRefs classification(ac);
848 cfg.VisitBlockStmts(classification);
849
Ted Kremenek37881932011-04-04 23:29:12 +0000850 // Mark all variables uninitialized at the entry.
851 const CFGBlock &entry = cfg.getEntry();
Ted Kremenek6080d322012-07-19 04:59:05 +0000852 ValueVector &vec = vals.getValueVector(&entry);
853 const unsigned n = vals.getNumEntries();
854 for (unsigned j = 0; j < n ; ++j) {
855 vec[j] = Uninitialized;
Ted Kremenek37881932011-04-04 23:29:12 +0000856 }
857
858 // Proceed with the workist.
Artyom Skrobov27720762014-09-23 08:34:41 +0000859 DataflowWorklist worklist(cfg, *ac.getAnalysis<PostOrderCFGView>());
Ted Kremenek9b15c962011-03-15 04:57:32 +0000860 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000861 worklist.enqueueSuccessors(&cfg.getEntry());
Ted Kremenek352a7082011-04-04 20:30:58 +0000862 llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
Ted Kremenekaed46772011-09-02 19:39:26 +0000863 wasAnalyzed[cfg.getEntry().getBlockID()] = true;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000864 PruneBlocksHandler PBH(cfg.getNumBlockIDs());
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000865
866 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000867 PBH.currentBlock = block->getBlockID();
868
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000869 // Did the block change?
Richard Smith6376d1f2012-07-17 00:06:14 +0000870 bool changed = runOnBlock(block, cfg, ac, vals,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000871 classification, wasAnalyzed, PBH);
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000872 ++stats.NumBlockVisits;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000873 if (changed || !previouslyVisited[block->getBlockID()])
874 worklist.enqueueSuccessors(block);
875 previouslyVisited[block->getBlockID()] = true;
876 }
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000877
878 if (!PBH.hadAnyUse)
879 return;
880
Enea Zaffanella392291f2013-01-11 11:37:08 +0000881 // Run through the blocks one more time, and report uninitialized variables.
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000882 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremenekaed46772011-09-02 19:39:26 +0000883 const CFGBlock *block = *BI;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000884 if (PBH.hadUse[block->getBlockID()]) {
885 runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000886 ++stats.NumBlockVisits;
887 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000888 }
889}
890
891UninitVariablesHandler::~UninitVariablesHandler() {}