blob: 32630549599a393e08492581e138f2bdd1e6ca88 [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"
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"
23#include "clang/Analysis/Analyses/UninitializedValuesV2.h"
Ted Kremenekc21fed32011-01-18 21:18:58 +000024#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek610068c2011-01-15 02:58:47 +000025
26using namespace clang;
27
Ted Kremenekc104e532011-01-18 04:53:25 +000028static bool isTrackedVar(const VarDecl *vd) {
29 return vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
30 vd->getType()->isScalarType();
31}
32
Ted Kremenek610068c2011-01-15 02:58:47 +000033//------------------------------------------------------------------------====//
34// DeclToBit: a mapping from Decls we track to bitvector indices.
35//====------------------------------------------------------------------------//
36
37namespace {
38class DeclToBit {
39 llvm::DenseMap<const VarDecl *, unsigned> map;
40public:
41 DeclToBit() {}
42
43 /// Compute the actual mapping from declarations to bits.
44 void computeMap(const DeclContext &dc);
45
46 /// Return the number of declarations in the map.
47 unsigned size() const { return map.size(); }
48
49 /// Returns the bit vector index for a given declaration.
50 llvm::Optional<unsigned> getBitVectorIndex(const VarDecl *d);
51};
52}
53
54void DeclToBit::computeMap(const DeclContext &dc) {
55 unsigned count = 0;
56 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
57 E(dc.decls_end());
58 for ( ; I != E; ++I) {
59 const VarDecl *vd = *I;
Ted Kremenekc104e532011-01-18 04:53:25 +000060 if (isTrackedVar(vd))
Ted Kremenek610068c2011-01-15 02:58:47 +000061 map[vd] = count++;
62 }
63}
64
65llvm::Optional<unsigned> DeclToBit::getBitVectorIndex(const VarDecl *d) {
66 llvm::DenseMap<const VarDecl *, unsigned>::iterator I = map.find(d);
67 if (I == map.end())
68 return llvm::Optional<unsigned>();
69 return I->second;
70}
71
72//------------------------------------------------------------------------====//
73// CFGBlockValues: dataflow values for CFG blocks.
74//====------------------------------------------------------------------------//
75
Ted Kremenek13bd4232011-01-20 17:37:17 +000076typedef std::pair<llvm::BitVector *, llvm::BitVector *> BVPair;
77
Ted Kremenek610068c2011-01-15 02:58:47 +000078namespace {
79class CFGBlockValues {
80 const CFG &cfg;
Ted Kremenek13bd4232011-01-20 17:37:17 +000081 BVPair *vals;
Ted Kremenek610068c2011-01-15 02:58:47 +000082 llvm::BitVector scratch;
83 DeclToBit declToBit;
Ted Kremenek13bd4232011-01-20 17:37:17 +000084
85 llvm::BitVector &lazyCreate(llvm::BitVector *&bv);
Ted Kremenek610068c2011-01-15 02:58:47 +000086public:
87 CFGBlockValues(const CFG &cfg);
88 ~CFGBlockValues();
89
90 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek13bd4232011-01-20 17:37:17 +000091 llvm::BitVector &getBitVector(const CFGBlock *block,
92 const CFGBlock *dstBlock);
93
94 BVPair &getBitVectors(const CFGBlock *block);
95
96 BVPair getPredBitVectors(const CFGBlock *block);
97
Ted Kremenek610068c2011-01-15 02:58:47 +000098 void mergeIntoScratch(llvm::BitVector const &source, bool isFirst);
99 bool updateBitVectorWithScratch(const CFGBlock *block);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000100 bool updateBitVectors(const CFGBlock *block, const BVPair &newVals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000101
102 bool hasNoDeclarations() const {
103 return declToBit.size() == 0;
104 }
105
106 void resetScratch();
Ted Kremenek13bd4232011-01-20 17:37:17 +0000107 llvm::BitVector &getScratch() { return scratch; }
108
Ted Kremenek610068c2011-01-15 02:58:47 +0000109 llvm::BitVector::reference operator[](const VarDecl *vd);
110};
111}
112
113CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {
114 unsigned n = cfg.getNumBlockIDs();
115 if (!n)
116 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000117 vals = new std::pair<llvm::BitVector*, llvm::BitVector*>[n];
Francois Pichet2d78c372011-01-15 13:27:47 +0000118 memset(vals, 0, sizeof(*vals) * n);
Ted Kremenek610068c2011-01-15 02:58:47 +0000119}
120
121CFGBlockValues::~CFGBlockValues() {
122 unsigned n = cfg.getNumBlockIDs();
123 if (n == 0)
124 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000125 for (unsigned i = 0; i < n; ++i) {
126 delete vals[i].first;
127 delete vals[i].second;
128 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000129 delete [] vals;
130}
131
132void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
133 declToBit.computeMap(dc);
134 scratch.resize(declToBit.size());
135}
136
Ted Kremenek13bd4232011-01-20 17:37:17 +0000137llvm::BitVector &CFGBlockValues::lazyCreate(llvm::BitVector *&bv) {
138 if (!bv)
Ted Kremenek610068c2011-01-15 02:58:47 +0000139 bv = new llvm::BitVector(declToBit.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000140 return *bv;
141}
142
Ted Kremenek13bd4232011-01-20 17:37:17 +0000143/// This function pattern matches for a '&&' or '||' that appears at
144/// the beginning of a CFGBlock that also (1) has a terminator and
145/// (2) has no other elements. If such an expression is found, it is returned.
146static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) {
147 if (block->empty())
148 return 0;
149
150 CFGStmt cstmt = block->front().getAs<CFGStmt>();
151 BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt.getStmt());
152 if (!b || !b->isLogicalOp() || block->getTerminatorCondition() != b)
153 return 0;
154 return b;
155}
156
157llvm::BitVector &CFGBlockValues::getBitVector(const CFGBlock *block,
158 const CFGBlock *dstBlock) {
159 unsigned idx = block->getBlockID();
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000160 if (dstBlock && block->succ_size() == 2 && block->pred_size() == 2) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000161 assert(block->getTerminator());
162 if (getLogicalOperatorInChain(block)) {
163 if (*block->succ_begin() == dstBlock)
164 return lazyCreate(vals[idx].first);
165 assert(*(block->succ_begin()+1) == dstBlock);
166 return lazyCreate(vals[idx].second);
167 }
168 }
169
170 assert(vals[idx].second == 0);
171 return lazyCreate(vals[idx].first);
172}
173
174BVPair &CFGBlockValues::getBitVectors(const clang::CFGBlock *block) {
175 unsigned idx = block->getBlockID();
176 lazyCreate(vals[idx].first);
177 lazyCreate(vals[idx].second);
178 return vals[idx];
179}
180
181BVPair CFGBlockValues::getPredBitVectors(const clang::CFGBlock *block) {
182 assert(block->pred_size() == 2);
183 CFGBlock::const_pred_iterator itr = block->pred_begin();
184 llvm::BitVector &bvA = getBitVector(*itr, block);
185 ++itr;
186 return BVPair(&bvA, &getBitVector(*itr, block));
187}
188
Ted Kremenek610068c2011-01-15 02:58:47 +0000189void CFGBlockValues::mergeIntoScratch(llvm::BitVector const &source,
190 bool isFirst) {
191 if (isFirst)
192 scratch = source;
193 else
Ted Kremenekc104e532011-01-18 04:53:25 +0000194 scratch |= source;
Ted Kremenek610068c2011-01-15 02:58:47 +0000195}
196
197bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000198 llvm::BitVector &dst = getBitVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000199 bool changed = (dst != scratch);
200 if (changed)
201 dst = scratch;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000202
Ted Kremenek13bd4232011-01-20 17:37:17 +0000203 return changed;
204}
205
206bool CFGBlockValues::updateBitVectors(const CFGBlock *block,
207 const BVPair &newVals) {
208 BVPair &vals = getBitVectors(block);
209 bool changed = *newVals.first != *vals.first ||
210 *newVals.second != *vals.second;
211 *vals.first = *newVals.first;
212 *vals.second = *newVals.second;
Ted Kremenek610068c2011-01-15 02:58:47 +0000213 return changed;
214}
215
216void CFGBlockValues::resetScratch() {
217 scratch.reset();
218}
219
220llvm::BitVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
221 const llvm::Optional<unsigned> &idx = declToBit.getBitVectorIndex(vd);
222 assert(idx.hasValue());
223 return scratch[idx.getValue()];
224}
225
226//------------------------------------------------------------------------====//
227// Worklist: worklist for dataflow analysis.
228//====------------------------------------------------------------------------//
229
230namespace {
231class DataflowWorklist {
232 llvm::SmallVector<const CFGBlock *, 20> worklist;
233 llvm::BitVector enqueuedBlocks;
234public:
235 DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {}
236
237 void enqueue(const CFGBlock *block);
238 void enqueueSuccessors(const CFGBlock *block);
239 const CFGBlock *dequeue();
240
241};
242}
243
244void DataflowWorklist::enqueue(const CFGBlock *block) {
Ted Kremenekc104e532011-01-18 04:53:25 +0000245 if (!block)
246 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000247 unsigned idx = block->getBlockID();
248 if (enqueuedBlocks[idx])
249 return;
250 worklist.push_back(block);
251 enqueuedBlocks[idx] = true;
252}
253
254void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
255 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
256 E = block->succ_end(); I != E; ++I) {
257 enqueue(*I);
258 }
259}
260
261const CFGBlock *DataflowWorklist::dequeue() {
262 if (worklist.empty())
263 return 0;
264 const CFGBlock *b = worklist.back();
265 worklist.pop_back();
266 enqueuedBlocks[b->getBlockID()] = false;
267 return b;
268}
269
270//------------------------------------------------------------------------====//
271// Transfer function for uninitialized values analysis.
272//====------------------------------------------------------------------------//
273
Ted Kremenekc104e532011-01-18 04:53:25 +0000274static const bool Initialized = false;
275static const bool Uninitialized = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000276
277namespace {
278class FindVarResult {
279 const VarDecl *vd;
280 const DeclRefExpr *dr;
281public:
282 FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {}
283
284 const DeclRefExpr *getDeclRefExpr() const { return dr; }
285 const VarDecl *getDecl() const { return vd; }
286};
287
288class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> {
289 CFGBlockValues &vals;
290 const CFG &cfg;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000291 AnalysisContext &ac;
Ted Kremenek610068c2011-01-15 02:58:47 +0000292 UninitVariablesHandler *handler;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000293 const DeclRefExpr *currentDR;
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000294 const Expr *currentVoidCast;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000295 const bool flagBlockUses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000296public:
297 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000298 AnalysisContext &ac,
299 UninitVariablesHandler *handler,
300 bool flagBlockUses)
301 : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0),
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000302 currentVoidCast(0), flagBlockUses(flagBlockUses) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000303
304 const CFG &getCFG() { return cfg; }
305 void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000306
307 void VisitBlockExpr(BlockExpr *be);
Ted Kremenek610068c2011-01-15 02:58:47 +0000308 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000309 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000310 void VisitUnaryOperator(UnaryOperator *uo);
311 void VisitBinaryOperator(BinaryOperator *bo);
312 void VisitCastExpr(CastExpr *ce);
Ted Kremenek96608032011-01-23 17:53:04 +0000313 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se);
Ted Kremenek610068c2011-01-15 02:58:47 +0000314};
315}
316
317void TransferFunctions::reportUninit(const DeclRefExpr *ex,
318 const VarDecl *vd) {
319 if (handler) handler->handleUseOfUninitVariable(ex, vd);
320}
321
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000322void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
323 if (!flagBlockUses || !handler)
324 return;
325 AnalysisContext::referenced_decls_iterator i, e;
326 llvm::tie(i, e) = ac.getReferencedBlockVars(be->getBlockDecl());
327 for ( ; i != e; ++i) {
328 const VarDecl *vd = *i;
329 if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage())
330 continue;
331 if (vals[vd] == Uninitialized)
332 handler->handleUseOfUninitVariable(be, vd);
333 }
334}
335
Ted Kremenek610068c2011-01-15 02:58:47 +0000336void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
337 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
338 DI != DE; ++DI) {
339 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000340 if (isTrackedVar(vd)) {
341 vals[vd] = Uninitialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000342 if (Stmt *init = vd->getInit()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000343 Visit(init);
Ted Kremenekc104e532011-01-18 04:53:25 +0000344 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000345 }
Ted Kremenek4dccb902011-01-18 05:00:42 +0000346 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000347 else if (Stmt *init = vd->getInit()) {
348 Visit(init);
349 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000350 }
351 }
352}
353
Ted Kremenekc21fed32011-01-18 21:18:58 +0000354void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
355 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
356 // cannot be block-level expressions. Therefore, we determine if
357 // a DeclRefExpr is involved in a "load" by comparing it to the current
358 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
359 // If a DeclRefExpr is not involved in a load, we are essentially computing
360 // its address, either for assignment to a reference or via the '&' operator.
361 // In such cases, treat the variable as being initialized, since this
362 // analysis isn't powerful enough to do alias tracking.
363 if (dr != currentDR)
364 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
365 if (isTrackedVar(vd))
366 vals[vd] = Initialized;
367}
368
Ted Kremenek610068c2011-01-15 02:58:47 +0000369static FindVarResult findBlockVarDecl(Expr* ex) {
370 if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
371 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
Ted Kremenekc104e532011-01-18 04:53:25 +0000372 if (isTrackedVar(vd))
Ted Kremenek610068c2011-01-15 02:58:47 +0000373 return FindVarResult(vd, dr);
374
375 return FindVarResult(0, 0);
376}
377
378void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000379 if (bo->isAssignmentOp()) {
380 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
381 if (const VarDecl* vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000382 // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment"
383 // cannot be block-level expressions. Therefore, we determine if
384 // a DeclRefExpr is involved in a "load" by comparing it to the current
385 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
386 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
387 res.getDeclRefExpr());
388 Visit(bo->getRHS());
389 Visit(bo->getLHS());
390
Ted Kremenek610068c2011-01-15 02:58:47 +0000391 llvm::BitVector::reference bit = vals[vd];
392 if (bit == Uninitialized) {
393 if (bo->getOpcode() != BO_Assign)
394 reportUninit(res.getDeclRefExpr(), vd);
395 bit = Initialized;
396 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000397 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000398 }
399 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000400 Visit(bo->getRHS());
401 Visit(bo->getLHS());
Ted Kremenek610068c2011-01-15 02:58:47 +0000402}
403
404void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000405 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000406 case clang::UO_PostDec:
407 case clang::UO_PostInc:
408 case clang::UO_PreDec:
409 case clang::UO_PreInc: {
410 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
411 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000412 // We assume that DeclRefExprs wrapped in a unary operator ++/--
413 // cannot be block-level expressions. Therefore, we determine if
414 // a DeclRefExpr is involved in a "load" by comparing it to the current
415 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
416 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
417 res.getDeclRefExpr());
418 Visit(uo->getSubExpr());
419
Ted Kremenek610068c2011-01-15 02:58:47 +0000420 llvm::BitVector::reference bit = vals[vd];
421 if (bit == Uninitialized) {
422 reportUninit(res.getDeclRefExpr(), vd);
423 bit = Initialized;
424 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000425 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000426 }
427 break;
428 }
429 default:
430 break;
431 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000432 Visit(uo->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000433}
434
435void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000436 if (ce->getCastKind() == CK_LValueToRValue) {
437 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc21fed32011-01-18 21:18:58 +0000438 if (const VarDecl *vd = res.getDecl()) {
439 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
440 // cannot be block-level expressions. Therefore, we determine if
441 // a DeclRefExpr is involved in a "load" by comparing it to the current
442 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
443 // Here we update 'currentDR' to be the one associated with this
444 // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we
445 // will know that we are not computing its lvalue for other purposes
446 // than to perform a load.
447 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
448 res.getDeclRefExpr());
449 Visit(ce->getSubExpr());
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000450 if (currentVoidCast != ce && vals[vd] == Uninitialized) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000451 reportUninit(res.getDeclRefExpr(), vd);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000452 // Don't cascade warnings.
453 vals[vd] = Initialized;
454 }
455 return;
456 }
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000457 }
458 else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
459 if (cse->getType()->isVoidType()) {
460 // e.g. (void) x;
461 SaveAndRestore<const Expr *>
462 lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens());
463 Visit(cse->getSubExpr());
464 return;
465 }
466 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000467 Visit(ce->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000468}
469
Ted Kremenek96608032011-01-23 17:53:04 +0000470void TransferFunctions::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se) {
471 if (se->isSizeOf()) {
472 if (se->getType()->isConstantSizeType())
473 return;
474 // Handle VLAs.
475 Visit(se->getArgumentExpr());
476 }
477}
478
Ted Kremenek610068c2011-01-15 02:58:47 +0000479//------------------------------------------------------------------------====//
480// High-level "driver" logic for uninitialized values analysis.
481//====------------------------------------------------------------------------//
482
Ted Kremenek13bd4232011-01-20 17:37:17 +0000483static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000484 AnalysisContext &ac, CFGBlockValues &vals,
485 UninitVariablesHandler *handler = 0,
486 bool flagBlockUses = false) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000487
488 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000489 if (block->pred_size() == 2 && block->succ_size() == 2) {
490 assert(block->getTerminatorCondition() == b);
491 BVPair valsAB = vals.getPredBitVectors(block);
492 vals.mergeIntoScratch(*valsAB.first, true);
493 vals.mergeIntoScratch(*valsAB.second, false);
494 valsAB.second = &vals.getScratch();
495 if (b->getOpcode() == BO_LOr) {
496 // Ensure the invariant that 'first' corresponds to the true
497 // branch and 'second' to the false.
498 std::swap(valsAB.first, valsAB.second);
499 }
500 return vals.updateBitVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000501 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000502 }
503
504 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000505 vals.resetScratch();
506 bool isFirst = true;
507 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
508 E = block->pred_end(); I != E; ++I) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000509 vals.mergeIntoScratch(vals.getBitVector(*I, block), isFirst);
Ted Kremenek610068c2011-01-15 02:58:47 +0000510 isFirst = false;
511 }
512 // Apply the transfer function.
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000513 TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses);
Ted Kremenek610068c2011-01-15 02:58:47 +0000514 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
515 I != E; ++I) {
516 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
517 tf.BlockStmt_Visit(cs->getStmt());
518 }
519 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000520 return vals.updateBitVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000521}
522
523void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
524 const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000525 AnalysisContext &ac,
Ted Kremenek610068c2011-01-15 02:58:47 +0000526 UninitVariablesHandler &handler) {
527 CFGBlockValues vals(cfg);
528 vals.computeSetOfDeclarations(dc);
529 if (vals.hasNoDeclarations())
530 return;
531 DataflowWorklist worklist(cfg);
532 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
533
534 worklist.enqueueSuccessors(&cfg.getEntry());
535
536 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000537 // Did the block change?
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000538 bool changed = runOnBlock(block, cfg, ac, vals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000539 if (changed || !previouslyVisited[block->getBlockID()])
540 worklist.enqueueSuccessors(block);
541 previouslyVisited[block->getBlockID()] = true;
542 }
543
544 // Run through the blocks one more time, and report uninitialized variabes.
545 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000546 runOnBlock(*BI, cfg, ac, vals, &handler, /* flagBlockUses */ true);
Ted Kremenek610068c2011-01-15 02:58:47 +0000547 }
548}
549
550UninitVariablesHandler::~UninitVariablesHandler() {}
551