blob: b9a7676e62cdae05bdb7e0052f2553054cbb5934 [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 Kremenek40900ee2011-01-27 02:29:34 +000028static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
Ted Kremenekc104e532011-01-18 04:53:25 +000029 return vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
Ted Kremenek40900ee2011-01-27 02:29:34 +000030 vd->getType()->isScalarType() &&
31 vd->getDeclContext() == dc;
Ted Kremenekc104e532011-01-18 04:53:25 +000032}
33
Ted Kremenek610068c2011-01-15 02:58:47 +000034//------------------------------------------------------------------------====//
35// DeclToBit: a mapping from Decls we track to bitvector indices.
36//====------------------------------------------------------------------------//
37
38namespace {
39class DeclToBit {
40 llvm::DenseMap<const VarDecl *, unsigned> map;
41public:
42 DeclToBit() {}
43
44 /// Compute the actual mapping from declarations to bits.
45 void computeMap(const DeclContext &dc);
46
47 /// Return the number of declarations in the map.
48 unsigned size() const { return map.size(); }
49
50 /// Returns the bit vector index for a given declaration.
51 llvm::Optional<unsigned> getBitVectorIndex(const VarDecl *d);
52};
53}
54
55void DeclToBit::computeMap(const DeclContext &dc) {
56 unsigned count = 0;
57 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
58 E(dc.decls_end());
59 for ( ; I != E; ++I) {
60 const VarDecl *vd = *I;
Ted Kremenek40900ee2011-01-27 02:29:34 +000061 if (isTrackedVar(vd, &dc))
Ted Kremenek610068c2011-01-15 02:58:47 +000062 map[vd] = count++;
63 }
64}
65
66llvm::Optional<unsigned> DeclToBit::getBitVectorIndex(const VarDecl *d) {
67 llvm::DenseMap<const VarDecl *, unsigned>::iterator I = map.find(d);
68 if (I == map.end())
69 return llvm::Optional<unsigned>();
70 return I->second;
71}
72
73//------------------------------------------------------------------------====//
74// CFGBlockValues: dataflow values for CFG blocks.
75//====------------------------------------------------------------------------//
76
Ted Kremenek13bd4232011-01-20 17:37:17 +000077typedef std::pair<llvm::BitVector *, llvm::BitVector *> BVPair;
78
Ted Kremenek610068c2011-01-15 02:58:47 +000079namespace {
80class CFGBlockValues {
81 const CFG &cfg;
Ted Kremenek13bd4232011-01-20 17:37:17 +000082 BVPair *vals;
Ted Kremenek610068c2011-01-15 02:58:47 +000083 llvm::BitVector scratch;
84 DeclToBit declToBit;
Ted Kremenek13bd4232011-01-20 17:37:17 +000085
86 llvm::BitVector &lazyCreate(llvm::BitVector *&bv);
Ted Kremenek610068c2011-01-15 02:58:47 +000087public:
88 CFGBlockValues(const CFG &cfg);
89 ~CFGBlockValues();
90
91 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek13bd4232011-01-20 17:37:17 +000092 llvm::BitVector &getBitVector(const CFGBlock *block,
93 const CFGBlock *dstBlock);
94
95 BVPair &getBitVectors(const CFGBlock *block);
96
97 BVPair getPredBitVectors(const CFGBlock *block);
98
Ted Kremenek610068c2011-01-15 02:58:47 +000099 void mergeIntoScratch(llvm::BitVector const &source, bool isFirst);
100 bool updateBitVectorWithScratch(const CFGBlock *block);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000101 bool updateBitVectors(const CFGBlock *block, const BVPair &newVals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000102
103 bool hasNoDeclarations() const {
104 return declToBit.size() == 0;
105 }
106
107 void resetScratch();
Ted Kremenek13bd4232011-01-20 17:37:17 +0000108 llvm::BitVector &getScratch() { return scratch; }
109
Ted Kremenek610068c2011-01-15 02:58:47 +0000110 llvm::BitVector::reference operator[](const VarDecl *vd);
111};
112}
113
114CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {
115 unsigned n = cfg.getNumBlockIDs();
116 if (!n)
117 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000118 vals = new std::pair<llvm::BitVector*, llvm::BitVector*>[n];
Francois Pichet2d78c372011-01-15 13:27:47 +0000119 memset(vals, 0, sizeof(*vals) * n);
Ted Kremenek610068c2011-01-15 02:58:47 +0000120}
121
122CFGBlockValues::~CFGBlockValues() {
123 unsigned n = cfg.getNumBlockIDs();
124 if (n == 0)
125 return;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000126 for (unsigned i = 0; i < n; ++i) {
127 delete vals[i].first;
128 delete vals[i].second;
129 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000130 delete [] vals;
131}
132
133void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
134 declToBit.computeMap(dc);
135 scratch.resize(declToBit.size());
136}
137
Ted Kremenek13bd4232011-01-20 17:37:17 +0000138llvm::BitVector &CFGBlockValues::lazyCreate(llvm::BitVector *&bv) {
139 if (!bv)
Ted Kremenek610068c2011-01-15 02:58:47 +0000140 bv = new llvm::BitVector(declToBit.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000141 return *bv;
142}
143
Ted Kremenek13bd4232011-01-20 17:37:17 +0000144/// This function pattern matches for a '&&' or '||' that appears at
145/// the beginning of a CFGBlock that also (1) has a terminator and
146/// (2) has no other elements. If such an expression is found, it is returned.
147static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) {
148 if (block->empty())
149 return 0;
150
151 CFGStmt cstmt = block->front().getAs<CFGStmt>();
152 BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt.getStmt());
153 if (!b || !b->isLogicalOp() || block->getTerminatorCondition() != b)
154 return 0;
155 return b;
156}
157
158llvm::BitVector &CFGBlockValues::getBitVector(const CFGBlock *block,
159 const CFGBlock *dstBlock) {
160 unsigned idx = block->getBlockID();
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000161 if (dstBlock && block->succ_size() == 2 && block->pred_size() == 2) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000162 assert(block->getTerminator());
163 if (getLogicalOperatorInChain(block)) {
164 if (*block->succ_begin() == dstBlock)
165 return lazyCreate(vals[idx].first);
166 assert(*(block->succ_begin()+1) == dstBlock);
167 return lazyCreate(vals[idx].second);
168 }
169 }
170
171 assert(vals[idx].second == 0);
172 return lazyCreate(vals[idx].first);
173}
174
175BVPair &CFGBlockValues::getBitVectors(const clang::CFGBlock *block) {
176 unsigned idx = block->getBlockID();
177 lazyCreate(vals[idx].first);
178 lazyCreate(vals[idx].second);
179 return vals[idx];
180}
181
182BVPair CFGBlockValues::getPredBitVectors(const clang::CFGBlock *block) {
183 assert(block->pred_size() == 2);
184 CFGBlock::const_pred_iterator itr = block->pred_begin();
185 llvm::BitVector &bvA = getBitVector(*itr, block);
186 ++itr;
187 return BVPair(&bvA, &getBitVector(*itr, block));
188}
189
Ted Kremenek610068c2011-01-15 02:58:47 +0000190void CFGBlockValues::mergeIntoScratch(llvm::BitVector const &source,
191 bool isFirst) {
192 if (isFirst)
193 scratch = source;
194 else
Ted Kremenekc104e532011-01-18 04:53:25 +0000195 scratch |= source;
Ted Kremenek610068c2011-01-15 02:58:47 +0000196}
197
198bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000199 llvm::BitVector &dst = getBitVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000200 bool changed = (dst != scratch);
201 if (changed)
202 dst = scratch;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000203
Ted Kremenek13bd4232011-01-20 17:37:17 +0000204 return changed;
205}
206
207bool CFGBlockValues::updateBitVectors(const CFGBlock *block,
208 const BVPair &newVals) {
209 BVPair &vals = getBitVectors(block);
210 bool changed = *newVals.first != *vals.first ||
211 *newVals.second != *vals.second;
212 *vals.first = *newVals.first;
213 *vals.second = *newVals.second;
Ted Kremenek610068c2011-01-15 02:58:47 +0000214 return changed;
215}
216
217void CFGBlockValues::resetScratch() {
218 scratch.reset();
219}
220
221llvm::BitVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
222 const llvm::Optional<unsigned> &idx = declToBit.getBitVectorIndex(vd);
223 assert(idx.hasValue());
224 return scratch[idx.getValue()];
225}
226
227//------------------------------------------------------------------------====//
228// Worklist: worklist for dataflow analysis.
229//====------------------------------------------------------------------------//
230
231namespace {
232class DataflowWorklist {
233 llvm::SmallVector<const CFGBlock *, 20> worklist;
234 llvm::BitVector enqueuedBlocks;
235public:
236 DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {}
237
238 void enqueue(const CFGBlock *block);
239 void enqueueSuccessors(const CFGBlock *block);
240 const CFGBlock *dequeue();
241
242};
243}
244
245void DataflowWorklist::enqueue(const CFGBlock *block) {
Ted Kremenekc104e532011-01-18 04:53:25 +0000246 if (!block)
247 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000248 unsigned idx = block->getBlockID();
249 if (enqueuedBlocks[idx])
250 return;
251 worklist.push_back(block);
252 enqueuedBlocks[idx] = true;
253}
254
255void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
256 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
257 E = block->succ_end(); I != E; ++I) {
258 enqueue(*I);
259 }
260}
261
262const CFGBlock *DataflowWorklist::dequeue() {
263 if (worklist.empty())
264 return 0;
265 const CFGBlock *b = worklist.back();
266 worklist.pop_back();
267 enqueuedBlocks[b->getBlockID()] = false;
268 return b;
269}
270
271//------------------------------------------------------------------------====//
272// Transfer function for uninitialized values analysis.
273//====------------------------------------------------------------------------//
274
Ted Kremenekc104e532011-01-18 04:53:25 +0000275static const bool Initialized = false;
276static const bool Uninitialized = true;
Ted Kremenek610068c2011-01-15 02:58:47 +0000277
278namespace {
279class FindVarResult {
280 const VarDecl *vd;
281 const DeclRefExpr *dr;
282public:
283 FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {}
284
285 const DeclRefExpr *getDeclRefExpr() const { return dr; }
286 const VarDecl *getDecl() const { return vd; }
287};
288
289class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> {
290 CFGBlockValues &vals;
291 const CFG &cfg;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000292 AnalysisContext &ac;
Ted Kremenek610068c2011-01-15 02:58:47 +0000293 UninitVariablesHandler *handler;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000294 const DeclRefExpr *currentDR;
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000295 const Expr *currentVoidCast;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000296 const bool flagBlockUses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000297public:
298 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000299 AnalysisContext &ac,
300 UninitVariablesHandler *handler,
301 bool flagBlockUses)
302 : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0),
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000303 currentVoidCast(0), flagBlockUses(flagBlockUses) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000304
305 const CFG &getCFG() { return cfg; }
306 void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000307
308 void VisitBlockExpr(BlockExpr *be);
Ted Kremenek610068c2011-01-15 02:58:47 +0000309 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000310 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000311 void VisitUnaryOperator(UnaryOperator *uo);
312 void VisitBinaryOperator(BinaryOperator *bo);
313 void VisitCastExpr(CastExpr *ce);
Ted Kremenek96608032011-01-23 17:53:04 +0000314 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se);
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000315 void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs);
Ted Kremenek40900ee2011-01-27 02:29:34 +0000316
317 bool isTrackedVar(const VarDecl *vd) {
318 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
319 }
320
321 FindVarResult findBlockVarDecl(Expr *ex);
Ted Kremenek610068c2011-01-15 02:58:47 +0000322};
323}
324
325void TransferFunctions::reportUninit(const DeclRefExpr *ex,
326 const VarDecl *vd) {
327 if (handler) handler->handleUseOfUninitVariable(ex, vd);
328}
329
Ted Kremenek40900ee2011-01-27 02:29:34 +0000330FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) {
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000331 if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
332 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
333 if (isTrackedVar(vd))
Ted Kremenek40900ee2011-01-27 02:29:34 +0000334 return FindVarResult(vd, dr);
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000335 return FindVarResult(0, 0);
336}
337
338void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt(
339 ObjCForCollectionStmt *fs) {
340
341 Visit(fs->getCollection());
342
343 // This represents an initialization of the 'element' value.
344 Stmt *element = fs->getElement();
345 const VarDecl* vd = 0;
346
347 if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) {
348 vd = cast<VarDecl>(ds->getSingleDecl());
349 if (!isTrackedVar(vd))
350 vd = 0;
351 }
352 else {
353 // Initialize the value of the reference variable.
354 const FindVarResult &res = findBlockVarDecl(cast<Expr>(element));
355 vd = res.getDecl();
356 if (!vd) {
357 Visit(element);
358 return;
359 }
360 }
361
362 if (vd)
363 vals[vd] = Initialized;
364}
365
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000366void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
367 if (!flagBlockUses || !handler)
368 return;
369 AnalysisContext::referenced_decls_iterator i, e;
370 llvm::tie(i, e) = ac.getReferencedBlockVars(be->getBlockDecl());
371 for ( ; i != e; ++i) {
372 const VarDecl *vd = *i;
Ted Kremenek40900ee2011-01-27 02:29:34 +0000373 if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage() ||
374 !isTrackedVar(vd))
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000375 continue;
376 if (vals[vd] == Uninitialized)
Ted Kremenek40900ee2011-01-27 02:29:34 +0000377 handler->handleUseOfUninitVariable(be, vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000378 }
379}
380
Ted Kremenek610068c2011-01-15 02:58:47 +0000381void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
382 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
383 DI != DE; ++DI) {
384 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000385 if (isTrackedVar(vd)) {
386 vals[vd] = Uninitialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000387 if (Stmt *init = vd->getInit()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000388 Visit(init);
Ted Kremenekc104e532011-01-18 04:53:25 +0000389 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000390 }
Ted Kremenek4dccb902011-01-18 05:00:42 +0000391 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000392 else if (Stmt *init = vd->getInit()) {
393 Visit(init);
394 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000395 }
396 }
397}
398
Ted Kremenekc21fed32011-01-18 21:18:58 +0000399void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
400 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
401 // cannot be block-level expressions. Therefore, we determine if
402 // a DeclRefExpr is involved in a "load" by comparing it to the current
403 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
404 // If a DeclRefExpr is not involved in a load, we are essentially computing
405 // its address, either for assignment to a reference or via the '&' operator.
406 // In such cases, treat the variable as being initialized, since this
407 // analysis isn't powerful enough to do alias tracking.
408 if (dr != currentDR)
409 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
410 if (isTrackedVar(vd))
411 vals[vd] = Initialized;
412}
413
Ted Kremenek610068c2011-01-15 02:58:47 +0000414void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000415 if (bo->isAssignmentOp()) {
416 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
417 if (const VarDecl* vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000418 // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment"
419 // cannot be block-level expressions. Therefore, we determine if
420 // a DeclRefExpr is involved in a "load" by comparing it to the current
421 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
422 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
423 res.getDeclRefExpr());
424 Visit(bo->getRHS());
425 Visit(bo->getLHS());
426
Ted Kremenek610068c2011-01-15 02:58:47 +0000427 llvm::BitVector::reference bit = vals[vd];
428 if (bit == Uninitialized) {
429 if (bo->getOpcode() != BO_Assign)
430 reportUninit(res.getDeclRefExpr(), vd);
431 bit = Initialized;
432 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000433 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000434 }
435 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000436 Visit(bo->getRHS());
437 Visit(bo->getLHS());
Ted Kremenek610068c2011-01-15 02:58:47 +0000438}
439
440void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000441 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000442 case clang::UO_PostDec:
443 case clang::UO_PostInc:
444 case clang::UO_PreDec:
445 case clang::UO_PreInc: {
446 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
447 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000448 // We assume that DeclRefExprs wrapped in a unary operator ++/--
449 // cannot be block-level expressions. Therefore, we determine if
450 // a DeclRefExpr is involved in a "load" by comparing it to the current
451 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
452 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
453 res.getDeclRefExpr());
454 Visit(uo->getSubExpr());
455
Ted Kremenek610068c2011-01-15 02:58:47 +0000456 llvm::BitVector::reference bit = vals[vd];
457 if (bit == Uninitialized) {
458 reportUninit(res.getDeclRefExpr(), vd);
459 bit = Initialized;
460 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000461 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000462 }
463 break;
464 }
465 default:
466 break;
467 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000468 Visit(uo->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000469}
470
471void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000472 if (ce->getCastKind() == CK_LValueToRValue) {
473 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc21fed32011-01-18 21:18:58 +0000474 if (const VarDecl *vd = res.getDecl()) {
475 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
476 // cannot be block-level expressions. Therefore, we determine if
477 // a DeclRefExpr is involved in a "load" by comparing it to the current
478 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
479 // Here we update 'currentDR' to be the one associated with this
480 // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we
481 // will know that we are not computing its lvalue for other purposes
482 // than to perform a load.
483 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
484 res.getDeclRefExpr());
485 Visit(ce->getSubExpr());
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000486 if (currentVoidCast != ce && vals[vd] == Uninitialized) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000487 reportUninit(res.getDeclRefExpr(), vd);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000488 // Don't cascade warnings.
489 vals[vd] = Initialized;
490 }
491 return;
492 }
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000493 }
494 else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
495 if (cse->getType()->isVoidType()) {
496 // e.g. (void) x;
497 SaveAndRestore<const Expr *>
498 lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens());
499 Visit(cse->getSubExpr());
500 return;
501 }
502 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000503 Visit(ce->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000504}
505
Ted Kremenek96608032011-01-23 17:53:04 +0000506void TransferFunctions::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se) {
507 if (se->isSizeOf()) {
508 if (se->getType()->isConstantSizeType())
509 return;
510 // Handle VLAs.
511 Visit(se->getArgumentExpr());
512 }
513}
514
Ted Kremenek610068c2011-01-15 02:58:47 +0000515//------------------------------------------------------------------------====//
516// High-level "driver" logic for uninitialized values analysis.
517//====------------------------------------------------------------------------//
518
Ted Kremenek13bd4232011-01-20 17:37:17 +0000519static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000520 AnalysisContext &ac, CFGBlockValues &vals,
521 UninitVariablesHandler *handler = 0,
522 bool flagBlockUses = false) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000523
524 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000525 if (block->pred_size() == 2 && block->succ_size() == 2) {
526 assert(block->getTerminatorCondition() == b);
527 BVPair valsAB = vals.getPredBitVectors(block);
528 vals.mergeIntoScratch(*valsAB.first, true);
529 vals.mergeIntoScratch(*valsAB.second, false);
530 valsAB.second = &vals.getScratch();
531 if (b->getOpcode() == BO_LOr) {
532 // Ensure the invariant that 'first' corresponds to the true
533 // branch and 'second' to the false.
534 std::swap(valsAB.first, valsAB.second);
535 }
536 return vals.updateBitVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000537 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000538 }
539
540 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000541 vals.resetScratch();
542 bool isFirst = true;
543 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
544 E = block->pred_end(); I != E; ++I) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000545 vals.mergeIntoScratch(vals.getBitVector(*I, block), isFirst);
Ted Kremenek610068c2011-01-15 02:58:47 +0000546 isFirst = false;
547 }
548 // Apply the transfer function.
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000549 TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses);
Ted Kremenek610068c2011-01-15 02:58:47 +0000550 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
551 I != E; ++I) {
552 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
553 tf.BlockStmt_Visit(cs->getStmt());
554 }
555 }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000556 return vals.updateBitVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000557}
558
559void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
560 const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000561 AnalysisContext &ac,
Ted Kremenek610068c2011-01-15 02:58:47 +0000562 UninitVariablesHandler &handler) {
563 CFGBlockValues vals(cfg);
564 vals.computeSetOfDeclarations(dc);
565 if (vals.hasNoDeclarations())
566 return;
567 DataflowWorklist worklist(cfg);
568 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
569
570 worklist.enqueueSuccessors(&cfg.getEntry());
571
572 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000573 // Did the block change?
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000574 bool changed = runOnBlock(block, cfg, ac, vals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000575 if (changed || !previouslyVisited[block->getBlockID()])
576 worklist.enqueueSuccessors(block);
577 previouslyVisited[block->getBlockID()] = true;
578 }
579
580 // Run through the blocks one more time, and report uninitialized variabes.
581 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000582 runOnBlock(*BI, cfg, ac, vals, &handler, /* flagBlockUses */ true);
Ted Kremenek610068c2011-01-15 02:58:47 +0000583 }
584}
585
586UninitVariablesHandler::~UninitVariablesHandler() {}
587