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" |
Argyrios Kyrtzidis | b2c60b0 | 2012-03-01 19:45:56 +0000 | [diff] [blame] | 24 | #include "llvm/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) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 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, |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 115 | const CFGBlock *dstBlock); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 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 | |
Richard Smith | a9e8b9e | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 119 | void setAllScratchValues(Value V); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 120 | void mergeIntoScratch(ValueVector const &source, bool isFirst); |
| 121 | bool updateValueVectorWithScratch(const CFGBlock *block); |
| 122 | bool updateValueVectors(const CFGBlock *block, const BVPair &newVals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 123 | |
| 124 | bool hasNoDeclarations() const { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 125 | return declToIndex.size() == 0; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 126 | } |
Ted Kremenek | e0e2933 | 2011-08-20 01:15:28 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 128 | void resetScratch(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 129 | ValueVector &getScratch() { return scratch; } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 131 | ValueVector::reference operator[](const VarDecl *vd); |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 132 | |
| 133 | Value getValue(const CFGBlock *block, const CFGBlock *dstBlock, |
| 134 | const VarDecl *vd) { |
| 135 | const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
| 136 | assert(idx.hasValue()); |
| 137 | return getValueVector(block, dstBlock)[idx.getValue()]; |
| 138 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 139 | }; |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 140 | } // end anonymous namespace |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 141 | |
| 142 | CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) { |
| 143 | unsigned n = cfg.getNumBlockIDs(); |
| 144 | if (!n) |
| 145 | return; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 146 | vals = new std::pair<ValueVector*, ValueVector*>[n]; |
Chandler Carruth | 75c4064 | 2011-04-28 08:19:45 +0000 | [diff] [blame] | 147 | memset((void*)vals, 0, sizeof(*vals) * n); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | CFGBlockValues::~CFGBlockValues() { |
| 151 | unsigned n = cfg.getNumBlockIDs(); |
| 152 | if (n == 0) |
| 153 | return; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 154 | for (unsigned i = 0; i < n; ++i) { |
| 155 | delete vals[i].first; |
| 156 | delete vals[i].second; |
| 157 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 158 | delete [] vals; |
| 159 | } |
| 160 | |
| 161 | void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 162 | declToIndex.computeMap(dc); |
| 163 | scratch.resize(declToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 166 | ValueVector &CFGBlockValues::lazyCreate(ValueVector *&bv) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 167 | if (!bv) |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 168 | bv = new ValueVector(declToIndex.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 169 | return *bv; |
| 170 | } |
| 171 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 172 | /// This function pattern matches for a '&&' or '||' that appears at |
| 173 | /// the beginning of a CFGBlock that also (1) has a terminator and |
| 174 | /// (2) has no other elements. If such an expression is found, it is returned. |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 175 | static const BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 176 | if (block->empty()) |
| 177 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 178 | |
Richard Smith | b86b855 | 2012-04-30 00:16:51 +0000 | [diff] [blame] | 179 | CFGElement front = block->front(); |
| 180 | const CFGStmt *cstmt = front.getAs<CFGStmt>(); |
Ted Kremenek | 76709bf | 2011-03-15 05:22:28 +0000 | [diff] [blame] | 181 | if (!cstmt) |
| 182 | return 0; |
| 183 | |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 184 | const BinaryOperator *b = dyn_cast_or_null<BinaryOperator>(cstmt->getStmt()); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 185 | |
| 186 | if (!b || !b->isLogicalOp()) |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 187 | return 0; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | e6c2803 | 2011-05-10 22:10:35 +0000 | [diff] [blame] | 189 | if (block->pred_size() == 2) { |
| 190 | if (block->getTerminatorCondition() == b) { |
| 191 | if (block->succ_size() == 2) |
| 192 | return b; |
| 193 | } |
| 194 | else if (block->size() == 1) |
| 195 | return b; |
| 196 | } |
| 197 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 198 | return 0; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 201 | ValueVector &CFGBlockValues::getValueVector(const CFGBlock *block, |
| 202 | const CFGBlock *dstBlock) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 203 | unsigned idx = block->getBlockID(); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 204 | if (dstBlock && getLogicalOperatorInChain(block)) { |
| 205 | if (*block->succ_begin() == dstBlock) |
| 206 | return lazyCreate(vals[idx].first); |
| 207 | assert(*(block->succ_begin()+1) == dstBlock); |
| 208 | return lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | assert(vals[idx].second == 0); |
| 212 | return lazyCreate(vals[idx].first); |
| 213 | } |
| 214 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 215 | BVPair &CFGBlockValues::getValueVectors(const clang::CFGBlock *block, |
| 216 | bool shouldLazyCreate) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 217 | unsigned idx = block->getBlockID(); |
| 218 | lazyCreate(vals[idx].first); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 219 | if (shouldLazyCreate) |
| 220 | lazyCreate(vals[idx].second); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 221 | return vals[idx]; |
| 222 | } |
| 223 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 224 | #if 0 |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 225 | static void printVector(const CFGBlock *block, ValueVector &bv, |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 226 | unsigned num) { |
| 227 | |
| 228 | llvm::errs() << block->getBlockID() << " :"; |
| 229 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 230 | llvm::errs() << ' ' << bv[i]; |
| 231 | } |
| 232 | llvm::errs() << " : " << num << '\n'; |
| 233 | } |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 234 | |
| 235 | static void printVector(const char *name, ValueVector const &bv) { |
| 236 | llvm::errs() << name << " : "; |
| 237 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 238 | llvm::errs() << ' ' << bv[i]; |
| 239 | } |
| 240 | llvm::errs() << "\n"; |
| 241 | } |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 242 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 243 | |
Richard Smith | a9e8b9e | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 244 | void CFGBlockValues::setAllScratchValues(Value V) { |
| 245 | for (unsigned I = 0, E = scratch.size(); I != E; ++I) |
| 246 | scratch[I] = V; |
| 247 | } |
| 248 | |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 249 | void CFGBlockValues::mergeIntoScratch(ValueVector const &source, |
| 250 | bool isFirst) { |
| 251 | if (isFirst) |
| 252 | scratch = source; |
| 253 | else |
| 254 | scratch |= source; |
| 255 | } |
| 256 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 257 | bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { |
| 258 | ValueVector &dst = getValueVector(block, 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 259 | bool changed = (dst != scratch); |
| 260 | if (changed) |
| 261 | dst = scratch; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 262 | #if 0 |
| 263 | printVector(block, scratch, 0); |
| 264 | #endif |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 265 | return changed; |
| 266 | } |
| 267 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 268 | bool CFGBlockValues::updateValueVectors(const CFGBlock *block, |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 269 | const BVPair &newVals) { |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 270 | BVPair &vals = getValueVectors(block, true); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 271 | bool changed = *newVals.first != *vals.first || |
| 272 | *newVals.second != *vals.second; |
| 273 | *vals.first = *newVals.first; |
| 274 | *vals.second = *newVals.second; |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 275 | #if 0 |
| 276 | printVector(block, *vals.first, 1); |
| 277 | printVector(block, *vals.second, 2); |
| 278 | #endif |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 279 | return changed; |
| 280 | } |
| 281 | |
| 282 | void CFGBlockValues::resetScratch() { |
| 283 | scratch.reset(); |
| 284 | } |
| 285 | |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 286 | ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
Ted Kremenek | 4ddb387 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 287 | const llvm::Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 288 | assert(idx.hasValue()); |
| 289 | return scratch[idx.getValue()]; |
| 290 | } |
| 291 | |
| 292 | //------------------------------------------------------------------------====// |
| 293 | // Worklist: worklist for dataflow analysis. |
| 294 | //====------------------------------------------------------------------------// |
| 295 | |
| 296 | namespace { |
| 297 | class DataflowWorklist { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 298 | SmallVector<const CFGBlock *, 20> worklist; |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 299 | llvm::BitVector enqueuedBlocks; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 300 | public: |
| 301 | DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {} |
| 302 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 303 | void enqueueSuccessors(const CFGBlock *block); |
| 304 | const CFGBlock *dequeue(); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 305 | }; |
| 306 | } |
| 307 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 308 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 309 | unsigned OldWorklistSize = worklist.size(); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 310 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 311 | E = block->succ_end(); I != E; ++I) { |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 312 | const CFGBlock *Successor = *I; |
| 313 | if (!Successor || enqueuedBlocks[Successor->getBlockID()]) |
| 314 | continue; |
| 315 | worklist.push_back(Successor); |
| 316 | enqueuedBlocks[Successor->getBlockID()] = true; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 317 | } |
Chandler Carruth | 8052050 | 2011-07-08 11:19:06 +0000 | [diff] [blame] | 318 | if (OldWorklistSize == 0 || OldWorklistSize == worklist.size()) |
| 319 | return; |
| 320 | |
| 321 | // Rotate the newly added blocks to the start of the worklist so that it forms |
| 322 | // a proper queue when we pop off the end of the worklist. |
| 323 | std::rotate(worklist.begin(), worklist.begin() + OldWorklistSize, |
| 324 | worklist.end()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | const CFGBlock *DataflowWorklist::dequeue() { |
| 328 | if (worklist.empty()) |
| 329 | return 0; |
| 330 | const CFGBlock *b = worklist.back(); |
| 331 | worklist.pop_back(); |
| 332 | enqueuedBlocks[b->getBlockID()] = false; |
| 333 | return b; |
| 334 | } |
| 335 | |
| 336 | //------------------------------------------------------------------------====// |
| 337 | // Transfer function for uninitialized values analysis. |
| 338 | //====------------------------------------------------------------------------// |
| 339 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 340 | namespace { |
| 341 | class FindVarResult { |
| 342 | const VarDecl *vd; |
| 343 | const DeclRefExpr *dr; |
| 344 | public: |
| 345 | FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 346 | |
| 347 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 348 | const VarDecl *getDecl() const { return vd; } |
| 349 | }; |
| 350 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 351 | class TransferFunctions : public StmtVisitor<TransferFunctions> { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 352 | CFGBlockValues &vals; |
| 353 | const CFG &cfg; |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 354 | const CFGBlock *block; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 355 | AnalysisDeclContext ∾ |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 356 | UninitVariablesHandler *handler; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 357 | |
| 358 | /// The last DeclRefExpr seen when analyzing a block. Used to |
| 359 | /// cheat when detecting cases when the address of a variable is taken. |
| 360 | DeclRefExpr *lastDR; |
| 361 | |
| 362 | /// The last lvalue-to-rvalue conversion of a variable whose value |
| 363 | /// was uninitialized. Normally this results in a warning, but it is |
| 364 | /// possible to either silence the warning in some cases, or we |
| 365 | /// propagate the uninitialized value. |
| 366 | CastExpr *lastLoad; |
Ted Kremenek | 57fb591 | 2011-08-04 22:40:57 +0000 | [diff] [blame] | 367 | |
| 368 | /// For some expressions, we want to ignore any post-processing after |
| 369 | /// visitation. |
| 370 | bool skipProcessUses; |
| 371 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 372 | public: |
| 373 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 374 | const CFGBlock *block, AnalysisDeclContext &ac, |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 375 | UninitVariablesHandler *handler) |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 376 | : vals(vals), cfg(cfg), block(block), ac(ac), handler(handler), |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 377 | lastDR(0), lastLoad(0), |
Ted Kremenek | 57fb591 | 2011-08-04 22:40:57 +0000 | [diff] [blame] | 378 | skipProcessUses(false) {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 379 | |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 380 | void reportUse(const Expr *ex, const VarDecl *vd); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 381 | |
| 382 | void VisitBlockExpr(BlockExpr *be); |
Richard Smith | a9e8b9e | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 383 | void VisitCallExpr(CallExpr *ce); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 384 | void VisitDeclStmt(DeclStmt *ds); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 385 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 386 | void VisitUnaryOperator(UnaryOperator *uo); |
| 387 | void VisitBinaryOperator(BinaryOperator *bo); |
| 388 | void VisitCastExpr(CastExpr *ce); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 389 | void VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs); |
| 390 | void Visit(Stmt *s); |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 392 | bool isTrackedVar(const VarDecl *vd) { |
| 393 | return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); |
| 394 | } |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 395 | |
| 396 | UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) { |
| 397 | UninitUse Use(ex, isAlwaysUninit(v)); |
| 398 | |
| 399 | assert(isUninitialized(v)); |
| 400 | if (Use.getKind() == UninitUse::Always) |
| 401 | return Use; |
| 402 | |
| 403 | // If an edge which leads unconditionally to this use did not initialize |
| 404 | // the variable, we can say something stronger than 'may be uninitialized': |
| 405 | // we can say 'either it's used uninitialized or you have dead code'. |
| 406 | // |
| 407 | // We track the number of successors of a node which have been visited, and |
| 408 | // visit a node once we have visited all of its successors. Only edges where |
| 409 | // the variable might still be uninitialized are followed. Since a variable |
| 410 | // can't transfer from being initialized to being uninitialized, this will |
| 411 | // trace out the subgraph which inevitably leads to the use and does not |
| 412 | // initialize the variable. We do not want to skip past loops, since their |
| 413 | // non-termination might be correlated with the initialization condition. |
| 414 | // |
| 415 | // For example: |
| 416 | // |
| 417 | // void f(bool a, bool b) { |
| 418 | // block1: int n; |
| 419 | // if (a) { |
| 420 | // block2: if (b) |
| 421 | // block3: n = 1; |
| 422 | // block4: } else if (b) { |
| 423 | // block5: while (!a) { |
| 424 | // block6: do_work(&a); |
| 425 | // n = 2; |
| 426 | // } |
| 427 | // } |
| 428 | // block7: if (a) |
| 429 | // block8: g(); |
| 430 | // block9: return n; |
| 431 | // } |
| 432 | // |
| 433 | // Starting from the maybe-uninitialized use in block 9: |
| 434 | // * Block 7 is not visited because we have only visited one of its two |
| 435 | // successors. |
| 436 | // * Block 8 is visited because we've visited its only successor. |
| 437 | // From block 8: |
| 438 | // * Block 7 is visited because we've now visited both of its successors. |
| 439 | // From block 7: |
| 440 | // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all |
| 441 | // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively). |
| 442 | // * Block 3 is not visited because it initializes 'n'. |
| 443 | // Now the algorithm terminates, having visited blocks 7 and 8, and having |
| 444 | // found the frontier is blocks 2, 4, and 5. |
| 445 | // |
| 446 | // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2 |
| 447 | // and 4), so we report that any time either of those edges is taken (in |
| 448 | // each case when 'b == false'), 'n' is used uninitialized. |
| 449 | llvm::SmallVector<const CFGBlock*, 32> Queue; |
| 450 | llvm::SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0); |
| 451 | Queue.push_back(block); |
| 452 | // Specify that we've already visited all successors of the starting block. |
| 453 | // This has the dual purpose of ensuring we never add it to the queue, and |
| 454 | // of marking it as not being a candidate element of the frontier. |
| 455 | SuccsVisited[block->getBlockID()] = block->succ_size(); |
| 456 | while (!Queue.empty()) { |
| 457 | const CFGBlock *B = Queue.back(); |
| 458 | Queue.pop_back(); |
| 459 | for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); |
| 460 | I != E; ++I) { |
| 461 | const CFGBlock *Pred = *I; |
| 462 | if (vals.getValue(Pred, B, vd) == Initialized) |
| 463 | // This block initializes the variable. |
| 464 | continue; |
| 465 | |
| 466 | if (++SuccsVisited[Pred->getBlockID()] == Pred->succ_size()) |
| 467 | // All paths from this block lead to the use and don't initialize the |
| 468 | // variable. |
| 469 | Queue.push_back(Pred); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Scan the frontier, looking for blocks where the variable was |
| 474 | // uninitialized. |
| 475 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
| 476 | const CFGBlock *Block = *BI; |
| 477 | unsigned BlockID = Block->getBlockID(); |
| 478 | const Stmt *Term = Block->getTerminator(); |
| 479 | if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() && |
| 480 | Term) { |
| 481 | // This block inevitably leads to the use. If we have an edge from here |
| 482 | // to a post-dominator block, and the variable is uninitialized on that |
| 483 | // edge, we have found a bug. |
| 484 | for (CFGBlock::const_succ_iterator I = Block->succ_begin(), |
| 485 | E = Block->succ_end(); I != E; ++I) { |
| 486 | const CFGBlock *Succ = *I; |
| 487 | if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() && |
| 488 | vals.getValue(Block, Succ, vd) == Uninitialized) { |
| 489 | // Switch cases are a special case: report the label to the caller |
| 490 | // as the 'terminator', not the switch statement itself. Suppress |
| 491 | // situations where no label matched: we can't be sure that's |
| 492 | // possible. |
| 493 | if (isa<SwitchStmt>(Term)) { |
| 494 | const Stmt *Label = Succ->getLabel(); |
| 495 | if (!Label || !isa<SwitchCase>(Label)) |
| 496 | // Might not be possible. |
| 497 | continue; |
| 498 | UninitUse::Branch Branch; |
| 499 | Branch.Terminator = Label; |
| 500 | Branch.Output = 0; // Ignored. |
| 501 | Use.addUninitBranch(Branch); |
| 502 | } else { |
| 503 | UninitUse::Branch Branch; |
| 504 | Branch.Terminator = Term; |
| 505 | Branch.Output = I - Block->succ_begin(); |
| 506 | Use.addUninitBranch(Branch); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return Use; |
| 514 | } |
| 515 | |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 516 | FindVarResult findBlockVarDecl(Expr *ex); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 517 | |
| 518 | void ProcessUses(Stmt *s = 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 519 | }; |
| 520 | } |
| 521 | |
Ted Kremenek | de091ae | 2011-08-08 21:43:08 +0000 | [diff] [blame] | 522 | static const Expr *stripCasts(ASTContext &C, const Expr *Ex) { |
| 523 | while (Ex) { |
| 524 | Ex = Ex->IgnoreParenNoopCasts(C); |
| 525 | if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) { |
| 526 | if (CE->getCastKind() == CK_LValueBitCast) { |
| 527 | Ex = CE->getSubExpr(); |
| 528 | continue; |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | } |
| 533 | return Ex; |
| 534 | } |
| 535 | |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 536 | void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) { |
| 537 | if (!handler) |
| 538 | return; |
| 539 | Value v = vals[vd]; |
| 540 | if (isUninitialized(v)) |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 541 | handler->handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 544 | FindVarResult TransferFunctions::findBlockVarDecl(Expr *ex) { |
| 545 | if (DeclRefExpr *dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts())) |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 546 | if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 547 | if (isTrackedVar(vd)) |
Ted Kremenek | 40900ee | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 548 | return FindVarResult(vd, dr); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 549 | return FindVarResult(0, 0); |
| 550 | } |
| 551 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 552 | void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 553 | // This represents an initialization of the 'element' value. |
| 554 | Stmt *element = fs->getElement(); |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 555 | const VarDecl *vd = 0; |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 556 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 557 | if (DeclStmt *ds = dyn_cast<DeclStmt>(element)) { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 558 | vd = cast<VarDecl>(ds->getSingleDecl()); |
| 559 | if (!isTrackedVar(vd)) |
| 560 | vd = 0; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 561 | } else { |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 562 | // Initialize the value of the reference variable. |
| 563 | const FindVarResult &res = findBlockVarDecl(cast<Expr>(element)); |
| 564 | vd = res.getDecl(); |
Ted Kremenek | 1ea800c | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if (vd) |
| 568 | vals[vd] = Initialized; |
| 569 | } |
| 570 | |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 571 | void TransferFunctions::VisitBlockExpr(BlockExpr *be) { |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 572 | const BlockDecl *bd = be->getBlockDecl(); |
| 573 | for (BlockDecl::capture_const_iterator i = bd->capture_begin(), |
| 574 | e = bd->capture_end() ; i != e; ++i) { |
| 575 | const VarDecl *vd = i->getVariable(); |
Ted Kremenek | bc8b44c | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 576 | if (!isTrackedVar(vd)) |
| 577 | continue; |
| 578 | if (i->isByRef()) { |
| 579 | vals[vd] = Initialized; |
| 580 | continue; |
| 581 | } |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 582 | reportUse(be, vd); |
Ted Kremenek | a8c17a5 | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Richard Smith | a9e8b9e | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 586 | void TransferFunctions::VisitCallExpr(CallExpr *ce) { |
| 587 | // After a call to a function like setjmp or vfork, any variable which is |
| 588 | // initialized anywhere within this function may now be initialized. For now, |
| 589 | // just assume such a call initializes all variables. |
| 590 | // FIXME: Only mark variables as initialized if they have an initializer which |
| 591 | // is reachable from here. |
| 592 | Decl *Callee = ce->getCalleeDecl(); |
| 593 | if (Callee && Callee->hasAttr<ReturnsTwiceAttr>()) |
| 594 | vals.setAllScratchValues(Initialized); |
| 595 | } |
| 596 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 597 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
| 598 | // Record the last DeclRefExpr seen. This is an lvalue computation. |
| 599 | // We use this value to later detect if a variable "escapes" the analysis. |
| 600 | if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
Ted Kremenek | dd4286b | 2011-07-20 19:49:47 +0000 | [diff] [blame] | 601 | if (isTrackedVar(vd)) { |
| 602 | ProcessUses(); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 603 | lastDR = dr; |
Ted Kremenek | dd4286b | 2011-07-20 19:49:47 +0000 | [diff] [blame] | 604 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 607 | void TransferFunctions::VisitDeclStmt(DeclStmt *ds) { |
| 608 | for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end(); |
| 609 | DI != DE; ++DI) { |
| 610 | if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) { |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 611 | if (isTrackedVar(vd)) { |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 612 | if (Expr *init = vd->getInit()) { |
Chandler Carruth | b88fb02 | 2011-04-05 21:36:30 +0000 | [diff] [blame] | 613 | // If the initializer consists solely of a reference to itself, we |
| 614 | // explicitly mark the variable as uninitialized. This allows code |
| 615 | // like the following: |
| 616 | // |
| 617 | // int x = x; |
| 618 | // |
| 619 | // to deliberately leave a variable uninitialized. Different analysis |
| 620 | // clients can detect this pattern and adjust their reporting |
| 621 | // appropriately, but we need to continue to analyze subsequent uses |
| 622 | // of the variable. |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 623 | if (init == lastLoad) { |
Ted Kremenek | de091ae | 2011-08-08 21:43:08 +0000 | [diff] [blame] | 624 | const DeclRefExpr *DR |
| 625 | = cast<DeclRefExpr>(stripCasts(ac.getASTContext(), |
| 626 | lastLoad->getSubExpr())); |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 627 | if (DR->getDecl() == vd) { |
| 628 | // int x = x; |
| 629 | // Propagate uninitialized value, but don't immediately report |
| 630 | // a problem. |
| 631 | vals[vd] = Uninitialized; |
| 632 | lastLoad = 0; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 633 | lastDR = 0; |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 634 | if (handler) |
| 635 | handler->handleSelfInit(vd); |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 636 | return; |
| 637 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 638 | } |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 639 | |
| 640 | // All other cases: treat the new variable as initialized. |
Ted Kremenek | 9e76172 | 2011-10-13 18:50:06 +0000 | [diff] [blame] | 641 | // This is a minor optimization to reduce the propagation |
| 642 | // of the analysis, since we will have already reported |
| 643 | // the use of the uninitialized value (which visiting the |
| 644 | // initializer). |
Ted Kremenek | 62d126e | 2011-07-19 21:41:51 +0000 | [diff] [blame] | 645 | vals[vd] = Initialized; |
Richard Smith | 8f40dcc | 2012-06-16 23:34:14 +0000 | [diff] [blame] | 646 | } else { |
| 647 | // No initializer: the variable is now uninitialized. This matters |
| 648 | // for cases like: |
| 649 | // while (...) { |
| 650 | // int n; |
| 651 | // use(n); |
| 652 | // n = 0; |
| 653 | // } |
| 654 | // FIXME: Mark the variable as uninitialized whenever its scope is |
| 655 | // left, since its scope could be re-entered by a jump over the |
| 656 | // declaration. |
| 657 | vals[vd] = Uninitialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 658 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 659 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 664 | void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 665 | if (bo->isAssignmentOp()) { |
| 666 | const FindVarResult &res = findBlockVarDecl(bo->getLHS()); |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 667 | if (const VarDecl *vd = res.getDecl()) { |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 668 | if (bo->getOpcode() != BO_Assign) |
| 669 | reportUse(res.getDeclRefExpr(), vd); |
| 670 | else |
| 671 | vals[vd] = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 677 | switch (uo->getOpcode()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 678 | case clang::UO_PostDec: |
| 679 | case clang::UO_PostInc: |
| 680 | case clang::UO_PreDec: |
| 681 | case clang::UO_PreInc: { |
| 682 | const FindVarResult &res = findBlockVarDecl(uo->getSubExpr()); |
| 683 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 684 | assert(res.getDeclRefExpr() == lastDR); |
| 685 | // We null out lastDR to indicate we have fully processed it |
| 686 | // and we don't want the auto-value setting in Visit(). |
| 687 | lastDR = 0; |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 688 | reportUse(res.getDeclRefExpr(), vd); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 689 | } |
| 690 | break; |
| 691 | } |
| 692 | default: |
| 693 | break; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 698 | if (ce->getCastKind() == CK_LValueToRValue) { |
| 699 | const FindVarResult &res = findBlockVarDecl(ce->getSubExpr()); |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 700 | if (res.getDecl()) { |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 701 | assert(res.getDeclRefExpr() == lastDR); |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 702 | lastLoad = ce; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 703 | } |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 704 | } |
Ted Kremenek | de091ae | 2011-08-08 21:43:08 +0000 | [diff] [blame] | 705 | else if (ce->getCastKind() == CK_NoOp || |
| 706 | ce->getCastKind() == CK_LValueBitCast) { |
Ted Kremenek | 57fb591 | 2011-08-04 22:40:57 +0000 | [diff] [blame] | 707 | skipProcessUses = true; |
| 708 | } |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 709 | else if (CStyleCastExpr *cse = dyn_cast<CStyleCastExpr>(ce)) { |
| 710 | if (cse->getType()->isVoidType()) { |
| 711 | // e.g. (void) x; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 712 | if (lastLoad == cse->getSubExpr()) { |
| 713 | // Squelch any detected load of an uninitialized value if |
| 714 | // we cast it to void. |
| 715 | lastLoad = 0; |
| 716 | lastDR = 0; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | void TransferFunctions::Visit(clang::Stmt *s) { |
Ted Kremenek | 57fb591 | 2011-08-04 22:40:57 +0000 | [diff] [blame] | 723 | skipProcessUses = false; |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 724 | StmtVisitor<TransferFunctions>::Visit(s); |
Ted Kremenek | 57fb591 | 2011-08-04 22:40:57 +0000 | [diff] [blame] | 725 | if (!skipProcessUses) |
| 726 | ProcessUses(s); |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void TransferFunctions::ProcessUses(Stmt *s) { |
| 730 | // This method is typically called after visiting a CFGElement statement |
| 731 | // in the CFG. We delay processing of reporting many loads of uninitialized |
| 732 | // values until here. |
| 733 | if (lastLoad) { |
| 734 | // If we just visited the lvalue-to-rvalue cast, there is nothing |
| 735 | // left to do. |
| 736 | if (lastLoad == s) |
| 737 | return; |
| 738 | |
Ted Kremenek | de091ae | 2011-08-08 21:43:08 +0000 | [diff] [blame] | 739 | const DeclRefExpr *DR = |
| 740 | cast<DeclRefExpr>(stripCasts(ac.getASTContext(), |
| 741 | lastLoad->getSubExpr())); |
| 742 | const VarDecl *VD = cast<VarDecl>(DR->getDecl()); |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 743 | |
| 744 | // If we reach here, we may have seen a load of an uninitialized value |
| 745 | // and it hasn't been casted to void or otherwise handled. In this |
| 746 | // situation, report the incident. |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 747 | reportUse(DR, VD); |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 749 | lastLoad = 0; |
Ted Kremenek | c5f740e | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 750 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 751 | if (DR == lastDR) { |
| 752 | lastDR = 0; |
Ted Kremenek | dd0f794 | 2011-01-26 04:49:43 +0000 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 756 | |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 757 | // Any other uses of 'lastDR' involve taking an lvalue of variable. |
| 758 | // In this case, it "escapes" the analysis. |
| 759 | if (lastDR && lastDR != s) { |
| 760 | vals[cast<VarDecl>(lastDR->getDecl())] = Initialized; |
| 761 | lastDR = 0; |
Chandler Carruth | 8668494 | 2011-04-13 08:18:42 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 765 | //------------------------------------------------------------------------====// |
| 766 | // High-level "driver" logic for uninitialized values analysis. |
| 767 | //====------------------------------------------------------------------------// |
| 768 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 769 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 770 | AnalysisDeclContext &ac, CFGBlockValues &vals, |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 771 | llvm::BitVector &wasAnalyzed, |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 772 | UninitVariablesHandler *handler = 0) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 773 | |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 774 | wasAnalyzed[block->getBlockID()] = true; |
| 775 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 776 | if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 777 | CFGBlock::const_pred_iterator itr = block->pred_begin(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 778 | BVPair vA = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 779 | ++itr; |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 780 | BVPair vB = vals.getValueVectors(*itr, false); |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 781 | |
| 782 | BVPair valsAB; |
| 783 | |
| 784 | if (b->getOpcode() == BO_LAnd) { |
| 785 | // Merge the 'F' bits from the first and second. |
| 786 | vals.mergeIntoScratch(*(vA.second ? vA.second : vA.first), true); |
| 787 | vals.mergeIntoScratch(*(vB.second ? vB.second : vB.first), false); |
| 788 | valsAB.first = vA.first; |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 789 | valsAB.second = &vals.getScratch(); |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 790 | } else { |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 791 | // Merge the 'T' bits from the first and second. |
| 792 | assert(b->getOpcode() == BO_LOr); |
| 793 | vals.mergeIntoScratch(*vA.first, true); |
| 794 | vals.mergeIntoScratch(*vB.first, false); |
| 795 | valsAB.first = &vals.getScratch(); |
| 796 | valsAB.second = vA.second ? vA.second : vA.first; |
| 797 | } |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 798 | return vals.updateValueVectors(block, valsAB); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Ted Kremenek | 9fcbcee | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 801 | // Default behavior: merge in values of predecessor blocks. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 802 | vals.resetScratch(); |
| 803 | bool isFirst = true; |
| 804 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 805 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 806 | const CFGBlock *pred = *I; |
| 807 | if (wasAnalyzed[pred->getBlockID()]) { |
| 808 | vals.mergeIntoScratch(vals.getValueVector(pred, block), isFirst); |
| 809 | isFirst = false; |
| 810 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 811 | } |
| 812 | // Apply the transfer function. |
Richard Smith | 2815e1a | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 813 | TransferFunctions tf(vals, cfg, block, ac, handler); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 814 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 815 | I != E; ++I) { |
| 816 | if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) { |
Ted Kremenek | f1d10d9 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 817 | tf.Visit(const_cast<Stmt*>(cs->getStmt())); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
Ted Kremenek | 0c8e5a0 | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 820 | tf.ProcessUses(); |
Ted Kremenek | 136f8f2 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 821 | return vals.updateValueVectorWithScratch(block); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 824 | void clang::runUninitializedVariablesAnalysis( |
| 825 | const DeclContext &dc, |
| 826 | const CFG &cfg, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 827 | AnalysisDeclContext &ac, |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 828 | UninitVariablesHandler &handler, |
| 829 | UninitVariablesAnalysisStats &stats) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 830 | CFGBlockValues vals(cfg); |
| 831 | vals.computeSetOfDeclarations(dc); |
| 832 | if (vals.hasNoDeclarations()) |
| 833 | return; |
Richard Smith | 8189188 | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 834 | #if 0 |
| 835 | cfg.dump(dc.getParentASTContext().getLangOpts(), true); |
| 836 | #endif |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 837 | |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 838 | stats.NumVariablesAnalyzed = vals.getNumEntries(); |
| 839 | |
Ted Kremenek | d40066b | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 840 | // Mark all variables uninitialized at the entry. |
| 841 | const CFGBlock &entry = cfg.getEntry(); |
| 842 | for (CFGBlock::const_succ_iterator i = entry.succ_begin(), |
| 843 | e = entry.succ_end(); i != e; ++i) { |
| 844 | if (const CFGBlock *succ = *i) { |
| 845 | ValueVector &vec = vals.getValueVector(&entry, succ); |
| 846 | const unsigned n = vals.getNumEntries(); |
| 847 | for (unsigned j = 0; j < n ; ++j) { |
| 848 | vec[j] = Uninitialized; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // Proceed with the workist. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 854 | DataflowWorklist worklist(cfg); |
Ted Kremenek | 496398d | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 855 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 856 | worklist.enqueueSuccessors(&cfg.getEntry()); |
Ted Kremenek | f8adeef | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 857 | llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 858 | wasAnalyzed[cfg.getEntry().getBlockID()] = true; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 859 | |
| 860 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 861 | // Did the block change? |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 862 | bool changed = runOnBlock(block, cfg, ac, vals, wasAnalyzed); |
| 863 | ++stats.NumBlockVisits; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 864 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 865 | worklist.enqueueSuccessors(block); |
| 866 | previouslyVisited[block->getBlockID()] = true; |
| 867 | } |
| 868 | |
| 869 | // Run through the blocks one more time, and report uninitialized variabes. |
| 870 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
Ted Kremenek | 6f27542 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 871 | const CFGBlock *block = *BI; |
| 872 | if (wasAnalyzed[block->getBlockID()]) { |
| 873 | runOnBlock(block, cfg, ac, vals, wasAnalyzed, &handler); |
Chandler Carruth | 5d98994 | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 874 | ++stats.NumBlockVisits; |
| 875 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
| 879 | UninitVariablesHandler::~UninitVariablesHandler() {} |