blob: 4c54885413195c59cce6d61b9b180c26229b174b [file] [log] [blame]
Ted Kremenek610068c2011-01-15 02:58:47 +00001//==- UninitializedValuesV2.cpp - Find Uninitialized Values -----*- C++ --*-==//
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// 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"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/DenseMap.h"
19#include "clang/AST/Decl.h"
20#include "clang/Analysis/CFG.h"
21#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
22#include "clang/Analysis/Analyses/UninitializedValuesV2.h"
Ted Kremenekc21fed32011-01-18 21:18:58 +000023#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek610068c2011-01-15 02:58:47 +000024
25using namespace clang;
26
Ted Kremenekc104e532011-01-18 04:53:25 +000027static bool isTrackedVar(const VarDecl *vd) {
28 return vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
29 vd->getType()->isScalarType();
30}
31
Ted Kremenek610068c2011-01-15 02:58:47 +000032//------------------------------------------------------------------------====//
33// DeclToBit: a mapping from Decls we track to bitvector indices.
34//====------------------------------------------------------------------------//
35
36namespace {
37class DeclToBit {
38 llvm::DenseMap<const VarDecl *, unsigned> map;
39public:
40 DeclToBit() {}
41
42 /// Compute the actual mapping from declarations to bits.
43 void computeMap(const DeclContext &dc);
44
45 /// Return the number of declarations in the map.
46 unsigned size() const { return map.size(); }
47
48 /// Returns the bit vector index for a given declaration.
49 llvm::Optional<unsigned> getBitVectorIndex(const VarDecl *d);
50};
51}
52
53void DeclToBit::computeMap(const DeclContext &dc) {
54 unsigned count = 0;
55 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
56 E(dc.decls_end());
57 for ( ; I != E; ++I) {
58 const VarDecl *vd = *I;
Ted Kremenekc104e532011-01-18 04:53:25 +000059 if (isTrackedVar(vd))
Ted Kremenek610068c2011-01-15 02:58:47 +000060 map[vd] = count++;
61 }
62}
63
64llvm::Optional<unsigned> DeclToBit::getBitVectorIndex(const VarDecl *d) {
65 llvm::DenseMap<const VarDecl *, unsigned>::iterator I = map.find(d);
66 if (I == map.end())
67 return llvm::Optional<unsigned>();
68 return I->second;
69}
70
71//------------------------------------------------------------------------====//
72// CFGBlockValues: dataflow values for CFG blocks.
73//====------------------------------------------------------------------------//
74
Ted Kremenek13bd4232011-01-20 17:37:17 +000075typedef std::pair<llvm::BitVector *, llvm::BitVector *> BVPair;
76
Ted Kremenek610068c2011-01-15 02:58:47 +000077namespace {
78class CFGBlockValues {
79 const CFG &cfg;
Ted Kremenek13bd4232011-01-20 17:37:17 +000080 BVPair *vals;
Ted Kremenek610068c2011-01-15 02:58:47 +000081 llvm::BitVector scratch;
82 DeclToBit declToBit;
Ted Kremenek13bd4232011-01-20 17:37:17 +000083
84 llvm::BitVector &lazyCreate(llvm::BitVector *&bv);
Ted Kremenek610068c2011-01-15 02:58:47 +000085public:
86 CFGBlockValues(const CFG &cfg);
87 ~CFGBlockValues();
88
89 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek13bd4232011-01-20 17:37:17 +000090 llvm::BitVector &getBitVector(const CFGBlock *block,
91 const CFGBlock *dstBlock);
92
93 BVPair &getBitVectors(const CFGBlock *block);
94
95 BVPair getPredBitVectors(const CFGBlock *block);
96
Ted Kremenek610068c2011-01-15 02:58:47 +000097 void mergeIntoScratch(llvm::BitVector const &source, bool isFirst);
98 bool updateBitVectorWithScratch(const CFGBlock *block);
Ted Kremenek13bd4232011-01-20 17:37:17 +000099 bool updateBitVectors(const CFGBlock *block, const BVPair &newVals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000100
101 bool hasNoDeclarations() const {
102 return declToBit.size() == 0;
103 }
104
105 void resetScratch();
Ted Kremenek13bd4232011-01-20 17:37:17 +0000106 llvm::BitVector &getScratch() { return scratch; }
107
Ted Kremenek610068c2011-01-15 02:58:47 +0000108 llvm::BitVector::reference operator[](const VarDecl *vd);
109};
110}
111
112CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {
113 unsigned n = cfg.getNumBlockIDs();
114 if (!n)
115 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000116 vals = new std::pair<llvm::BitVector*, llvm::BitVector*>[n];
Francois Pichet2d78c372011-01-15 13:27:47 +0000117 memset(vals, 0, sizeof(*vals) * n);
Ted Kremenek610068c2011-01-15 02:58:47 +0000118}
119
120CFGBlockValues::~CFGBlockValues() {
121 unsigned n = cfg.getNumBlockIDs();
122 if (n == 0)
123 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000124 for (unsigned i = 0; i < n; ++i) {
125 delete vals[i].first;
126 delete vals[i].second;
127 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000128 delete [] vals;
129}
130
131void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
132 declToBit.computeMap(dc);
133 scratch.resize(declToBit.size());
134}
135
Ted Kremenek13bd4232011-01-20 17:37:17 +0000136llvm::BitVector &CFGBlockValues::lazyCreate(llvm::BitVector *&bv) {
137 if (!bv)
Ted Kremenek610068c2011-01-15 02:58:47 +0000138 bv = new llvm::BitVector(declToBit.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000139 return *bv;
140}
141
Ted Kremenek13bd4232011-01-20 17:37:17 +0000142/// This function pattern matches for a '&&' or '||' that appears at
143/// the beginning of a CFGBlock that also (1) has a terminator and
144/// (2) has no other elements. If such an expression is found, it is returned.
145static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) {
146 if (block->empty())
147 return 0;
148
149 CFGStmt cstmt = block->front().getAs<CFGStmt>();
150 BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt.getStmt());
151 if (!b || !b->isLogicalOp() || block->getTerminatorCondition() != b)
152 return 0;
153 return b;
154}
155
156llvm::BitVector &CFGBlockValues::getBitVector(const CFGBlock *block,
157 const CFGBlock *dstBlock) {
158 unsigned idx = block->getBlockID();
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000159 if (dstBlock && block->succ_size() == 2 && block->pred_size() == 2) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000160 assert(block->getTerminator());
161 if (getLogicalOperatorInChain(block)) {
162 if (*block->succ_begin() == dstBlock)
163 return lazyCreate(vals[idx].first);
164 assert(*(block->succ_begin()+1) == dstBlock);
165 return lazyCreate(vals[idx].second);
166 }
167 }
168
169 assert(vals[idx].second == 0);
170 return lazyCreate(vals[idx].first);
171}
172
173BVPair &CFGBlockValues::getBitVectors(const clang::CFGBlock *block) {
174 unsigned idx = block->getBlockID();
175 lazyCreate(vals[idx].first);
176 lazyCreate(vals[idx].second);
177 return vals[idx];
178}
179
180BVPair CFGBlockValues::getPredBitVectors(const clang::CFGBlock *block) {
181 assert(block->pred_size() == 2);
182 CFGBlock::const_pred_iterator itr = block->pred_begin();
183 llvm::BitVector &bvA = getBitVector(*itr, block);
184 ++itr;
185 return BVPair(&bvA, &getBitVector(*itr, block));
186}
187
Ted Kremenek610068c2011-01-15 02:58:47 +0000188void CFGBlockValues::mergeIntoScratch(llvm::BitVector const &source,
189 bool isFirst) {
190 if (isFirst)
191 scratch = source;
192 else
Ted Kremenekc104e532011-01-18 04:53:25 +0000193 scratch |= source;
Ted Kremenek610068c2011-01-15 02:58:47 +0000194}
195
196bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000197 llvm::BitVector &dst = getBitVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000198 bool changed = (dst != scratch);
199 if (changed)
200 dst = scratch;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000201
Ted Kremenek13bd4232011-01-20 17:37:17 +0000202 return changed;
203}
204
205bool CFGBlockValues::updateBitVectors(const CFGBlock *block,
206 const BVPair &newVals) {
207 BVPair &vals = getBitVectors(block);
208 bool changed = *newVals.first != *vals.first ||
209 *newVals.second != *vals.second;
210 *vals.first = *newVals.first;
211 *vals.second = *newVals.second;
Ted Kremenek610068c2011-01-15 02:58:47 +0000212 return changed;
213}
214
215void CFGBlockValues::resetScratch() {
216 scratch.reset();
217}
218
219llvm::BitVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
220 const llvm::Optional<unsigned> &idx = declToBit.getBitVectorIndex(vd);
221 assert(idx.hasValue());
222 return scratch[idx.getValue()];
223}
224
225//------------------------------------------------------------------------====//
226// Worklist: worklist for dataflow analysis.
227//====------------------------------------------------------------------------//
228
229namespace {
230class DataflowWorklist {
231 llvm::SmallVector<const CFGBlock *, 20> worklist;
232 llvm::BitVector enqueuedBlocks;
233public:
234 DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {}
235
236 void enqueue(const CFGBlock *block);
237 void enqueueSuccessors(const CFGBlock *block);
238 const CFGBlock *dequeue();
239
240};
241}
242
243void DataflowWorklist::enqueue(const CFGBlock *block) {
Ted Kremenekc104e532011-01-18 04:53:25 +0000244 if (!block)
245 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000246 unsigned idx = block->getBlockID();
247 if (enqueuedBlocks[idx])
248 return;
249 worklist.push_back(block);
250 enqueuedBlocks[idx] = true;
251}
252
253void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
254 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
255 E = block->succ_end(); I != E; ++I) {
256 enqueue(*I);
257 }
258}
259
260const CFGBlock *DataflowWorklist::dequeue() {
261 if (worklist.empty())
262 return 0;
263 const CFGBlock *b = worklist.back();
264 worklist.pop_back();
265 enqueuedBlocks[b->getBlockID()] = false;
266 return b;
267}
268
269//------------------------------------------------------------------------====//
270// Transfer function for uninitialized values analysis.
271//====------------------------------------------------------------------------//
272
Ted Kremenekc104e532011-01-18 04:53:25 +0000273static const bool Initialized = false;
274static const bool Uninitialized = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000275
276namespace {
277class FindVarResult {
278 const VarDecl *vd;
279 const DeclRefExpr *dr;
280public:
281 FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {}
282
283 const DeclRefExpr *getDeclRefExpr() const { return dr; }
284 const VarDecl *getDecl() const { return vd; }
285};
286
287class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> {
288 CFGBlockValues &vals;
289 const CFG &cfg;
290 UninitVariablesHandler *handler;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000291 const DeclRefExpr *currentDR;
Ted Kremenek610068c2011-01-15 02:58:47 +0000292public:
293 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
294 UninitVariablesHandler *handler)
Ted Kremenekc21fed32011-01-18 21:18:58 +0000295 : vals(vals), cfg(cfg), handler(handler), currentDR(0) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000296
297 const CFG &getCFG() { return cfg; }
298 void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
299
300 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000301 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000302 void VisitUnaryOperator(UnaryOperator *uo);
303 void VisitBinaryOperator(BinaryOperator *bo);
304 void VisitCastExpr(CastExpr *ce);
Ted Kremenek96608032011-01-23 17:53:04 +0000305 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se);
Ted Kremenek610068c2011-01-15 02:58:47 +0000306};
307}
308
309void TransferFunctions::reportUninit(const DeclRefExpr *ex,
310 const VarDecl *vd) {
311 if (handler) handler->handleUseOfUninitVariable(ex, vd);
312}
313
314void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
315 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
316 DI != DE; ++DI) {
317 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000318 if (isTrackedVar(vd)) {
319 vals[vd] = Uninitialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000320 if (Stmt *init = vd->getInit()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000321 Visit(init);
Ted Kremenekc104e532011-01-18 04:53:25 +0000322 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000323 }
Ted Kremenek4dccb902011-01-18 05:00:42 +0000324 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000325 else if (Stmt *init = vd->getInit()) {
326 Visit(init);
327 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000328 }
329 }
330}
331
Ted Kremenekc21fed32011-01-18 21:18:58 +0000332void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
333 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
334 // cannot be block-level expressions. Therefore, we determine if
335 // a DeclRefExpr is involved in a "load" by comparing it to the current
336 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
337 // If a DeclRefExpr is not involved in a load, we are essentially computing
338 // its address, either for assignment to a reference or via the '&' operator.
339 // In such cases, treat the variable as being initialized, since this
340 // analysis isn't powerful enough to do alias tracking.
341 if (dr != currentDR)
342 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
343 if (isTrackedVar(vd))
344 vals[vd] = Initialized;
345}
346
Ted Kremenek610068c2011-01-15 02:58:47 +0000347static FindVarResult findBlockVarDecl(Expr* ex) {
348 if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
349 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
Ted Kremenekc104e532011-01-18 04:53:25 +0000350 if (isTrackedVar(vd))
Ted Kremenek610068c2011-01-15 02:58:47 +0000351 return FindVarResult(vd, dr);
352
353 return FindVarResult(0, 0);
354}
355
356void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000357 if (bo->isAssignmentOp()) {
358 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
359 if (const VarDecl* vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000360 // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment"
361 // cannot be block-level expressions. Therefore, we determine if
362 // a DeclRefExpr is involved in a "load" by comparing it to the current
363 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
364 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
365 res.getDeclRefExpr());
366 Visit(bo->getRHS());
367 Visit(bo->getLHS());
368
Ted Kremenek610068c2011-01-15 02:58:47 +0000369 llvm::BitVector::reference bit = vals[vd];
370 if (bit == Uninitialized) {
371 if (bo->getOpcode() != BO_Assign)
372 reportUninit(res.getDeclRefExpr(), vd);
373 bit = Initialized;
374 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000375 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000376 }
377 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000378 Visit(bo->getRHS());
379 Visit(bo->getLHS());
Ted Kremenek610068c2011-01-15 02:58:47 +0000380}
381
382void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000383 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000384 case clang::UO_PostDec:
385 case clang::UO_PostInc:
386 case clang::UO_PreDec:
387 case clang::UO_PreInc: {
388 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
389 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000390 // We assume that DeclRefExprs wrapped in a unary operator ++/--
391 // cannot be block-level expressions. Therefore, we determine if
392 // a DeclRefExpr is involved in a "load" by comparing it to the current
393 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
394 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
395 res.getDeclRefExpr());
396 Visit(uo->getSubExpr());
397
Ted Kremenek610068c2011-01-15 02:58:47 +0000398 llvm::BitVector::reference bit = vals[vd];
399 if (bit == Uninitialized) {
400 reportUninit(res.getDeclRefExpr(), vd);
401 bit = Initialized;
402 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000403 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000404 }
405 break;
406 }
407 default:
408 break;
409 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000410 Visit(uo->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000411}
412
413void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000414 if (ce->getCastKind() == CK_LValueToRValue) {
415 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc21fed32011-01-18 21:18:58 +0000416 if (const VarDecl *vd = res.getDecl()) {
417 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
418 // cannot be block-level expressions. Therefore, we determine if
419 // a DeclRefExpr is involved in a "load" by comparing it to the current
420 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
421 // Here we update 'currentDR' to be the one associated with this
422 // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we
423 // will know that we are not computing its lvalue for other purposes
424 // than to perform a load.
425 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
426 res.getDeclRefExpr());
427 Visit(ce->getSubExpr());
428 if (vals[vd] == Uninitialized) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000429 reportUninit(res.getDeclRefExpr(), vd);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000430 // Don't cascade warnings.
431 vals[vd] = Initialized;
432 }
433 return;
434 }
435 }
436 Visit(ce->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000437}
438
Ted Kremenek96608032011-01-23 17:53:04 +0000439void TransferFunctions::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se) {
440 if (se->isSizeOf()) {
441 if (se->getType()->isConstantSizeType())
442 return;
443 // Handle VLAs.
444 Visit(se->getArgumentExpr());
445 }
446}
447
Ted Kremenek610068c2011-01-15 02:58:47 +0000448//------------------------------------------------------------------------====//
449// High-level "driver" logic for uninitialized values analysis.
450//====------------------------------------------------------------------------//
451
Ted Kremenek13bd4232011-01-20 17:37:17 +0000452static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremenek610068c2011-01-15 02:58:47 +0000453 CFGBlockValues &vals,
454 UninitVariablesHandler *handler = 0) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000455
456 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000457 if (block->pred_size() == 2 && block->succ_size() == 2) {
458 assert(block->getTerminatorCondition() == b);
459 BVPair valsAB = vals.getPredBitVectors(block);
460 vals.mergeIntoScratch(*valsAB.first, true);
461 vals.mergeIntoScratch(*valsAB.second, false);
462 valsAB.second = &vals.getScratch();
463 if (b->getOpcode() == BO_LOr) {
464 // Ensure the invariant that 'first' corresponds to the true
465 // branch and 'second' to the false.
466 std::swap(valsAB.first, valsAB.second);
467 }
468 return vals.updateBitVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000469 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000470 }
471
472 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000473 vals.resetScratch();
474 bool isFirst = true;
475 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
476 E = block->pred_end(); I != E; ++I) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000477 vals.mergeIntoScratch(vals.getBitVector(*I, block), isFirst);
Ted Kremenek610068c2011-01-15 02:58:47 +0000478 isFirst = false;
479 }
480 // Apply the transfer function.
481 TransferFunctions tf(vals, cfg, handler);
482 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
483 I != E; ++I) {
484 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
485 tf.BlockStmt_Visit(cs->getStmt());
486 }
487 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000488 return vals.updateBitVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000489}
490
491void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
492 const CFG &cfg,
493 UninitVariablesHandler &handler) {
494 CFGBlockValues vals(cfg);
495 vals.computeSetOfDeclarations(dc);
496 if (vals.hasNoDeclarations())
497 return;
498 DataflowWorklist worklist(cfg);
499 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
500
501 worklist.enqueueSuccessors(&cfg.getEntry());
502
503 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000504 // Did the block change?
Ted Kremenek13bd4232011-01-20 17:37:17 +0000505 bool changed = runOnBlock(block, cfg, vals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000506 if (changed || !previouslyVisited[block->getBlockID()])
507 worklist.enqueueSuccessors(block);
508 previouslyVisited[block->getBlockID()] = true;
509 }
510
511 // Run through the blocks one more time, and report uninitialized variabes.
512 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
513 runOnBlock(*BI, cfg, vals, &handler);
514 }
515}
516
517UninitVariablesHandler::~UninitVariablesHandler() {}
518