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