Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 1 | //==- UninitializedValuesV2.cpp - Find Uninitialized Values -----*- C++ --*-==// |
| 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" |
| 17 | #include "llvm/ADT/BitVector.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/Analysis/CFG.h" |
| 21 | #include "clang/Analysis/Visitors/CFGRecStmtDeclVisitor.h" |
| 22 | #include "clang/Analysis/Analyses/UninitializedValuesV2.h" |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 27 | static bool isTrackedVar(const VarDecl *vd) { |
| 28 | return vd->isLocalVarDecl() && !vd->hasGlobalStorage() && |
| 29 | vd->getType()->isScalarType(); |
| 30 | } |
| 31 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 32 | //------------------------------------------------------------------------====// |
| 33 | // DeclToBit: a mapping from Decls we track to bitvector indices. |
| 34 | //====------------------------------------------------------------------------// |
| 35 | |
| 36 | namespace { |
| 37 | class DeclToBit { |
| 38 | llvm::DenseMap<const VarDecl *, unsigned> map; |
| 39 | public: |
| 40 | DeclToBit() {} |
| 41 | |
| 42 | /// Compute the actual mapping from declarations to bits. |
| 43 | void computeMap(const DeclContext &dc); |
| 44 | |
| 45 | /// Return the number of declarations in the map. |
| 46 | unsigned size() const { return map.size(); } |
| 47 | |
| 48 | /// Returns the bit vector index for a given declaration. |
| 49 | llvm::Optional<unsigned> getBitVectorIndex(const VarDecl *d); |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | void DeclToBit::computeMap(const DeclContext &dc) { |
| 54 | unsigned count = 0; |
| 55 | DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()), |
| 56 | E(dc.decls_end()); |
| 57 | for ( ; I != E; ++I) { |
| 58 | const VarDecl *vd = *I; |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 59 | if (isTrackedVar(vd)) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 60 | map[vd] = count++; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | llvm::Optional<unsigned> DeclToBit::getBitVectorIndex(const VarDecl *d) { |
| 65 | llvm::DenseMap<const VarDecl *, unsigned>::iterator I = map.find(d); |
| 66 | if (I == map.end()) |
| 67 | return llvm::Optional<unsigned>(); |
| 68 | return I->second; |
| 69 | } |
| 70 | |
| 71 | //------------------------------------------------------------------------====// |
| 72 | // CFGBlockValues: dataflow values for CFG blocks. |
| 73 | //====------------------------------------------------------------------------// |
| 74 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 75 | typedef std::pair<llvm::BitVector *, llvm::BitVector *> BVPair; |
| 76 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 77 | namespace { |
| 78 | class CFGBlockValues { |
| 79 | const CFG &cfg; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 80 | BVPair *vals; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 81 | llvm::BitVector scratch; |
| 82 | DeclToBit declToBit; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 83 | |
| 84 | llvm::BitVector &lazyCreate(llvm::BitVector *&bv); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 85 | public: |
| 86 | CFGBlockValues(const CFG &cfg); |
| 87 | ~CFGBlockValues(); |
| 88 | |
| 89 | void computeSetOfDeclarations(const DeclContext &dc); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 90 | llvm::BitVector &getBitVector(const CFGBlock *block, |
| 91 | const CFGBlock *dstBlock); |
| 92 | |
| 93 | BVPair &getBitVectors(const CFGBlock *block); |
| 94 | |
| 95 | BVPair getPredBitVectors(const CFGBlock *block); |
| 96 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 97 | void mergeIntoScratch(llvm::BitVector const &source, bool isFirst); |
| 98 | bool updateBitVectorWithScratch(const CFGBlock *block); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 99 | bool updateBitVectors(const CFGBlock *block, const BVPair &newVals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 100 | |
| 101 | bool hasNoDeclarations() const { |
| 102 | return declToBit.size() == 0; |
| 103 | } |
| 104 | |
| 105 | void resetScratch(); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 106 | llvm::BitVector &getScratch() { return scratch; } |
| 107 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 108 | llvm::BitVector::reference operator[](const VarDecl *vd); |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) { |
| 113 | unsigned n = cfg.getNumBlockIDs(); |
| 114 | if (!n) |
| 115 | return; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 116 | vals = new std::pair<llvm::BitVector*, llvm::BitVector*>[n]; |
Francois Pichet | 2d78c37 | 2011-01-15 13:27:47 +0000 | [diff] [blame] | 117 | memset(vals, 0, sizeof(*vals) * n); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | CFGBlockValues::~CFGBlockValues() { |
| 121 | unsigned n = cfg.getNumBlockIDs(); |
| 122 | if (n == 0) |
| 123 | return; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 124 | for (unsigned i = 0; i < n; ++i) { |
| 125 | delete vals[i].first; |
| 126 | delete vals[i].second; |
| 127 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 128 | delete [] vals; |
| 129 | } |
| 130 | |
| 131 | void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { |
| 132 | declToBit.computeMap(dc); |
| 133 | scratch.resize(declToBit.size()); |
| 134 | } |
| 135 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 136 | llvm::BitVector &CFGBlockValues::lazyCreate(llvm::BitVector *&bv) { |
| 137 | if (!bv) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 138 | bv = new llvm::BitVector(declToBit.size()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 139 | return *bv; |
| 140 | } |
| 141 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 142 | /// This function pattern matches for a '&&' or '||' that appears at |
| 143 | /// the beginning of a CFGBlock that also (1) has a terminator and |
| 144 | /// (2) has no other elements. If such an expression is found, it is returned. |
| 145 | static BinaryOperator *getLogicalOperatorInChain(const CFGBlock *block) { |
| 146 | if (block->empty()) |
| 147 | return 0; |
| 148 | |
| 149 | CFGStmt cstmt = block->front().getAs<CFGStmt>(); |
| 150 | BinaryOperator *b = llvm::dyn_cast_or_null<BinaryOperator>(cstmt.getStmt()); |
| 151 | if (!b || !b->isLogicalOp() || block->getTerminatorCondition() != b) |
| 152 | return 0; |
| 153 | return b; |
| 154 | } |
| 155 | |
| 156 | llvm::BitVector &CFGBlockValues::getBitVector(const CFGBlock *block, |
| 157 | const CFGBlock *dstBlock) { |
| 158 | unsigned idx = block->getBlockID(); |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 159 | if (dstBlock && block->succ_size() == 2 && block->pred_size() == 2) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 160 | assert(block->getTerminator()); |
| 161 | if (getLogicalOperatorInChain(block)) { |
| 162 | if (*block->succ_begin() == dstBlock) |
| 163 | return lazyCreate(vals[idx].first); |
| 164 | assert(*(block->succ_begin()+1) == dstBlock); |
| 165 | return lazyCreate(vals[idx].second); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | assert(vals[idx].second == 0); |
| 170 | return lazyCreate(vals[idx].first); |
| 171 | } |
| 172 | |
| 173 | BVPair &CFGBlockValues::getBitVectors(const clang::CFGBlock *block) { |
| 174 | unsigned idx = block->getBlockID(); |
| 175 | lazyCreate(vals[idx].first); |
| 176 | lazyCreate(vals[idx].second); |
| 177 | return vals[idx]; |
| 178 | } |
| 179 | |
| 180 | BVPair CFGBlockValues::getPredBitVectors(const clang::CFGBlock *block) { |
| 181 | assert(block->pred_size() == 2); |
| 182 | CFGBlock::const_pred_iterator itr = block->pred_begin(); |
| 183 | llvm::BitVector &bvA = getBitVector(*itr, block); |
| 184 | ++itr; |
| 185 | return BVPair(&bvA, &getBitVector(*itr, block)); |
| 186 | } |
| 187 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 188 | void CFGBlockValues::mergeIntoScratch(llvm::BitVector const &source, |
| 189 | bool isFirst) { |
| 190 | if (isFirst) |
| 191 | scratch = source; |
| 192 | else |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 193 | scratch |= source; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool CFGBlockValues::updateBitVectorWithScratch(const CFGBlock *block) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 197 | llvm::BitVector &dst = getBitVector(block, 0); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 198 | bool changed = (dst != scratch); |
| 199 | if (changed) |
| 200 | dst = scratch; |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 202 | return changed; |
| 203 | } |
| 204 | |
| 205 | bool CFGBlockValues::updateBitVectors(const CFGBlock *block, |
| 206 | const BVPair &newVals) { |
| 207 | BVPair &vals = getBitVectors(block); |
| 208 | bool changed = *newVals.first != *vals.first || |
| 209 | *newVals.second != *vals.second; |
| 210 | *vals.first = *newVals.first; |
| 211 | *vals.second = *newVals.second; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 212 | return changed; |
| 213 | } |
| 214 | |
| 215 | void CFGBlockValues::resetScratch() { |
| 216 | scratch.reset(); |
| 217 | } |
| 218 | |
| 219 | llvm::BitVector::reference CFGBlockValues::operator[](const VarDecl *vd) { |
| 220 | const llvm::Optional<unsigned> &idx = declToBit.getBitVectorIndex(vd); |
| 221 | assert(idx.hasValue()); |
| 222 | return scratch[idx.getValue()]; |
| 223 | } |
| 224 | |
| 225 | //------------------------------------------------------------------------====// |
| 226 | // Worklist: worklist for dataflow analysis. |
| 227 | //====------------------------------------------------------------------------// |
| 228 | |
| 229 | namespace { |
| 230 | class DataflowWorklist { |
| 231 | llvm::SmallVector<const CFGBlock *, 20> worklist; |
| 232 | llvm::BitVector enqueuedBlocks; |
| 233 | public: |
| 234 | DataflowWorklist(const CFG &cfg) : enqueuedBlocks(cfg.getNumBlockIDs()) {} |
| 235 | |
| 236 | void enqueue(const CFGBlock *block); |
| 237 | void enqueueSuccessors(const CFGBlock *block); |
| 238 | const CFGBlock *dequeue(); |
| 239 | |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | void DataflowWorklist::enqueue(const CFGBlock *block) { |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 244 | if (!block) |
| 245 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 246 | unsigned idx = block->getBlockID(); |
| 247 | if (enqueuedBlocks[idx]) |
| 248 | return; |
| 249 | worklist.push_back(block); |
| 250 | enqueuedBlocks[idx] = true; |
| 251 | } |
| 252 | |
| 253 | void DataflowWorklist::enqueueSuccessors(const clang::CFGBlock *block) { |
| 254 | for (CFGBlock::const_succ_iterator I = block->succ_begin(), |
| 255 | E = block->succ_end(); I != E; ++I) { |
| 256 | enqueue(*I); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | const CFGBlock *DataflowWorklist::dequeue() { |
| 261 | if (worklist.empty()) |
| 262 | return 0; |
| 263 | const CFGBlock *b = worklist.back(); |
| 264 | worklist.pop_back(); |
| 265 | enqueuedBlocks[b->getBlockID()] = false; |
| 266 | return b; |
| 267 | } |
| 268 | |
| 269 | //------------------------------------------------------------------------====// |
| 270 | // Transfer function for uninitialized values analysis. |
| 271 | //====------------------------------------------------------------------------// |
| 272 | |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 273 | static const bool Initialized = false; |
| 274 | static const bool Uninitialized = true; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 275 | |
| 276 | namespace { |
| 277 | class FindVarResult { |
| 278 | const VarDecl *vd; |
| 279 | const DeclRefExpr *dr; |
| 280 | public: |
| 281 | FindVarResult(VarDecl *vd, DeclRefExpr *dr) : vd(vd), dr(dr) {} |
| 282 | |
| 283 | const DeclRefExpr *getDeclRefExpr() const { return dr; } |
| 284 | const VarDecl *getDecl() const { return vd; } |
| 285 | }; |
| 286 | |
| 287 | class TransferFunctions : public CFGRecStmtVisitor<TransferFunctions> { |
| 288 | CFGBlockValues &vals; |
| 289 | const CFG &cfg; |
| 290 | UninitVariablesHandler *handler; |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 291 | const DeclRefExpr *currentDR; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 292 | public: |
| 293 | TransferFunctions(CFGBlockValues &vals, const CFG &cfg, |
| 294 | UninitVariablesHandler *handler) |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 295 | : vals(vals), cfg(cfg), handler(handler), currentDR(0) {} |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 296 | |
| 297 | const CFG &getCFG() { return cfg; } |
| 298 | void reportUninit(const DeclRefExpr *ex, const VarDecl *vd); |
| 299 | |
| 300 | void VisitDeclStmt(DeclStmt *ds); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 301 | void VisitDeclRefExpr(DeclRefExpr *dr); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 302 | void VisitUnaryOperator(UnaryOperator *uo); |
| 303 | void VisitBinaryOperator(BinaryOperator *bo); |
| 304 | void VisitCastExpr(CastExpr *ce); |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame^] | 305 | void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 306 | }; |
| 307 | } |
| 308 | |
| 309 | void TransferFunctions::reportUninit(const DeclRefExpr *ex, |
| 310 | const VarDecl *vd) { |
| 311 | if (handler) handler->handleUseOfUninitVariable(ex, vd); |
| 312 | } |
| 313 | |
| 314 | void TransferFunctions::VisitDeclStmt(DeclStmt *ds) { |
| 315 | for (DeclStmt::decl_iterator DI = ds->decl_begin(), DE = ds->decl_end(); |
| 316 | DI != DE; ++DI) { |
| 317 | if (VarDecl *vd = dyn_cast<VarDecl>(*DI)) { |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 318 | if (isTrackedVar(vd)) { |
| 319 | vals[vd] = Uninitialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 320 | if (Stmt *init = vd->getInit()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 321 | Visit(init); |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 322 | vals[vd] = Initialized; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 323 | } |
Ted Kremenek | 4dccb90 | 2011-01-18 05:00:42 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 325 | else if (Stmt *init = vd->getInit()) { |
| 326 | Visit(init); |
| 327 | } |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 332 | void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { |
| 333 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 334 | // cannot be block-level expressions. Therefore, we determine if |
| 335 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 336 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 337 | // If a DeclRefExpr is not involved in a load, we are essentially computing |
| 338 | // its address, either for assignment to a reference or via the '&' operator. |
| 339 | // In such cases, treat the variable as being initialized, since this |
| 340 | // analysis isn't powerful enough to do alias tracking. |
| 341 | if (dr != currentDR) |
| 342 | if (const VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
| 343 | if (isTrackedVar(vd)) |
| 344 | vals[vd] = Initialized; |
| 345 | } |
| 346 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 347 | static FindVarResult findBlockVarDecl(Expr* ex) { |
| 348 | if (DeclRefExpr* dr = dyn_cast<DeclRefExpr>(ex->IgnoreParenCasts())) |
| 349 | if (VarDecl *vd = dyn_cast<VarDecl>(dr->getDecl())) |
Ted Kremenek | c104e53 | 2011-01-18 04:53:25 +0000 | [diff] [blame] | 350 | if (isTrackedVar(vd)) |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 351 | return FindVarResult(vd, dr); |
| 352 | |
| 353 | return FindVarResult(0, 0); |
| 354 | } |
| 355 | |
| 356 | void TransferFunctions::VisitBinaryOperator(clang::BinaryOperator *bo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 357 | if (bo->isAssignmentOp()) { |
| 358 | const FindVarResult &res = findBlockVarDecl(bo->getLHS()); |
| 359 | if (const VarDecl* vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 360 | // We assume that DeclRefExprs wrapped in a BinaryOperator "assignment" |
| 361 | // cannot be block-level expressions. Therefore, we determine if |
| 362 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 363 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 364 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 365 | res.getDeclRefExpr()); |
| 366 | Visit(bo->getRHS()); |
| 367 | Visit(bo->getLHS()); |
| 368 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 369 | llvm::BitVector::reference bit = vals[vd]; |
| 370 | if (bit == Uninitialized) { |
| 371 | if (bo->getOpcode() != BO_Assign) |
| 372 | reportUninit(res.getDeclRefExpr(), vd); |
| 373 | bit = Initialized; |
| 374 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 375 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 378 | Visit(bo->getRHS()); |
| 379 | Visit(bo->getLHS()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void TransferFunctions::VisitUnaryOperator(clang::UnaryOperator *uo) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 383 | switch (uo->getOpcode()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 384 | case clang::UO_PostDec: |
| 385 | case clang::UO_PostInc: |
| 386 | case clang::UO_PreDec: |
| 387 | case clang::UO_PreInc: { |
| 388 | const FindVarResult &res = findBlockVarDecl(uo->getSubExpr()); |
| 389 | if (const VarDecl *vd = res.getDecl()) { |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 390 | // We assume that DeclRefExprs wrapped in a unary operator ++/-- |
| 391 | // cannot be block-level expressions. Therefore, we determine if |
| 392 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 393 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 394 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 395 | res.getDeclRefExpr()); |
| 396 | Visit(uo->getSubExpr()); |
| 397 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 398 | llvm::BitVector::reference bit = vals[vd]; |
| 399 | if (bit == Uninitialized) { |
| 400 | reportUninit(res.getDeclRefExpr(), vd); |
| 401 | bit = Initialized; |
| 402 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 403 | return; |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 404 | } |
| 405 | break; |
| 406 | } |
| 407 | default: |
| 408 | break; |
| 409 | } |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 410 | Visit(uo->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void TransferFunctions::VisitCastExpr(clang::CastExpr *ce) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 414 | if (ce->getCastKind() == CK_LValueToRValue) { |
| 415 | const FindVarResult &res = findBlockVarDecl(ce->getSubExpr()); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 416 | if (const VarDecl *vd = res.getDecl()) { |
| 417 | // We assume that DeclRefExprs wrapped in an lvalue-to-rvalue cast |
| 418 | // cannot be block-level expressions. Therefore, we determine if |
| 419 | // a DeclRefExpr is involved in a "load" by comparing it to the current |
| 420 | // DeclRefExpr found when analyzing the last lvalue-to-rvalue CastExpr. |
| 421 | // Here we update 'currentDR' to be the one associated with this |
| 422 | // lvalue-to-rvalue cast. Then, when we analyze the DeclRefExpr, we |
| 423 | // will know that we are not computing its lvalue for other purposes |
| 424 | // than to perform a load. |
| 425 | SaveAndRestore<const DeclRefExpr*> lastDR(currentDR, |
| 426 | res.getDeclRefExpr()); |
| 427 | Visit(ce->getSubExpr()); |
| 428 | if (vals[vd] == Uninitialized) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 429 | reportUninit(res.getDeclRefExpr(), vd); |
Ted Kremenek | c21fed3 | 2011-01-18 21:18:58 +0000 | [diff] [blame] | 430 | // Don't cascade warnings. |
| 431 | vals[vd] = Initialized; |
| 432 | } |
| 433 | return; |
| 434 | } |
| 435 | } |
| 436 | Visit(ce->getSubExpr()); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Ted Kremenek | 9660803 | 2011-01-23 17:53:04 +0000 | [diff] [blame^] | 439 | void TransferFunctions::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *se) { |
| 440 | if (se->isSizeOf()) { |
| 441 | if (se->getType()->isConstantSizeType()) |
| 442 | return; |
| 443 | // Handle VLAs. |
| 444 | Visit(se->getArgumentExpr()); |
| 445 | } |
| 446 | } |
| 447 | |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 448 | //------------------------------------------------------------------------====// |
| 449 | // High-level "driver" logic for uninitialized values analysis. |
| 450 | //====------------------------------------------------------------------------// |
| 451 | |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 452 | static bool runOnBlock(const CFGBlock *block, const CFG &cfg, |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 453 | CFGBlockValues &vals, |
| 454 | UninitVariablesHandler *handler = 0) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 455 | |
| 456 | if (const BinaryOperator *b = getLogicalOperatorInChain(block)) { |
Ted Kremenek | 2d4bed1 | 2011-01-20 21:25:31 +0000 | [diff] [blame] | 457 | if (block->pred_size() == 2 && block->succ_size() == 2) { |
| 458 | assert(block->getTerminatorCondition() == b); |
| 459 | BVPair valsAB = vals.getPredBitVectors(block); |
| 460 | vals.mergeIntoScratch(*valsAB.first, true); |
| 461 | vals.mergeIntoScratch(*valsAB.second, false); |
| 462 | valsAB.second = &vals.getScratch(); |
| 463 | if (b->getOpcode() == BO_LOr) { |
| 464 | // Ensure the invariant that 'first' corresponds to the true |
| 465 | // branch and 'second' to the false. |
| 466 | std::swap(valsAB.first, valsAB.second); |
| 467 | } |
| 468 | return vals.updateBitVectors(block, valsAB); |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 469 | } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // Default behavior: merge in values of predecessor blocks. |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 473 | vals.resetScratch(); |
| 474 | bool isFirst = true; |
| 475 | for (CFGBlock::const_pred_iterator I = block->pred_begin(), |
| 476 | E = block->pred_end(); I != E; ++I) { |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 477 | vals.mergeIntoScratch(vals.getBitVector(*I, block), isFirst); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 478 | isFirst = false; |
| 479 | } |
| 480 | // Apply the transfer function. |
| 481 | TransferFunctions tf(vals, cfg, handler); |
| 482 | for (CFGBlock::const_iterator I = block->begin(), E = block->end(); |
| 483 | I != E; ++I) { |
| 484 | if (const CFGStmt *cs = dyn_cast<CFGStmt>(&*I)) { |
| 485 | tf.BlockStmt_Visit(cs->getStmt()); |
| 486 | } |
| 487 | } |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 488 | return vals.updateBitVectorWithScratch(block); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void clang::runUninitializedVariablesAnalysis(const DeclContext &dc, |
| 492 | const CFG &cfg, |
| 493 | UninitVariablesHandler &handler) { |
| 494 | CFGBlockValues vals(cfg); |
| 495 | vals.computeSetOfDeclarations(dc); |
| 496 | if (vals.hasNoDeclarations()) |
| 497 | return; |
| 498 | DataflowWorklist worklist(cfg); |
| 499 | llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); |
| 500 | |
| 501 | worklist.enqueueSuccessors(&cfg.getEntry()); |
| 502 | |
| 503 | while (const CFGBlock *block = worklist.dequeue()) { |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 504 | // Did the block change? |
Ted Kremenek | 13bd423 | 2011-01-20 17:37:17 +0000 | [diff] [blame] | 505 | bool changed = runOnBlock(block, cfg, vals); |
Ted Kremenek | 610068c | 2011-01-15 02:58:47 +0000 | [diff] [blame] | 506 | if (changed || !previouslyVisited[block->getBlockID()]) |
| 507 | worklist.enqueueSuccessors(block); |
| 508 | previouslyVisited[block->getBlockID()] = true; |
| 509 | } |
| 510 | |
| 511 | // Run through the blocks one more time, and report uninitialized variabes. |
| 512 | for (CFG::const_iterator BI = cfg.begin(), BE = cfg.end(); BI != BE; ++BI) { |
| 513 | runOnBlock(*BI, cfg, vals, &handler); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | UninitVariablesHandler::~UninitVariablesHandler() {} |
| 518 | |