blob: bbf59bf3b0be9d9d7bb995461b82bacf51b216e5 [file] [log] [blame]
Ted Kremeneka0a5ca12011-03-15 03:17:07 +00001//==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==//
Ted Kremenekb749a6d2011-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 Kremenekb82ddd62011-01-20 17:37:17 +000014#include <utility>
Ted Kremenekb749a6d2011-01-15 02:58:47 +000015#include "llvm/ADT/Optional.h"
Benjamin Kramer5721daa2012-09-28 16:44:29 +000016#include "llvm/ADT/SmallBitVector.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000017#include "llvm/ADT/SmallVector.h"
Argyrios Kyrtzidisb3483b32011-05-31 03:56:09 +000018#include "llvm/ADT/PackedVector.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000019#include "llvm/ADT/DenseMap.h"
Richard Smith130b8d42012-07-13 23:33:44 +000020#include "clang/AST/ASTContext.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000021#include "clang/AST/Decl.h"
22#include "clang/Analysis/CFG.h"
Ted Kremenekbcf848f2011-01-25 19:13:48 +000023#include "clang/Analysis/AnalysisContext.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000024#include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h"
Ted Kremenek4431a032012-11-17 02:00:00 +000025#include "clang/Analysis/Analyses/PostOrderCFGView.h"
Ted Kremeneka0a5ca12011-03-15 03:17:07 +000026#include "clang/Analysis/Analyses/UninitializedValues.h"
Ted Kremenekedf22ed2012-09-13 00:21:35 +000027#include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
Argyrios Kyrtzidis981a9612012-03-01 19:45:56 +000028#include "llvm/Support/SaveAndRestore.h"
Ted Kremenekb749a6d2011-01-15 02:58:47 +000029
30using namespace clang;
31
Richard Smith130b8d42012-07-13 23:33:44 +000032#define DEBUG_LOGGING 0
33
Ted Kremenek93a31382011-01-27 02:29:34 +000034static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
Ted Kremenekc15a4e42011-03-17 03:06:11 +000035 if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
Ted Kremenek97c39382011-04-07 20:02:56 +000036 !vd->isExceptionVariable() &&
Ted Kremenekc15a4e42011-03-17 03:06:11 +000037 vd->getDeclContext() == dc) {
38 QualType ty = vd->getType();
39 return ty->isScalarType() || ty->isVectorType();
40 }
41 return false;
Ted Kremenekcab479f2011-01-18 04:53:25 +000042}
43
Ted Kremenekb749a6d2011-01-15 02:58:47 +000044//------------------------------------------------------------------------====//
Ted Kremeneka895fe92011-03-15 04:57:27 +000045// DeclToIndex: a mapping from Decls we track to value indices.
Ted Kremenekb749a6d2011-01-15 02:58:47 +000046//====------------------------------------------------------------------------//
47
48namespace {
Ted Kremeneka895fe92011-03-15 04:57:27 +000049class DeclToIndex {
Ted Kremenekb749a6d2011-01-15 02:58:47 +000050 llvm::DenseMap<const VarDecl *, unsigned> map;
51public:
Ted Kremeneka895fe92011-03-15 04:57:27 +000052 DeclToIndex() {}
Ted Kremenekb749a6d2011-01-15 02:58:47 +000053
54 /// Compute the actual mapping from declarations to bits.
55 void computeMap(const DeclContext &dc);
56
57 /// Return the number of declarations in the map.
58 unsigned size() const { return map.size(); }
59
60 /// Returns the bit vector index for a given declaration.
Ted Kremenek03325c42011-03-29 01:40:00 +000061 llvm::Optional<unsigned> getValueIndex(const VarDecl *d) const;
Ted Kremenekb749a6d2011-01-15 02:58:47 +000062};
63}
64
Ted Kremeneka895fe92011-03-15 04:57:27 +000065void DeclToIndex::computeMap(const DeclContext &dc) {
Ted Kremenekb749a6d2011-01-15 02:58:47 +000066 unsigned count = 0;
67 DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
68 E(dc.decls_end());
69 for ( ; I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +000070 const VarDecl *vd = *I;
Ted Kremenek93a31382011-01-27 02:29:34 +000071 if (isTrackedVar(vd, &dc))
Ted Kremenekb749a6d2011-01-15 02:58:47 +000072 map[vd] = count++;
73 }
74}
75
Ted Kremenek03325c42011-03-29 01:40:00 +000076llvm::Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
77 llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
Ted Kremenekb749a6d2011-01-15 02:58:47 +000078 if (I == map.end())
79 return llvm::Optional<unsigned>();
80 return I->second;
81}
82
83//------------------------------------------------------------------------====//
84// CFGBlockValues: dataflow values for CFG blocks.
85//====------------------------------------------------------------------------//
86
Ted Kremenekc8c4e5f2011-03-15 04:57:38 +000087// These values are defined in such a way that a merge can be done using
88// a bitwise OR.
89enum Value { Unknown = 0x0, /* 00 */
90 Initialized = 0x1, /* 01 */
91 Uninitialized = 0x2, /* 10 */
92 MayUninitialized = 0x3 /* 11 */ };
93
94static bool isUninitialized(const Value v) {
95 return v >= Uninitialized;
96}
97static bool isAlwaysUninit(const Value v) {
98 return v == Uninitialized;
99}
Ted Kremenekd3def382011-03-15 04:57:29 +0000100
Benjamin Kramer8aef5962011-03-26 12:38:21 +0000101namespace {
Ted Kremenek9b15c962011-03-15 04:57:32 +0000102
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000103typedef llvm::PackedVector<Value, 2, llvm::SmallBitVector> ValueVector;
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000104
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000105class CFGBlockValues {
106 const CFG &cfg;
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000107 SmallVector<ValueVector, 8> vals;
Ted Kremeneka895fe92011-03-15 04:57:27 +0000108 ValueVector scratch;
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000109 DeclToIndex declToIndex;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000110public:
111 CFGBlockValues(const CFG &cfg);
Ted Kremenek6080d322012-07-19 04:59:05 +0000112
Ted Kremenek37881932011-04-04 23:29:12 +0000113 unsigned getNumEntries() const { return declToIndex.size(); }
114
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000115 void computeSetOfDeclarations(const DeclContext &dc);
Ted Kremenek6080d322012-07-19 04:59:05 +0000116 ValueVector &getValueVector(const CFGBlock *block) {
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000117 return vals[block->getBlockID()];
Ted Kremenek6080d322012-07-19 04:59:05 +0000118 }
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000119
Richard Smithb721e302012-07-02 23:23:04 +0000120 void setAllScratchValues(Value V);
Ted Kremeneka895fe92011-03-15 04:57:27 +0000121 void mergeIntoScratch(ValueVector const &source, bool isFirst);
122 bool updateValueVectorWithScratch(const CFGBlock *block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000123
124 bool hasNoDeclarations() const {
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000125 return declToIndex.size() == 0;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000126 }
Ted Kremenek417d5662011-08-20 01:15:28 +0000127
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000128 void resetScratch();
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000129
Ted Kremeneka895fe92011-03-15 04:57:27 +0000130 ValueVector::reference operator[](const VarDecl *vd);
Richard Smith4323bf82012-05-25 02:17:09 +0000131
132 Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
133 const VarDecl *vd) {
134 const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
135 assert(idx.hasValue());
Ted Kremenek6080d322012-07-19 04:59:05 +0000136 return getValueVector(block)[idx.getValue()];
Richard Smith4323bf82012-05-25 02:17:09 +0000137 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000138};
Benjamin Kramer8aef5962011-03-26 12:38:21 +0000139} // end anonymous namespace
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000140
Ted Kremenek6080d322012-07-19 04:59:05 +0000141CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000142
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000143void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000144 declToIndex.computeMap(dc);
Ted Kremenek6080d322012-07-19 04:59:05 +0000145 unsigned decls = declToIndex.size();
146 scratch.resize(decls);
147 unsigned n = cfg.getNumBlockIDs();
148 if (!n)
149 return;
150 vals.resize(n);
151 for (unsigned i = 0; i < n; ++i)
Benjamin Kramer5721daa2012-09-28 16:44:29 +0000152 vals[i].resize(decls);
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000153}
154
Richard Smith130b8d42012-07-13 23:33:44 +0000155#if DEBUG_LOGGING
Ted Kremeneka895fe92011-03-15 04:57:27 +0000156static void printVector(const CFGBlock *block, ValueVector &bv,
Ted Kremenekba357292011-02-01 17:43:18 +0000157 unsigned num) {
Ted Kremenekba357292011-02-01 17:43:18 +0000158 llvm::errs() << block->getBlockID() << " :";
159 for (unsigned i = 0; i < bv.size(); ++i) {
160 llvm::errs() << ' ' << bv[i];
161 }
162 llvm::errs() << " : " << num << '\n';
163}
164#endif
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000165
Richard Smithb721e302012-07-02 23:23:04 +0000166void CFGBlockValues::setAllScratchValues(Value V) {
167 for (unsigned I = 0, E = scratch.size(); I != E; ++I)
168 scratch[I] = V;
169}
170
Ted Kremenekf8fd4d42011-10-07 00:42:48 +0000171void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
172 bool isFirst) {
173 if (isFirst)
174 scratch = source;
175 else
176 scratch |= source;
177}
178
Ted Kremeneka895fe92011-03-15 04:57:27 +0000179bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
Ted Kremenek6080d322012-07-19 04:59:05 +0000180 ValueVector &dst = getValueVector(block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000181 bool changed = (dst != scratch);
182 if (changed)
183 dst = scratch;
Richard Smith130b8d42012-07-13 23:33:44 +0000184#if DEBUG_LOGGING
Ted Kremenekba357292011-02-01 17:43:18 +0000185 printVector(block, scratch, 0);
186#endif
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000187 return changed;
188}
189
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000190void CFGBlockValues::resetScratch() {
191 scratch.reset();
192}
193
Ted Kremeneka895fe92011-03-15 04:57:27 +0000194ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
Ted Kremeneke3ae0a42011-03-15 05:30:12 +0000195 const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000196 assert(idx.hasValue());
197 return scratch[idx.getValue()];
198}
199
200//------------------------------------------------------------------------====//
201// Worklist: worklist for dataflow analysis.
202//====------------------------------------------------------------------------//
203
204namespace {
205class DataflowWorklist {
Ted Kremenek4431a032012-11-17 02:00:00 +0000206 PostOrderCFGView::iterator PO_I, PO_E;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000207 SmallVector<const CFGBlock *, 20> worklist;
Ted Kremenek9b15c962011-03-15 04:57:32 +0000208 llvm::BitVector enqueuedBlocks;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000209public:
Ted Kremenek4431a032012-11-17 02:00:00 +0000210 DataflowWorklist(const CFG &cfg, PostOrderCFGView &view)
211 : PO_I(view.begin()), PO_E(view.end()),
212 enqueuedBlocks(cfg.getNumBlockIDs(), true) {
213 // Treat the first block as already analyzed.
214 if (PO_I != PO_E) {
215 assert(*PO_I == &cfg.getEntry());
216 enqueuedBlocks[(*PO_I)->getBlockID()] = false;
217 ++PO_I;
218 }
219 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000220
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000221 void enqueueSuccessors(const CFGBlock *block);
222 const CFGBlock *dequeue();
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000223};
224}
225
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000226void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) {
227 for (CFGBlock::const_succ_iterator I = block->succ_begin(),
228 E = block->succ_end(); I != E; ++I) {
Chandler Carrutha5328632011-07-08 11:19:06 +0000229 const CFGBlock *Successor = *I;
230 if (!Successor || enqueuedBlocks[Successor->getBlockID()])
231 continue;
232 worklist.push_back(Successor);
233 enqueuedBlocks[Successor->getBlockID()] = true;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000234 }
235}
236
237const CFGBlock *DataflowWorklist::dequeue() {
Ted Kremenek4431a032012-11-17 02:00:00 +0000238 const CFGBlock *B = 0;
239
240 // First dequeue from the worklist. This can represent
241 // updates along backedges that we want propagated as quickly as possible.
242 if (!worklist.empty()) {
243 B = worklist.back();
244 worklist.pop_back();
245 }
246 // Next dequeue from the initial reverse post order. This is the
247 // theoretical ideal in the presence of no back edges.
248 else if (PO_I != PO_E) {
249 B = *PO_I;
250 ++PO_I;
251 }
252 else {
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000253 return 0;
Ted Kremenek4431a032012-11-17 02:00:00 +0000254 }
255
256 assert(enqueuedBlocks[B->getBlockID()] == true);
257 enqueuedBlocks[B->getBlockID()] = false;
258 return B;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000259}
260
261//------------------------------------------------------------------------====//
Richard Smith6376d1f2012-07-17 00:06:14 +0000262// Classification of DeclRefExprs as use or initialization.
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000263//====------------------------------------------------------------------------//
264
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000265namespace {
266class FindVarResult {
267 const VarDecl *vd;
268 const DeclRefExpr *dr;
269public:
Richard Smith6376d1f2012-07-17 00:06:14 +0000270 FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
271
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000272 const DeclRefExpr *getDeclRefExpr() const { return dr; }
273 const VarDecl *getDecl() const { return vd; }
274};
Richard Smith6376d1f2012-07-17 00:06:14 +0000275
276static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
277 while (Ex) {
278 Ex = Ex->IgnoreParenNoopCasts(C);
279 if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
280 if (CE->getCastKind() == CK_LValueBitCast) {
281 Ex = CE->getSubExpr();
282 continue;
283 }
284 }
285 break;
286 }
287 return Ex;
288}
289
290/// If E is an expression comprising a reference to a single variable, find that
291/// variable.
292static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
293 if (const DeclRefExpr *DRE =
294 dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
295 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
296 if (isTrackedVar(VD, DC))
297 return FindVarResult(VD, DRE);
298 return FindVarResult(0, 0);
299}
300
301/// \brief Classify each DeclRefExpr as an initialization or a use. Any
302/// DeclRefExpr which isn't explicitly classified will be assumed to have
303/// escaped the analysis and will be treated as an initialization.
304class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
305public:
306 enum Class {
307 Init,
308 Use,
309 SelfInit,
310 Ignore
311 };
312
313private:
314 const DeclContext *DC;
315 llvm::DenseMap<const DeclRefExpr*, Class> Classification;
316
317 bool isTrackedVar(const VarDecl *VD) const {
318 return ::isTrackedVar(VD, DC);
319 }
320
321 void classify(const Expr *E, Class C);
322
323public:
324 ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
325
326 void VisitDeclStmt(DeclStmt *DS);
327 void VisitUnaryOperator(UnaryOperator *UO);
328 void VisitBinaryOperator(BinaryOperator *BO);
329 void VisitCallExpr(CallExpr *CE);
330 void VisitCastExpr(CastExpr *CE);
331
332 void operator()(Stmt *S) { Visit(S); }
333
334 Class get(const DeclRefExpr *DRE) const {
335 llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
336 = Classification.find(DRE);
337 if (I != Classification.end())
338 return I->second;
339
340 const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl());
341 if (!VD || !isTrackedVar(VD))
342 return Ignore;
343
344 return Init;
345 }
346};
347}
348
349static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
350 if (Expr *Init = VD->getInit()) {
351 const DeclRefExpr *DRE
352 = dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
353 if (DRE && DRE->getDecl() == VD)
354 return DRE;
355 }
356 return 0;
357}
358
359void ClassifyRefs::classify(const Expr *E, Class C) {
360 FindVarResult Var = findVar(E, DC);
361 if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
362 Classification[DRE] = std::max(Classification[DRE], C);
363}
364
365void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
366 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
367 DI != DE; ++DI) {
368 VarDecl *VD = dyn_cast<VarDecl>(*DI);
369 if (VD && isTrackedVar(VD))
370 if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
371 Classification[DRE] = SelfInit;
372 }
373}
374
375void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
376 // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
377 // is not a compound-assignment, we will treat it as initializing the variable
378 // when TransferFunctions visits it. A compound-assignment does not affect
379 // whether a variable is uninitialized, and there's no point counting it as a
380 // use.
Richard Smithb21dd022012-07-17 01:27:33 +0000381 if (BO->isCompoundAssignmentOp())
382 classify(BO->getLHS(), Use);
383 else if (BO->getOpcode() == BO_Assign)
Richard Smith6376d1f2012-07-17 00:06:14 +0000384 classify(BO->getLHS(), Ignore);
385}
386
387void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
388 // Increment and decrement are uses despite there being no lvalue-to-rvalue
389 // conversion.
390 if (UO->isIncrementDecrementOp())
391 classify(UO->getSubExpr(), Use);
392}
393
394void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
395 // If a value is passed by const reference to a function, we should not assume
396 // that it is initialized by the call, and we conservatively do not assume
397 // that it is used.
398 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
399 I != E; ++I)
400 if ((*I)->getType().isConstQualified() && (*I)->isGLValue())
401 classify(*I, Ignore);
402}
403
404void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
405 if (CE->getCastKind() == CK_LValueToRValue)
406 classify(CE->getSubExpr(), Use);
407 else if (CStyleCastExpr *CSE = dyn_cast<CStyleCastExpr>(CE)) {
408 if (CSE->getType()->isVoidType()) {
409 // Squelch any detected load of an uninitialized value if
410 // we cast it to void.
411 // e.g. (void) x;
412 classify(CSE->getSubExpr(), Ignore);
413 }
414 }
415}
416
417//------------------------------------------------------------------------====//
418// Transfer function for uninitialized values analysis.
419//====------------------------------------------------------------------------//
420
421namespace {
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000422class TransferFunctions : public StmtVisitor<TransferFunctions> {
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000423 CFGBlockValues &vals;
424 const CFG &cfg;
Richard Smith4323bf82012-05-25 02:17:09 +0000425 const CFGBlock *block;
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000426 AnalysisDeclContext &ac;
Richard Smith6376d1f2012-07-17 00:06:14 +0000427 const ClassifyRefs &classification;
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000428 ObjCNoReturn objCNoRet;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000429 UninitVariablesHandler &handler;
Richard Smith6376d1f2012-07-17 00:06:14 +0000430
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000431public:
432 TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
Richard Smith4323bf82012-05-25 02:17:09 +0000433 const CFGBlock *block, AnalysisDeclContext &ac,
Richard Smith6376d1f2012-07-17 00:06:14 +0000434 const ClassifyRefs &classification,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000435 UninitVariablesHandler &handler)
Richard Smith6376d1f2012-07-17 00:06:14 +0000436 : vals(vals), cfg(cfg), block(block), ac(ac),
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000437 classification(classification), objCNoRet(ac.getASTContext()),
438 handler(handler) {}
Richard Smith6376d1f2012-07-17 00:06:14 +0000439
Richard Smith3d31e8b2012-05-24 23:45:35 +0000440 void reportUse(const Expr *ex, const VarDecl *vd);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000441
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000442 void VisitBinaryOperator(BinaryOperator *bo);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000443 void VisitBlockExpr(BlockExpr *be);
Richard Smithb721e302012-07-02 23:23:04 +0000444 void VisitCallExpr(CallExpr *ce);
Ted Kremenekb63931e2011-01-18 21:18:58 +0000445 void VisitDeclRefExpr(DeclRefExpr *dr);
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000446 void VisitDeclStmt(DeclStmt *ds);
447 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
448 void VisitObjCMessageExpr(ObjCMessageExpr *ME);
Richard Smith4323bf82012-05-25 02:17:09 +0000449
Ted Kremenek93a31382011-01-27 02:29:34 +0000450 bool isTrackedVar(const VarDecl *vd) {
451 return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
452 }
Richard Smith4323bf82012-05-25 02:17:09 +0000453
Richard Smith6376d1f2012-07-17 00:06:14 +0000454 FindVarResult findVar(const Expr *ex) {
455 return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
456 }
457
Richard Smith4323bf82012-05-25 02:17:09 +0000458 UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
459 UninitUse Use(ex, isAlwaysUninit(v));
460
461 assert(isUninitialized(v));
462 if (Use.getKind() == UninitUse::Always)
463 return Use;
464
465 // If an edge which leads unconditionally to this use did not initialize
466 // the variable, we can say something stronger than 'may be uninitialized':
467 // we can say 'either it's used uninitialized or you have dead code'.
468 //
469 // We track the number of successors of a node which have been visited, and
470 // visit a node once we have visited all of its successors. Only edges where
471 // the variable might still be uninitialized are followed. Since a variable
472 // can't transfer from being initialized to being uninitialized, this will
473 // trace out the subgraph which inevitably leads to the use and does not
474 // initialize the variable. We do not want to skip past loops, since their
475 // non-termination might be correlated with the initialization condition.
476 //
477 // For example:
478 //
479 // void f(bool a, bool b) {
480 // block1: int n;
481 // if (a) {
482 // block2: if (b)
483 // block3: n = 1;
484 // block4: } else if (b) {
485 // block5: while (!a) {
486 // block6: do_work(&a);
487 // n = 2;
488 // }
489 // }
490 // block7: if (a)
491 // block8: g();
492 // block9: return n;
493 // }
494 //
495 // Starting from the maybe-uninitialized use in block 9:
496 // * Block 7 is not visited because we have only visited one of its two
497 // successors.
498 // * Block 8 is visited because we've visited its only successor.
499 // From block 8:
500 // * Block 7 is visited because we've now visited both of its successors.
501 // From block 7:
502 // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
503 // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
504 // * Block 3 is not visited because it initializes 'n'.
505 // Now the algorithm terminates, having visited blocks 7 and 8, and having
506 // found the frontier is blocks 2, 4, and 5.
507 //
508 // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
509 // and 4), so we report that any time either of those edges is taken (in
510 // each case when 'b == false'), 'n' is used uninitialized.
511 llvm::SmallVector<const CFGBlock*, 32> Queue;
512 llvm::SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
513 Queue.push_back(block);
514 // Specify that we've already visited all successors of the starting block.
515 // This has the dual purpose of ensuring we never add it to the queue, and
516 // of marking it as not being a candidate element of the frontier.
517 SuccsVisited[block->getBlockID()] = block->succ_size();
518 while (!Queue.empty()) {
519 const CFGBlock *B = Queue.back();
520 Queue.pop_back();
521 for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
522 I != E; ++I) {
523 const CFGBlock *Pred = *I;
524 if (vals.getValue(Pred, B, vd) == Initialized)
525 // This block initializes the variable.
526 continue;
527
Richard Smith130b8d42012-07-13 23:33:44 +0000528 unsigned &SV = SuccsVisited[Pred->getBlockID()];
529 if (!SV) {
530 // When visiting the first successor of a block, mark all NULL
531 // successors as having been visited.
532 for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
533 SE = Pred->succ_end();
534 SI != SE; ++SI)
535 if (!*SI)
536 ++SV;
537 }
538
539 if (++SV == Pred->succ_size())
Richard Smith4323bf82012-05-25 02:17:09 +0000540 // All paths from this block lead to the use and don't initialize the
541 // variable.
542 Queue.push_back(Pred);
543 }
544 }
545
546 // Scan the frontier, looking for blocks where the variable was
547 // uninitialized.
548 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
549 const CFGBlock *Block = *BI;
550 unsigned BlockID = Block->getBlockID();
551 const Stmt *Term = Block->getTerminator();
552 if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
553 Term) {
554 // This block inevitably leads to the use. If we have an edge from here
555 // to a post-dominator block, and the variable is uninitialized on that
556 // edge, we have found a bug.
557 for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
558 E = Block->succ_end(); I != E; ++I) {
559 const CFGBlock *Succ = *I;
560 if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
561 vals.getValue(Block, Succ, vd) == Uninitialized) {
562 // Switch cases are a special case: report the label to the caller
563 // as the 'terminator', not the switch statement itself. Suppress
564 // situations where no label matched: we can't be sure that's
565 // possible.
566 if (isa<SwitchStmt>(Term)) {
567 const Stmt *Label = Succ->getLabel();
568 if (!Label || !isa<SwitchCase>(Label))
569 // Might not be possible.
570 continue;
571 UninitUse::Branch Branch;
572 Branch.Terminator = Label;
573 Branch.Output = 0; // Ignored.
574 Use.addUninitBranch(Branch);
575 } else {
576 UninitUse::Branch Branch;
577 Branch.Terminator = Term;
578 Branch.Output = I - Block->succ_begin();
579 Use.addUninitBranch(Branch);
580 }
581 }
582 }
583 }
584 }
585
586 return Use;
587 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000588};
589}
590
Richard Smith3d31e8b2012-05-24 23:45:35 +0000591void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
Richard Smith3d31e8b2012-05-24 23:45:35 +0000592 Value v = vals[vd];
593 if (isUninitialized(v))
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000594 handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000595}
596
Richard Smith6376d1f2012-07-17 00:06:14 +0000597void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
Ted Kremenek4058d872011-01-27 02:01:31 +0000598 // This represents an initialization of the 'element' value.
Richard Smith6376d1f2012-07-17 00:06:14 +0000599 if (DeclStmt *DS = dyn_cast<DeclStmt>(FS->getElement())) {
600 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
601 if (isTrackedVar(VD))
602 vals[VD] = Initialized;
Ted Kremenek4058d872011-01-27 02:01:31 +0000603 }
Ted Kremenek4058d872011-01-27 02:01:31 +0000604}
605
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000606void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
Ted Kremenek77361762011-03-31 22:32:41 +0000607 const BlockDecl *bd = be->getBlockDecl();
608 for (BlockDecl::capture_const_iterator i = bd->capture_begin(),
609 e = bd->capture_end() ; i != e; ++i) {
610 const VarDecl *vd = i->getVariable();
Ted Kremenek77361762011-03-31 22:32:41 +0000611 if (!isTrackedVar(vd))
612 continue;
613 if (i->isByRef()) {
614 vals[vd] = Initialized;
615 continue;
616 }
Richard Smith3d31e8b2012-05-24 23:45:35 +0000617 reportUse(be, vd);
Ted Kremenekbcf848f2011-01-25 19:13:48 +0000618 }
619}
620
Richard Smithb721e302012-07-02 23:23:04 +0000621void TransferFunctions::VisitCallExpr(CallExpr *ce) {
Ted Kremenek7979ccf2012-09-12 05:53:43 +0000622 if (Decl *Callee = ce->getCalleeDecl()) {
623 if (Callee->hasAttr<ReturnsTwiceAttr>()) {
624 // After a call to a function like setjmp or vfork, any variable which is
625 // initialized anywhere within this function may now be initialized. For
626 // now, just assume such a call initializes all variables. FIXME: Only
627 // mark variables as initialized if they have an initializer which is
628 // reachable from here.
629 vals.setAllScratchValues(Initialized);
630 }
631 else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
632 // Functions labeled like "analyzer_noreturn" are often used to denote
633 // "panic" functions that in special debug situations can still return,
634 // but for the most part should not be treated as returning. This is a
635 // useful annotation borrowed from the static analyzer that is useful for
636 // suppressing branch-specific false positives when we call one of these
637 // functions but keep pretending the path continues (when in reality the
638 // user doesn't care).
639 vals.setAllScratchValues(Unknown);
640 }
641 }
Richard Smithb721e302012-07-02 23:23:04 +0000642}
643
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000644void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
Richard Smith6376d1f2012-07-17 00:06:14 +0000645 switch (classification.get(dr)) {
646 case ClassifyRefs::Ignore:
647 break;
648 case ClassifyRefs::Use:
649 reportUse(dr, cast<VarDecl>(dr->getDecl()));
650 break;
651 case ClassifyRefs::Init:
652 vals[cast<VarDecl>(dr->getDecl())] = Initialized;
653 break;
654 case ClassifyRefs::SelfInit:
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000655 handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
Richard Smith6376d1f2012-07-17 00:06:14 +0000656 break;
657 }
Ted Kremenek9e100ea2011-07-19 14:18:48 +0000658}
659
Richard Smith6376d1f2012-07-17 00:06:14 +0000660void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
661 if (BO->getOpcode() == BO_Assign) {
662 FindVarResult Var = findVar(BO->getLHS());
663 if (const VarDecl *VD = Var.getDecl())
664 vals[VD] = Initialized;
665 }
666}
667
668void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
669 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000670 DI != DE; ++DI) {
Richard Smith6376d1f2012-07-17 00:06:14 +0000671 VarDecl *VD = dyn_cast<VarDecl>(*DI);
672 if (VD && isTrackedVar(VD)) {
673 if (getSelfInitExpr(VD)) {
674 // If the initializer consists solely of a reference to itself, we
675 // explicitly mark the variable as uninitialized. This allows code
676 // like the following:
677 //
678 // int x = x;
679 //
680 // to deliberately leave a variable uninitialized. Different analysis
681 // clients can detect this pattern and adjust their reporting
682 // appropriately, but we need to continue to analyze subsequent uses
683 // of the variable.
684 vals[VD] = Uninitialized;
685 } else if (VD->getInit()) {
686 // Treat the new variable as initialized.
687 vals[VD] = Initialized;
688 } else {
689 // No initializer: the variable is now uninitialized. This matters
690 // for cases like:
691 // while (...) {
692 // int n;
693 // use(n);
694 // n = 0;
695 // }
696 // FIXME: Mark the variable as uninitialized whenever its scope is
697 // left, since its scope could be re-entered by a jump over the
698 // declaration.
699 vals[VD] = Uninitialized;
Ted Kremenekb63931e2011-01-18 21:18:58 +0000700 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000701 }
702 }
703}
704
Ted Kremenekedf22ed2012-09-13 00:21:35 +0000705void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
706 // If the Objective-C message expression is an implicit no-return that
707 // is not modeled in the CFG, set the tracked dataflow values to Unknown.
708 if (objCNoRet.isImplicitNoReturn(ME)) {
709 vals.setAllScratchValues(Unknown);
710 }
711}
712
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000713//------------------------------------------------------------------------====//
714// High-level "driver" logic for uninitialized values analysis.
715//====------------------------------------------------------------------------//
716
Ted Kremenekb82ddd62011-01-20 17:37:17 +0000717static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000718 AnalysisDeclContext &ac, CFGBlockValues &vals,
Richard Smith6376d1f2012-07-17 00:06:14 +0000719 const ClassifyRefs &classification,
Ted Kremenek352a7082011-04-04 20:30:58 +0000720 llvm::BitVector &wasAnalyzed,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000721 UninitVariablesHandler &handler) {
Ted Kremenek352a7082011-04-04 20:30:58 +0000722 wasAnalyzed[block->getBlockID()] = true;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000723 vals.resetScratch();
Ted Kremenek6080d322012-07-19 04:59:05 +0000724 // Merge in values of predecessor blocks.
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000725 bool isFirst = true;
726 for (CFGBlock::const_pred_iterator I = block->pred_begin(),
727 E = block->pred_end(); I != E; ++I) {
Ted Kremenekaed46772011-09-02 19:39:26 +0000728 const CFGBlock *pred = *I;
729 if (wasAnalyzed[pred->getBlockID()]) {
Ted Kremenek6080d322012-07-19 04:59:05 +0000730 vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
Ted Kremenekaed46772011-09-02 19:39:26 +0000731 isFirst = false;
732 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000733 }
734 // Apply the transfer function.
Richard Smith6376d1f2012-07-17 00:06:14 +0000735 TransferFunctions tf(vals, cfg, block, ac, classification, handler);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000736 for (CFGBlock::const_iterator I = block->begin(), E = block->end();
737 I != E; ++I) {
738 if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) {
Ted Kremenekadfb4452011-08-23 23:05:04 +0000739 tf.Visit(const_cast<Stmt*>(cs->getStmt()));
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000740 }
741 }
Ted Kremeneka895fe92011-03-15 04:57:27 +0000742 return vals.updateValueVectorWithScratch(block);
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000743}
744
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000745/// PruneBlocksHandler is a special UninitVariablesHandler that is used
746/// to detect when a CFGBlock has any *potential* use of an uninitialized
747/// variable. It is mainly used to prune out work during the final
748/// reporting pass.
749namespace {
750struct PruneBlocksHandler : public UninitVariablesHandler {
751 PruneBlocksHandler(unsigned numBlocks)
752 : hadUse(numBlocks, false), hadAnyUse(false),
753 currentBlock(0) {}
754
755 virtual ~PruneBlocksHandler() {}
756
757 /// Records if a CFGBlock had a potential use of an uninitialized variable.
758 llvm::BitVector hadUse;
759
760 /// Records if any CFGBlock had a potential use of an uninitialized variable.
761 bool hadAnyUse;
762
763 /// The current block to scribble use information.
764 unsigned currentBlock;
765
766 virtual void handleUseOfUninitVariable(const VarDecl *vd,
767 const UninitUse &use) {
768 hadUse[currentBlock] = true;
769 hadAnyUse = true;
770 }
771
772 /// Called when the uninitialized variable analysis detects the
773 /// idiom 'int x = x'. All other uses of 'x' within the initializer
774 /// are handled by handleUseOfUninitVariable.
775 virtual void handleSelfInit(const VarDecl *vd) {
776 hadUse[currentBlock] = true;
777 hadAnyUse = true;
778 }
779};
780}
781
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000782void clang::runUninitializedVariablesAnalysis(
783 const DeclContext &dc,
784 const CFG &cfg,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000785 AnalysisDeclContext &ac,
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000786 UninitVariablesHandler &handler,
787 UninitVariablesAnalysisStats &stats) {
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000788 CFGBlockValues vals(cfg);
789 vals.computeSetOfDeclarations(dc);
790 if (vals.hasNoDeclarations())
791 return;
Ted Kremenek37881932011-04-04 23:29:12 +0000792
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000793 stats.NumVariablesAnalyzed = vals.getNumEntries();
794
Richard Smith6376d1f2012-07-17 00:06:14 +0000795 // Precompute which expressions are uses and which are initializations.
796 ClassifyRefs classification(ac);
797 cfg.VisitBlockStmts(classification);
798
Ted Kremenek37881932011-04-04 23:29:12 +0000799 // Mark all variables uninitialized at the entry.
800 const CFGBlock &entry = cfg.getEntry();
Ted Kremenek6080d322012-07-19 04:59:05 +0000801 ValueVector &vec = vals.getValueVector(&entry);
802 const unsigned n = vals.getNumEntries();
803 for (unsigned j = 0; j < n ; ++j) {
804 vec[j] = Uninitialized;
Ted Kremenek37881932011-04-04 23:29:12 +0000805 }
806
807 // Proceed with the workist.
Ted Kremenek4431a032012-11-17 02:00:00 +0000808 DataflowWorklist worklist(cfg, *ac.getAnalysis<PostOrderCFGView>());
Ted Kremenek9b15c962011-03-15 04:57:32 +0000809 llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000810 worklist.enqueueSuccessors(&cfg.getEntry());
Ted Kremenek352a7082011-04-04 20:30:58 +0000811 llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
Ted Kremenekaed46772011-09-02 19:39:26 +0000812 wasAnalyzed[cfg.getEntry().getBlockID()] = true;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000813 PruneBlocksHandler PBH(cfg.getNumBlockIDs());
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000814
815 while (const CFGBlock *block = worklist.dequeue()) {
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000816 PBH.currentBlock = block->getBlockID();
817
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000818 // Did the block change?
Richard Smith6376d1f2012-07-17 00:06:14 +0000819 bool changed = runOnBlock(block, cfg, ac, vals,
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000820 classification, wasAnalyzed, PBH);
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000821 ++stats.NumBlockVisits;
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000822 if (changed || !previouslyVisited[block->getBlockID()])
823 worklist.enqueueSuccessors(block);
824 previouslyVisited[block->getBlockID()] = true;
825 }
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000826
827 if (!PBH.hadAnyUse)
828 return;
829
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000830 // Run through the blocks one more time, and report uninitialized variabes.
831 for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) {
Ted Kremenekaed46772011-09-02 19:39:26 +0000832 const CFGBlock *block = *BI;
Ted Kremenek778a6ed2012-11-17 07:18:30 +0000833 if (PBH.hadUse[block->getBlockID()]) {
834 runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
Chandler Carruthb4836ea2011-07-06 16:21:37 +0000835 ++stats.NumBlockVisits;
836 }
Ted Kremenekb749a6d2011-01-15 02:58:47 +0000837 }
838}
839
840UninitVariablesHandler::~UninitVariablesHandler() {}