blob: 5dd4c236aace2040d3f284203eeff92b864f770c [file] [log] [blame]
Ted Kremenek6f342132011-03-15 03:17:07 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenek610068c2011-01-15 02:58:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements uninitialized values analysis for source-level CFGs.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek13bd4232011-01-20 17:37:17 +000014#include <utility>
Ted Kremenek610068c2011-01-15 02:58:47 +000015#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/SmallVector.h"
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"
Ted Kremenek6f342132011-03-15 03:17:07 +000023#include "clang/Analysis/Analyses/UninitializedValues.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//------------------------------------------------------------------------====//
Ted Kremenek136f8f22011-03-15 04:57:27 +000035// DeclToIndex: a mapping from Decls we track to value indices.
Ted Kremenek610068c2011-01-15 02:58:47 +000036//====------------------------------------------------------------------------//
37
38namespace {
Ted Kremenek136f8f22011-03-15 04:57:27 +000039class DeclToIndex {
Ted Kremenek610068c2011-01-15 02:58:47 +000040 llvm::DenseMap<const VarDecl *, unsigned> map;
41public:
Ted Kremenek136f8f22011-03-15 04:57:27 +000042 DeclToIndex() {}
Ted Kremenek610068c2011-01-15 02:58:47 +000043
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.
Ted Kremenek136f8f22011-03-15 04:57:27 +000051 llvm::Optional<unsigned> getValueIndex(const VarDecl *d);
Ted Kremenek610068c2011-01-15 02:58:47 +000052};
53}
54
Ted Kremenek136f8f22011-03-15 04:57:27 +000055void DeclToIndex::computeMap(const DeclContext &dc) {
Ted Kremenek610068c2011-01-15 02:58:47 +000056 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
Ted Kremenek136f8f22011-03-15 04:57:27 +000066llvm::Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) {
Ted Kremenek610068c2011-01-15 02:58:47 +000067 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 Kremenek136f8f22011-03-15 04:57:27 +000077typedef llvm::BitVector ValueVector;
78typedef std::pair<ValueVector *, ValueVector *> BVPair;
Ted Kremenek13bd4232011-01-20 17:37:17 +000079
Ted Kremenek610068c2011-01-15 02:58:47 +000080namespace {
81class CFGBlockValues {
82 const CFG &cfg;
Ted Kremenek13bd4232011-01-20 17:37:17 +000083 BVPair *vals;
Ted Kremenek136f8f22011-03-15 04:57:27 +000084 ValueVector scratch;
85 DeclToIndex DeclToIndex;
Ted Kremenek13bd4232011-01-20 17:37:17 +000086
Ted Kremenek136f8f22011-03-15 04:57:27 +000087 ValueVector &lazyCreate(ValueVector *&bv);
Ted Kremenek610068c2011-01-15 02:58:47 +000088public:
89 CFGBlockValues(const CFG &cfg);
90 ~CFGBlockValues();
91
92 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek136f8f22011-03-15 04:57:27 +000093 ValueVector &getValueVector(const CFGBlock *block,
Ted Kremenek13bd4232011-01-20 17:37:17 +000094 const CFGBlock *dstBlock);
95
Ted Kremenek136f8f22011-03-15 04:57:27 +000096 BVPair &getValueVectors(const CFGBlock *block, bool shouldLazyCreate);
Ted Kremenek13bd4232011-01-20 17:37:17 +000097
Ted Kremenek136f8f22011-03-15 04:57:27 +000098 void mergeIntoScratch(ValueVector const &source, bool isFirst);
99 bool updateValueVectorWithScratch(const CFGBlock *block);
100 bool updateValueVectors(const CFGBlock *block, const BVPair &newVals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000101
102 bool hasNoDeclarations() const {
Ted Kremenek136f8f22011-03-15 04:57:27 +0000103 return DeclToIndex.size() == 0;
Ted Kremenek610068c2011-01-15 02:58:47 +0000104 }
105
106 void resetScratch();
Ted Kremenek136f8f22011-03-15 04:57:27 +0000107 ValueVector &getScratch() { return scratch; }
Ted Kremenek13bd4232011-01-20 17:37:17 +0000108
Ted Kremenek136f8f22011-03-15 04:57:27 +0000109 ValueVector::reference operator[](const VarDecl *vd);
Ted Kremenek610068c2011-01-15 02:58:47 +0000110};
111}
112
113CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {
114 unsigned n = cfg.getNumBlockIDs();
115 if (!n)
116 return;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000117 vals = new std::pair<ValueVector*, ValueVector*>[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) {
Ted Kremenek136f8f22011-03-15 04:57:27 +0000133 DeclToIndex.computeMap(dc);
134 scratch.resize(DeclToIndex.size());
Ted Kremenek610068c2011-01-15 02:58:47 +0000135}
136
Ted Kremenek136f8f22011-03-15 04:57:27 +0000137ValueVector &CFGBlockValues::lazyCreate(ValueVector *&bv) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000138 if (!bv)
Ted Kremenek136f8f22011-03-15 04:57:27 +0000139 bv = new ValueVector(DeclToIndex.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;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000149
Ted Kremenek3c0349e2011-03-01 03:15:10 +0000150 const CFGStmt *cstmt = block->front().getAs<CFGStmt>();
151 BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt->getStmt());
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000152
153 if (!b || !b->isLogicalOp())
Ted Kremenek13bd4232011-01-20 17:37:17 +0000154 return 0;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000155
156 if (block->pred_size() == 2 &&
157 ((block->succ_size() == 2 && block->getTerminatorCondition() == b) ||
158 block->size() == 1))
159 return b;
160
161 return 0;
Ted Kremenek13bd4232011-01-20 17:37:17 +0000162}
163
Ted Kremenek136f8f22011-03-15 04:57:27 +0000164ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block,
165 const CFGBlock *dstBlock) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000166 unsigned idx = block->getBlockID();
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000167 if (dstBlock && getLogicalOperatorInChain(block)) {
168 if (*block->succ_begin() == dstBlock)
169 return lazyCreate(vals[idx].first);
170 assert(*(block->succ_begin()+1) == dstBlock);
171 return lazyCreate(vals[idx].second);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000172 }
173
174 assert(vals[idx].second == 0);
175 return lazyCreate(vals[idx].first);
176}
177
Ted Kremenek136f8f22011-03-15 04:57:27 +0000178BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block,
179 bool shouldLazyCreate) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000180 unsigned idx = block->getBlockID();
181 lazyCreate(vals[idx].first);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000182 if (shouldLazyCreate)
183 lazyCreate(vals[idx].second);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000184 return vals[idx];
185}
186
Ted Kremenek136f8f22011-03-15 04:57:27 +0000187void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
Ted Kremenek610068c2011-01-15 02:58:47 +0000188 bool isFirst) {
189 if (isFirst)
190 scratch = source;
191 else
Ted Kremenekc104e532011-01-18 04:53:25 +0000192 scratch |= source;
Ted Kremenek610068c2011-01-15 02:58:47 +0000193}
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000194#if 0
Ted Kremenek136f8f22011-03-15 04:57:27 +0000195static void printVector(const CFGBlock *block, ValueVector &bv,
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000196 unsigned num) {
197
198 llvm::errs() << block->getBlockID() << " :";
199 for (unsigned i = 0; i < bv.size(); ++i) {
200 llvm::errs() << ' ' << bv[i];
201 }
202 llvm::errs() << " : " << num << '\n';
203}
204#endif
Ted Kremenek610068c2011-01-15 02:58:47 +0000205
Ted Kremenek136f8f22011-03-15 04:57:27 +0000206bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
207 ValueVector &dst = getValueVector(block, 0);
Ted Kremenek610068c2011-01-15 02:58:47 +0000208 bool changed = (dst != scratch);
209 if (changed)
210 dst = scratch;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000211#if 0
212 printVector(block, scratch, 0);
213#endif
Ted Kremenek13bd4232011-01-20 17:37:17 +0000214 return changed;
215}
216
Ted Kremenek136f8f22011-03-15 04:57:27 +0000217bool CFGBlockValues::updateValueVectors(const CFGBlock *block,
Ted Kremenek13bd4232011-01-20 17:37:17 +0000218 const BVPair &newVals) {
Ted Kremenek136f8f22011-03-15 04:57:27 +0000219 BVPair &vals = getValueVectors(block, true);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000220 bool changed = *newVals.first != *vals.first ||
221 *newVals.second != *vals.second;
222 *vals.first = *newVals.first;
223 *vals.second = *newVals.second;
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000224#if 0
225 printVector(block, *vals.first, 1);
226 printVector(block, *vals.second, 2);
227#endif
Ted Kremenek610068c2011-01-15 02:58:47 +0000228 return changed;
229}
230
231void CFGBlockValues::resetScratch() {
232 scratch.reset();
233}
234
Ted Kremenek136f8f22011-03-15 04:57:27 +0000235ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
236 const llvm::Optional<unsigned> &idx = DeclToIndex.getValueIndex(vd);
Ted Kremenek610068c2011-01-15 02:58:47 +0000237 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;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000248 ValueVector enqueuedBlocks;
Ted Kremenek610068c2011-01-15 02:58:47 +0000249public:
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;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000306 AnalysisContext &ac;
Ted Kremenek610068c2011-01-15 02:58:47 +0000307 UninitVariablesHandler *handler;
Ted Kremenekc21fed32011-01-18 21:18:58 +0000308 const DeclRefExpr *currentDR;
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000309 const Expr *currentVoidCast;
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000310 const bool flagBlockUses;
Ted Kremenek610068c2011-01-15 02:58:47 +0000311public:
312 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000313 AnalysisContext &ac,
314 UninitVariablesHandler *handler,
315 bool flagBlockUses)
316 : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0),
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000317 currentVoidCast(0), flagBlockUses(flagBlockUses) {}
Ted Kremenek610068c2011-01-15 02:58:47 +0000318
319 const CFG &getCFG() { return cfg; }
320 void reportUninit(const DeclRefExpr *ex, const VarDecl *vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000321
322 void VisitBlockExpr(BlockExpr *be);
Ted Kremenek610068c2011-01-15 02:58:47 +0000323 void VisitDeclStmt(DeclStmt *ds);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000324 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenek610068c2011-01-15 02:58:47 +0000325 void VisitUnaryOperator(UnaryOperator *uo);
326 void VisitBinaryOperator(BinaryOperator *bo);
327 void VisitCastExpr(CastExpr *ce);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000328 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *se);
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000329 void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs);
Ted Kremenek40900ee2011-01-27 02:29:34 +0000330
331 bool isTrackedVar(const VarDecl *vd) {
332 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
333 }
334
335 FindVarResult findBlockVarDecl(Expr *ex);
Ted Kremenek610068c2011-01-15 02:58:47 +0000336};
337}
338
339void TransferFunctions::reportUninit(const DeclRefExpr *ex,
340 const VarDecl *vd) {
341 if (handler) handler->handleUseOfUninitVariable(ex, vd);
342}
343
Ted Kremenek40900ee2011-01-27 02:29:34 +0000344FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) {
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000345 if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts()))
346 if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
347 if (isTrackedVar(vd))
Ted Kremenek40900ee2011-01-27 02:29:34 +0000348 return FindVarResult(vd, dr);
Ted Kremenek1ea800c2011-01-27 02:01:31 +0000349 return FindVarResult(0, 0);
350}
351
352void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt(
353 ObjCForCollectionStmt *fs) {
354
355 Visit(fs->getCollection());
356
357 // This represents an initialization of the 'element' value.
358 Stmt *element = fs->getElement();
359 const VarDecl* vd = 0;
360
361 if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) {
362 vd = cast<VarDecl>(ds->getSingleDecl());
363 if (!isTrackedVar(vd))
364 vd = 0;
365 }
366 else {
367 // Initialize the value of the reference variable.
368 const FindVarResult &res = findBlockVarDecl(cast<Expr>(element));
369 vd = res.getDecl();
370 if (!vd) {
371 Visit(element);
372 return;
373 }
374 }
375
376 if (vd)
377 vals[vd] = Initialized;
378}
379
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000380void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
381 if (!flagBlockUses || !handler)
382 return;
383 AnalysisContext::referenced_decls_iterator i, e;
384 llvm::tie(i, e) = ac.getReferencedBlockVars(be->getBlockDecl());
385 for ( ; i != e; ++i) {
386 const VarDecl *vd = *i;
Ted Kremenek40900ee2011-01-27 02:29:34 +0000387 if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage() ||
388 !isTrackedVar(vd))
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000389 continue;
390 if (vals[vd] == Uninitialized)
Ted Kremenek40900ee2011-01-27 02:29:34 +0000391 handler->handleUseOfUninitVariable(be, vd);
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000392 }
393}
394
Ted Kremenek610068c2011-01-15 02:58:47 +0000395void TransferFunctions::VisitDeclStmt(DeclStmt *ds) {
396 for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end();
397 DI != DE; ++DI) {
398 if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) {
Ted Kremenek4dccb902011-01-18 05:00:42 +0000399 if (isTrackedVar(vd)) {
400 vals[vd] = Uninitialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000401 if (Stmt *init = vd->getInit()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000402 Visit(init);
Ted Kremenekc104e532011-01-18 04:53:25 +0000403 vals[vd] = Initialized;
Ted Kremenek610068c2011-01-15 02:58:47 +0000404 }
Ted Kremenek4dccb902011-01-18 05:00:42 +0000405 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000406 else if (Stmt *init = vd->getInit()) {
407 Visit(init);
408 }
Ted Kremenek610068c2011-01-15 02:58:47 +0000409 }
410 }
411}
412
Ted Kremenekc21fed32011-01-18 21:18:58 +0000413void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
414 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
415 // cannot be block-level expressions. Therefore, we determine if
416 // a DeclRefExpr is involved in a "load" by comparing it to the current
417 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
418 // If a DeclRefExpr is not involved in a load, we are essentially computing
419 // its address, either for assignment to a reference or via the '&' operator.
420 // In such cases, treat the variable as being initialized, since this
421 // analysis isn't powerful enough to do alias tracking.
422 if (dr != currentDR)
423 if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl()))
424 if (isTrackedVar(vd))
425 vals[vd] = Initialized;
426}
427
Ted Kremenek610068c2011-01-15 02:58:47 +0000428void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000429 if (bo->isAssignmentOp()) {
430 const FindVarResult &res = findBlockVarDecl(bo->getLHS());
431 if (const VarDecl* vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000432 // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment"
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 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
437 res.getDeclRefExpr());
438 Visit(bo->getRHS());
439 Visit(bo->getLHS());
440
Ted Kremenek136f8f22011-03-15 04:57:27 +0000441 ValueVector::reference bit = vals[vd];
Ted Kremenek610068c2011-01-15 02:58:47 +0000442 if (bit == Uninitialized) {
443 if (bo->getOpcode() != BO_Assign)
444 reportUninit(res.getDeclRefExpr(), vd);
445 bit = Initialized;
446 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000447 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000448 }
449 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000450 Visit(bo->getRHS());
451 Visit(bo->getLHS());
Ted Kremenek610068c2011-01-15 02:58:47 +0000452}
453
454void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000455 switch (uo->getOpcode()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000456 case clang::UO_PostDec:
457 case clang::UO_PostInc:
458 case clang::UO_PreDec:
459 case clang::UO_PreInc: {
460 const FindVarResult &res = findBlockVarDecl(uo->getSubExpr());
461 if (const VarDecl *vd = res.getDecl()) {
Ted Kremenekc21fed32011-01-18 21:18:58 +0000462 // We assume that DeclRefExprs wrapped in a unary operator ++/--
463 // cannot be block-level expressions. Therefore, we determine if
464 // a DeclRefExpr is involved in a "load" by comparing it to the current
465 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
466 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
467 res.getDeclRefExpr());
468 Visit(uo->getSubExpr());
469
Ted Kremenek136f8f22011-03-15 04:57:27 +0000470 ValueVector::reference bit = vals[vd];
Ted Kremenek610068c2011-01-15 02:58:47 +0000471 if (bit == Uninitialized) {
472 reportUninit(res.getDeclRefExpr(), vd);
473 bit = Initialized;
474 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000475 return;
Ted Kremenek610068c2011-01-15 02:58:47 +0000476 }
477 break;
478 }
479 default:
480 break;
481 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000482 Visit(uo->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000483}
484
485void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000486 if (ce->getCastKind() == CK_LValueToRValue) {
487 const FindVarResult &res = findBlockVarDecl(ce->getSubExpr());
Ted Kremenekc21fed32011-01-18 21:18:58 +0000488 if (const VarDecl *vd = res.getDecl()) {
489 // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast
490 // cannot be block-level expressions. Therefore, we determine if
491 // a DeclRefExpr is involved in a "load" by comparing it to the current
492 // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr.
493 // Here we update 'currentDR' to be the one associated with this
494 // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we
495 // will know that we are not computing its lvalue for other purposes
496 // than to perform a load.
497 SaveAndRestore<const DeclRefExpr*> lastDR(currentDR,
498 res.getDeclRefExpr());
499 Visit(ce->getSubExpr());
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000500 if (currentVoidCast != ce && vals[vd] == Uninitialized) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000501 reportUninit(res.getDeclRefExpr(), vd);
Ted Kremenekc21fed32011-01-18 21:18:58 +0000502 // Don't cascade warnings.
503 vals[vd] = Initialized;
504 }
505 return;
506 }
Ted Kremenekdd0f7942011-01-26 04:49:43 +0000507 }
508 else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) {
509 if (cse->getType()->isVoidType()) {
510 // e.g. (void) x;
511 SaveAndRestore<const Expr *>
512 lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens());
513 Visit(cse->getSubExpr());
514 return;
515 }
516 }
Ted Kremenekc21fed32011-01-18 21:18:58 +0000517 Visit(ce->getSubExpr());
Ted Kremenek610068c2011-01-15 02:58:47 +0000518}
519
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000520void TransferFunctions::VisitUnaryExprOrTypeTraitExpr(
521 UnaryExprOrTypeTraitExpr *se) {
522 if (se->getKind() == UETT_SizeOf) {
Ted Kremenek96608032011-01-23 17:53:04 +0000523 if (se->getType()->isConstantSizeType())
524 return;
525 // Handle VLAs.
526 Visit(se->getArgumentExpr());
527 }
528}
529
Ted Kremenek610068c2011-01-15 02:58:47 +0000530//------------------------------------------------------------------------====//
531// High-level "driver" logic for uninitialized values analysis.
532//====------------------------------------------------------------------------//
533
Ted Kremenek13bd4232011-01-20 17:37:17 +0000534static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000535 AnalysisContext &ac, CFGBlockValues &vals,
536 UninitVariablesHandler *handler = 0,
537 bool flagBlockUses = false) {
Ted Kremenek13bd4232011-01-20 17:37:17 +0000538
539 if (const BinaryOperator *b = getLogicalOperatorInChain(block)) {
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000540 CFGBlock::const_pred_iterator itr = block->pred_begin();
Ted Kremenek136f8f22011-03-15 04:57:27 +0000541 BVPair vA = vals.getValueVectors(*itr, false);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000542 ++itr;
Ted Kremenek136f8f22011-03-15 04:57:27 +0000543 BVPair vB = vals.getValueVectors(*itr, false);
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000544
545 BVPair valsAB;
546
547 if (b->getOpcode() == BO_LAnd) {
548 // Merge the 'F' bits from the first and second.
549 vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true);
550 vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false);
551 valsAB.first = vA.first;
Ted Kremenek2d4bed12011-01-20 21:25:31 +0000552 valsAB.second = &vals.getScratch();
Ted Kremenek13bd4232011-01-20 17:37:17 +0000553 }
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000554 else {
555 // Merge the 'T' bits from the first and second.
556 assert(b->getOpcode() == BO_LOr);
557 vals.mergeIntoScratch(*vA.first, true);
558 vals.mergeIntoScratch(*vB.first, false);
559 valsAB.first = &vals.getScratch();
560 valsAB.second = vA.second ? vA.second : vA.first;
561 }
Ted Kremenek136f8f22011-03-15 04:57:27 +0000562 return vals.updateValueVectors(block, valsAB);
Ted Kremenek13bd4232011-01-20 17:37:17 +0000563 }
564
Ted Kremenek9fcbcee2011-02-01 17:43:18 +0000565 // Default behavior: merge in values of predecessor blocks.
Ted Kremenek610068c2011-01-15 02:58:47 +0000566 vals.resetScratch();
567 bool isFirst = true;
568 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
569 E = block->pred_end(); I != E; ++I) {
Ted Kremenek136f8f22011-03-15 04:57:27 +0000570 vals.mergeIntoScratch(vals.getValueVector(*I, block), isFirst);
Ted Kremenek610068c2011-01-15 02:58:47 +0000571 isFirst = false;
572 }
573 // Apply the transfer function.
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000574 TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses);
Ted Kremenek610068c2011-01-15 02:58:47 +0000575 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
576 I != E; ++I) {
577 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
578 tf.BlockStmt_Visit(cs->getStmt());
579 }
580 }
Ted Kremenek136f8f22011-03-15 04:57:27 +0000581 return vals.updateValueVectorWithScratch(block);
Ted Kremenek610068c2011-01-15 02:58:47 +0000582}
583
584void clang::runUninitializedVariablesAnalysis(const DeclContext &dc,
585 const CFG &cfg,
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000586 AnalysisContext &ac,
Ted Kremenek610068c2011-01-15 02:58:47 +0000587 UninitVariablesHandler &handler) {
588 CFGBlockValues vals(cfg);
589 vals.computeSetOfDeclarations(dc);
590 if (vals.hasNoDeclarations())
591 return;
592 DataflowWorklist worklist(cfg);
Ted Kremenek136f8f22011-03-15 04:57:27 +0000593 ValueVector previouslyVisited(cfg.getNumBlockIDs());
Ted Kremenek610068c2011-01-15 02:58:47 +0000594
595 worklist.enqueueSuccessors(&cfg.getEntry());
596
597 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek610068c2011-01-15 02:58:47 +0000598 // Did the block change?
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000599 bool changed = runOnBlock(block, cfg, ac, vals);
Ted Kremenek610068c2011-01-15 02:58:47 +0000600 if (changed || !previouslyVisited[block->getBlockID()])
601 worklist.enqueueSuccessors(block);
602 previouslyVisited[block->getBlockID()] = true;
603 }
604
605 // Run through the blocks one more time, and report uninitialized variabes.
606 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremeneka8c17a52011-01-25 19:13:48 +0000607 runOnBlock(*BI, cfg, ac, vals, &handler, /* flagBlockUses */ true);
Ted Kremenek610068c2011-01-15 02:58:47 +0000608 }
609}
610
611UninitVariablesHandler::~UninitVariablesHandler() {}
612