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