Ted Kremenek | a0a5ca1 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 1 | //==- UninitializedValues.cpp - Find Uninitialized Values -------*- C++ --*-==// |
Ted Kremenek | b749a6d | 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 | |
Richard Smith | 130b8d4 | 2012-07-13 23:33:44 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 15 | #include "clang/AST/Attr.h" |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Jordan Rose | a7f94ce | 2013-05-15 23:22:55 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
Ted Kremenek | a0a5ca1 | 2011-03-15 03:17:07 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/AnalysisContext.h" |
| 22 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
| 25 | #include "llvm/ADT/Optional.h" |
| 26 | #include "llvm/ADT/PackedVector.h" |
| 27 | #include "llvm/ADT/SmallBitVector.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Argyrios Kyrtzidis | 981a961 | 2012-03-01 19:45:56 +0000 | [diff] [blame] | 29 | #include "llvm/Support/SaveAndRestore.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 30 | #include <utility> |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace clang; |
| 33 | |
Richard Smith | 130b8d4 | 2012-07-13 23:33:44 +0000 | [diff] [blame] | 34 | #define DEBUG_LOGGING 0 |
| 35 | |
Ted Kremenek | 93a3138 | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 36 | static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) { |
Ted Kremenek | c15a4e4 | 2011-03-17 03:06:11 +0000 | [diff] [blame] | 37 | if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() && |
Richard Smith | d88b44d | 2014-06-11 00:31:00 +0000 | [diff] [blame] | 38 | !vd->isExceptionVariable() && !vd->isInitCapture() && |
Richard Smith | c38498f | 2015-04-27 21:27:54 +0000 | [diff] [blame] | 39 | !vd->isImplicit() && vd->getDeclContext() == dc) { |
Ted Kremenek | c15a4e4 | 2011-03-17 03:06:11 +0000 | [diff] [blame] | 40 | QualType ty = vd->getType(); |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 41 | return ty->isScalarType() || ty->isVectorType() || ty->isRecordType(); |
Ted Kremenek | c15a4e4 | 2011-03-17 03:06:11 +0000 | [diff] [blame] | 42 | } |
| 43 | return false; |
Ted Kremenek | cab479f | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 46 | //------------------------------------------------------------------------====// |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 47 | // DeclToIndex: a mapping from Decls we track to value indices. |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 48 | //====------------------------------------------------------------------------// |
| 49 | |
| 50 | namespace { |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 51 | class DeclToIndex { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 52 | llvm::DenseMap<const VarDecl *, unsigned> map; |
| 53 | public: |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 54 | DeclToIndex() {} |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 55 | |
| 56 | /// Compute the actual mapping from declarations to bits. |
| 57 | void computeMap(const DeclContext &dc); |
| 58 | |
| 59 | /// Return the number of declarations in the map. |
| 60 | unsigned size() const { return map.size(); } |
| 61 | |
| 62 | /// Returns the bit vector index for a given declaration. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 63 | Optional<unsigned> getValueIndex(const VarDecl *d) const; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 64 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 65 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 67 | void DeclToIndex::computeMap(const DeclContext &dc) { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 68 | unsigned count = 0; |
| 69 | DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()), |
| 70 | E(dc.decls_end()); |
| 71 | for ( ; I != E; ++I) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 72 | const VarDecl *vd = *I; |
Ted Kremenek | 93a3138 | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 73 | if (isTrackedVar(vd, &dc)) |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 74 | map[vd] = count++; |
| 75 | } |
| 76 | } |
| 77 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 78 | Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const { |
Ted Kremenek | 03325c4 | 2011-03-29 01:40:00 +0000 | [diff] [blame] | 79 | llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 80 | if (I == map.end()) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 81 | return None; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 82 | return I->second; |
| 83 | } |
| 84 | |
| 85 | //------------------------------------------------------------------------====// |
| 86 | // CFGBlockValues: dataflow values for CFG blocks. |
| 87 | //====------------------------------------------------------------------------// |
| 88 | |
Ted Kremenek | c8c4e5f | 2011-03-15 04:57:38 +0000 | [diff] [blame] | 89 | // These values are defined in such a way that a merge can be done using |
| 90 | // a bitwise OR. |
| 91 | enum Value { Unknown = 0x0, /* 00 */ |
| 92 | Initialized = 0x1, /* 01 */ |
| 93 | Uninitialized = 0x2, /* 10 */ |
| 94 | MayUninitialized = 0x3 /* 11 */ }; |
| 95 | |
| 96 | static bool isUninitialized(const Value v) { |
| 97 | return v >= Uninitialized; |
| 98 | } |
| 99 | static bool isAlwaysUninit(const Value v) { |
| 100 | return v == Uninitialized; |
| 101 | } |
Ted Kremenek | d3def38 | 2011-03-15 04:57:29 +0000 | [diff] [blame] | 102 | |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 103 | namespace { |
Ted Kremenek | 9b15c96 | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 104 | |
Benjamin Kramer | 5721daa | 2012-09-28 16:44:29 +0000 | [diff] [blame] | 105 | typedef llvm::PackedVector<Value, 2, llvm::SmallBitVector> ValueVector; |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 107 | class CFGBlockValues { |
| 108 | const CFG &cfg; |
Benjamin Kramer | 5721daa | 2012-09-28 16:44:29 +0000 | [diff] [blame] | 109 | SmallVector<ValueVector, 8> vals; |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 110 | ValueVector scratch; |
Ted Kremenek | e3ae0a4 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 111 | DeclToIndex declToIndex; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 112 | public: |
| 113 | CFGBlockValues(const CFG &cfg); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 115 | unsigned getNumEntries() const { return declToIndex.size(); } |
| 116 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 117 | void computeSetOfDeclarations(const DeclContext &dc); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 118 | ValueVector &getValueVector(const CFGBlock *block) { |
Benjamin Kramer | 5721daa | 2012-09-28 16:44:29 +0000 | [diff] [blame] | 119 | return vals[block->getBlockID()]; |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 120 | } |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 121 | |
Richard Smith | b721e30 | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 122 | void setAllScratchValues(Value V); |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 123 | void mergeIntoScratch(ValueVector const &source, bool isFirst); |
| 124 | bool updateValueVectorWithScratch(const CFGBlock *block); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 125 | |
| 126 | bool hasNoDeclarations() const { |
Ted Kremenek | e3ae0a4 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 127 | return declToIndex.size() == 0; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 128 | } |
Ted Kremenek | 417d566 | 2011-08-20 01:15:28 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 130 | void resetScratch(); |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 132 | ValueVector::reference operator[](const VarDecl *vd); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 133 | |
| 134 | Value getValue(const CFGBlock *block, const CFGBlock *dstBlock, |
| 135 | const VarDecl *vd) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 136 | const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 137 | assert(idx.hasValue()); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 138 | return getValueVector(block)[idx.getValue()]; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 140 | }; |
Benjamin Kramer | 8aef596 | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 141 | } // end anonymous namespace |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 143 | CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {} |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 145 | void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { |
Ted Kremenek | e3ae0a4 | 2011-03-15 05:30:12 +0000 | [diff] [blame] | 146 | declToIndex.computeMap(dc); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 147 | unsigned decls = declToIndex.size(); |
| 148 | scratch.resize(decls); |
| 149 | unsigned n = cfg.getNumBlockIDs(); |
| 150 | if (!n) |
| 151 | return; |
| 152 | vals.resize(n); |
| 153 | for (unsigned i = 0; i < n; ++i) |
Benjamin Kramer | 5721daa | 2012-09-28 16:44:29 +0000 | [diff] [blame] | 154 | vals[i].resize(decls); |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Richard Smith | 130b8d4 | 2012-07-13 23:33:44 +0000 | [diff] [blame] | 157 | #if DEBUG_LOGGING |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 158 | static void printVector(const CFGBlock *block, ValueVector &bv, |
Ted Kremenek | ba35729 | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 159 | unsigned num) { |
Ted Kremenek | ba35729 | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 160 | llvm::errs() << block->getBlockID() << " :"; |
| 161 | for (unsigned i = 0; i < bv.size(); ++i) { |
| 162 | llvm::errs() << ' ' << bv[i]; |
| 163 | } |
| 164 | llvm::errs() << " : " << num << '\n'; |
| 165 | } |
| 166 | #endif |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 167 | |
Richard Smith | b721e30 | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 168 | void CFGBlockValues::setAllScratchValues(Value V) { |
| 169 | for (unsigned I = 0, E = scratch.size(); I != E; ++I) |
| 170 | scratch[I] = V; |
| 171 | } |
| 172 | |
Ted Kremenek | f8fd4d4 | 2011-10-07 00:42:48 +0000 | [diff] [blame] | 173 | void CFGBlockValues::mergeIntoScratch(ValueVector const &source, |
| 174 | bool isFirst) { |
| 175 | if (isFirst) |
| 176 | scratch = source; |
| 177 | else |
| 178 | scratch |= source; |
| 179 | } |
| 180 | |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 181 | bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 182 | ValueVector &dst = getValueVector(block); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 183 | bool changed = (dst != scratch); |
| 184 | if (changed) |
| 185 | dst = scratch; |
Richard Smith | 130b8d4 | 2012-07-13 23:33:44 +0000 | [diff] [blame] | 186 | #if DEBUG_LOGGING |
Ted Kremenek | ba35729 | 2011-02-01 17:43:18 +0000 | [diff] [blame] | 187 | printVector(block, scratch, 0); |
| 188 | #endif |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 189 | return changed; |
| 190 | } |
| 191 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 192 | void CFGBlockValues::resetScratch() { |
| 193 | scratch.reset(); |
| 194 | } |
| 195 | |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 196 | ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 197 | const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 198 | assert(idx.hasValue()); |
| 199 | return scratch[idx.getValue()]; |
| 200 | } |
| 201 | |
| 202 | //------------------------------------------------------------------------====// |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 203 | // Worklist: worklist for dataflow analysis. |
| 204 | //====------------------------------------------------------------------------// |
| 205 | |
| 206 | namespace { |
| 207 | class DataflowWorklist { |
| 208 | PostOrderCFGView::iterator PO_I, PO_E; |
| 209 | SmallVector<const CFGBlock *, 20> worklist; |
| 210 | llvm::BitVector enqueuedBlocks; |
| 211 | public: |
| 212 | DataflowWorklist(const CFG &cfg, PostOrderCFGView &view) |
| 213 | : PO_I(view.begin()), PO_E(view.end()), |
| 214 | enqueuedBlocks(cfg.getNumBlockIDs(), true) { |
| 215 | // Treat the first block as already analyzed. |
| 216 | if (PO_I != PO_E) { |
| 217 | assert(*PO_I == &cfg.getEntry()); |
| 218 | enqueuedBlocks[(*PO_I)->getBlockID()] = false; |
| 219 | ++PO_I; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void enqueueSuccessors(const CFGBlock *block); |
| 224 | const CFGBlock *dequeue(); |
| 225 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 226 | } |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 227 | |
| 228 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
| 229 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 230 | E = block->succ_end(); I != E; ++I) { |
| 231 | const CFGBlock *Successor = *I; |
| 232 | if (!Successor || enqueuedBlocks[Successor->getBlockID()]) |
| 233 | continue; |
| 234 | worklist.push_back(Successor); |
| 235 | enqueuedBlocks[Successor->getBlockID()] = true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | const CFGBlock *DataflowWorklist::dequeue() { |
| 240 | const CFGBlock *B = nullptr; |
| 241 | |
| 242 | // First dequeue from the worklist. This can represent |
| 243 | // updates along backedges that we want propagated as quickly as possible. |
| 244 | if (!worklist.empty()) |
| 245 | B = worklist.pop_back_val(); |
| 246 | |
| 247 | // Next dequeue from the initial reverse post order. This is the |
| 248 | // theoretical ideal in the presence of no back edges. |
| 249 | else if (PO_I != PO_E) { |
| 250 | B = *PO_I; |
| 251 | ++PO_I; |
| 252 | } |
| 253 | else { |
| 254 | return nullptr; |
| 255 | } |
| 256 | |
| 257 | assert(enqueuedBlocks[B->getBlockID()] == true); |
| 258 | enqueuedBlocks[B->getBlockID()] = false; |
| 259 | return B; |
| 260 | } |
| 261 | |
| 262 | //------------------------------------------------------------------------====// |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 263 | // Classification of DeclRefExprs as use or initialization. |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 264 | //====------------------------------------------------------------------------// |
| 265 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 266 | namespace { |
| 267 | class FindVarResult { |
| 268 | const VarDecl *vd; |
| 269 | const DeclRefExpr *dr; |
| 270 | public: |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 271 | FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 272 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 273 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 274 | const VarDecl *getDecl() const { return vd; } |
| 275 | }; |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 276 | |
| 277 | static const Expr *stripCasts(ASTContext &C, const Expr *Ex) { |
| 278 | while (Ex) { |
| 279 | Ex = Ex->IgnoreParenNoopCasts(C); |
| 280 | if (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) { |
| 281 | if (CE->getCastKind() == CK_LValueBitCast) { |
| 282 | Ex = CE->getSubExpr(); |
| 283 | continue; |
| 284 | } |
| 285 | } |
| 286 | break; |
| 287 | } |
| 288 | return Ex; |
| 289 | } |
| 290 | |
| 291 | /// If E is an expression comprising a reference to a single variable, find that |
| 292 | /// variable. |
| 293 | static FindVarResult findVar(const Expr *E, const DeclContext *DC) { |
| 294 | if (const DeclRefExpr *DRE = |
| 295 | dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E))) |
| 296 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 297 | if (isTrackedVar(VD, DC)) |
| 298 | return FindVarResult(VD, DRE); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 299 | return FindVarResult(nullptr, nullptr); |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /// \brief Classify each DeclRefExpr as an initialization or a use. Any |
| 303 | /// DeclRefExpr which isn't explicitly classified will be assumed to have |
| 304 | /// escaped the analysis and will be treated as an initialization. |
| 305 | class ClassifyRefs : public StmtVisitor<ClassifyRefs> { |
| 306 | public: |
| 307 | enum Class { |
| 308 | Init, |
| 309 | Use, |
| 310 | SelfInit, |
| 311 | Ignore |
| 312 | }; |
| 313 | |
| 314 | private: |
| 315 | const DeclContext *DC; |
| 316 | llvm::DenseMap<const DeclRefExpr*, Class> Classification; |
| 317 | |
| 318 | bool isTrackedVar(const VarDecl *VD) const { |
| 319 | return ::isTrackedVar(VD, DC); |
| 320 | } |
| 321 | |
| 322 | void classify(const Expr *E, Class C); |
| 323 | |
| 324 | public: |
| 325 | ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {} |
| 326 | |
| 327 | void VisitDeclStmt(DeclStmt *DS); |
| 328 | void VisitUnaryOperator(UnaryOperator *UO); |
| 329 | void VisitBinaryOperator(BinaryOperator *BO); |
| 330 | void VisitCallExpr(CallExpr *CE); |
| 331 | void VisitCastExpr(CastExpr *CE); |
| 332 | |
| 333 | void operator()(Stmt *S) { Visit(S); } |
| 334 | |
| 335 | Class get(const DeclRefExpr *DRE) const { |
| 336 | llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I |
| 337 | = Classification.find(DRE); |
| 338 | if (I != Classification.end()) |
| 339 | return I->second; |
| 340 | |
| 341 | const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 342 | if (!VD || !isTrackedVar(VD)) |
| 343 | return Ignore; |
| 344 | |
| 345 | return Init; |
| 346 | } |
| 347 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 348 | } |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 349 | |
| 350 | static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) { |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 351 | if (VD->getType()->isRecordType()) return nullptr; |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 352 | if (Expr *Init = VD->getInit()) { |
| 353 | const DeclRefExpr *DRE |
| 354 | = dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init)); |
| 355 | if (DRE && DRE->getDecl() == VD) |
| 356 | return DRE; |
| 357 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 358 | return nullptr; |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void ClassifyRefs::classify(const Expr *E, Class C) { |
Ted Kremenek | 7ba78c6 | 2013-01-19 00:25:06 +0000 | [diff] [blame] | 362 | // The result of a ?: could also be an lvalue. |
| 363 | E = E->IgnoreParens(); |
| 364 | if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 365 | classify(CO->getTrueExpr(), C); |
Ted Kremenek | 7ba78c6 | 2013-01-19 00:25:06 +0000 | [diff] [blame] | 366 | classify(CO->getFalseExpr(), C); |
| 367 | return; |
| 368 | } |
| 369 | |
Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 370 | if (const BinaryConditionalOperator *BCO = |
| 371 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 372 | classify(BCO->getFalseExpr(), C); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) { |
| 377 | classify(OVE->getSourceExpr(), C); |
| 378 | return; |
| 379 | } |
| 380 | |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 381 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 382 | if (VarDecl *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { |
| 383 | if (!VD->isStaticDataMember()) |
| 384 | classify(ME->getBase(), C); |
| 385 | } |
Richard Trieu | abf6ec4 | 2014-08-27 22:15:10 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 389 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 390 | switch (BO->getOpcode()) { |
| 391 | case BO_PtrMemD: |
| 392 | case BO_PtrMemI: |
| 393 | classify(BO->getLHS(), C); |
| 394 | return; |
| 395 | case BO_Comma: |
| 396 | classify(BO->getRHS(), C); |
| 397 | return; |
| 398 | default: |
| 399 | return; |
| 400 | } |
| 401 | } |
| 402 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 403 | FindVarResult Var = findVar(E, DC); |
| 404 | if (const DeclRefExpr *DRE = Var.getDeclRefExpr()) |
| 405 | Classification[DRE] = std::max(Classification[DRE], C); |
| 406 | } |
| 407 | |
| 408 | void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 409 | for (auto *DI : DS->decls()) { |
| 410 | VarDecl *VD = dyn_cast<VarDecl>(DI); |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 411 | if (VD && isTrackedVar(VD)) |
| 412 | if (const DeclRefExpr *DRE = getSelfInitExpr(VD)) |
| 413 | Classification[DRE] = SelfInit; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) { |
| 418 | // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this |
| 419 | // is not a compound-assignment, we will treat it as initializing the variable |
| 420 | // when TransferFunctions visits it. A compound-assignment does not affect |
| 421 | // whether a variable is uninitialized, and there's no point counting it as a |
| 422 | // use. |
Richard Smith | b21dd02 | 2012-07-17 01:27:33 +0000 | [diff] [blame] | 423 | if (BO->isCompoundAssignmentOp()) |
| 424 | classify(BO->getLHS(), Use); |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 425 | else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma) |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 426 | classify(BO->getLHS(), Ignore); |
| 427 | } |
| 428 | |
| 429 | void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) { |
| 430 | // Increment and decrement are uses despite there being no lvalue-to-rvalue |
| 431 | // conversion. |
| 432 | if (UO->isIncrementDecrementOp()) |
| 433 | classify(UO->getSubExpr(), Use); |
| 434 | } |
| 435 | |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 436 | static bool isPointerToConst(const QualType &QT) { |
| 437 | return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified(); |
| 438 | } |
| 439 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 440 | void ClassifyRefs::VisitCallExpr(CallExpr *CE) { |
Richard Trieu | 11fd079 | 2014-08-26 04:30:55 +0000 | [diff] [blame] | 441 | // Classify arguments to std::move as used. |
| 442 | if (CE->getNumArgs() == 1) { |
| 443 | if (FunctionDecl *FD = CE->getDirectCallee()) { |
Richard Trieu | c321b93 | 2014-11-27 01:29:32 +0000 | [diff] [blame] | 444 | if (FD->isInStdNamespace() && FD->getIdentifier() && |
| 445 | FD->getIdentifier()->isStr("move")) { |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 446 | // RecordTypes are handled in SemaDeclCXX.cpp. |
| 447 | if (!CE->getArg(0)->getType()->isRecordType()) |
| 448 | classify(CE->getArg(0), Use); |
Richard Trieu | 11fd079 | 2014-08-26 04:30:55 +0000 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 454 | // If a value is passed by const pointer or by const reference to a function, |
| 455 | // we should not assume that it is initialized by the call, and we |
| 456 | // conservatively do not assume that it is used. |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 457 | for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); |
Manuel Klimek | 27ee25f | 2015-03-03 14:54:25 +0000 | [diff] [blame] | 458 | I != E; ++I) { |
| 459 | if ((*I)->isGLValue()) { |
| 460 | if ((*I)->getType().isConstQualified()) |
| 461 | classify((*I), Ignore); |
| 462 | } else if (isPointerToConst((*I)->getType())) { |
| 463 | const Expr *Ex = stripCasts(DC->getParentASTContext(), *I); |
| 464 | const UnaryOperator *UO = dyn_cast<UnaryOperator>(Ex); |
| 465 | if (UO && UO->getOpcode() == UO_AddrOf) |
| 466 | Ex = UO->getSubExpr(); |
| 467 | classify(Ex, Ignore); |
| 468 | } |
| 469 | } |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void ClassifyRefs::VisitCastExpr(CastExpr *CE) { |
| 473 | if (CE->getCastKind() == CK_LValueToRValue) |
| 474 | classify(CE->getSubExpr(), Use); |
| 475 | else if (CStyleCastExpr *CSE = dyn_cast<CStyleCastExpr>(CE)) { |
| 476 | if (CSE->getType()->isVoidType()) { |
| 477 | // Squelch any detected load of an uninitialized value if |
| 478 | // we cast it to void. |
| 479 | // e.g. (void) x; |
| 480 | classify(CSE->getSubExpr(), Ignore); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | //------------------------------------------------------------------------====// |
| 486 | // Transfer function for uninitialized values analysis. |
| 487 | //====------------------------------------------------------------------------// |
| 488 | |
| 489 | namespace { |
Ted Kremenek | 9e100ea | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 490 | class TransferFunctions : public StmtVisitor<TransferFunctions> { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 491 | CFGBlockValues &vals; |
| 492 | const CFG &cfg; |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 493 | const CFGBlock *block; |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 494 | AnalysisDeclContext ∾ |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 495 | const ClassifyRefs &classification; |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 496 | ObjCNoReturn objCNoRet; |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 497 | UninitVariablesHandler &handler; |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 499 | public: |
| 500 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 501 | const CFGBlock *block, AnalysisDeclContext &ac, |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 502 | const ClassifyRefs &classification, |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 503 | UninitVariablesHandler &handler) |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 504 | : vals(vals), cfg(cfg), block(block), ac(ac), |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 505 | classification(classification), objCNoRet(ac.getASTContext()), |
| 506 | handler(handler) {} |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 507 | |
Richard Smith | 3d31e8b | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 508 | void reportUse(const Expr *ex, const VarDecl *vd); |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 509 | |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 510 | void VisitBinaryOperator(BinaryOperator *bo); |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 511 | void VisitBlockExpr(BlockExpr *be); |
Richard Smith | b721e30 | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 512 | void VisitCallExpr(CallExpr *ce); |
Ted Kremenek | b63931e | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 513 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 514 | void VisitDeclStmt(DeclStmt *ds); |
| 515 | void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS); |
| 516 | void VisitObjCMessageExpr(ObjCMessageExpr *ME); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | 93a3138 | 2011-01-27 02:29:34 +0000 | [diff] [blame] | 518 | bool isTrackedVar(const VarDecl *vd) { |
| 519 | return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); |
| 520 | } |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 521 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 522 | FindVarResult findVar(const Expr *ex) { |
| 523 | return ::findVar(ex, cast<DeclContext>(ac.getDecl())); |
| 524 | } |
| 525 | |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 526 | UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) { |
| 527 | UninitUse Use(ex, isAlwaysUninit(v)); |
| 528 | |
| 529 | assert(isUninitialized(v)); |
| 530 | if (Use.getKind() == UninitUse::Always) |
| 531 | return Use; |
| 532 | |
| 533 | // If an edge which leads unconditionally to this use did not initialize |
| 534 | // the variable, we can say something stronger than 'may be uninitialized': |
| 535 | // we can say 'either it's used uninitialized or you have dead code'. |
| 536 | // |
| 537 | // We track the number of successors of a node which have been visited, and |
| 538 | // visit a node once we have visited all of its successors. Only edges where |
| 539 | // the variable might still be uninitialized are followed. Since a variable |
| 540 | // can't transfer from being initialized to being uninitialized, this will |
| 541 | // trace out the subgraph which inevitably leads to the use and does not |
| 542 | // initialize the variable. We do not want to skip past loops, since their |
| 543 | // non-termination might be correlated with the initialization condition. |
| 544 | // |
| 545 | // For example: |
| 546 | // |
| 547 | // void f(bool a, bool b) { |
| 548 | // block1: int n; |
| 549 | // if (a) { |
| 550 | // block2: if (b) |
| 551 | // block3: n = 1; |
| 552 | // block4: } else if (b) { |
| 553 | // block5: while (!a) { |
| 554 | // block6: do_work(&a); |
| 555 | // n = 2; |
| 556 | // } |
| 557 | // } |
| 558 | // block7: if (a) |
| 559 | // block8: g(); |
| 560 | // block9: return n; |
| 561 | // } |
| 562 | // |
| 563 | // Starting from the maybe-uninitialized use in block 9: |
| 564 | // * Block 7 is not visited because we have only visited one of its two |
| 565 | // successors. |
| 566 | // * Block 8 is visited because we've visited its only successor. |
| 567 | // From block 8: |
| 568 | // * Block 7 is visited because we've now visited both of its successors. |
| 569 | // From block 7: |
| 570 | // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all |
| 571 | // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively). |
| 572 | // * Block 3 is not visited because it initializes 'n'. |
| 573 | // Now the algorithm terminates, having visited blocks 7 and 8, and having |
| 574 | // found the frontier is blocks 2, 4, and 5. |
| 575 | // |
| 576 | // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2 |
| 577 | // and 4), so we report that any time either of those edges is taken (in |
| 578 | // each case when 'b == false'), 'n' is used uninitialized. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 579 | SmallVector<const CFGBlock*, 32> Queue; |
| 580 | SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0); |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 581 | Queue.push_back(block); |
| 582 | // Specify that we've already visited all successors of the starting block. |
| 583 | // This has the dual purpose of ensuring we never add it to the queue, and |
| 584 | // of marking it as not being a candidate element of the frontier. |
| 585 | SuccsVisited[block->getBlockID()] = block->succ_size(); |
| 586 | while (!Queue.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 587 | const CFGBlock *B = Queue.pop_back_val(); |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 588 | |
| 589 | // If the use is always reached from the entry block, make a note of that. |
| 590 | if (B == &cfg.getEntry()) |
| 591 | Use.setUninitAfterCall(); |
| 592 | |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 593 | for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); |
| 594 | I != E; ++I) { |
| 595 | const CFGBlock *Pred = *I; |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 596 | if (!Pred) |
| 597 | continue; |
| 598 | |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 599 | Value AtPredExit = vals.getValue(Pred, B, vd); |
| 600 | if (AtPredExit == Initialized) |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 601 | // This block initializes the variable. |
| 602 | continue; |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 603 | if (AtPredExit == MayUninitialized && |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 604 | vals.getValue(B, nullptr, vd) == Uninitialized) { |
Richard Smith | ba8071e | 2013-09-12 18:49:10 +0000 | [diff] [blame] | 605 | // This block declares the variable (uninitialized), and is reachable |
| 606 | // from a block that initializes the variable. We can't guarantee to |
| 607 | // give an earlier location for the diagnostic (and it appears that |
| 608 | // this code is intended to be reachable) so give a diagnostic here |
| 609 | // and go no further down this path. |
| 610 | Use.setUninitAfterDecl(); |
| 611 | continue; |
| 612 | } |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 613 | |
Richard Smith | 130b8d4 | 2012-07-13 23:33:44 +0000 | [diff] [blame] | 614 | unsigned &SV = SuccsVisited[Pred->getBlockID()]; |
| 615 | if (!SV) { |
| 616 | // When visiting the first successor of a block, mark all NULL |
| 617 | // successors as having been visited. |
| 618 | for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(), |
| 619 | SE = Pred->succ_end(); |
| 620 | SI != SE; ++SI) |
| 621 | if (!*SI) |
| 622 | ++SV; |
| 623 | } |
| 624 | |
| 625 | if (++SV == Pred->succ_size()) |
Richard Smith | 4323bf8 | 2012-05-25 02:17:09 +0000 | [diff] [blame] | 626 | // All paths from this block lead to the use and don't initialize the |
| 627 | // variable. |
| 628 | Queue.push_back(Pred); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // Scan the frontier, looking for blocks where the variable was |
| 633 | // uninitialized. |
| 634 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
| 635 | const CFGBlock *Block = *BI; |
| 636 | unsigned BlockID = Block->getBlockID(); |
| 637 | const Stmt *Term = Block->getTerminator(); |
| 638 | if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() && |
| 639 | Term) { |
| 640 | // This block inevitably leads to the use. If we have an edge from here |
| 641 | // to a post-dominator block, and the variable is uninitialized on that |
| 642 | // edge, we have found a bug. |
| 643 | for (CFGBlock::const_succ_iterator I = Block->succ_begin(), |
| 644 | E = Block->succ_end(); I != E; ++I) { |
| 645 | const CFGBlock *Succ = *I; |
| 646 | if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() && |
| 647 | vals.getValue(Block, Succ, vd) == Uninitialized) { |
| 648 | // Switch cases are a special case: report the label to the caller |
| 649 | // as the 'terminator', not the switch statement itself. Suppress |
| 650 | // situations where no label matched: we can't be sure that's |
| 651 | // possible. |
| 652 | if (isa<SwitchStmt>(Term)) { |
| 653 | const Stmt *Label = Succ->getLabel(); |
| 654 | if (!Label || !isa<SwitchCase>(Label)) |
| 655 | // Might not be possible. |
| 656 | continue; |
| 657 | UninitUse::Branch Branch; |
| 658 | Branch.Terminator = Label; |
| 659 | Branch.Output = 0; // Ignored. |
| 660 | Use.addUninitBranch(Branch); |
| 661 | } else { |
| 662 | UninitUse::Branch Branch; |
| 663 | Branch.Terminator = Term; |
| 664 | Branch.Output = I - Block->succ_begin(); |
| 665 | Use.addUninitBranch(Branch); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return Use; |
| 673 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 674 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 675 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 676 | |
Richard Smith | 3d31e8b | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 677 | void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) { |
Richard Smith | 3d31e8b | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 678 | Value v = vals[vd]; |
| 679 | if (isUninitialized(v)) |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 680 | handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 683 | void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) { |
Ted Kremenek | 4058d87 | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 684 | // This represents an initialization of the 'element' value. |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 685 | if (DeclStmt *DS = dyn_cast<DeclStmt>(FS->getElement())) { |
| 686 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 687 | if (isTrackedVar(VD)) |
| 688 | vals[VD] = Initialized; |
Ted Kremenek | 4058d87 | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 689 | } |
Ted Kremenek | 4058d87 | 2011-01-27 02:01:31 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 692 | void TransferFunctions::VisitBlockExpr(BlockExpr *be) { |
Ted Kremenek | 7736176 | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 693 | const BlockDecl *bd = be->getBlockDecl(); |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 694 | for (const auto &I : bd->captures()) { |
| 695 | const VarDecl *vd = I.getVariable(); |
Ted Kremenek | 7736176 | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 696 | if (!isTrackedVar(vd)) |
| 697 | continue; |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 698 | if (I.isByRef()) { |
Ted Kremenek | 7736176 | 2011-03-31 22:32:41 +0000 | [diff] [blame] | 699 | vals[vd] = Initialized; |
| 700 | continue; |
| 701 | } |
Richard Smith | 3d31e8b | 2012-05-24 23:45:35 +0000 | [diff] [blame] | 702 | reportUse(be, vd); |
Ted Kremenek | bcf848f | 2011-01-25 19:13:48 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Richard Smith | b721e30 | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 706 | void TransferFunctions::VisitCallExpr(CallExpr *ce) { |
Ted Kremenek | 7979ccf | 2012-09-12 05:53:43 +0000 | [diff] [blame] | 707 | if (Decl *Callee = ce->getCalleeDecl()) { |
| 708 | if (Callee->hasAttr<ReturnsTwiceAttr>()) { |
| 709 | // After a call to a function like setjmp or vfork, any variable which is |
| 710 | // initialized anywhere within this function may now be initialized. For |
| 711 | // now, just assume such a call initializes all variables. FIXME: Only |
| 712 | // mark variables as initialized if they have an initializer which is |
| 713 | // reachable from here. |
| 714 | vals.setAllScratchValues(Initialized); |
| 715 | } |
| 716 | else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) { |
| 717 | // Functions labeled like "analyzer_noreturn" are often used to denote |
| 718 | // "panic" functions that in special debug situations can still return, |
| 719 | // but for the most part should not be treated as returning. This is a |
| 720 | // useful annotation borrowed from the static analyzer that is useful for |
| 721 | // suppressing branch-specific false positives when we call one of these |
| 722 | // functions but keep pretending the path continues (when in reality the |
| 723 | // user doesn't care). |
| 724 | vals.setAllScratchValues(Unknown); |
| 725 | } |
| 726 | } |
Richard Smith | b721e30 | 2012-07-02 23:23:04 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Ted Kremenek | 9e100ea | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 729 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 730 | switch (classification.get(dr)) { |
| 731 | case ClassifyRefs::Ignore: |
| 732 | break; |
| 733 | case ClassifyRefs::Use: |
| 734 | reportUse(dr, cast<VarDecl>(dr->getDecl())); |
| 735 | break; |
| 736 | case ClassifyRefs::Init: |
| 737 | vals[cast<VarDecl>(dr->getDecl())] = Initialized; |
| 738 | break; |
| 739 | case ClassifyRefs::SelfInit: |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 740 | handler.handleSelfInit(cast<VarDecl>(dr->getDecl())); |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 741 | break; |
| 742 | } |
Ted Kremenek | 9e100ea | 2011-07-19 14:18:48 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 745 | void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) { |
| 746 | if (BO->getOpcode() == BO_Assign) { |
| 747 | FindVarResult Var = findVar(BO->getLHS()); |
| 748 | if (const VarDecl *VD = Var.getDecl()) |
| 749 | vals[VD] = Initialized; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 754 | for (auto *DI : DS->decls()) { |
| 755 | VarDecl *VD = dyn_cast<VarDecl>(DI); |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 756 | if (VD && isTrackedVar(VD)) { |
| 757 | if (getSelfInitExpr(VD)) { |
| 758 | // If the initializer consists solely of a reference to itself, we |
| 759 | // explicitly mark the variable as uninitialized. This allows code |
| 760 | // like the following: |
| 761 | // |
| 762 | // int x = x; |
| 763 | // |
| 764 | // to deliberately leave a variable uninitialized. Different analysis |
| 765 | // clients can detect this pattern and adjust their reporting |
| 766 | // appropriately, but we need to continue to analyze subsequent uses |
| 767 | // of the variable. |
| 768 | vals[VD] = Uninitialized; |
| 769 | } else if (VD->getInit()) { |
| 770 | // Treat the new variable as initialized. |
| 771 | vals[VD] = Initialized; |
| 772 | } else { |
| 773 | // No initializer: the variable is now uninitialized. This matters |
| 774 | // for cases like: |
| 775 | // while (...) { |
| 776 | // int n; |
| 777 | // use(n); |
| 778 | // n = 0; |
| 779 | // } |
| 780 | // FIXME: Mark the variable as uninitialized whenever its scope is |
| 781 | // left, since its scope could be re-entered by a jump over the |
| 782 | // declaration. |
| 783 | vals[VD] = Uninitialized; |
Ted Kremenek | b63931e | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 784 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
Ted Kremenek | edf22ed | 2012-09-13 00:21:35 +0000 | [diff] [blame] | 789 | void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) { |
| 790 | // If the Objective-C message expression is an implicit no-return that |
| 791 | // is not modeled in the CFG, set the tracked dataflow values to Unknown. |
| 792 | if (objCNoRet.isImplicitNoReturn(ME)) { |
| 793 | vals.setAllScratchValues(Unknown); |
| 794 | } |
| 795 | } |
| 796 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 797 | //------------------------------------------------------------------------====// |
| 798 | // High-level "driver" logic for uninitialized values analysis. |
| 799 | //====------------------------------------------------------------------------// |
| 800 | |
Ted Kremenek | b82ddd6 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 801 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 802 | AnalysisDeclContext &ac, CFGBlockValues &vals, |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 803 | const ClassifyRefs &classification, |
Ted Kremenek | 352a708 | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 804 | llvm::BitVector &wasAnalyzed, |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 805 | UninitVariablesHandler &handler) { |
Ted Kremenek | 352a708 | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 806 | wasAnalyzed[block->getBlockID()] = true; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 807 | vals.resetScratch(); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 808 | // Merge in values of predecessor blocks. |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 809 | bool isFirst = true; |
| 810 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 811 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | aed4677 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 812 | const CFGBlock *pred = *I; |
Ted Kremenek | 4b6fee6 | 2014-02-27 00:24:00 +0000 | [diff] [blame] | 813 | if (!pred) |
| 814 | continue; |
Ted Kremenek | aed4677 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 815 | if (wasAnalyzed[pred->getBlockID()]) { |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 816 | vals.mergeIntoScratch(vals.getValueVector(pred), isFirst); |
Ted Kremenek | aed4677 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 817 | isFirst = false; |
| 818 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 819 | } |
| 820 | // Apply the transfer function. |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 821 | TransferFunctions tf(vals, cfg, block, ac, classification, handler); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 822 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 823 | I != E; ++I) { |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 824 | if (Optional<CFGStmt> cs = I->getAs<CFGStmt>()) |
| 825 | tf.Visit(const_cast<Stmt*>(cs->getStmt())); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 826 | } |
Ted Kremenek | a895fe9 | 2011-03-15 04:57:27 +0000 | [diff] [blame] | 827 | return vals.updateValueVectorWithScratch(block); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 830 | /// PruneBlocksHandler is a special UninitVariablesHandler that is used |
| 831 | /// to detect when a CFGBlock has any *potential* use of an uninitialized |
| 832 | /// variable. It is mainly used to prune out work during the final |
| 833 | /// reporting pass. |
| 834 | namespace { |
| 835 | struct PruneBlocksHandler : public UninitVariablesHandler { |
| 836 | PruneBlocksHandler(unsigned numBlocks) |
| 837 | : hadUse(numBlocks, false), hadAnyUse(false), |
| 838 | currentBlock(0) {} |
| 839 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 840 | ~PruneBlocksHandler() override {} |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 841 | |
| 842 | /// Records if a CFGBlock had a potential use of an uninitialized variable. |
| 843 | llvm::BitVector hadUse; |
| 844 | |
| 845 | /// Records if any CFGBlock had a potential use of an uninitialized variable. |
| 846 | bool hadAnyUse; |
| 847 | |
| 848 | /// The current block to scribble use information. |
| 849 | unsigned currentBlock; |
| 850 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 851 | void handleUseOfUninitVariable(const VarDecl *vd, |
| 852 | const UninitUse &use) override { |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 853 | hadUse[currentBlock] = true; |
| 854 | hadAnyUse = true; |
| 855 | } |
| 856 | |
| 857 | /// Called when the uninitialized variable analysis detects the |
| 858 | /// idiom 'int x = x'. All other uses of 'x' within the initializer |
| 859 | /// are handled by handleUseOfUninitVariable. |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 860 | void handleSelfInit(const VarDecl *vd) override { |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 861 | hadUse[currentBlock] = true; |
| 862 | hadAnyUse = true; |
| 863 | } |
| 864 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 865 | } |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 866 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 867 | void clang::runUninitializedVariablesAnalysis( |
| 868 | const DeclContext &dc, |
| 869 | const CFG &cfg, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 870 | AnalysisDeclContext &ac, |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 871 | UninitVariablesHandler &handler, |
| 872 | UninitVariablesAnalysisStats &stats) { |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 873 | CFGBlockValues vals(cfg); |
| 874 | vals.computeSetOfDeclarations(dc); |
| 875 | if (vals.hasNoDeclarations()) |
| 876 | return; |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 877 | |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 878 | stats.NumVariablesAnalyzed = vals.getNumEntries(); |
| 879 | |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 880 | // Precompute which expressions are uses and which are initializations. |
| 881 | ClassifyRefs classification(ac); |
| 882 | cfg.VisitBlockStmts(classification); |
| 883 | |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 884 | // Mark all variables uninitialized at the entry. |
| 885 | const CFGBlock &entry = cfg.getEntry(); |
Ted Kremenek | 6080d32 | 2012-07-19 04:59:05 +0000 | [diff] [blame] | 886 | ValueVector &vec = vals.getValueVector(&entry); |
| 887 | const unsigned n = vals.getNumEntries(); |
| 888 | for (unsigned j = 0; j < n ; ++j) { |
| 889 | vec[j] = Uninitialized; |
Ted Kremenek | 3788193 | 2011-04-04 23:29:12 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | // Proceed with the workist. |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 893 | DataflowWorklist worklist(cfg, *ac.getAnalysis<PostOrderCFGView>()); |
Ted Kremenek | 9b15c96 | 2011-03-15 04:57:32 +0000 | [diff] [blame] | 894 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 895 | worklist.enqueueSuccessors(&cfg.getEntry()); |
Ted Kremenek | 352a708 | 2011-04-04 20:30:58 +0000 | [diff] [blame] | 896 | llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); |
Ted Kremenek | aed4677 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 897 | wasAnalyzed[cfg.getEntry().getBlockID()] = true; |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 898 | PruneBlocksHandler PBH(cfg.getNumBlockIDs()); |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 899 | |
| 900 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 901 | PBH.currentBlock = block->getBlockID(); |
| 902 | |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 903 | // Did the block change? |
Richard Smith | 6376d1f | 2012-07-17 00:06:14 +0000 | [diff] [blame] | 904 | bool changed = runOnBlock(block, cfg, ac, vals, |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 905 | classification, wasAnalyzed, PBH); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 906 | ++stats.NumBlockVisits; |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 907 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 908 | worklist.enqueueSuccessors(block); |
| 909 | previouslyVisited[block->getBlockID()] = true; |
| 910 | } |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 911 | |
| 912 | if (!PBH.hadAnyUse) |
| 913 | return; |
| 914 | |
Enea Zaffanella | 392291f | 2013-01-11 11:37:08 +0000 | [diff] [blame] | 915 | // Run through the blocks one more time, and report uninitialized variables. |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 916 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
Ted Kremenek | aed4677 | 2011-09-02 19:39:26 +0000 | [diff] [blame] | 917 | const CFGBlock *block = *BI; |
Ted Kremenek | 778a6ed | 2012-11-17 07:18:30 +0000 | [diff] [blame] | 918 | if (PBH.hadUse[block->getBlockID()]) { |
| 919 | runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler); |
Chandler Carruth | b4836ea | 2011-07-06 16:21:37 +0000 | [diff] [blame] | 920 | ++stats.NumBlockVisits; |
| 921 | } |
Ted Kremenek | b749a6d | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 925 | UninitVariablesHandler::~UninitVariablesHandler() {} |