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