blob: 57dfab36fddefb664de35f52938e77902377d725 [file] [log] [blame]
Ted Kremenek6f342132011-03-15 03:17:07 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek610068c2011-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
Ted Kremenek13bd4232011-01-20 17:37:17 +000014#include <utility>
Ted Kremenek610068c2011-01-15 02:58:47 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/SmallVector.h"
Argyrios Kyrtzidis049f6d02011-05-31 03:56:09 +000017#include "llvm/ADT/PackedVector.h"
Ted Kremenek610068c2011-01-15 02:58:47 +000018#include "llvm/ADT/DenseMap.h"
19#include "clang/AST/Decl.h"
20#include "clang/Analysis/CFG.h"
Ted Kremeneka8c17a52011-01-25 19:13:48 +000021#include "clang/Analysis/AnalysisContext.h"
Ted Kremenek610068c2011-01-15 02:58:47 +000022#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek6f342132011-03-15 03:17:07 +000023#include "clang/Analysis/Analyses/UninitializedValues.h"
Argyrios Kyrtzidisb2c60b02012-03-01 19:45:56 +000024#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek610068c2011-01-15 02:58:47 +000025
26using namespace clang;
27
Ted Kremenek40900ee2011-01-27 02:29:34 +000028static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
Ted Kremenek1cbc3152011-03-17 03:06:11 +000029 if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
Ted Kremeneka21612f2011-04-07 20:02:56 +000030 !vd->isExceptionVariable() &&
Ted Kremenek1cbc3152011-03-17 03:06:11 +000031 vd->getDeclContext() == dc) {
32 QualType ty = vd->getType();
33 return ty->isScalarType() || ty->isVectorType();
34 }
35 return false;
Ted Kremenekc104e532011-01-18 04:53:25 +000036}
37
Ted Kremenek610068c2011-01-15 02:58:47 +000038//------------------------------------------------------------------------====//
Ted Kremenek136f8f22011-03-15 04:57:27 +000039// DeclToIndex: a mapping from Decls we track to value indices.
Ted Kremenek610068c2011-01-15 02:58:47 +000040//====------------------------------------------------------------------------//
41
42namespace {
Ted Kremenek136f8f22011-03-15 04:57:27 +000043class DeclToIndex {
Ted Kremenek610068c2011-01-15 02:58:47 +000044 llvm::DenseMap<const VarDecl *, unsigned> map;
45public:
Ted Kremenek136f8f22011-03-15 04:57:27 +000046 DeclToIndex() {}
Ted Kremenek610068c2011-01-15 02:58:47 +000047
48 /// Compute the actual mapping from declarations to bits.
49 void computeMap(const DeclContext &dc);
50
51 /// Return the number of declarations in the map.
52 unsigned size() const { return map.size(); }
53
54 /// Returns the bit vector index for a given declaration.
Ted Kremenekb831c672011-03-29 01:40:00 +000055 llvm::Optional<unsigned> getValueIndex(const VarDecl *d) const;
Ted Kremenek610068c2011-01-15 02:58:47 +000056};
57}
58
Ted Kremenek136f8f22011-03-15 04:57:27 +000059void DeclToIndex::computeMap(const DeclContext &dc) {
Ted Kremenek610068c2011-01-15 02:58:47 +000060 unsigned count = 0;
61 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
62 E(dc.decls_end());
63 for ( ; I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +000064 const VarDecl *vd = *I;
Ted Kremenek40900ee2011-01-27 02:29:34 +000065 if (isTrackedVar(vd, &dc))
Ted Kremenek610068c2011-01-15 02:58:47 +000066 map[vd] = count++;
67 }
68}
69
Ted Kremenekb831c672011-03-29 01:40:00 +000070llvm::Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
71 llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
Ted Kremenek610068c2011-01-15 02:58:47 +000072 if (I == map.end())
73 return llvm::Optional<unsigned>();
74 return I->second;
75}
76
77//------------------------------------------------------------------------====//
78// CFGBlockValues: dataflow values for CFG blocks.
79//====------------------------------------------------------------------------//
80
Ted Kremenekf7bafc72011-03-15 04:57:38 +000081// These values are defined in such a way that a merge can be done using
82// a bitwise OR.
83enum Value { Unknown = 0x0, /* 00 */
84 Initialized = 0x1, /* 01 */
85 Uninitialized = 0x2, /* 10 */
86 MayUninitialized = 0x3 /* 11 */ };
87
88static bool isUninitialized(const Value v) {
89 return v >= Uninitialized;
90}
91static bool isAlwaysUninit(const Value v) {
92 return v == Uninitialized;
93}
Ted Kremenekafb10c42011-03-15 04:57:29 +000094
Benjamin Kramerda57f3e2011-03-26 12:38:21 +000095namespace {
Ted Kremenek496398d2011-03-15 04:57:32 +000096
Argyrios Kyrtzidis049f6d02011-05-31 03:56:09 +000097typedef llvm::PackedVector<Value, 2> ValueVector;
Ted Kremenek136f8f22011-03-15 04:57:27 +000098typedef std::pair<ValueVector *, ValueVector *> BVPair;
Ted Kremenek13bd4232011-01-20 17:37:17 +000099
Ted Kremenek610068c2011-01-15 02:58:47 +0000100class CFGBlockValues {
101 const CFG &cfg;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000102 BVPair *vals;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000103 ValueVector scratch;
Ted Kremenek4ddb3872011-03-15 05:30:12 +0000104 DeclToIndex declToIndex;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000105
Ted Kremenek136f8f22011-03-15 04:57:27 +0000106 ValueVector &lazyCreate(ValueVector *&bv);
Ted Kremenek610068c2011-01-15 02:58:47 +0000107public:
108 CFGBlockValues(const CFG &cfg);
109 ~CFGBlockValues();
110
Ted Kremenekd40066b2011-04-04 23:29:12 +0000111 unsigned getNumEntries() const { return declToIndex.size(); }
112
Ted Kremenek610068c2011-01-15 02:58:47 +0000113 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek136f8f22011-03-15 04:57:27 +0000114 ValueVector &getValueVector(const CFGBlock *block,
Richard Smith81891882012-05-24 23:45:35 +0000115 const CFGBlock *dstBlock);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000116
Ted Kremenek136f8f22011-03-15 04:57:27 +0000117 BVPair &getValueVectors(const CFGBlock *block, bool shouldLazyCreate);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000118
Ted Kremenek136f8f22011-03-15 04:57:27 +0000119 void mergeIntoScratch(ValueVector const &source, bool isFirst);
120 bool updateValueVectorWithScratch(const CFGBlock *block);
121 bool updateValueVectors(const CFGBlock *block, const BVPair &newVals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000122
123 bool hasNoDeclarations() const {
Ted Kremenek4ddb3872011-03-15 05:30:12 +0000124 return declToIndex.size() == 0;
Ted Kremenek610068c2011-01-15 02:58:47 +0000125 }
Ted Kremeneke0e29332011-08-20 01:15:28 +0000126
Ted Kremenek610068c2011-01-15 02:58:47 +0000127 void resetScratch();
Ted Kremenek136f8f22011-03-15 04:57:27 +0000128 ValueVector &getScratch() { return scratch; }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000129
Ted Kremenek136f8f22011-03-15 04:57:27 +0000130 ValueVector::reference operator[](const VarDecl *vd);
Richard Smith2815e1a2012-05-25 02:17:09 +0000131
132 Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
133 const VarDecl *vd) {
134 const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
135 assert(idx.hasValue());
136 return getValueVector(block, dstBlock)[idx.getValue()];
137 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000138};
Benjamin Kramerda57f3e2011-03-26 12:38:21 +0000139} // end anonymous namespace
Ted Kremenek610068c2011-01-15 02:58:47 +0000140
141CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {
142 unsigned n = cfg.getNumBlockIDs();
143 if (!n)
144 return;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000145 vals = new std::pair<ValueVector*, ValueVector*>[n];
Chandler Carruth75c40642011-04-28 08:19:45 +0000146 memset((void*)vals, 0, sizeof(*vals) * n);
Ted Kremenek610068c2011-01-15 02:58:47 +0000147}
148
149CFGBlockValues::~CFGBlockValues() {
150 unsigned n = cfg.getNumBlockIDs();
151 if (n == 0)
152 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000153 for (unsigned i = 0; i < n; ++i) {
154 delete vals[i].first;
155 delete vals[i].second;
156 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000157 delete [] vals;
158}
159
160void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
Ted Kremenek4ddb3872011-03-15 05:30:12 +0000161 declToIndex.computeMap(dc);
162 scratch.resize(declToIndex.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000163}
164
Ted Kremenek136f8f22011-03-15 04:57:27 +0000165ValueVector &CFGBlockValues::lazyCreate(ValueVector *&bv) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000166 if (!bv)
Ted Kremenek4ddb3872011-03-15 05:30:12 +0000167 bv = new ValueVector(declToIndex.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000168 return *bv;
169}
170
Ted Kremenek13bd4232011-01-20 17:37:17 +0000171/// This function pattern matches for a '&&' or '||' that appears at
172/// the beginning of a CFGBlock that also (1) has a terminator and
173/// (2) has no other elements. If such an expression is found, it is returned.
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000174static const BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000175 if (block->empty())
176 return 0;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000177
Richard Smithb86b8552012-04-30 00:16:51 +0000178 CFGElement front = block->front();
179 const CFGStmt *cstmt = front.getAs<CFGStmt>();
Ted Kremenek76709bf2011-03-15 05:22:28 +0000180 if (!cstmt)
181 return 0;
182
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000183 const BinaryOperator *b = dyn_cast_or_null<BinaryOperator>(cstmt->getStmt());
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000184
185 if (!b || !b->isLogicalOp())
Ted Kremenek13bd4232011-01-20 17:37:17 +0000186 return 0;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000187
Ted Kremeneke6c28032011-05-10 22:10:35 +0000188 if (block->pred_size() == 2) {
189 if (block->getTerminatorCondition() == b) {
190 if (block->succ_size() == 2)
191 return b;
192 }
193 else if (block->size() == 1)
194 return b;
195 }
196
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000197 return 0;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000198}
199
Ted Kremenek136f8f22011-03-15 04:57:27 +0000200ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block,
201 const CFGBlock *dstBlock) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000202 unsigned idx = block->getBlockID();
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000203 if (dstBlock && getLogicalOperatorInChain(block)) {
204 if (*block->succ_begin() == dstBlock)
205 return lazyCreate(vals[idx].first);
206 assert(*(block->succ_begin()+1) == dstBlock);
207 return lazyCreate(vals[idx].second);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000208 }
209
210 assert(vals[idx].second == 0);
211 return lazyCreate(vals[idx].first);
212}
213
Ted Kremenek136f8f22011-03-15 04:57:27 +0000214BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block,
215 bool shouldLazyCreate) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000216 unsigned idx = block->getBlockID();
217 lazyCreate(vals[idx].first);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000218 if (shouldLazyCreate)
219 lazyCreate(vals[idx].second);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000220 return vals[idx];
221}
222
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000223#if 0
Ted Kremenek136f8f22011-03-15 04:57:27 +0000224static void printVector(const CFGBlock *block, ValueVector &bv,
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000225 unsigned num) {
226
227 llvm::errs() << block->getBlockID() << " :";
228 for (unsigned i = 0; i < bv.size(); ++i) {
229 llvm::errs() << ' ' << bv[i];
230 }
231 llvm::errs() << " : " << num << '\n';
232}
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000233
234static void printVector(const char *name, ValueVector const &bv) {
235 llvm::errs() << name << " : ";
236 for (unsigned i = 0; i < bv.size(); ++i) {
237 llvm::errs() << ' ' << bv[i];
238 }
239 llvm::errs() << "\n";
240}
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000241#endif
Ted Kremenek610068c2011-01-15 02:58:47 +0000242
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000243void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
244 bool isFirst) {
245 if (isFirst)
246 scratch = source;
247 else
248 scratch |= source;
249}
250
Ted Kremenek136f8f22011-03-15 04:57:27 +0000251bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
252 ValueVector &dst = getValueVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000253 bool changed = (dst != scratch);
254 if (changed)
255 dst = scratch;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000256#if 0
257 printVector(block, scratch, 0);
258#endif
Ted Kremenek13bd4232011-01-20 17:37:17 +0000259 return changed;
260}
261
Ted Kremenek136f8f22011-03-15 04:57:27 +0000262bool CFGBlockValues::updateValueVectors(const CFGBlock *block,
Ted Kremenek13bd4232011-01-20 17:37:17 +0000263 const BVPair &newVals) {
Ted Kremenek136f8f22011-03-15 04:57:27 +0000264 BVPair &vals = getValueVectors(block, true);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000265 bool changed = *newVals.first != *vals.first ||
266 *newVals.second != *vals.second;
267 *vals.first = *newVals.first;
268 *vals.second = *newVals.second;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000269#if 0
270 printVector(block, *vals.first, 1);
271 printVector(block, *vals.second, 2);
272#endif
Ted Kremenek610068c2011-01-15 02:58:47 +0000273 return changed;
274}
275
276void CFGBlockValues::resetScratch() {
277 scratch.reset();
278}
279
Ted Kremenek136f8f22011-03-15 04:57:27 +0000280ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
Ted Kremenek4ddb3872011-03-15 05:30:12 +0000281 const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
Ted Kremenek610068c2011-01-15 02:58:47 +0000282 assert(idx.hasValue());
283 return scratch[idx.getValue()];
284}
285
286//------------------------------------------------------------------------====//
287// Worklist: worklist for dataflow analysis.
288//====------------------------------------------------------------------------//
289
290namespace {
291class DataflowWorklist {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292 SmallVector<const CFGBlock *, 20> worklist;
Ted Kremenek496398d2011-03-15 04:57:32 +0000293 llvm::BitVector enqueuedBlocks;
Ted Kremenek610068c2011-01-15 02:58:47 +0000294public:
295 DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {}
296
Ted Kremenek610068c2011-01-15 02:58:47 +0000297 void enqueueSuccessors(const CFGBlock *block);
298 const CFGBlock *dequeue();
Ted Kremenek610068c2011-01-15 02:58:47 +0000299};
300}
301
Ted Kremenek610068c2011-01-15 02:58:47 +0000302void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
Chandler Carruth80520502011-07-08 11:19:06 +0000303 unsigned OldWorklistSize = worklist.size();
Ted Kremenek610068c2011-01-15 02:58:47 +0000304 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
305 E = block->succ_end(); I != E; ++I) {
Chandler Carruth80520502011-07-08 11:19:06 +0000306 const CFGBlock *Successor = *I;
307 if (!Successor || enqueuedBlocks[Successor->getBlockID()])
308 continue;
309 worklist.push_back(Successor);
310 enqueuedBlocks[Successor->getBlockID()] = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000311 }
Chandler Carruth80520502011-07-08 11:19:06 +0000312 if (OldWorklistSize == 0 || OldWorklistSize == worklist.size())
313 return;
314
315 // Rotate the newly added blocks to the start of the worklist so that it forms
316 // a proper queue when we pop off the end of the worklist.
317 std::rotate(worklist.begin(), worklist.begin() + OldWorklistSize,
318 worklist.end());
Ted Kremenek610068c2011-01-15 02:58:47 +0000319}
320
321const CFGBlock *DataflowWorklist::dequeue() {
322 if (worklist.empty())
323 return 0;
324 const CFGBlock *b = worklist.back();
325 worklist.pop_back();
326 enqueuedBlocks[b->getBlockID()] = false;
327 return b;
328}
329
330//------------------------------------------------------------------------====//
331// Transfer function for uninitialized values analysis.
332//====------------------------------------------------------------------------//
333
Ted Kremenek610068c2011-01-15 02:58:47 +0000334namespace {
335class FindVarResult {
336 const VarDecl *vd;
337 const DeclRefExpr *dr;
338public:
339 FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {}
340
341 const DeclRefExpr *getDeclRefExpr() const { return dr; }
342 const VarDecl *getDecl() const { return vd; }
343};
344
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000345class TransferFunctions : public StmtVisitor<TransferFunctions> {
Ted Kremenek610068c2011-01-15 02:58:47 +0000346 CFGBlockValues &vals;
347 const CFG &cfg;
Richard Smith2815e1a2012-05-25 02:17:09 +0000348 const CFGBlock *block;
Ted Kremenek1d26f482011-10-24 01:32:45 +0000349 AnalysisDeclContext &ac;
Ted Kremenek610068c2011-01-15 02:58:47 +0000350 UninitVariablesHandler *handler;
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000351
352 /// The last DeclRefExpr seen when analyzing a block. Used to
353 /// cheat when detecting cases when the address of a variable is taken.
354 DeclRefExpr *lastDR;
355
356 /// The last lvalue-to-rvalue conversion of a variable whose value
357 /// was uninitialized. Normally this results in a warning, but it is
358 /// possible to either silence the warning in some cases, or we
359 /// propagate the uninitialized value.
360 CastExpr *lastLoad;
Ted Kremenek57fb5912011-08-04 22:40:57 +0000361
362 /// For some expressions, we want to ignore any post-processing after
363 /// visitation.
364 bool skipProcessUses;
365
Ted Kremenek610068c2011-01-15 02:58:47 +0000366public:
367 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Richard Smith2815e1a2012-05-25 02:17:09 +0000368 const CFGBlock *block, AnalysisDeclContext &ac,
Ted Kremenek6f275422011-09-02 19:39:26 +0000369 UninitVariablesHandler *handler)
Richard Smith2815e1a2012-05-25 02:17:09 +0000370 : vals(vals), cfg(cfg), block(block), ac(ac), handler(handler),
Ted Kremenek6f275422011-09-02 19:39:26 +0000371 lastDR(0), lastLoad(0),
Ted Kremenek57fb5912011-08-04 22:40:57 +0000372 skipProcessUses(false) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000373
Richard Smith81891882012-05-24 23:45:35 +0000374 void reportUse(const Expr *ex, const VarDecl *vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000375
376 void VisitBlockExpr(BlockExpr *be);
Ted Kremenek610068c2011-01-15 02:58:47 +0000377 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000378 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000379 void VisitUnaryOperator(UnaryOperator *uo);
380 void VisitBinaryOperator(BinaryOperator *bo);
381 void VisitCastExpr(CastExpr *ce);
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000382 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs);
383 void Visit(Stmt *s);
Richard Smith2815e1a2012-05-25 02:17:09 +0000384
Ted Kremenek40900ee2011-01-27 02:29:34 +0000385 bool isTrackedVar(const VarDecl *vd) {
386 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
387 }
Richard Smith2815e1a2012-05-25 02:17:09 +0000388
389 UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
390 UninitUse Use(ex, isAlwaysUninit(v));
391
392 assert(isUninitialized(v));
393 if (Use.getKind() == UninitUse::Always)
394 return Use;
395
396 // If an edge which leads unconditionally to this use did not initialize
397 // the variable, we can say something stronger than 'may be uninitialized':
398 // we can say 'either it's used uninitialized or you have dead code'.
399 //
400 // We track the number of successors of a node which have been visited, and
401 // visit a node once we have visited all of its successors. Only edges where
402 // the variable might still be uninitialized are followed. Since a variable
403 // can't transfer from being initialized to being uninitialized, this will
404 // trace out the subgraph which inevitably leads to the use and does not
405 // initialize the variable. We do not want to skip past loops, since their
406 // non-termination might be correlated with the initialization condition.
407 //
408 // For example:
409 //
410 // void f(bool a, bool b) {
411 // block1: int n;
412 // if (a) {
413 // block2: if (b)
414 // block3: n = 1;
415 // block4: } else if (b) {
416 // block5: while (!a) {
417 // block6: do_work(&a);
418 // n = 2;
419 // }
420 // }
421 // block7: if (a)
422 // block8: g();
423 // block9: return n;
424 // }
425 //
426 // Starting from the maybe-uninitialized use in block 9:
427 // * Block 7 is not visited because we have only visited one of its two
428 // successors.
429 // * Block 8 is visited because we've visited its only successor.
430 // From block 8:
431 // * Block 7 is visited because we've now visited both of its successors.
432 // From block 7:
433 // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
434 // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
435 // * Block 3 is not visited because it initializes 'n'.
436 // Now the algorithm terminates, having visited blocks 7 and 8, and having
437 // found the frontier is blocks 2, 4, and 5.
438 //
439 // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
440 // and 4), so we report that any time either of those edges is taken (in
441 // each case when 'b == false'), 'n' is used uninitialized.
442 llvm::SmallVector<const CFGBlock*, 32> Queue;
443 llvm::SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
444 Queue.push_back(block);
445 // Specify that we've already visited all successors of the starting block.
446 // This has the dual purpose of ensuring we never add it to the queue, and
447 // of marking it as not being a candidate element of the frontier.
448 SuccsVisited[block->getBlockID()] = block->succ_size();
449 while (!Queue.empty()) {
450 const CFGBlock *B = Queue.back();
451 Queue.pop_back();
452 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
453 I != E; ++I) {
454 const CFGBlock *Pred = *I;
455 if (vals.getValue(Pred, B, vd) == Initialized)
456 // This block initializes the variable.
457 continue;
458
459 if (++SuccsVisited[Pred->getBlockID()] == Pred->succ_size())
460 // All paths from this block lead to the use and don't initialize the
461 // variable.
462 Queue.push_back(Pred);
463 }
464 }
465
466 // Scan the frontier, looking for blocks where the variable was
467 // uninitialized.
468 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
469 const CFGBlock *Block = *BI;
470 unsigned BlockID = Block->getBlockID();
471 const Stmt *Term = Block->getTerminator();
472 if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
473 Term) {
474 // This block inevitably leads to the use. If we have an edge from here
475 // to a post-dominator block, and the variable is uninitialized on that
476 // edge, we have found a bug.
477 for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
478 E = Block->succ_end(); I != E; ++I) {
479 const CFGBlock *Succ = *I;
480 if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
481 vals.getValue(Block, Succ, vd) == Uninitialized) {
482 // Switch cases are a special case: report the label to the caller
483 // as the 'terminator', not the switch statement itself. Suppress
484 // situations where no label matched: we can't be sure that's
485 // possible.
486 if (isa<SwitchStmt>(Term)) {
487 const Stmt *Label = Succ->getLabel();
488 if (!Label || !isa<SwitchCase>(Label))
489 // Might not be possible.
490 continue;
491 UninitUse::Branch Branch;
492 Branch.Terminator = Label;
493 Branch.Output = 0; // Ignored.
494 Use.addUninitBranch(Branch);
495 } else {
496 UninitUse::Branch Branch;
497 Branch.Terminator = Term;
498 Branch.Output = I - Block->succ_begin();
499 Use.addUninitBranch(Branch);
500 }
501 }
502 }
503 }
504 }
505
506 return Use;
507 }
508
Ted Kremenek40900ee2011-01-27 02:29:34 +0000509 FindVarResult findBlockVarDecl(Expr *ex);
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000510
511 void ProcessUses(Stmt *s = 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000512};
513}
514
Ted Kremenekde091ae2011-08-08 21:43:08 +0000515static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
516 while (Ex) {
517 Ex = Ex->IgnoreParenNoopCasts(C);
518 if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
519 if (CE->getCastKind() == CK_LValueBitCast) {
520 Ex = CE->getSubExpr();
521 continue;
522 }
523 }
524 break;
525 }
526 return Ex;
527}
528
Richard Smith81891882012-05-24 23:45:35 +0000529void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
530 if (!handler)
531 return;
532 Value v = vals[vd];
533 if (isUninitialized(v))
Richard Smith2815e1a2012-05-25 02:17:09 +0000534 handler->handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
Ted Kremenek610068c2011-01-15 02:58:47 +0000535}
536
Ted Kremenek9c378f72011-08-12 23:37:29 +0000537FindVarResult TransferFunctions::findBlockVarDecl(Expr *ex) {
538 if (DeclRefExpr *dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000539 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
540 if (isTrackedVar(vd))
Ted Kremenek40900ee2011-01-27 02:29:34 +0000541 return FindVarResult(vd, dr);
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000542 return FindVarResult(0, 0);
543}
544
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000545void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs) {
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000546 // This represents an initialization of the 'element' value.
547 Stmt *element = fs->getElement();
Ted Kremenek9c378f72011-08-12 23:37:29 +0000548 const VarDecl *vd = 0;
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000549
Ted Kremenek9c378f72011-08-12 23:37:29 +0000550 if (DeclStmt *ds = dyn_cast<DeclStmt>(element)) {
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000551 vd = cast<VarDecl>(ds->getSingleDecl());
552 if (!isTrackedVar(vd))
553 vd = 0;
Chad Rosier30601782011-08-17 23:08:45 +0000554 } else {
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000555 // Initialize the value of the reference variable.
556 const FindVarResult &res = findBlockVarDecl(cast<Expr>(element));
557 vd = res.getDecl();
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000558 }
559
560 if (vd)
561 vals[vd] = Initialized;
562}
563
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000564void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000565 const BlockDecl *bd = be->getBlockDecl();
566 for (BlockDecl::capture_const_iterator i = bd->capture_begin(),
567 e = bd->capture_end() ; i != e; ++i) {
568 const VarDecl *vd = i->getVariable();
Ted Kremenekbc8b44c2011-03-31 22:32:41 +0000569 if (!isTrackedVar(vd))
570 continue;
571 if (i->isByRef()) {
572 vals[vd] = Initialized;
573 continue;
574 }
Richard Smith81891882012-05-24 23:45:35 +0000575 reportUse(be, vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000576 }
577}
578
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000579void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
580 // Record the last DeclRefExpr seen. This is an lvalue computation.
581 // We use this value to later detect if a variable "escapes" the analysis.
582 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
Ted Kremenekdd4286b2011-07-20 19:49:47 +0000583 if (isTrackedVar(vd)) {
584 ProcessUses();
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000585 lastDR = dr;
Ted Kremenekdd4286b2011-07-20 19:49:47 +0000586 }
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000587}
588
Ted Kremenek610068c2011-01-15 02:58:47 +0000589void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
590 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
591 DI != DE; ++DI) {
592 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000593 if (isTrackedVar(vd)) {
Chandler Carruthb88fb022011-04-05 21:36:30 +0000594 if (Expr *init = vd->getInit()) {
Chandler Carruthb88fb022011-04-05 21:36:30 +0000595 // If the initializer consists solely of a reference to itself, we
596 // explicitly mark the variable as uninitialized. This allows code
597 // like the following:
598 //
599 // int x = x;
600 //
601 // to deliberately leave a variable uninitialized. Different analysis
602 // clients can detect this pattern and adjust their reporting
603 // appropriately, but we need to continue to analyze subsequent uses
604 // of the variable.
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000605 if (init == lastLoad) {
Ted Kremenekde091ae2011-08-08 21:43:08 +0000606 const DeclRefExpr *DR
607 = cast<DeclRefExpr>(stripCasts(ac.getASTContext(),
608 lastLoad->getSubExpr()));
Ted Kremenek62d126e2011-07-19 21:41:51 +0000609 if (DR->getDecl() == vd) {
610 // int x = x;
611 // Propagate uninitialized value, but don't immediately report
612 // a problem.
613 vals[vd] = Uninitialized;
614 lastLoad = 0;
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000615 lastDR = 0;
Ted Kremenek9e761722011-10-13 18:50:06 +0000616 if (handler)
617 handler->handleSelfInit(vd);
Ted Kremenek62d126e2011-07-19 21:41:51 +0000618 return;
619 }
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000620 }
Ted Kremenek62d126e2011-07-19 21:41:51 +0000621
622 // All other cases: treat the new variable as initialized.
Ted Kremenek9e761722011-10-13 18:50:06 +0000623 // This is a minor optimization to reduce the propagation
624 // of the analysis, since we will have already reported
625 // the use of the uninitialized value (which visiting the
626 // initializer).
Ted Kremenek62d126e2011-07-19 21:41:51 +0000627 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000628 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000629 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000630 }
631 }
632}
633
Ted Kremenek610068c2011-01-15 02:58:47 +0000634void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000635 if (bo->isAssignmentOp()) {
636 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
Ted Kremenek9c378f72011-08-12 23:37:29 +0000637 if (const VarDecl *vd = res.getDecl()) {
Richard Smith81891882012-05-24 23:45:35 +0000638 if (bo->getOpcode() != BO_Assign)
639 reportUse(res.getDeclRefExpr(), vd);
640 else
641 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000642 }
643 }
644}
645
646void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000647 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000648 case clang::UO_PostDec:
649 case clang::UO_PostInc:
650 case clang::UO_PreDec:
651 case clang::UO_PreInc: {
652 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
653 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000654 assert(res.getDeclRefExpr() == lastDR);
655 // We null out lastDR to indicate we have fully processed it
656 // and we don't want the auto-value setting in Visit().
657 lastDR = 0;
Richard Smith81891882012-05-24 23:45:35 +0000658 reportUse(res.getDeclRefExpr(), vd);
Ted Kremenek610068c2011-01-15 02:58:47 +0000659 }
660 break;
661 }
662 default:
663 break;
664 }
665}
666
667void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000668 if (ce->getCastKind() == CK_LValueToRValue) {
669 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000670 if (res.getDecl()) {
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000671 assert(res.getDeclRefExpr() == lastDR);
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000672 lastLoad = ce;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000673 }
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000674 }
Ted Kremenekde091ae2011-08-08 21:43:08 +0000675 else if (ce->getCastKind() == CK_NoOp ||
676 ce->getCastKind() == CK_LValueBitCast) {
Ted Kremenek57fb5912011-08-04 22:40:57 +0000677 skipProcessUses = true;
678 }
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000679 else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
680 if (cse->getType()->isVoidType()) {
681 // e.g. (void) x;
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000682 if (lastLoad == cse->getSubExpr()) {
683 // Squelch any detected load of an uninitialized value if
684 // we cast it to void.
685 lastLoad = 0;
686 lastDR = 0;
687 }
688 }
689 }
690}
691
692void TransferFunctions::Visit(clang::Stmt *s) {
Ted Kremenek57fb5912011-08-04 22:40:57 +0000693 skipProcessUses = false;
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000694 StmtVisitor<TransferFunctions>::Visit(s);
Ted Kremenek57fb5912011-08-04 22:40:57 +0000695 if (!skipProcessUses)
696 ProcessUses(s);
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000697}
698
699void TransferFunctions::ProcessUses(Stmt *s) {
700 // This method is typically called after visiting a CFGElement statement
701 // in the CFG. We delay processing of reporting many loads of uninitialized
702 // values until here.
703 if (lastLoad) {
704 // If we just visited the lvalue-to-rvalue cast, there is nothing
705 // left to do.
706 if (lastLoad == s)
707 return;
708
Ted Kremenekde091ae2011-08-08 21:43:08 +0000709 const DeclRefExpr *DR =
710 cast<DeclRefExpr>(stripCasts(ac.getASTContext(),
711 lastLoad->getSubExpr()));
712 const VarDecl *VD = cast<VarDecl>(DR->getDecl());
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000713
714 // If we reach here, we may have seen a load of an uninitialized value
715 // and it hasn't been casted to void or otherwise handled. In this
716 // situation, report the incident.
Richard Smith81891882012-05-24 23:45:35 +0000717 reportUse(DR, VD);
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000718
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000719 lastLoad = 0;
Ted Kremenekc5f740e2011-10-07 00:42:48 +0000720
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000721 if (DR == lastDR) {
722 lastDR = 0;
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000723 return;
724 }
725 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000726
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000727 // Any other uses of 'lastDR' involve taking an lvalue of variable.
728 // In this case, it "escapes" the analysis.
729 if (lastDR && lastDR != s) {
730 vals[cast<VarDecl>(lastDR->getDecl())] = Initialized;
731 lastDR = 0;
Chandler Carruth86684942011-04-13 08:18:42 +0000732 }
733}
734
Ted Kremenek610068c2011-01-15 02:58:47 +0000735//------------------------------------------------------------------------====//
736// High-level "driver" logic for uninitialized values analysis.
737//====------------------------------------------------------------------------//
738
Ted Kremenek13bd4232011-01-20 17:37:17 +0000739static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000740 AnalysisDeclContext &ac, CFGBlockValues &vals,
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000741 llvm::BitVector &wasAnalyzed,
Ted Kremenek6f275422011-09-02 19:39:26 +0000742 UninitVariablesHandler *handler = 0) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000743
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000744 wasAnalyzed[block->getBlockID()] = true;
745
Ted Kremenek13bd4232011-01-20 17:37:17 +0000746 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000747 CFGBlock::const_pred_iterator itr = block->pred_begin();
Ted Kremenek136f8f22011-03-15 04:57:27 +0000748 BVPair vA = vals.getValueVectors(*itr, false);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000749 ++itr;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000750 BVPair vB = vals.getValueVectors(*itr, false);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000751
752 BVPair valsAB;
753
754 if (b->getOpcode() == BO_LAnd) {
755 // Merge the 'F' bits from the first and second.
756 vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true);
757 vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false);
758 valsAB.first = vA.first;
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000759 valsAB.second = &vals.getScratch();
Chad Rosier30601782011-08-17 23:08:45 +0000760 } else {
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000761 // Merge the 'T' bits from the first and second.
762 assert(b->getOpcode() == BO_LOr);
763 vals.mergeIntoScratch(*vA.first, true);
764 vals.mergeIntoScratch(*vB.first, false);
765 valsAB.first = &vals.getScratch();
766 valsAB.second = vA.second ? vA.second : vA.first;
767 }
Ted Kremenek136f8f22011-03-15 04:57:27 +0000768 return vals.updateValueVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000769 }
770
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000771 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000772 vals.resetScratch();
773 bool isFirst = true;
774 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
775 E = block->pred_end(); I != E; ++I) {
Ted Kremenek6f275422011-09-02 19:39:26 +0000776 const CFGBlock *pred = *I;
777 if (wasAnalyzed[pred->getBlockID()]) {
778 vals.mergeIntoScratch(vals.getValueVector(pred, block), isFirst);
779 isFirst = false;
780 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000781 }
782 // Apply the transfer function.
Richard Smith2815e1a2012-05-25 02:17:09 +0000783 TransferFunctions tf(vals, cfg, block, ac, handler);
Ted Kremenek610068c2011-01-15 02:58:47 +0000784 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
785 I != E; ++I) {
786 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
Ted Kremenekf1d10d92011-08-23 23:05:04 +0000787 tf.Visit(const_cast<Stmt*>(cs->getStmt()));
Ted Kremenek610068c2011-01-15 02:58:47 +0000788 }
789 }
Ted Kremenek0c8e5a02011-07-19 14:18:48 +0000790 tf.ProcessUses();
Ted Kremenek136f8f22011-03-15 04:57:27 +0000791 return vals.updateValueVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000792}
793
Chandler Carruth5d989942011-07-06 16:21:37 +0000794void clang::runUninitializedVariablesAnalysis(
795 const DeclContext &dc,
796 const CFG &cfg,
Ted Kremenek1d26f482011-10-24 01:32:45 +0000797 AnalysisDeclContext &ac,
Chandler Carruth5d989942011-07-06 16:21:37 +0000798 UninitVariablesHandler &handler,
799 UninitVariablesAnalysisStats &stats) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000800 CFGBlockValues vals(cfg);
801 vals.computeSetOfDeclarations(dc);
802 if (vals.hasNoDeclarations())
803 return;
Richard Smith81891882012-05-24 23:45:35 +0000804#if 0
805 cfg.dump(dc.getParentASTContext().getLangOpts(), true);
806#endif
Ted Kremenekd40066b2011-04-04 23:29:12 +0000807
Chandler Carruth5d989942011-07-06 16:21:37 +0000808 stats.NumVariablesAnalyzed = vals.getNumEntries();
809
Ted Kremenekd40066b2011-04-04 23:29:12 +0000810 // Mark all variables uninitialized at the entry.
811 const CFGBlock &entry = cfg.getEntry();
812 for (CFGBlock::const_succ_iterator i = entry.succ_begin(),
813 e = entry.succ_end(); i != e; ++i) {
814 if (const CFGBlock *succ = *i) {
815 ValueVector &vec = vals.getValueVector(&entry, succ);
816 const unsigned n = vals.getNumEntries();
817 for (unsigned j = 0; j < n ; ++j) {
818 vec[j] = Uninitialized;
819 }
820 }
821 }
822
823 // Proceed with the workist.
Ted Kremenek610068c2011-01-15 02:58:47 +0000824 DataflowWorklist worklist(cfg);
Ted Kremenek496398d2011-03-15 04:57:32 +0000825 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
Ted Kremenek610068c2011-01-15 02:58:47 +0000826 worklist.enqueueSuccessors(&cfg.getEntry());
Ted Kremenekf8adeef2011-04-04 20:30:58 +0000827 llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
Ted Kremenek6f275422011-09-02 19:39:26 +0000828 wasAnalyzed[cfg.getEntry().getBlockID()] = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000829
830 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000831 // Did the block change?
Chandler Carruth5d989942011-07-06 16:21:37 +0000832 bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed);
833 ++stats.NumBlockVisits;
Ted Kremenek610068c2011-01-15 02:58:47 +0000834 if (changed || !previouslyVisited[block->getBlockID()])
835 worklist.enqueueSuccessors(block);
836 previouslyVisited[block->getBlockID()] = true;
837 }
838
839 // Run through the blocks one more time, and report uninitialized variabes.
840 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremenek6f275422011-09-02 19:39:26 +0000841 const CFGBlock *block = *BI;
842 if (wasAnalyzed[block->getBlockID()]) {
843 runOnBlock(block, cfg, ac, vals, wasAnalyzed, &handler);
Chandler Carruth5d989942011-07-06 16:21:37 +0000844 ++stats.NumBlockVisits;
845 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000846 }
847}
848
849UninitVariablesHandler::~UninitVariablesHandler() {}