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