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