Ted Kremenek | 6f34213 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 1 | //==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==// |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 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 Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 14 | #include <utility> |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 15 | #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 Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/AnalysisContext.h" |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
Ted Kremenek | 6f34213 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 28 | static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) { |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 29 | return vd->isLocalVarDecl() && !vd->hasGlobalStorage() && |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 30 | vd->getType()->isScalarType() && |
| 31 | vd->getDeclContext() == dc; |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 34 | //------------------------------------------------------------------------====// |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 35 | // DeclToIndex: a mapping from Decls we track to value indices. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 36 | //====------------------------------------------------------------------------// |
| 37 | |
| 38 | namespace { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 39 | class DeclToIndex { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 40 | llvm::DenseMap<const VarDecl *, unsigned> map; |
| 41 | public: |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 42 | DeclToIndex() {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 51 | llvm::Optional<unsigned> getValueIndex(const VarDecl *d); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 55 | void DeclToIndex::computeMap(const DeclContext &dc) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 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 Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 61 | if (isTrackedVar(vd, &dc)) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 62 | map[vd] = count++; |
| 63 | } |
| 64 | } |
| 65 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 66 | llvm::Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 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 Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 77 | enum Value { Initialized = 0, Uninitialized = 1 }; |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 78 | |
| 79 | class ValueVector { |
| 80 | llvm::BitVector vec; |
| 81 | public: |
| 82 | ValueVector() {} |
| 83 | ValueVector(unsigned size) : vec(size) {} |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 84 | void resize(unsigned n) { vec.resize(n); } |
| 85 | void merge(const ValueVector &rhs) { vec |= rhs.vec; } |
| 86 | bool operator!=(const ValueVector &rhs) const { return vec != rhs.vec; } |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 87 | void reset() { vec.reset(); } |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 88 | |
| 89 | class reference { |
| 90 | ValueVector &vv; |
| 91 | const unsigned idx; |
| 92 | |
| 93 | reference(); // Undefined |
| 94 | public: |
| 95 | reference(ValueVector &vv, unsigned idx) : vv(vv), idx(idx) {} |
| 96 | ~reference() {} |
| 97 | |
| 98 | reference &operator=(Value v) { |
| 99 | vv.vec[idx] = (v == Initialized ? false : true); |
| 100 | return *this; |
| 101 | } |
| 102 | bool operator==(Value v) { |
| 103 | return vv.vec[idx] == (v == Initialized ? false : true); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | reference operator[](unsigned idx) { return reference(*this, idx); } |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 110 | typedef std::pair<ValueVector *, ValueVector *> BVPair; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 112 | namespace { |
| 113 | class CFGBlockValues { |
| 114 | const CFG &cfg; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 115 | BVPair *vals; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 116 | ValueVector scratch; |
| 117 | DeclToIndex DeclToIndex; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 119 | ValueVector &lazyCreate(ValueVector *&bv); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 120 | public: |
| 121 | CFGBlockValues(const CFG &cfg); |
| 122 | ~CFGBlockValues(); |
| 123 | |
| 124 | void computeSetOfDeclarations(const DeclContext &dc); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 125 | ValueVector &getValueVector(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 126 | const CFGBlock *dstBlock); |
| 127 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 128 | BVPair &getValueVectors(const CFGBlock *block, bool shouldLazyCreate); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 130 | void mergeIntoScratch(ValueVector const &source, bool isFirst); |
| 131 | bool updateValueVectorWithScratch(const CFGBlock *block); |
| 132 | bool updateValueVectors(const CFGBlock *block, const BVPair &newVals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 133 | |
| 134 | bool hasNoDeclarations() const { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 135 | return DeclToIndex.size() == 0; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void resetScratch(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 139 | ValueVector &getScratch() { return scratch; } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 141 | ValueVector::reference operator[](const VarDecl *vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 142 | }; |
| 143 | } |
| 144 | |
| 145 | CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) { |
| 146 | unsigned n = cfg.getNumBlockIDs(); |
| 147 | if (!n) |
| 148 | return; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 149 | vals = new std::pair<ValueVector*, ValueVector*>[n]; |
Francois Pichet | 2d78c37 | 2011-01-15 13:27:47 +0000 | [diff] [blame] | 150 | memset(vals, 0, sizeof(*vals) * n); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | CFGBlockValues::~CFGBlockValues() { |
| 154 | unsigned n = cfg.getNumBlockIDs(); |
| 155 | if (n == 0) |
| 156 | return; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 157 | for (unsigned i = 0; i < n; ++i) { |
| 158 | delete vals[i].first; |
| 159 | delete vals[i].second; |
| 160 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 161 | delete [] vals; |
| 162 | } |
| 163 | |
| 164 | void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 165 | DeclToIndex.computeMap(dc); |
| 166 | scratch.resize(DeclToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 169 | ValueVector &CFGBlockValues::lazyCreate(ValueVector *&bv) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 170 | if (!bv) |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 171 | bv = new ValueVector(DeclToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 172 | return *bv; |
| 173 | } |
| 174 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 175 | /// This function pattern matches for a '&&' or '||' that appears at |
| 176 | /// the beginning of a CFGBlock that also (1) has a terminator and |
| 177 | /// (2) has no other elements. If such an expression is found, it is returned. |
| 178 | static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) { |
| 179 | if (block->empty()) |
| 180 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 3c0349e | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 182 | const CFGStmt *cstmt = block->front().getAs<CFGStmt>(); |
| 183 | BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt->getStmt()); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 184 | |
| 185 | if (!b || !b->isLogicalOp()) |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 186 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 187 | |
| 188 | if (block->pred_size() == 2 && |
| 189 | ((block->succ_size() == 2 && block->getTerminatorCondition() == b) || |
| 190 | block->size() == 1)) |
| 191 | return b; |
| 192 | |
| 193 | return 0; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 196 | ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block, |
| 197 | const CFGBlock *dstBlock) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 198 | unsigned idx = block->getBlockID(); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 199 | if (dstBlock && getLogicalOperatorInChain(block)) { |
| 200 | if (*block->succ_begin() == dstBlock) |
| 201 | return lazyCreate(vals[idx].first); |
| 202 | assert(*(block->succ_begin()+1) == dstBlock); |
| 203 | return lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | assert(vals[idx].second == 0); |
| 207 | return lazyCreate(vals[idx].first); |
| 208 | } |
| 209 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 210 | BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block, |
| 211 | bool shouldLazyCreate) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 212 | unsigned idx = block->getBlockID(); |
| 213 | lazyCreate(vals[idx].first); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 214 | if (shouldLazyCreate) |
| 215 | lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 216 | return vals[idx]; |
| 217 | } |
| 218 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 219 | void CFGBlockValues::mergeIntoScratch(ValueVector const &source, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 220 | bool isFirst) { |
| 221 | if (isFirst) |
| 222 | scratch = source; |
| 223 | else |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 224 | scratch.merge(source); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 225 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 226 | #if 0 |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 227 | static void printVector(const CFGBlock *block, ValueVector &bv, |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 228 | unsigned num) { |
| 229 | |
| 230 | llvm::errs() << block->getBlockID() << " :"; |
| 231 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 232 | llvm::errs() << ' ' << bv[i]; |
| 233 | } |
| 234 | llvm::errs() << " : " << num << '\n'; |
| 235 | } |
| 236 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 238 | bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { |
| 239 | ValueVector &dst = getValueVector(block, 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 240 | bool changed = (dst != scratch); |
| 241 | if (changed) |
| 242 | dst = scratch; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 243 | #if 0 |
| 244 | printVector(block, scratch, 0); |
| 245 | #endif |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 246 | return changed; |
| 247 | } |
| 248 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 249 | bool CFGBlockValues::updateValueVectors(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 250 | const BVPair &newVals) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 251 | BVPair &vals = getValueVectors(block, true); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 252 | bool changed = *newVals.first != *vals.first || |
| 253 | *newVals.second != *vals.second; |
| 254 | *vals.first = *newVals.first; |
| 255 | *vals.second = *newVals.second; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 256 | #if 0 |
| 257 | printVector(block, *vals.first, 1); |
| 258 | printVector(block, *vals.second, 2); |
| 259 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 260 | return changed; |
| 261 | } |
| 262 | |
| 263 | void CFGBlockValues::resetScratch() { |
| 264 | scratch.reset(); |
| 265 | } |
| 266 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 267 | ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
| 268 | const llvm::Optional<unsigned> &idx = DeclToIndex.getValueIndex(vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 269 | assert(idx.hasValue()); |
| 270 | return scratch[idx.getValue()]; |
| 271 | } |
| 272 | |
| 273 | //------------------------------------------------------------------------====// |
| 274 | // Worklist: worklist for dataflow analysis. |
| 275 | //====------------------------------------------------------------------------// |
| 276 | |
| 277 | namespace { |
| 278 | class DataflowWorklist { |
| 279 | llvm::SmallVector<const CFGBlock *, 20> worklist; |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 280 | llvm::BitVector enqueuedBlocks; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 281 | public: |
| 282 | DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {} |
| 283 | |
| 284 | void enqueue(const CFGBlock *block); |
| 285 | void enqueueSuccessors(const CFGBlock *block); |
| 286 | const CFGBlock *dequeue(); |
| 287 | |
| 288 | }; |
| 289 | } |
| 290 | |
| 291 | void DataflowWorklist::enqueue(const CFGBlock *block) { |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 292 | if (!block) |
| 293 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 294 | unsigned idx = block->getBlockID(); |
| 295 | if (enqueuedBlocks[idx]) |
| 296 | return; |
| 297 | worklist.push_back(block); |
| 298 | enqueuedBlocks[idx] = true; |
| 299 | } |
| 300 | |
| 301 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
| 302 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 303 | E = block->succ_end(); I != E; ++I) { |
| 304 | enqueue(*I); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | const CFGBlock *DataflowWorklist::dequeue() { |
| 309 | if (worklist.empty()) |
| 310 | return 0; |
| 311 | const CFGBlock *b = worklist.back(); |
| 312 | worklist.pop_back(); |
| 313 | enqueuedBlocks[b->getBlockID()] = false; |
| 314 | return b; |
| 315 | } |
| 316 | |
| 317 | //------------------------------------------------------------------------====// |
| 318 | // Transfer function for uninitialized values analysis. |
| 319 | //====------------------------------------------------------------------------// |
| 320 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 321 | namespace { |
| 322 | class FindVarResult { |
| 323 | const VarDecl *vd; |
| 324 | const DeclRefExpr *dr; |
| 325 | public: |
| 326 | FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 327 | |
| 328 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 329 | const VarDecl *getDecl() const { return vd; } |
| 330 | }; |
| 331 | |
| 332 | class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> { |
| 333 | CFGBlockValues &vals; |
| 334 | const CFG &cfg; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 335 | AnalysisContext ∾ |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 336 | UninitVariablesHandler *handler; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 337 | const DeclRefExpr *currentDR; |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 338 | const Expr *currentVoidCast; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 339 | const bool flagBlockUses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 340 | public: |
| 341 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 342 | AnalysisContext &ac, |
| 343 | UninitVariablesHandler *handler, |
| 344 | bool flagBlockUses) |
| 345 | : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0), |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 346 | currentVoidCast(0), flagBlockUses(flagBlockUses) {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 347 | |
| 348 | const CFG &getCFG() { return cfg; } |
| 349 | void reportUninit(const DeclRefExpr *ex, const VarDecl *vd); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 350 | |
| 351 | void VisitBlockExpr(BlockExpr *be); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 352 | void VisitDeclStmt(DeclStmt *ds); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 353 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 354 | void VisitUnaryOperator(UnaryOperator *uo); |
| 355 | void VisitBinaryOperator(BinaryOperator *bo); |
| 356 | void VisitCastExpr(CastExpr *ce); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 357 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *se); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 358 | void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs); |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 359 | |
| 360 | bool isTrackedVar(const VarDecl *vd) { |
| 361 | return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); |
| 362 | } |
| 363 | |
| 364 | FindVarResult findBlockVarDecl(Expr *ex); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 365 | }; |
| 366 | } |
| 367 | |
| 368 | void TransferFunctions::reportUninit(const DeclRefExpr *ex, |
| 369 | const VarDecl *vd) { |
| 370 | if (handler) handler->handleUseOfUninitVariable(ex, vd); |
| 371 | } |
| 372 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 373 | FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 374 | if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts())) |
| 375 | if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 376 | if (isTrackedVar(vd)) |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 377 | return FindVarResult(vd, dr); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 378 | return FindVarResult(0, 0); |
| 379 | } |
| 380 | |
| 381 | void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt( |
| 382 | ObjCForCollectionStmt *fs) { |
| 383 | |
| 384 | Visit(fs->getCollection()); |
| 385 | |
| 386 | // This represents an initialization of the 'element' value. |
| 387 | Stmt *element = fs->getElement(); |
| 388 | const VarDecl* vd = 0; |
| 389 | |
| 390 | if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) { |
| 391 | vd = cast<VarDecl>(ds->getSingleDecl()); |
| 392 | if (!isTrackedVar(vd)) |
| 393 | vd = 0; |
| 394 | } |
| 395 | else { |
| 396 | // Initialize the value of the reference variable. |
| 397 | const FindVarResult &res = findBlockVarDecl(cast<Expr>(element)); |
| 398 | vd = res.getDecl(); |
| 399 | if (!vd) { |
| 400 | Visit(element); |
| 401 | return; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (vd) |
| 406 | vals[vd] = Initialized; |
| 407 | } |
| 408 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 409 | void TransferFunctions::VisitBlockExpr(BlockExpr *be) { |
| 410 | if (!flagBlockUses || !handler) |
| 411 | return; |
| 412 | AnalysisContext::referenced_decls_iterator i, e; |
| 413 | llvm::tie(i, e) = ac.getReferencedBlockVars(be->getBlockDecl()); |
| 414 | for ( ; i != e; ++i) { |
| 415 | const VarDecl *vd = *i; |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 416 | if (vd->getAttr<BlocksAttr>() || !vd->hasLocalStorage() || |
| 417 | !isTrackedVar(vd)) |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 418 | continue; |
| 419 | if (vals[vd] == Uninitialized) |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 420 | handler->handleUseOfUninitVariable(be, vd); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 424 | void TransferFunctions::VisitDeclStmt(DeclStmt *ds) { |
| 425 | for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end(); |
| 426 | DI != DE; ++DI) { |
| 427 | if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) { |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 428 | if (isTrackedVar(vd)) { |
| 429 | vals[vd] = Uninitialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 430 | if (Stmt *init = vd->getInit()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 431 | Visit(init); |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 432 | vals[vd] = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 433 | } |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 434 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 435 | else if (Stmt *init = vd->getInit()) { |
| 436 | Visit(init); |
| 437 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 442 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
| 443 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 444 | // cannot be block-level expressions. Therefore, we determine if |
| 445 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 446 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 447 | // If a DeclRefExpr is not involved in a load, we are essentially computing |
| 448 | // its address, either for assignment to a reference or via the '&' operator. |
| 449 | // In such cases, treat the variable as being initialized, since this |
| 450 | // analysis isn't powerful enough to do alias tracking. |
| 451 | if (dr != currentDR) |
| 452 | if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 453 | if (isTrackedVar(vd)) |
| 454 | vals[vd] = Initialized; |
| 455 | } |
| 456 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 457 | void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 458 | if (bo->isAssignmentOp()) { |
| 459 | const FindVarResult &res = findBlockVarDecl(bo->getLHS()); |
| 460 | if (const VarDecl* vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 461 | // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment" |
| 462 | // cannot be block-level expressions. Therefore, we determine if |
| 463 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 464 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 465 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 466 | res.getDeclRefExpr()); |
| 467 | Visit(bo->getRHS()); |
| 468 | Visit(bo->getLHS()); |
| 469 | |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 470 | ValueVector::reference val = vals[vd]; |
| 471 | if (val == Uninitialized) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 472 | if (bo->getOpcode() != BO_Assign) |
| 473 | reportUninit(res.getDeclRefExpr(), vd); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 474 | val = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 475 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 476 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 479 | Visit(bo->getRHS()); |
| 480 | Visit(bo->getLHS()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 484 | switch (uo->getOpcode()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 485 | case clang::UO_PostDec: |
| 486 | case clang::UO_PostInc: |
| 487 | case clang::UO_PreDec: |
| 488 | case clang::UO_PreInc: { |
| 489 | const FindVarResult &res = findBlockVarDecl(uo->getSubExpr()); |
| 490 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 491 | // We assume that DeclRefExprs wrapped in a unary operator ++/-- |
| 492 | // cannot be block-level expressions. Therefore, we determine if |
| 493 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 494 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 495 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 496 | res.getDeclRefExpr()); |
| 497 | Visit(uo->getSubExpr()); |
| 498 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 499 | ValueVector::reference bit = vals[vd]; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 500 | if (bit == Uninitialized) { |
| 501 | reportUninit(res.getDeclRefExpr(), vd); |
| 502 | bit = Initialized; |
| 503 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 504 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 505 | } |
| 506 | break; |
| 507 | } |
| 508 | default: |
| 509 | break; |
| 510 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 511 | Visit(uo->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 515 | if (ce->getCastKind() == CK_LValueToRValue) { |
| 516 | const FindVarResult &res = findBlockVarDecl(ce->getSubExpr()); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 517 | if (const VarDecl *vd = res.getDecl()) { |
| 518 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 519 | // cannot be block-level expressions. Therefore, we determine if |
| 520 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 521 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 522 | // Here we update 'currentDR' to be the one associated with this |
| 523 | // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we |
| 524 | // will know that we are not computing its lvalue for other purposes |
| 525 | // than to perform a load. |
| 526 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 527 | res.getDeclRefExpr()); |
| 528 | Visit(ce->getSubExpr()); |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 529 | if (currentVoidCast != ce && vals[vd] == Uninitialized) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 530 | reportUninit(res.getDeclRefExpr(), vd); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 531 | // Don't cascade warnings. |
| 532 | vals[vd] = Initialized; |
| 533 | } |
| 534 | return; |
| 535 | } |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 536 | } |
| 537 | else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) { |
| 538 | if (cse->getType()->isVoidType()) { |
| 539 | // e.g. (void) x; |
| 540 | SaveAndRestore<const Expr *> |
| 541 | lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens()); |
| 542 | Visit(cse->getSubExpr()); |
| 543 | return; |
| 544 | } |
| 545 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 546 | Visit(ce->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 549 | void TransferFunctions::VisitUnaryExprOrTypeTraitExpr( |
| 550 | UnaryExprOrTypeTraitExpr *se) { |
| 551 | if (se->getKind() == UETT_SizeOf) { |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame] | 552 | if (se->getType()->isConstantSizeType()) |
| 553 | return; |
| 554 | // Handle VLAs. |
| 555 | Visit(se->getArgumentExpr()); |
| 556 | } |
| 557 | } |
| 558 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 559 | //------------------------------------------------------------------------====// |
| 560 | // High-level "driver" logic for uninitialized values analysis. |
| 561 | //====------------------------------------------------------------------------// |
| 562 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 563 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 564 | AnalysisContext &ac, CFGBlockValues &vals, |
| 565 | UninitVariablesHandler *handler = 0, |
| 566 | bool flagBlockUses = false) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 567 | |
| 568 | if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 569 | CFGBlock::const_pred_iterator itr = block->pred_begin(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 570 | BVPair vA = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 571 | ++itr; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 572 | BVPair vB = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 573 | |
| 574 | BVPair valsAB; |
| 575 | |
| 576 | if (b->getOpcode() == BO_LAnd) { |
| 577 | // Merge the 'F' bits from the first and second. |
| 578 | vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true); |
| 579 | vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false); |
| 580 | valsAB.first = vA.first; |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 581 | valsAB.second = &vals.getScratch(); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 582 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 583 | else { |
| 584 | // Merge the 'T' bits from the first and second. |
| 585 | assert(b->getOpcode() == BO_LOr); |
| 586 | vals.mergeIntoScratch(*vA.first, true); |
| 587 | vals.mergeIntoScratch(*vB.first, false); |
| 588 | valsAB.first = &vals.getScratch(); |
| 589 | valsAB.second = vA.second ? vA.second : vA.first; |
| 590 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 591 | return vals.updateValueVectors(block, valsAB); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 594 | // Default behavior: merge in values of predecessor blocks. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 595 | vals.resetScratch(); |
| 596 | bool isFirst = true; |
| 597 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 598 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 599 | vals.mergeIntoScratch(vals.getValueVector(*I, block), isFirst); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 600 | isFirst = false; |
| 601 | } |
| 602 | // Apply the transfer function. |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 603 | TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 604 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 605 | I != E; ++I) { |
| 606 | if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) { |
| 607 | tf.BlockStmt_Visit(cs->getStmt()); |
| 608 | } |
| 609 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 610 | return vals.updateValueVectorWithScratch(block); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void clang::runUninitializedVariablesAnalysis(const DeclContext &dc, |
| 614 | const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 615 | AnalysisContext &ac, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 616 | UninitVariablesHandler &handler) { |
| 617 | CFGBlockValues vals(cfg); |
| 618 | vals.computeSetOfDeclarations(dc); |
| 619 | if (vals.hasNoDeclarations()) |
| 620 | return; |
| 621 | DataflowWorklist worklist(cfg); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame^] | 622 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 623 | |
| 624 | worklist.enqueueSuccessors(&cfg.getEntry()); |
| 625 | |
| 626 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 627 | // Did the block change? |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 628 | bool changed = runOnBlock(block, cfg, ac, vals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 629 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 630 | worklist.enqueueSuccessors(block); |
| 631 | previouslyVisited[block->getBlockID()] = true; |
| 632 | } |
| 633 | |
| 634 | // Run through the blocks one more time, and report uninitialized variabes. |
| 635 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 636 | runOnBlock(*BI, cfg, ac, vals, &handler, /* flagBlockUses */ true); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | UninitVariablesHandler::~UninitVariablesHandler() {} |
| 641 | |