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" |
Argyrios Kyrtzidis | 049f6d0 | 2011-05-31 03:56:09 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/PackedVector.h" |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 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 | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 96 | |
Argyrios Kyrtzidis | 049f6d0 | 2011-05-31 03:56:09 +0000 | [diff] [blame] | 97 | typedef llvm::PackedVector<Value, 2> ValueVector; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 98 | typedef std::pair<ValueVector *, ValueVector *> BVPair; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 100 | class CFGBlockValues { |
| 101 | const CFG &cfg; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 102 | BVPair *vals; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 103 | ValueVector scratch; |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 104 | DeclToIndex declToIndex; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 106 | ValueVector &lazyCreate(ValueVector *&bv); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 107 | public: |
| 108 | CFGBlockValues(const CFG &cfg); |
| 109 | ~CFGBlockValues(); |
| 110 | |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 111 | unsigned getNumEntries() const { return declToIndex.size(); } |
| 112 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 113 | void computeSetOfDeclarations(const DeclContext &dc); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 114 | ValueVector &getValueVector(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 115 | const CFGBlock *dstBlock); |
| 116 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 117 | BVPair &getValueVectors(const CFGBlock *block, bool shouldLazyCreate); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 119 | void mergeIntoScratch(ValueVector const &source, bool isFirst); |
| 120 | bool updateValueVectorWithScratch(const CFGBlock *block); |
| 121 | bool updateValueVectors(const CFGBlock *block, const BVPair &newVals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 122 | |
| 123 | bool hasNoDeclarations() const { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 124 | return declToIndex.size() == 0; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Ted Kremenek | b831c67 | 2011-03-29 01:40:00 +0000 | [diff] [blame] | 127 | bool hasEntry(const VarDecl *vd) const { |
| 128 | return declToIndex.getValueIndex(vd).hasValue(); |
| 129 | } |
| 130 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 131 | bool hasValues(const CFGBlock *block); |
| 132 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 133 | void resetScratch(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 134 | ValueVector &getScratch() { return scratch; } |
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::reference operator[](const VarDecl *vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 137 | }; |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 138 | } // end anonymous namespace |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 139 | |
| 140 | CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) { |
| 141 | unsigned n = cfg.getNumBlockIDs(); |
| 142 | if (!n) |
| 143 | return; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 144 | vals = new std::pair<ValueVector*, ValueVector*>[n]; |
Chandler Carruth | 75c4064 | 2011-04-28 08:19:45 +0000 | [diff] [blame] | 145 | memset((void*)vals, 0, sizeof(*vals) * n); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | CFGBlockValues::~CFGBlockValues() { |
| 149 | unsigned n = cfg.getNumBlockIDs(); |
| 150 | if (n == 0) |
| 151 | return; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 152 | for (unsigned i = 0; i < n; ++i) { |
| 153 | delete vals[i].first; |
| 154 | delete vals[i].second; |
| 155 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 156 | delete [] vals; |
| 157 | } |
| 158 | |
| 159 | void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 160 | declToIndex.computeMap(dc); |
| 161 | scratch.resize(declToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 164 | ValueVector &CFGBlockValues::lazyCreate(ValueVector *&bv) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 165 | if (!bv) |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 166 | bv = new ValueVector(declToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 167 | return *bv; |
| 168 | } |
| 169 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 170 | /// This function pattern matches for a '&&' or '||' that appears at |
| 171 | /// the beginning of a CFGBlock that also (1) has a terminator and |
| 172 | /// (2) has no other elements. If such an expression is found, it is returned. |
| 173 | static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) { |
| 174 | if (block->empty()) |
| 175 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 3c0349e | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 177 | const CFGStmt *cstmt = block->front().getAs<CFGStmt>(); |
Ted Kremenek | 76709bf | 2011-03-15 05:22:28 +0000 | [diff] [blame] | 178 | if (!cstmt) |
| 179 | return 0; |
| 180 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 181 | BinaryOperator *b = dyn_cast_or_null<BinaryOperator>(cstmt->getStmt()); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 182 | |
| 183 | if (!b || !b->isLogicalOp()) |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 184 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | e6c2803 | 2011-05-10 22:10:35 +0000 | [diff] [blame] | 186 | if (block->pred_size() == 2) { |
| 187 | if (block->getTerminatorCondition() == b) { |
| 188 | if (block->succ_size() == 2) |
| 189 | return b; |
| 190 | } |
| 191 | else if (block->size() == 1) |
| 192 | return b; |
| 193 | } |
| 194 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 195 | return 0; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 198 | ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block, |
| 199 | const CFGBlock *dstBlock) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 200 | unsigned idx = block->getBlockID(); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 201 | if (dstBlock && getLogicalOperatorInChain(block)) { |
| 202 | if (*block->succ_begin() == dstBlock) |
| 203 | return lazyCreate(vals[idx].first); |
| 204 | assert(*(block->succ_begin()+1) == dstBlock); |
| 205 | return lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | assert(vals[idx].second == 0); |
| 209 | return lazyCreate(vals[idx].first); |
| 210 | } |
| 211 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 212 | bool CFGBlockValues::hasValues(const CFGBlock *block) { |
| 213 | unsigned idx = block->getBlockID(); |
| 214 | return vals[idx].second != 0; |
| 215 | } |
| 216 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 217 | BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block, |
| 218 | bool shouldLazyCreate) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 219 | unsigned idx = block->getBlockID(); |
| 220 | lazyCreate(vals[idx].first); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 221 | if (shouldLazyCreate) |
| 222 | lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 223 | return vals[idx]; |
| 224 | } |
| 225 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 226 | void CFGBlockValues::mergeIntoScratch(ValueVector const &source, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 227 | bool isFirst) { |
| 228 | if (isFirst) |
| 229 | scratch = source; |
| 230 | else |
Argyrios Kyrtzidis | 049f6d0 | 2011-05-31 03:56:09 +0000 | [diff] [blame] | 231 | scratch |= source; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 232 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 233 | #if 0 |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 234 | static void printVector(const CFGBlock *block, ValueVector &bv, |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 235 | unsigned num) { |
| 236 | |
| 237 | llvm::errs() << block->getBlockID() << " :"; |
| 238 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 239 | llvm::errs() << ' ' << bv[i]; |
| 240 | } |
| 241 | llvm::errs() << " : " << num << '\n'; |
| 242 | } |
| 243 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 245 | bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { |
| 246 | ValueVector &dst = getValueVector(block, 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 247 | bool changed = (dst != scratch); |
| 248 | if (changed) |
| 249 | dst = scratch; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 250 | #if 0 |
| 251 | printVector(block, scratch, 0); |
| 252 | #endif |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 253 | return changed; |
| 254 | } |
| 255 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 256 | bool CFGBlockValues::updateValueVectors(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 257 | const BVPair &newVals) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 258 | BVPair &vals = getValueVectors(block, true); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 259 | bool changed = *newVals.first != *vals.first || |
| 260 | *newVals.second != *vals.second; |
| 261 | *vals.first = *newVals.first; |
| 262 | *vals.second = *newVals.second; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 263 | #if 0 |
| 264 | printVector(block, *vals.first, 1); |
| 265 | printVector(block, *vals.second, 2); |
| 266 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 267 | return changed; |
| 268 | } |
| 269 | |
| 270 | void CFGBlockValues::resetScratch() { |
| 271 | scratch.reset(); |
| 272 | } |
| 273 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 274 | ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 275 | const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 276 | assert(idx.hasValue()); |
| 277 | return scratch[idx.getValue()]; |
| 278 | } |
| 279 | |
| 280 | //------------------------------------------------------------------------====// |
| 281 | // Worklist: worklist for dataflow analysis. |
| 282 | //====------------------------------------------------------------------------// |
| 283 | |
| 284 | namespace { |
| 285 | class DataflowWorklist { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 286 | SmallVector<const CFGBlock *, 20> worklist; |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 287 | llvm::BitVector enqueuedBlocks; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 288 | public: |
| 289 | DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {} |
| 290 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 291 | void enqueueSuccessors(const CFGBlock *block); |
| 292 | const CFGBlock *dequeue(); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 293 | }; |
| 294 | } |
| 295 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 296 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 297 | unsigned OldWorklistSize = worklist.size(); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 298 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 299 | E = block->succ_end(); I != E; ++I) { |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 300 | const CFGBlock *Successor = *I; |
| 301 | if (!Successor || enqueuedBlocks[Successor->getBlockID()]) |
| 302 | continue; |
| 303 | worklist.push_back(Successor); |
| 304 | enqueuedBlocks[Successor->getBlockID()] = true; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 305 | } |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 306 | if (OldWorklistSize == 0 || OldWorklistSize == worklist.size()) |
| 307 | return; |
| 308 | |
| 309 | // Rotate the newly added blocks to the start of the worklist so that it forms |
| 310 | // a proper queue when we pop off the end of the worklist. |
| 311 | std::rotate(worklist.begin(), worklist.begin() + OldWorklistSize, |
| 312 | worklist.end()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | const CFGBlock *DataflowWorklist::dequeue() { |
| 316 | if (worklist.empty()) |
| 317 | return 0; |
| 318 | const CFGBlock *b = worklist.back(); |
| 319 | worklist.pop_back(); |
| 320 | enqueuedBlocks[b->getBlockID()] = false; |
| 321 | return b; |
| 322 | } |
| 323 | |
| 324 | //------------------------------------------------------------------------====// |
| 325 | // Transfer function for uninitialized values analysis. |
| 326 | //====------------------------------------------------------------------------// |
| 327 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 328 | namespace { |
| 329 | class FindVarResult { |
| 330 | const VarDecl *vd; |
| 331 | const DeclRefExpr *dr; |
| 332 | public: |
| 333 | FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 334 | |
| 335 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 336 | const VarDecl *getDecl() const { return vd; } |
| 337 | }; |
| 338 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 339 | class TransferFunctions : public StmtVisitor<TransferFunctions> { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 340 | CFGBlockValues &vals; |
| 341 | const CFG &cfg; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 342 | AnalysisContext ∾ |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 343 | UninitVariablesHandler *handler; |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 344 | const bool flagBlockUses; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 345 | |
| 346 | /// The last DeclRefExpr seen when analyzing a block. Used to |
| 347 | /// cheat when detecting cases when the address of a variable is taken. |
| 348 | DeclRefExpr *lastDR; |
| 349 | |
| 350 | /// The last lvalue-to-rvalue conversion of a variable whose value |
| 351 | /// was uninitialized. Normally this results in a warning, but it is |
| 352 | /// possible to either silence the warning in some cases, or we |
| 353 | /// propagate the uninitialized value. |
| 354 | CastExpr *lastLoad; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 355 | public: |
| 356 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 357 | AnalysisContext &ac, |
| 358 | UninitVariablesHandler *handler, |
| 359 | bool flagBlockUses) |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 360 | : vals(vals), cfg(cfg), ac(ac), handler(handler), |
| 361 | flagBlockUses(flagBlockUses), lastDR(0), lastLoad(0) {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 362 | |
| 363 | const CFG &getCFG() { return cfg; } |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 364 | void reportUninit(const DeclRefExpr *ex, const VarDecl *vd, |
| 365 | bool isAlwaysUninit); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 366 | |
| 367 | void VisitBlockExpr(BlockExpr *be); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 368 | void VisitDeclStmt(DeclStmt *ds); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 369 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 370 | void VisitUnaryOperator(UnaryOperator *uo); |
| 371 | void VisitBinaryOperator(BinaryOperator *bo); |
| 372 | void VisitCastExpr(CastExpr *ce); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 373 | void VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs); |
| 374 | void Visit(Stmt *s); |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 375 | |
| 376 | bool isTrackedVar(const VarDecl *vd) { |
| 377 | return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); |
| 378 | } |
| 379 | |
| 380 | FindVarResult findBlockVarDecl(Expr *ex); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 381 | |
| 382 | void ProcessUses(Stmt *s = 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 383 | }; |
| 384 | } |
| 385 | |
| 386 | void TransferFunctions::reportUninit(const DeclRefExpr *ex, |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 387 | const VarDecl *vd, bool isAlwaysUnit) { |
| 388 | if (handler) handler->handleUseOfUninitVariable(ex, vd, isAlwaysUnit); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 391 | FindVarResult TransferFunctions::findBlockVarDecl(Expr* ex) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 392 | if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts())) |
| 393 | if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 394 | if (isTrackedVar(vd)) |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 395 | return FindVarResult(vd, dr); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 396 | return FindVarResult(0, 0); |
| 397 | } |
| 398 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 399 | void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 400 | // This represents an initialization of the 'element' value. |
| 401 | Stmt *element = fs->getElement(); |
| 402 | const VarDecl* vd = 0; |
| 403 | |
| 404 | if (DeclStmt* ds = dyn_cast<DeclStmt>(element)) { |
| 405 | vd = cast<VarDecl>(ds->getSingleDecl()); |
| 406 | if (!isTrackedVar(vd)) |
| 407 | vd = 0; |
| 408 | } |
| 409 | else { |
| 410 | // Initialize the value of the reference variable. |
| 411 | const FindVarResult &res = findBlockVarDecl(cast<Expr>(element)); |
| 412 | vd = res.getDecl(); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if (vd) |
| 416 | vals[vd] = Initialized; |
| 417 | } |
| 418 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 419 | void TransferFunctions::VisitBlockExpr(BlockExpr *be) { |
| 420 | if (!flagBlockUses || !handler) |
| 421 | return; |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 422 | const BlockDecl *bd = be->getBlockDecl(); |
| 423 | for (BlockDecl::capture_const_iterator i = bd->capture_begin(), |
| 424 | e = bd->capture_end() ; i != e; ++i) { |
| 425 | const VarDecl *vd = i->getVariable(); |
| 426 | if (!vd->hasLocalStorage()) |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 427 | continue; |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 428 | if (!isTrackedVar(vd)) |
| 429 | continue; |
| 430 | if (i->isByRef()) { |
| 431 | vals[vd] = Initialized; |
| 432 | continue; |
| 433 | } |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 434 | Value v = vals[vd]; |
| 435 | if (isUninitialized(v)) |
| 436 | handler->handleUseOfUninitVariable(be, vd, isAlwaysUninit(v)); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 440 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
| 441 | // Record the last DeclRefExpr seen. This is an lvalue computation. |
| 442 | // We use this value to later detect if a variable "escapes" the analysis. |
| 443 | if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
Ted Kremenek | dd4286b | 2011-07-20 19:49:47 +0000 | [diff] [blame] | 444 | if (isTrackedVar(vd)) { |
| 445 | ProcessUses(); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 446 | lastDR = dr; |
Ted Kremenek | dd4286b | 2011-07-20 19:49:47 +0000 | [diff] [blame] | 447 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 450 | void TransferFunctions::VisitDeclStmt(DeclStmt *ds) { |
| 451 | for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end(); |
| 452 | DI != DE; ++DI) { |
| 453 | if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) { |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 454 | if (isTrackedVar(vd)) { |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 455 | if (Expr *init = vd->getInit()) { |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 456 | // If the initializer consists solely of a reference to itself, we |
| 457 | // explicitly mark the variable as uninitialized. This allows code |
| 458 | // like the following: |
| 459 | // |
| 460 | // int x = x; |
| 461 | // |
| 462 | // to deliberately leave a variable uninitialized. Different analysis |
| 463 | // clients can detect this pattern and adjust their reporting |
| 464 | // appropriately, but we need to continue to analyze subsequent uses |
| 465 | // of the variable. |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 466 | if (init == lastLoad) { |
Ted Kremenek | d626ec4 | 2011-07-19 20:33:49 +0000 | [diff] [blame] | 467 | DeclRefExpr *DR = |
| 468 | cast<DeclRefExpr>(lastLoad->getSubExpr()->IgnoreParens()); |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 469 | if (DR->getDecl() == vd) { |
| 470 | // int x = x; |
| 471 | // Propagate uninitialized value, but don't immediately report |
| 472 | // a problem. |
| 473 | vals[vd] = Uninitialized; |
| 474 | lastLoad = 0; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 475 | lastDR = 0; |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 476 | return; |
| 477 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 478 | } |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 479 | |
| 480 | // All other cases: treat the new variable as initialized. |
| 481 | vals[vd] = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 482 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 483 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 488 | void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 489 | if (bo->isAssignmentOp()) { |
| 490 | const FindVarResult &res = findBlockVarDecl(bo->getLHS()); |
| 491 | if (const VarDecl* vd = res.getDecl()) { |
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)) { |
Chandler Carruth | 8435069 | 2011-07-16 22:27:02 +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)); |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 496 | else |
| 497 | val = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 504 | switch (uo->getOpcode()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 505 | case clang::UO_PostDec: |
| 506 | case clang::UO_PostInc: |
| 507 | case clang::UO_PreDec: |
| 508 | case clang::UO_PreInc: { |
| 509 | const FindVarResult &res = findBlockVarDecl(uo->getSubExpr()); |
| 510 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 511 | assert(res.getDeclRefExpr() == lastDR); |
| 512 | // We null out lastDR to indicate we have fully processed it |
| 513 | // and we don't want the auto-value setting in Visit(). |
| 514 | lastDR = 0; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 516 | ValueVector::reference val = vals[vd]; |
Chandler Carruth | d837c0d | 2011-07-22 05:27:52 +0000 | [diff] [blame] | 517 | if (isUninitialized(val)) |
Ted Kremenek | f7bafc7 | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 518 | reportUninit(res.getDeclRefExpr(), vd, isAlwaysUninit(val)); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 519 | } |
| 520 | break; |
| 521 | } |
| 522 | default: |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 528 | if (ce->getCastKind() == CK_LValueToRValue) { |
| 529 | const FindVarResult &res = findBlockVarDecl(ce->getSubExpr()); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 530 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 531 | assert(res.getDeclRefExpr() == lastDR); |
| 532 | if (isUninitialized(vals[vd])) { |
| 533 | // Record this load of an uninitialized value. Normally this |
| 534 | // results in a warning, but we delay reporting the issue |
| 535 | // in case it is wrapped in a void cast, etc. |
| 536 | lastLoad = ce; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 537 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 538 | } |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 539 | } |
| 540 | else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) { |
| 541 | if (cse->getType()->isVoidType()) { |
| 542 | // e.g. (void) x; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 543 | if (lastLoad == cse->getSubExpr()) { |
| 544 | // Squelch any detected load of an uninitialized value if |
| 545 | // we cast it to void. |
| 546 | lastLoad = 0; |
| 547 | lastDR = 0; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void TransferFunctions::Visit(clang::Stmt *s) { |
| 554 | StmtVisitor<TransferFunctions>::Visit(s); |
| 555 | ProcessUses(s); |
| 556 | } |
| 557 | |
| 558 | void TransferFunctions::ProcessUses(Stmt *s) { |
| 559 | // This method is typically called after visiting a CFGElement statement |
| 560 | // in the CFG. We delay processing of reporting many loads of uninitialized |
| 561 | // values until here. |
| 562 | if (lastLoad) { |
| 563 | // If we just visited the lvalue-to-rvalue cast, there is nothing |
| 564 | // left to do. |
| 565 | if (lastLoad == s) |
| 566 | return; |
| 567 | |
| 568 | // If we reach here, we have seen a load of an uninitialized value |
| 569 | // and it hasn't been casted to void or otherwise handled. In this |
| 570 | // situation, report the incident. |
Ted Kremenek | d626ec4 | 2011-07-19 20:33:49 +0000 | [diff] [blame] | 571 | DeclRefExpr *DR = cast<DeclRefExpr>(lastLoad->getSubExpr()->IgnoreParens()); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 572 | VarDecl *VD = cast<VarDecl>(DR->getDecl()); |
| 573 | reportUninit(DR, VD, isAlwaysUninit(vals[VD])); |
| 574 | lastLoad = 0; |
| 575 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 576 | if (DR == lastDR) { |
| 577 | lastDR = 0; |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 581 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 582 | // Any other uses of 'lastDR' involve taking an lvalue of variable. |
| 583 | // In this case, it "escapes" the analysis. |
| 584 | if (lastDR && lastDR != s) { |
| 585 | vals[cast<VarDecl>(lastDR->getDecl())] = Initialized; |
| 586 | lastDR = 0; |
Chandler Carruth | 8668494 | 2011-04-13 08:18:42 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 590 | //------------------------------------------------------------------------====// |
| 591 | // High-level "driver" logic for uninitialized values analysis. |
| 592 | //====------------------------------------------------------------------------// |
| 593 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 594 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 595 | AnalysisContext &ac, CFGBlockValues &vals, |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 596 | llvm::BitVector &wasAnalyzed, |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 597 | UninitVariablesHandler *handler = 0, |
| 598 | bool flagBlockUses = false) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 599 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 600 | wasAnalyzed[block->getBlockID()] = true; |
| 601 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 602 | if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 603 | CFGBlock::const_pred_iterator itr = block->pred_begin(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 604 | BVPair vA = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 605 | ++itr; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 606 | BVPair vB = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 607 | |
| 608 | BVPair valsAB; |
| 609 | |
| 610 | if (b->getOpcode() == BO_LAnd) { |
| 611 | // Merge the 'F' bits from the first and second. |
| 612 | vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true); |
| 613 | vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false); |
| 614 | valsAB.first = vA.first; |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 615 | valsAB.second = &vals.getScratch(); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 616 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 617 | else { |
| 618 | // Merge the 'T' bits from the first and second. |
| 619 | assert(b->getOpcode() == BO_LOr); |
| 620 | vals.mergeIntoScratch(*vA.first, true); |
| 621 | vals.mergeIntoScratch(*vB.first, false); |
| 622 | valsAB.first = &vals.getScratch(); |
| 623 | valsAB.second = vA.second ? vA.second : vA.first; |
| 624 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 625 | return vals.updateValueVectors(block, valsAB); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 628 | // Default behavior: merge in values of predecessor blocks. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 629 | vals.resetScratch(); |
| 630 | bool isFirst = true; |
| 631 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 632 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 633 | vals.mergeIntoScratch(vals.getValueVector(*I, block), isFirst); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 634 | isFirst = false; |
| 635 | } |
| 636 | // Apply the transfer function. |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 637 | TransferFunctions tf(vals, cfg, ac, handler, flagBlockUses); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 638 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 639 | I != E; ++I) { |
| 640 | if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) { |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 641 | tf.Visit(cs->getStmt()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 644 | tf.ProcessUses(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 645 | return vals.updateValueVectorWithScratch(block); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 648 | void clang::runUninitializedVariablesAnalysis( |
| 649 | const DeclContext &dc, |
| 650 | const CFG &cfg, |
| 651 | AnalysisContext &ac, |
| 652 | UninitVariablesHandler &handler, |
| 653 | UninitVariablesAnalysisStats &stats) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 654 | CFGBlockValues vals(cfg); |
| 655 | vals.computeSetOfDeclarations(dc); |
| 656 | if (vals.hasNoDeclarations()) |
| 657 | return; |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 658 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 659 | stats.NumVariablesAnalyzed = vals.getNumEntries(); |
| 660 | |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 661 | // Mark all variables uninitialized at the entry. |
| 662 | const CFGBlock &entry = cfg.getEntry(); |
| 663 | for (CFGBlock::const_succ_iterator i = entry.succ_begin(), |
| 664 | e = entry.succ_end(); i != e; ++i) { |
| 665 | if (const CFGBlock *succ = *i) { |
| 666 | ValueVector &vec = vals.getValueVector(&entry, succ); |
| 667 | const unsigned n = vals.getNumEntries(); |
| 668 | for (unsigned j = 0; j < n ; ++j) { |
| 669 | vec[j] = Uninitialized; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Proceed with the workist. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 675 | DataflowWorklist worklist(cfg); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 676 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 677 | worklist.enqueueSuccessors(&cfg.getEntry()); |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 678 | llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 679 | |
| 680 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 681 | // Did the block change? |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 682 | bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed); |
| 683 | ++stats.NumBlockVisits; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 684 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 685 | worklist.enqueueSuccessors(block); |
| 686 | previouslyVisited[block->getBlockID()] = true; |
| 687 | } |
| 688 | |
| 689 | // Run through the blocks one more time, and report uninitialized variabes. |
| 690 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 691 | if (wasAnalyzed[(*BI)->getBlockID()]) { |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 692 | runOnBlock(*BI, cfg, ac, vals, wasAnalyzed, &handler, |
| 693 | /* flagBlockUses */ true); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 694 | ++stats.NumBlockVisits; |
| 695 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | UninitVariablesHandler::~UninitVariablesHandler() {} |