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