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]; |
Chandler Carruth | 75c4064 | 2011-04-28 08:19:45 +0000 | [diff] [blame] | 176 | memset((void*)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 | |
Ted Kremenek | e6c2803 | 2011-05-10 22:10:35 +0000 | [diff] [blame] | 217 | if (block->pred_size() == 2) { |
| 218 | if (block->getTerminatorCondition() == b) { |
| 219 | if (block->succ_size() == 2) |
| 220 | return b; |
| 221 | } |
| 222 | else if (block->size() == 1) |
| 223 | return b; |
| 224 | } |
| 225 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 226 | return 0; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 229 | ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block, |
| 230 | const CFGBlock *dstBlock) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 231 | unsigned idx = block->getBlockID(); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 232 | if (dstBlock && getLogicalOperatorInChain(block)) { |
| 233 | if (*block->succ_begin() == dstBlock) |
| 234 | return lazyCreate(vals[idx].first); |
| 235 | assert(*(block->succ_begin()+1) == dstBlock); |
| 236 | return lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | assert(vals[idx].second == 0); |
| 240 | return lazyCreate(vals[idx].first); |
| 241 | } |
| 242 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 243 | bool CFGBlockValues::hasValues(const CFGBlock *block) { |
| 244 | unsigned idx = block->getBlockID(); |
| 245 | return vals[idx].second != 0; |
| 246 | } |
| 247 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 248 | BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block, |
| 249 | bool shouldLazyCreate) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 250 | unsigned idx = block->getBlockID(); |
| 251 | lazyCreate(vals[idx].first); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 252 | if (shouldLazyCreate) |
| 253 | lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 254 | return vals[idx]; |
| 255 | } |
| 256 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 257 | void CFGBlockValues::mergeIntoScratch(ValueVector const &source, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 258 | bool isFirst) { |
| 259 | if (isFirst) |
| 260 | scratch = source; |
| 261 | else |
Ted Kremenek | afb10c4 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 262 | scratch.merge(source); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 263 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 264 | #if 0 |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 265 | static void printVector(const CFGBlock *block, ValueVector &bv, |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 266 | unsigned num) { |
| 267 | |
| 268 | llvm::errs() << block->getBlockID() << " :"; |
| 269 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 270 | llvm::errs() << ' ' << bv[i]; |
| 271 | } |
| 272 | llvm::errs() << " : " << num << '\n'; |
| 273 | } |
| 274 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 276 | bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { |
| 277 | ValueVector &dst = getValueVector(block, 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 278 | bool changed = (dst != scratch); |
| 279 | if (changed) |
| 280 | dst = scratch; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 281 | #if 0 |
| 282 | printVector(block, scratch, 0); |
| 283 | #endif |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 284 | return changed; |
| 285 | } |
| 286 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 287 | bool CFGBlockValues::updateValueVectors(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 288 | const BVPair &newVals) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 289 | BVPair &vals = getValueVectors(block, true); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 290 | bool changed = *newVals.first != *vals.first || |
| 291 | *newVals.second != *vals.second; |
| 292 | *vals.first = *newVals.first; |
| 293 | *vals.second = *newVals.second; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 294 | #if 0 |
| 295 | printVector(block, *vals.first, 1); |
| 296 | printVector(block, *vals.second, 2); |
| 297 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 298 | return changed; |
| 299 | } |
| 300 | |
| 301 | void CFGBlockValues::resetScratch() { |
| 302 | scratch.reset(); |
| 303 | } |
| 304 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 305 | ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 306 | const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 307 | assert(idx.hasValue()); |
| 308 | return scratch[idx.getValue()]; |
| 309 | } |
| 310 | |
| 311 | //------------------------------------------------------------------------====// |
| 312 | // Worklist: worklist for dataflow analysis. |
| 313 | //====------------------------------------------------------------------------// |
| 314 | |
| 315 | namespace { |
| 316 | class DataflowWorklist { |
| 317 | llvm::SmallVector<const CFGBlock *, 20> worklist; |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 318 | llvm::BitVector enqueuedBlocks; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 319 | public: |
| 320 | DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {} |
| 321 | |
| 322 | void enqueue(const CFGBlock *block); |
| 323 | void enqueueSuccessors(const CFGBlock *block); |
| 324 | const CFGBlock *dequeue(); |
| 325 | |
| 326 | }; |
| 327 | } |
| 328 | |
| 329 | void DataflowWorklist::enqueue(const CFGBlock *block) { |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 330 | if (!block) |
| 331 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 332 | unsigned idx = block->getBlockID(); |
| 333 | if (enqueuedBlocks[idx]) |
| 334 | return; |
| 335 | worklist.push_back(block); |
| 336 | enqueuedBlocks[idx] = true; |
| 337 | } |
| 338 | |
| 339 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
| 340 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 341 | E = block->succ_end(); I != E; ++I) { |
| 342 | enqueue(*I); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | const CFGBlock *DataflowWorklist::dequeue() { |
| 347 | if (worklist.empty()) |
| 348 | return 0; |
| 349 | const CFGBlock *b = worklist.back(); |
| 350 | worklist.pop_back(); |
| 351 | enqueuedBlocks[b->getBlockID()] = false; |
| 352 | return b; |
| 353 | } |
| 354 | |
| 355 | //------------------------------------------------------------------------====// |
| 356 | // Transfer function for uninitialized values analysis. |
| 357 | //====------------------------------------------------------------------------// |
| 358 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 359 | namespace { |
| 360 | class FindVarResult { |
| 361 | const VarDecl *vd; |
| 362 | const DeclRefExpr *dr; |
| 363 | public: |
| 364 | FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 365 | |
| 366 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 367 | const VarDecl *getDecl() const { return vd; } |
| 368 | }; |
| 369 | |
| 370 | class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> { |
| 371 | CFGBlockValues &vals; |
| 372 | const CFG &cfg; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 373 | AnalysisContext ∾ |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 374 | UninitVariablesHandler *handler; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 375 | const DeclRefExpr *currentDR; |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 376 | const Expr *currentVoidCast; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 377 | const bool flagBlockUses; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 378 | public: |
| 379 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 380 | AnalysisContext &ac, |
| 381 | UninitVariablesHandler *handler, |
| 382 | bool flagBlockUses) |
| 383 | : vals(vals), cfg(cfg), ac(ac), handler(handler), currentDR(0), |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 384 | currentVoidCast(0), flagBlockUses(flagBlockUses) {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 385 | |
| 386 | const CFG &getCFG() { return cfg; } |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 387 | void reportUninit(const DeclRefExpr *ex, const VarDecl *vd, |
| 388 | bool isAlwaysUninit); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 389 | |
| 390 | void VisitBlockExpr(BlockExpr *be); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 391 | void VisitDeclStmt(DeclStmt *ds); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 392 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 393 | void VisitUnaryOperator(UnaryOperator *uo); |
| 394 | void VisitBinaryOperator(BinaryOperator *bo); |
| 395 | void VisitCastExpr(CastExpr *ce); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 396 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *se); |
Chandler Carruth | 8668494 | 2011-04-13 08:18:42 +0000 | [diff] [blame] | 397 | void VisitCXXTypeidExpr(CXXTypeidExpr *E); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 398 | void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs); |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 399 | |
| 400 | bool isTrackedVar(const VarDecl *vd) { |
| 401 | return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); |
| 402 | } |
| 403 | |
| 404 | FindVarResult findBlockVarDecl(Expr *ex); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 405 | }; |
| 406 | } |
| 407 | |
| 408 | void TransferFunctions::reportUninit(const DeclRefExpr *ex, |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 409 | const VarDecl *vd, bool isAlwaysUnit) { |
| 410 | if (handler) handler->handleUseOfUninitVariable(ex, vd, isAlwaysUnit); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 413 | FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 414 | if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts())) |
| 415 | if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 416 | if (isTrackedVar(vd)) |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 417 | return FindVarResult(vd, dr); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 418 | return FindVarResult(0, 0); |
| 419 | } |
| 420 | |
| 421 | void TransferFunctions::BlockStmt_VisitObjCForCollectionStmt( |
| 422 | ObjCForCollectionStmt *fs) { |
| 423 | |
| 424 | Visit(fs->getCollection()); |
| 425 | |
| 426 | // This represents an initialization of the 'element' value. |
| 427 | Stmt *element = fs->getElement(); |
| 428 | const VarDecl* vd = 0; |
| 429 | |
| 430 | if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) { |
| 431 | vd = cast<VarDecl>(ds->getSingleDecl()); |
| 432 | if (!isTrackedVar(vd)) |
| 433 | vd = 0; |
| 434 | } |
| 435 | else { |
| 436 | // Initialize the value of the reference variable. |
| 437 | const FindVarResult &res = findBlockVarDecl(cast<Expr>(element)); |
| 438 | vd = res.getDecl(); |
| 439 | if (!vd) { |
| 440 | Visit(element); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (vd) |
| 446 | vals[vd] = Initialized; |
| 447 | } |
| 448 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 449 | void TransferFunctions::VisitBlockExpr(BlockExpr *be) { |
| 450 | if (!flagBlockUses || !handler) |
| 451 | return; |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 452 | const BlockDecl *bd = be->getBlockDecl(); |
| 453 | for (BlockDecl::capture_const_iterator i = bd->capture_begin(), |
| 454 | e = bd->capture_end() ; i != e; ++i) { |
| 455 | const VarDecl *vd = i->getVariable(); |
| 456 | if (!vd->hasLocalStorage()) |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 457 | continue; |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 458 | if (!isTrackedVar(vd)) |
| 459 | continue; |
| 460 | if (i->isByRef()) { |
| 461 | vals[vd] = Initialized; |
| 462 | continue; |
| 463 | } |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 464 | Value v = vals[vd]; |
| 465 | if (isUninitialized(v)) |
| 466 | handler->handleUseOfUninitVariable(be, vd, isAlwaysUninit(v)); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 470 | void TransferFunctions::VisitDeclStmt(DeclStmt *ds) { |
| 471 | for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end(); |
| 472 | DI != DE; ++DI) { |
| 473 | if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) { |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 474 | if (isTrackedVar(vd)) { |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 475 | if (Expr *init = vd->getInit()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 476 | Visit(init); |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 477 | |
| 478 | // If the initializer consists solely of a reference to itself, we |
| 479 | // explicitly mark the variable as uninitialized. This allows code |
| 480 | // like the following: |
| 481 | // |
| 482 | // int x = x; |
| 483 | // |
| 484 | // to deliberately leave a variable uninitialized. Different analysis |
| 485 | // clients can detect this pattern and adjust their reporting |
| 486 | // appropriately, but we need to continue to analyze subsequent uses |
| 487 | // of the variable. |
| 488 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(init->IgnoreParenImpCasts()); |
| 489 | vals[vd] = (DRE && DRE->getDecl() == vd) ? Uninitialized |
| 490 | : Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 491 | } |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 492 | } else if (Stmt *init = vd->getInit()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 493 | Visit(init); |
| 494 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 499 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
| 500 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 501 | // cannot be block-level expressions. Therefore, we determine if |
| 502 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 503 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 504 | // If a DeclRefExpr is not involved in a load, we are essentially computing |
| 505 | // its address, either for assignment to a reference or via the '&' operator. |
| 506 | // In such cases, treat the variable as being initialized, since this |
| 507 | // analysis isn't powerful enough to do alias tracking. |
| 508 | if (dr != currentDR) |
| 509 | if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 510 | if (isTrackedVar(vd)) |
| 511 | vals[vd] = Initialized; |
| 512 | } |
| 513 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 514 | void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 515 | if (bo->isAssignmentOp()) { |
| 516 | const FindVarResult &res = findBlockVarDecl(bo->getLHS()); |
| 517 | if (const VarDecl* vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 518 | // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment" |
| 519 | // cannot be block-level expressions. Therefore, we determine if |
| 520 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 521 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 522 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 523 | res.getDeclRefExpr()); |
| 524 | Visit(bo->getRHS()); |
| 525 | Visit(bo->getLHS()); |
| 526 | |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 527 | ValueVector::reference val = vals[vd]; |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 528 | if (isUninitialized(val)) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 529 | if (bo->getOpcode() != BO_Assign) |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 530 | reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val)); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 531 | val = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 532 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 533 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 536 | Visit(bo->getRHS()); |
| 537 | Visit(bo->getLHS()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 541 | switch (uo->getOpcode()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 542 | case clang::UO_PostDec: |
| 543 | case clang::UO_PostInc: |
| 544 | case clang::UO_PreDec: |
| 545 | case clang::UO_PreInc: { |
| 546 | const FindVarResult &res = findBlockVarDecl(uo->getSubExpr()); |
| 547 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 548 | // We assume that DeclRefExprs wrapped in a unary operator ++/-- |
| 549 | // cannot be block-level expressions. Therefore, we determine if |
| 550 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 551 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 552 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 553 | res.getDeclRefExpr()); |
| 554 | Visit(uo->getSubExpr()); |
| 555 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 556 | ValueVector::reference val = vals[vd]; |
| 557 | if (isUninitialized(val)) { |
| 558 | reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val)); |
| 559 | // Don't cascade warnings. |
| 560 | val = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 561 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 562 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 563 | } |
| 564 | break; |
| 565 | } |
| 566 | default: |
| 567 | break; |
| 568 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 569 | Visit(uo->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 573 | if (ce->getCastKind() == CK_LValueToRValue) { |
| 574 | const FindVarResult &res = findBlockVarDecl(ce->getSubExpr()); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 575 | if (const VarDecl *vd = res.getDecl()) { |
| 576 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 577 | // cannot be block-level expressions. Therefore, we determine if |
| 578 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 579 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 580 | // Here we update 'currentDR' to be the one associated with this |
| 581 | // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we |
| 582 | // will know that we are not computing its lvalue for other purposes |
| 583 | // than to perform a load. |
| 584 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 585 | res.getDeclRefExpr()); |
| 586 | Visit(ce->getSubExpr()); |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 587 | if (currentVoidCast != ce) { |
| 588 | Value val = vals[vd]; |
| 589 | if (isUninitialized(val)) { |
| 590 | reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val)); |
| 591 | // Don't cascade warnings. |
| 592 | vals[vd] = Initialized; |
| 593 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 594 | } |
| 595 | return; |
| 596 | } |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 597 | } |
| 598 | else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) { |
| 599 | if (cse->getType()->isVoidType()) { |
| 600 | // e.g. (void) x; |
| 601 | SaveAndRestore<const Expr *> |
| 602 | lastVoidCast(currentVoidCast, cse->getSubExpr()->IgnoreParens()); |
| 603 | Visit(cse->getSubExpr()); |
| 604 | return; |
| 605 | } |
| 606 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 607 | Visit(ce->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 610 | void TransferFunctions::VisitUnaryExprOrTypeTraitExpr( |
| 611 | UnaryExprOrTypeTraitExpr *se) { |
| 612 | if (se->getKind() == UETT_SizeOf) { |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame] | 613 | if (se->getType()->isConstantSizeType()) |
| 614 | return; |
| 615 | // Handle VLAs. |
| 616 | Visit(se->getArgumentExpr()); |
| 617 | } |
| 618 | } |
| 619 | |
Chandler Carruth | 8668494 | 2011-04-13 08:18:42 +0000 | [diff] [blame] | 620 | void TransferFunctions::VisitCXXTypeidExpr(CXXTypeidExpr *E) { |
| 621 | // typeid(expression) is potentially evaluated when the argument is |
| 622 | // a glvalue of polymorphic type. (C++ 5.2.8p2-3) |
| 623 | if (!E->isTypeOperand() && E->Classify(ac.getASTContext()).isGLValue()) { |
| 624 | QualType SubExprTy = E->getExprOperand()->getType(); |
| 625 | if (const RecordType *Record = SubExprTy->getAs<RecordType>()) |
| 626 | if (cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic()) |
| 627 | Visit(E->getExprOperand()); |
| 628 | } |
| 629 | } |
| 630 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 631 | //------------------------------------------------------------------------====// |
| 632 | // High-level "driver" logic for uninitialized values analysis. |
| 633 | //====------------------------------------------------------------------------// |
| 634 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 635 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 636 | AnalysisContext &ac, CFGBlockValues &vals, |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 637 | llvm::BitVector &wasAnalyzed, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 638 | UninitVariablesHandler *handler = 0, |
| 639 | bool flagBlockUses = false) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 641 | wasAnalyzed[block->getBlockID()] = true; |
| 642 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 643 | if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 644 | CFGBlock::const_pred_iterator itr = block->pred_begin(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 645 | BVPair vA = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 646 | ++itr; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 647 | BVPair vB = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 648 | |
| 649 | BVPair valsAB; |
| 650 | |
| 651 | if (b->getOpcode() == BO_LAnd) { |
| 652 | // Merge the 'F' bits from the first and second. |
| 653 | vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true); |
| 654 | vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false); |
| 655 | valsAB.first = vA.first; |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 656 | valsAB.second = &vals.getScratch(); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 657 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 658 | else { |
| 659 | // Merge the 'T' bits from the first and second. |
| 660 | assert(b->getOpcode() == BO_LOr); |
| 661 | vals.mergeIntoScratch(*vA.first, true); |
| 662 | vals.mergeIntoScratch(*vB.first, false); |
| 663 | valsAB.first = &vals.getScratch(); |
| 664 | valsAB.second = vA.second ? vA.second : vA.first; |
| 665 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 666 | return vals.updateValueVectors(block, valsAB); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 669 | // Default behavior: merge in values of predecessor blocks. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 670 | vals.resetScratch(); |
| 671 | bool isFirst = true; |
| 672 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 673 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 674 | vals.mergeIntoScratch(vals.getValueVector(*I, block), isFirst); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 675 | isFirst = false; |
| 676 | } |
| 677 | // Apply the transfer function. |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 678 | TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 679 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 680 | I != E; ++I) { |
| 681 | if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) { |
| 682 | tf.BlockStmt_Visit(cs->getStmt()); |
| 683 | } |
| 684 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 685 | return vals.updateValueVectorWithScratch(block); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void clang::runUninitializedVariablesAnalysis(const DeclContext &dc, |
| 689 | const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 690 | AnalysisContext &ac, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 691 | UninitVariablesHandler &handler) { |
| 692 | CFGBlockValues vals(cfg); |
| 693 | vals.computeSetOfDeclarations(dc); |
| 694 | if (vals.hasNoDeclarations()) |
| 695 | return; |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 696 | |
| 697 | // Mark all variables uninitialized at the entry. |
| 698 | const CFGBlock &entry = cfg.getEntry(); |
| 699 | for (CFGBlock::const_succ_iterator i = entry.succ_begin(), |
| 700 | e = entry.succ_end(); i != e; ++i) { |
| 701 | if (const CFGBlock *succ = *i) { |
| 702 | ValueVector &vec = vals.getValueVector(&entry, succ); |
| 703 | const unsigned n = vals.getNumEntries(); |
| 704 | for (unsigned j = 0; j < n ; ++j) { |
| 705 | vec[j] = Uninitialized; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | // Proceed with the workist. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 711 | DataflowWorklist worklist(cfg); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 712 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 713 | worklist.enqueueSuccessors(&cfg.getEntry()); |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 714 | llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 715 | |
| 716 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 717 | // Did the block change? |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 718 | bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 719 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 720 | worklist.enqueueSuccessors(block); |
| 721 | previouslyVisited[block->getBlockID()] = true; |
| 722 | } |
| 723 | |
| 724 | // Run through the blocks one more time, and report uninitialized variabes. |
| 725 | 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] | 726 | if (wasAnalyzed[(*BI)->getBlockID()]) |
| 727 | runOnBlock(*BI, cfg, ac, vals, wasAnalyzed, &handler, |
| 728 | /* flagBlockUses */ true); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | UninitVariablesHandler::~UninitVariablesHandler() {} |
| 733 | |