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