blob: 121dcf909e423681f41290c255d1ad768d39b421 [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
188
189static void printVector(const CFGBlock *block, llvm::BitVector &bv,
190 unsigned num) {
191
192 #if 0
193 llvm::errs() << block->getBlockID() << " :";
194 for (unsigned i = 0; i < bv.size(); ++i) {
195 llvm::errs() << ' ' << bv[i];
196 }
197 llvm::errs() << " : " << num << '\n';
198 #endif
199}
200
Ted Kremenek610068c2011-01-15 02:58:47 +0000201void CFGBlockValues::mergeIntoScratch(llvm::BitVector const &source,
202 bool isFirst) {
203 if (isFirst)
204 scratch = source;
205 else
Ted Kremenekc104e532011-01-18 04:53:25 +0000206 scratch |= source;
Ted Kremenek610068c2011-01-15 02:58:47 +0000207}
208
209bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000210 llvm::BitVector &dst = getBitVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000211 bool changed = (dst != scratch);
212 if (changed)
213 dst = scratch;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000214
215 printVector(block, scratch, 0);
216 return changed;
217}
218
219bool CFGBlockValues::updateBitVectors(const CFGBlock *block,
220 const BVPair &newVals) {
221 BVPair &vals = getBitVectors(block);
222 bool changed = *newVals.first != *vals.first ||
223 *newVals.second != *vals.second;
224 *vals.first = *newVals.first;
225 *vals.second = *newVals.second;
226 printVector(block, *vals.first, 1);
227 printVector(block, *vals.second, 2);
Ted Kremenek610068c2011-01-15 02:58:47 +0000228 return changed;
229}
230
231void CFGBlockValues::resetScratch() {
232 scratch.reset();
233}
234
235llvm::BitVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
236 const llvm::Optional<unsigned> &idx = declToBit.getBitVectorIndex(vd);
237 assert(idx.hasValue());
238 return scratch[idx.getValue()];
239}
240
241//------------------------------------------------------------------------====//
242// Worklist: worklist for dataflow analysis.
243//====------------------------------------------------------------------------//
244
245namespace {
246class DataflowWorklist {
247 llvm::SmallVector<const CFGBlock *, 20> worklist;
248 llvm::BitVector enqueuedBlocks;
249public:
250 DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {}
251
252 void enqueue(const CFGBlock *block);
253 void enqueueSuccessors(const CFGBlock *block);
254 const CFGBlock *dequeue();
255
256};
257}
258
259void DataflowWorklist::enqueue(const CFGBlock *block) {
Ted Kremenekc104e532011-01-18 04:53:25 +0000260 if (!block)
261 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000262 unsigned idx = block->getBlockID();
263 if (enqueuedBlocks[idx])
264 return;
265 worklist.push_back(block);
266 enqueuedBlocks[idx] = true;
267}
268
269void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
270 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
271 E = block->succ_end(); I != E; ++I) {
272 enqueue(*I);
273 }
274}
275
276const CFGBlock *DataflowWorklist::dequeue() {
277 if (worklist.empty())
278 return 0;
279 const CFGBlock *b = worklist.back();
280 worklist.pop_back();
281 enqueuedBlocks[b->getBlockID()] = false;
282 return b;
283}
284
285//------------------------------------------------------------------------====//
286// Transfer function for uninitialized values analysis.
287//====------------------------------------------------------------------------//
288
Ted Kremenekc104e532011-01-18 04:53:25 +0000289static const bool Initialized = false;
290static const bool Uninitialized = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000291
292namespace {
293class FindVarResult {
294 const VarDecl *vd;
295 const DeclRefExpr *dr;
296public:
297 FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {}
298
299 const DeclRefExpr *getDeclRefExpr() const { return dr; }
300 const VarDecl *getDecl() const { return vd; }
301};
302
303class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> {
304 CFGBlockValues &vals;
305 const CFG &cfg;
306 UninitVariablesHandler *handler;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000307 const DeclRefExpr *currentDR;
Ted Kremenek610068c2011-01-15 02:58:47 +0000308public:
309 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
310 UninitVariablesHandler *handler)
Ted Kremenekc21fed32011-01-18 21:18:58 +0000311 : vals(vals), cfg(cfg), handler(handler), currentDR(0) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000312
313 const CFG &getCFG() { return cfg; }
314 void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
315
316 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000317 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000318 void VisitUnaryOperator(UnaryOperator *uo);
319 void VisitBinaryOperator(BinaryOperator *bo);
320 void VisitCastExpr(CastExpr *ce);
321};
322}
323
324void TransferFunctions::reportUninit(const DeclRefExpr *ex,
325 const VarDecl *vd) {
326 if (handler) handler->handleUseOfUninitVariable(ex, vd);
327}
328
329void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
330 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
331 DI != DE; ++DI) {
332 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000333 if (isTrackedVar(vd)) {
334 vals[vd] = Uninitialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000335 if (Stmt *init = vd->getInit()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000336 Visit(init);
Ted Kremenekc104e532011-01-18 04:53:25 +0000337 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000338 }
Ted Kremenek4dccb902011-01-18 05:00:42 +0000339 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000340 else if (Stmt *init = vd->getInit()) {
341 Visit(init);
342 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000343 }
344 }
345}
346
Ted Kremenekc21fed32011-01-18 21:18:58 +0000347void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
348 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
349 // cannot be block-level expressions. Therefore, we determine if
350 // a DeclRefExpr is involved in a "load" by comparing it to the current
351 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
352 // If a DeclRefExpr is not involved in a load, we are essentially computing
353 // its address, either for assignment to a reference or via the '&' operator.
354 // In such cases, treat the variable as being initialized, since this
355 // analysis isn't powerful enough to do alias tracking.
356 if (dr != currentDR)
357 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
358 if (isTrackedVar(vd))
359 vals[vd] = Initialized;
360}
361
Ted Kremenek610068c2011-01-15 02:58:47 +0000362static FindVarResult findBlockVarDecl(Expr* ex) {
363 if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
364 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
Ted Kremenekc104e532011-01-18 04:53:25 +0000365 if (isTrackedVar(vd))
Ted Kremenek610068c2011-01-15 02:58:47 +0000366 return FindVarResult(vd, dr);
367
368 return FindVarResult(0, 0);
369}
370
371void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000372 if (bo->isAssignmentOp()) {
373 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
374 if (const VarDecl* vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000375 // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment"
376 // cannot be block-level expressions. Therefore, we determine if
377 // a DeclRefExpr is involved in a "load" by comparing it to the current
378 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
379 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
380 res.getDeclRefExpr());
381 Visit(bo->getRHS());
382 Visit(bo->getLHS());
383
Ted Kremenek610068c2011-01-15 02:58:47 +0000384 llvm::BitVector::reference bit = vals[vd];
385 if (bit == Uninitialized) {
386 if (bo->getOpcode() != BO_Assign)
387 reportUninit(res.getDeclRefExpr(), vd);
388 bit = Initialized;
389 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000390 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000391 }
392 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000393 Visit(bo->getRHS());
394 Visit(bo->getLHS());
Ted Kremenek610068c2011-01-15 02:58:47 +0000395}
396
397void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000398 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000399 case clang::UO_PostDec:
400 case clang::UO_PostInc:
401 case clang::UO_PreDec:
402 case clang::UO_PreInc: {
403 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
404 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000405 // We assume that DeclRefExprs wrapped in a unary operator ++/--
406 // cannot be block-level expressions. Therefore, we determine if
407 // a DeclRefExpr is involved in a "load" by comparing it to the current
408 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
409 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
410 res.getDeclRefExpr());
411 Visit(uo->getSubExpr());
412
Ted Kremenek610068c2011-01-15 02:58:47 +0000413 llvm::BitVector::reference bit = vals[vd];
414 if (bit == Uninitialized) {
415 reportUninit(res.getDeclRefExpr(), vd);
416 bit = Initialized;
417 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000418 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000419 }
420 break;
421 }
422 default:
423 break;
424 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000425 Visit(uo->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000426}
427
428void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000429 if (ce->getCastKind() == CK_LValueToRValue) {
430 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc21fed32011-01-18 21:18:58 +0000431 if (const VarDecl *vd = res.getDecl()) {
432 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
433 // cannot be block-level expressions. Therefore, we determine if
434 // a DeclRefExpr is involved in a "load" by comparing it to the current
435 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
436 // Here we update 'currentDR' to be the one associated with this
437 // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we
438 // will know that we are not computing its lvalue for other purposes
439 // than to perform a load.
440 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
441 res.getDeclRefExpr());
442 Visit(ce->getSubExpr());
443 if (vals[vd] == Uninitialized) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000444 reportUninit(res.getDeclRefExpr(), vd);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000445 // Don't cascade warnings.
446 vals[vd] = Initialized;
447 }
448 return;
449 }
450 }
451 Visit(ce->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000452}
453
454//------------------------------------------------------------------------====//
455// High-level "driver" logic for uninitialized values analysis.
456//====------------------------------------------------------------------------//
457
Ted Kremenek13bd4232011-01-20 17:37:17 +0000458static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremenek610068c2011-01-15 02:58:47 +0000459 CFGBlockValues &vals,
460 UninitVariablesHandler *handler = 0) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000461
462 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000463 if (block->pred_size() == 2 && block->succ_size() == 2) {
464 assert(block->getTerminatorCondition() == b);
465 BVPair valsAB = vals.getPredBitVectors(block);
466 vals.mergeIntoScratch(*valsAB.first, true);
467 vals.mergeIntoScratch(*valsAB.second, false);
468 valsAB.second = &vals.getScratch();
469 if (b->getOpcode() == BO_LOr) {
470 // Ensure the invariant that 'first' corresponds to the true
471 // branch and 'second' to the false.
472 std::swap(valsAB.first, valsAB.second);
473 }
474 return vals.updateBitVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000475 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000476 }
477
478 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000479 vals.resetScratch();
480 bool isFirst = true;
481 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
482 E = block->pred_end(); I != E; ++I) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000483 vals.mergeIntoScratch(vals.getBitVector(*I, block), isFirst);
Ted Kremenek610068c2011-01-15 02:58:47 +0000484 isFirst = false;
485 }
486 // Apply the transfer function.
487 TransferFunctions tf(vals, cfg, handler);
488 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
489 I != E; ++I) {
490 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
491 tf.BlockStmt_Visit(cs->getStmt());
492 }
493 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000494 return vals.updateBitVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000495}
496
497void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
498 const CFG &cfg,
499 UninitVariablesHandler &handler) {
500 CFGBlockValues vals(cfg);
501 vals.computeSetOfDeclarations(dc);
502 if (vals.hasNoDeclarations())
503 return;
504 DataflowWorklist worklist(cfg);
505 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
506
507 worklist.enqueueSuccessors(&cfg.getEntry());
508
509 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000510 // Did the block change?
Ted Kremenek13bd4232011-01-20 17:37:17 +0000511 bool changed = runOnBlock(block, cfg, vals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000512 if (changed || !previouslyVisited[block->getBlockID()])
513 worklist.enqueueSuccessors(block);
514 previouslyVisited[block->getBlockID()] = true;
515 }
516
517 // Run through the blocks one more time, and report uninitialized variabes.
518 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
519 runOnBlock(*BI, cfg, vals, &handler);
520 }
521}
522
523UninitVariablesHandler::~UninitVariablesHandler() {}
524