10b57cec5SDimitry Andric //===- UninitializedValues.cpp - Find Uninitialized Values ----------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements uninitialized values analysis for source-level CFGs. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/Analysis/Analyses/UninitializedValues.h" 140b57cec5SDimitry Andric #include "clang/AST/Attr.h" 150b57cec5SDimitry Andric #include "clang/AST/Decl.h" 160b57cec5SDimitry Andric #include "clang/AST/DeclBase.h" 170b57cec5SDimitry Andric #include "clang/AST/Expr.h" 180b57cec5SDimitry Andric #include "clang/AST/OperationKinds.h" 190b57cec5SDimitry Andric #include "clang/AST/Stmt.h" 200b57cec5SDimitry Andric #include "clang/AST/StmtObjC.h" 210b57cec5SDimitry Andric #include "clang/AST/StmtVisitor.h" 220b57cec5SDimitry Andric #include "clang/AST/Type.h" 230b57cec5SDimitry Andric #include "clang/Analysis/Analyses/PostOrderCFGView.h" 240b57cec5SDimitry Andric #include "clang/Analysis/AnalysisDeclContext.h" 250b57cec5SDimitry Andric #include "clang/Analysis/CFG.h" 260b57cec5SDimitry Andric #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h" 27*5ffd83dbSDimitry Andric #include "clang/Analysis/FlowSensitive/DataflowWorklist.h" 280b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 290b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 300b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 310b57cec5SDimitry Andric #include "llvm/ADT/None.h" 320b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 330b57cec5SDimitry Andric #include "llvm/ADT/PackedVector.h" 340b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 350b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 360b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 370b57cec5SDimitry Andric #include <algorithm> 380b57cec5SDimitry Andric #include <cassert> 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric using namespace clang; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric #define DEBUG_LOGGING 0 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) { 450b57cec5SDimitry Andric if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() && 460b57cec5SDimitry Andric !vd->isExceptionVariable() && !vd->isInitCapture() && 470b57cec5SDimitry Andric !vd->isImplicit() && vd->getDeclContext() == dc) { 480b57cec5SDimitry Andric QualType ty = vd->getType(); 490b57cec5SDimitry Andric return ty->isScalarType() || ty->isVectorType() || ty->isRecordType(); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric return false; 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric //------------------------------------------------------------------------====// 550b57cec5SDimitry Andric // DeclToIndex: a mapping from Decls we track to value indices. 560b57cec5SDimitry Andric //====------------------------------------------------------------------------// 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric namespace { 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric class DeclToIndex { 610b57cec5SDimitry Andric llvm::DenseMap<const VarDecl *, unsigned> map; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric public: 640b57cec5SDimitry Andric DeclToIndex() = default; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric /// Compute the actual mapping from declarations to bits. 670b57cec5SDimitry Andric void computeMap(const DeclContext &dc); 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric /// Return the number of declarations in the map. 700b57cec5SDimitry Andric unsigned size() const { return map.size(); } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /// Returns the bit vector index for a given declaration. 730b57cec5SDimitry Andric Optional<unsigned> getValueIndex(const VarDecl *d) const; 740b57cec5SDimitry Andric }; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric } // namespace 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric void DeclToIndex::computeMap(const DeclContext &dc) { 790b57cec5SDimitry Andric unsigned count = 0; 800b57cec5SDimitry Andric DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()), 810b57cec5SDimitry Andric E(dc.decls_end()); 820b57cec5SDimitry Andric for ( ; I != E; ++I) { 830b57cec5SDimitry Andric const VarDecl *vd = *I; 840b57cec5SDimitry Andric if (isTrackedVar(vd, &dc)) 850b57cec5SDimitry Andric map[vd] = count++; 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric Optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const { 900b57cec5SDimitry Andric llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d); 910b57cec5SDimitry Andric if (I == map.end()) 920b57cec5SDimitry Andric return None; 930b57cec5SDimitry Andric return I->second; 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric //------------------------------------------------------------------------====// 970b57cec5SDimitry Andric // CFGBlockValues: dataflow values for CFG blocks. 980b57cec5SDimitry Andric //====------------------------------------------------------------------------// 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric // These values are defined in such a way that a merge can be done using 1010b57cec5SDimitry Andric // a bitwise OR. 1020b57cec5SDimitry Andric enum Value { Unknown = 0x0, /* 00 */ 1030b57cec5SDimitry Andric Initialized = 0x1, /* 01 */ 1040b57cec5SDimitry Andric Uninitialized = 0x2, /* 10 */ 1050b57cec5SDimitry Andric MayUninitialized = 0x3 /* 11 */ }; 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric static bool isUninitialized(const Value v) { 1080b57cec5SDimitry Andric return v >= Uninitialized; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric static bool isAlwaysUninit(const Value v) { 1120b57cec5SDimitry Andric return v == Uninitialized; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric namespace { 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric using ValueVector = llvm::PackedVector<Value, 2, llvm::SmallBitVector>; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric class CFGBlockValues { 1200b57cec5SDimitry Andric const CFG &cfg; 1210b57cec5SDimitry Andric SmallVector<ValueVector, 8> vals; 1220b57cec5SDimitry Andric ValueVector scratch; 1230b57cec5SDimitry Andric DeclToIndex declToIndex; 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric public: 1260b57cec5SDimitry Andric CFGBlockValues(const CFG &cfg); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric unsigned getNumEntries() const { return declToIndex.size(); } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric void computeSetOfDeclarations(const DeclContext &dc); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric ValueVector &getValueVector(const CFGBlock *block) { 1330b57cec5SDimitry Andric return vals[block->getBlockID()]; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric void setAllScratchValues(Value V); 1370b57cec5SDimitry Andric void mergeIntoScratch(ValueVector const &source, bool isFirst); 1380b57cec5SDimitry Andric bool updateValueVectorWithScratch(const CFGBlock *block); 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric bool hasNoDeclarations() const { 1410b57cec5SDimitry Andric return declToIndex.size() == 0; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric void resetScratch(); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric ValueVector::reference operator[](const VarDecl *vd); 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric Value getValue(const CFGBlock *block, const CFGBlock *dstBlock, 1490b57cec5SDimitry Andric const VarDecl *vd) { 1500b57cec5SDimitry Andric const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); 1510b57cec5SDimitry Andric assert(idx.hasValue()); 1520b57cec5SDimitry Andric return getValueVector(block)[idx.getValue()]; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric }; 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric } // namespace 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {} 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { 1610b57cec5SDimitry Andric declToIndex.computeMap(dc); 1620b57cec5SDimitry Andric unsigned decls = declToIndex.size(); 1630b57cec5SDimitry Andric scratch.resize(decls); 1640b57cec5SDimitry Andric unsigned n = cfg.getNumBlockIDs(); 1650b57cec5SDimitry Andric if (!n) 1660b57cec5SDimitry Andric return; 1670b57cec5SDimitry Andric vals.resize(n); 1680b57cec5SDimitry Andric for (auto &val : vals) 1690b57cec5SDimitry Andric val.resize(decls); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric #if DEBUG_LOGGING 1730b57cec5SDimitry Andric static void printVector(const CFGBlock *block, ValueVector &bv, 1740b57cec5SDimitry Andric unsigned num) { 1750b57cec5SDimitry Andric llvm::errs() << block->getBlockID() << " :"; 1760b57cec5SDimitry Andric for (const auto &i : bv) 1770b57cec5SDimitry Andric llvm::errs() << ' ' << i; 1780b57cec5SDimitry Andric llvm::errs() << " : " << num << '\n'; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric #endif 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric void CFGBlockValues::setAllScratchValues(Value V) { 1830b57cec5SDimitry Andric for (unsigned I = 0, E = scratch.size(); I != E; ++I) 1840b57cec5SDimitry Andric scratch[I] = V; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric void CFGBlockValues::mergeIntoScratch(ValueVector const &source, 1880b57cec5SDimitry Andric bool isFirst) { 1890b57cec5SDimitry Andric if (isFirst) 1900b57cec5SDimitry Andric scratch = source; 1910b57cec5SDimitry Andric else 1920b57cec5SDimitry Andric scratch |= source; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { 1960b57cec5SDimitry Andric ValueVector &dst = getValueVector(block); 1970b57cec5SDimitry Andric bool changed = (dst != scratch); 1980b57cec5SDimitry Andric if (changed) 1990b57cec5SDimitry Andric dst = scratch; 2000b57cec5SDimitry Andric #if DEBUG_LOGGING 2010b57cec5SDimitry Andric printVector(block, scratch, 0); 2020b57cec5SDimitry Andric #endif 2030b57cec5SDimitry Andric return changed; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric void CFGBlockValues::resetScratch() { 2070b57cec5SDimitry Andric scratch.reset(); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { 2110b57cec5SDimitry Andric const Optional<unsigned> &idx = declToIndex.getValueIndex(vd); 2120b57cec5SDimitry Andric assert(idx.hasValue()); 2130b57cec5SDimitry Andric return scratch[idx.getValue()]; 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric //------------------------------------------------------------------------====// 2170b57cec5SDimitry Andric // Classification of DeclRefExprs as use or initialization. 2180b57cec5SDimitry Andric //====------------------------------------------------------------------------// 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric namespace { 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric class FindVarResult { 2230b57cec5SDimitry Andric const VarDecl *vd; 2240b57cec5SDimitry Andric const DeclRefExpr *dr; 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric public: 2270b57cec5SDimitry Andric FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {} 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric const DeclRefExpr *getDeclRefExpr() const { return dr; } 2300b57cec5SDimitry Andric const VarDecl *getDecl() const { return vd; } 2310b57cec5SDimitry Andric }; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric } // namespace 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric static const Expr *stripCasts(ASTContext &C, const Expr *Ex) { 2360b57cec5SDimitry Andric while (Ex) { 2370b57cec5SDimitry Andric Ex = Ex->IgnoreParenNoopCasts(C); 2380b57cec5SDimitry Andric if (const auto *CE = dyn_cast<CastExpr>(Ex)) { 2390b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueBitCast) { 2400b57cec5SDimitry Andric Ex = CE->getSubExpr(); 2410b57cec5SDimitry Andric continue; 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric break; 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric return Ex; 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric /// If E is an expression comprising a reference to a single variable, find that 2500b57cec5SDimitry Andric /// variable. 2510b57cec5SDimitry Andric static FindVarResult findVar(const Expr *E, const DeclContext *DC) { 2520b57cec5SDimitry Andric if (const auto *DRE = 2530b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E))) 2540b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 2550b57cec5SDimitry Andric if (isTrackedVar(VD, DC)) 2560b57cec5SDimitry Andric return FindVarResult(VD, DRE); 2570b57cec5SDimitry Andric return FindVarResult(nullptr, nullptr); 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric namespace { 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric /// Classify each DeclRefExpr as an initialization or a use. Any 2630b57cec5SDimitry Andric /// DeclRefExpr which isn't explicitly classified will be assumed to have 2640b57cec5SDimitry Andric /// escaped the analysis and will be treated as an initialization. 2650b57cec5SDimitry Andric class ClassifyRefs : public StmtVisitor<ClassifyRefs> { 2660b57cec5SDimitry Andric public: 2670b57cec5SDimitry Andric enum Class { 2680b57cec5SDimitry Andric Init, 2690b57cec5SDimitry Andric Use, 2700b57cec5SDimitry Andric SelfInit, 271*5ffd83dbSDimitry Andric ConstRefUse, 2720b57cec5SDimitry Andric Ignore 2730b57cec5SDimitry Andric }; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric private: 2760b57cec5SDimitry Andric const DeclContext *DC; 2770b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr *, Class> Classification; 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *VD) const { 2800b57cec5SDimitry Andric return ::isTrackedVar(VD, DC); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric void classify(const Expr *E, Class C); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric public: 2860b57cec5SDimitry Andric ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {} 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *DS); 2890b57cec5SDimitry Andric void VisitUnaryOperator(UnaryOperator *UO); 2900b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *BO); 2910b57cec5SDimitry Andric void VisitCallExpr(CallExpr *CE); 2920b57cec5SDimitry Andric void VisitCastExpr(CastExpr *CE); 2930b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric void operator()(Stmt *S) { Visit(S); } 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric Class get(const DeclRefExpr *DRE) const { 2980b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I 2990b57cec5SDimitry Andric = Classification.find(DRE); 3000b57cec5SDimitry Andric if (I != Classification.end()) 3010b57cec5SDimitry Andric return I->second; 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); 3040b57cec5SDimitry Andric if (!VD || !isTrackedVar(VD)) 3050b57cec5SDimitry Andric return Ignore; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric return Init; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric }; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric } // namespace 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) { 3140b57cec5SDimitry Andric if (VD->getType()->isRecordType()) 3150b57cec5SDimitry Andric return nullptr; 3160b57cec5SDimitry Andric if (Expr *Init = VD->getInit()) { 3170b57cec5SDimitry Andric const auto *DRE = 3180b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init)); 3190b57cec5SDimitry Andric if (DRE && DRE->getDecl() == VD) 3200b57cec5SDimitry Andric return DRE; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric return nullptr; 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric void ClassifyRefs::classify(const Expr *E, Class C) { 3260b57cec5SDimitry Andric // The result of a ?: could also be an lvalue. 3270b57cec5SDimitry Andric E = E->IgnoreParens(); 3280b57cec5SDimitry Andric if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 3290b57cec5SDimitry Andric classify(CO->getTrueExpr(), C); 3300b57cec5SDimitry Andric classify(CO->getFalseExpr(), C); 3310b57cec5SDimitry Andric return; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) { 3350b57cec5SDimitry Andric classify(BCO->getFalseExpr(), C); 3360b57cec5SDimitry Andric return; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) { 3400b57cec5SDimitry Andric classify(OVE->getSourceExpr(), C); 3410b57cec5SDimitry Andric return; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric if (const auto *ME = dyn_cast<MemberExpr>(E)) { 3450b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { 3460b57cec5SDimitry Andric if (!VD->isStaticDataMember()) 3470b57cec5SDimitry Andric classify(ME->getBase(), C); 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric return; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 3530b57cec5SDimitry Andric switch (BO->getOpcode()) { 3540b57cec5SDimitry Andric case BO_PtrMemD: 3550b57cec5SDimitry Andric case BO_PtrMemI: 3560b57cec5SDimitry Andric classify(BO->getLHS(), C); 3570b57cec5SDimitry Andric return; 3580b57cec5SDimitry Andric case BO_Comma: 3590b57cec5SDimitry Andric classify(BO->getRHS(), C); 3600b57cec5SDimitry Andric return; 3610b57cec5SDimitry Andric default: 3620b57cec5SDimitry Andric return; 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric FindVarResult Var = findVar(E, DC); 3670b57cec5SDimitry Andric if (const DeclRefExpr *DRE = Var.getDeclRefExpr()) 3680b57cec5SDimitry Andric Classification[DRE] = std::max(Classification[DRE], C); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) { 3720b57cec5SDimitry Andric for (auto *DI : DS->decls()) { 3730b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI); 3740b57cec5SDimitry Andric if (VD && isTrackedVar(VD)) 3750b57cec5SDimitry Andric if (const DeclRefExpr *DRE = getSelfInitExpr(VD)) 3760b57cec5SDimitry Andric Classification[DRE] = SelfInit; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) { 3810b57cec5SDimitry Andric // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this 3820b57cec5SDimitry Andric // is not a compound-assignment, we will treat it as initializing the variable 3830b57cec5SDimitry Andric // when TransferFunctions visits it. A compound-assignment does not affect 3840b57cec5SDimitry Andric // whether a variable is uninitialized, and there's no point counting it as a 3850b57cec5SDimitry Andric // use. 3860b57cec5SDimitry Andric if (BO->isCompoundAssignmentOp()) 3870b57cec5SDimitry Andric classify(BO->getLHS(), Use); 3880b57cec5SDimitry Andric else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma) 3890b57cec5SDimitry Andric classify(BO->getLHS(), Ignore); 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) { 3930b57cec5SDimitry Andric // Increment and decrement are uses despite there being no lvalue-to-rvalue 3940b57cec5SDimitry Andric // conversion. 3950b57cec5SDimitry Andric if (UO->isIncrementDecrementOp()) 3960b57cec5SDimitry Andric classify(UO->getSubExpr(), Use); 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) { 4000b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) 4010b57cec5SDimitry Andric classify(cast<Expr>(S), Use); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric static bool isPointerToConst(const QualType &QT) { 4050b57cec5SDimitry Andric return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified(); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 408*5ffd83dbSDimitry Andric static bool hasTrivialBody(CallExpr *CE) { 409*5ffd83dbSDimitry Andric if (FunctionDecl *FD = CE->getDirectCallee()) { 410*5ffd83dbSDimitry Andric if (FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) 411*5ffd83dbSDimitry Andric return FTD->getTemplatedDecl()->hasTrivialBody(); 412*5ffd83dbSDimitry Andric return FD->hasTrivialBody(); 413*5ffd83dbSDimitry Andric } 414*5ffd83dbSDimitry Andric return false; 415*5ffd83dbSDimitry Andric } 416*5ffd83dbSDimitry Andric 4170b57cec5SDimitry Andric void ClassifyRefs::VisitCallExpr(CallExpr *CE) { 4180b57cec5SDimitry Andric // Classify arguments to std::move as used. 4190b57cec5SDimitry Andric if (CE->isCallToStdMove()) { 4200b57cec5SDimitry Andric // RecordTypes are handled in SemaDeclCXX.cpp. 4210b57cec5SDimitry Andric if (!CE->getArg(0)->getType()->isRecordType()) 4220b57cec5SDimitry Andric classify(CE->getArg(0), Use); 4230b57cec5SDimitry Andric return; 4240b57cec5SDimitry Andric } 425*5ffd83dbSDimitry Andric bool isTrivialBody = hasTrivialBody(CE); 426*5ffd83dbSDimitry Andric // If a value is passed by const pointer to a function, 4270b57cec5SDimitry Andric // we should not assume that it is initialized by the call, and we 4280b57cec5SDimitry Andric // conservatively do not assume that it is used. 429*5ffd83dbSDimitry Andric // If a value is passed by const reference to a function, 430*5ffd83dbSDimitry Andric // it should already be initialized. 4310b57cec5SDimitry Andric for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); 4320b57cec5SDimitry Andric I != E; ++I) { 4330b57cec5SDimitry Andric if ((*I)->isGLValue()) { 4340b57cec5SDimitry Andric if ((*I)->getType().isConstQualified()) 435*5ffd83dbSDimitry Andric classify((*I), isTrivialBody ? Ignore : ConstRefUse); 4360b57cec5SDimitry Andric } else if (isPointerToConst((*I)->getType())) { 4370b57cec5SDimitry Andric const Expr *Ex = stripCasts(DC->getParentASTContext(), *I); 4380b57cec5SDimitry Andric const auto *UO = dyn_cast<UnaryOperator>(Ex); 4390b57cec5SDimitry Andric if (UO && UO->getOpcode() == UO_AddrOf) 4400b57cec5SDimitry Andric Ex = UO->getSubExpr(); 4410b57cec5SDimitry Andric classify(Ex, Ignore); 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric void ClassifyRefs::VisitCastExpr(CastExpr *CE) { 4470b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueToRValue) 4480b57cec5SDimitry Andric classify(CE->getSubExpr(), Use); 4490b57cec5SDimitry Andric else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) { 4500b57cec5SDimitry Andric if (CSE->getType()->isVoidType()) { 4510b57cec5SDimitry Andric // Squelch any detected load of an uninitialized value if 4520b57cec5SDimitry Andric // we cast it to void. 4530b57cec5SDimitry Andric // e.g. (void) x; 4540b57cec5SDimitry Andric classify(CSE->getSubExpr(), Ignore); 4550b57cec5SDimitry Andric } 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric //------------------------------------------------------------------------====// 4600b57cec5SDimitry Andric // Transfer function for uninitialized values analysis. 4610b57cec5SDimitry Andric //====------------------------------------------------------------------------// 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric namespace { 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric class TransferFunctions : public StmtVisitor<TransferFunctions> { 4660b57cec5SDimitry Andric CFGBlockValues &vals; 4670b57cec5SDimitry Andric const CFG &cfg; 4680b57cec5SDimitry Andric const CFGBlock *block; 4690b57cec5SDimitry Andric AnalysisDeclContext ∾ 4700b57cec5SDimitry Andric const ClassifyRefs &classification; 4710b57cec5SDimitry Andric ObjCNoReturn objCNoRet; 4720b57cec5SDimitry Andric UninitVariablesHandler &handler; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric public: 4750b57cec5SDimitry Andric TransferFunctions(CFGBlockValues &vals, const CFG &cfg, 4760b57cec5SDimitry Andric const CFGBlock *block, AnalysisDeclContext &ac, 4770b57cec5SDimitry Andric const ClassifyRefs &classification, 4780b57cec5SDimitry Andric UninitVariablesHandler &handler) 4790b57cec5SDimitry Andric : vals(vals), cfg(cfg), block(block), ac(ac), 4800b57cec5SDimitry Andric classification(classification), objCNoRet(ac.getASTContext()), 4810b57cec5SDimitry Andric handler(handler) {} 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric void reportUse(const Expr *ex, const VarDecl *vd); 484*5ffd83dbSDimitry Andric void reportConstRefUse(const Expr *ex, const VarDecl *vd); 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *bo); 4870b57cec5SDimitry Andric void VisitBlockExpr(BlockExpr *be); 4880b57cec5SDimitry Andric void VisitCallExpr(CallExpr *ce); 4890b57cec5SDimitry Andric void VisitDeclRefExpr(DeclRefExpr *dr); 4900b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *ds); 491*5ffd83dbSDimitry Andric void VisitGCCAsmStmt(GCCAsmStmt *as); 4920b57cec5SDimitry Andric void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS); 4930b57cec5SDimitry Andric void VisitObjCMessageExpr(ObjCMessageExpr *ME); 4940b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *vd) { 4970b57cec5SDimitry Andric return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric FindVarResult findVar(const Expr *ex) { 5010b57cec5SDimitry Andric return ::findVar(ex, cast<DeclContext>(ac.getDecl())); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) { 5050b57cec5SDimitry Andric UninitUse Use(ex, isAlwaysUninit(v)); 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric assert(isUninitialized(v)); 5080b57cec5SDimitry Andric if (Use.getKind() == UninitUse::Always) 5090b57cec5SDimitry Andric return Use; 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // If an edge which leads unconditionally to this use did not initialize 5120b57cec5SDimitry Andric // the variable, we can say something stronger than 'may be uninitialized': 5130b57cec5SDimitry Andric // we can say 'either it's used uninitialized or you have dead code'. 5140b57cec5SDimitry Andric // 5150b57cec5SDimitry Andric // We track the number of successors of a node which have been visited, and 5160b57cec5SDimitry Andric // visit a node once we have visited all of its successors. Only edges where 5170b57cec5SDimitry Andric // the variable might still be uninitialized are followed. Since a variable 5180b57cec5SDimitry Andric // can't transfer from being initialized to being uninitialized, this will 5190b57cec5SDimitry Andric // trace out the subgraph which inevitably leads to the use and does not 5200b57cec5SDimitry Andric // initialize the variable. We do not want to skip past loops, since their 5210b57cec5SDimitry Andric // non-termination might be correlated with the initialization condition. 5220b57cec5SDimitry Andric // 5230b57cec5SDimitry Andric // For example: 5240b57cec5SDimitry Andric // 5250b57cec5SDimitry Andric // void f(bool a, bool b) { 5260b57cec5SDimitry Andric // block1: int n; 5270b57cec5SDimitry Andric // if (a) { 5280b57cec5SDimitry Andric // block2: if (b) 5290b57cec5SDimitry Andric // block3: n = 1; 5300b57cec5SDimitry Andric // block4: } else if (b) { 5310b57cec5SDimitry Andric // block5: while (!a) { 5320b57cec5SDimitry Andric // block6: do_work(&a); 5330b57cec5SDimitry Andric // n = 2; 5340b57cec5SDimitry Andric // } 5350b57cec5SDimitry Andric // } 5360b57cec5SDimitry Andric // block7: if (a) 5370b57cec5SDimitry Andric // block8: g(); 5380b57cec5SDimitry Andric // block9: return n; 5390b57cec5SDimitry Andric // } 5400b57cec5SDimitry Andric // 5410b57cec5SDimitry Andric // Starting from the maybe-uninitialized use in block 9: 5420b57cec5SDimitry Andric // * Block 7 is not visited because we have only visited one of its two 5430b57cec5SDimitry Andric // successors. 5440b57cec5SDimitry Andric // * Block 8 is visited because we've visited its only successor. 5450b57cec5SDimitry Andric // From block 8: 5460b57cec5SDimitry Andric // * Block 7 is visited because we've now visited both of its successors. 5470b57cec5SDimitry Andric // From block 7: 5480b57cec5SDimitry Andric // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all 5490b57cec5SDimitry Andric // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively). 5500b57cec5SDimitry Andric // * Block 3 is not visited because it initializes 'n'. 5510b57cec5SDimitry Andric // Now the algorithm terminates, having visited blocks 7 and 8, and having 5520b57cec5SDimitry Andric // found the frontier is blocks 2, 4, and 5. 5530b57cec5SDimitry Andric // 5540b57cec5SDimitry Andric // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2 5550b57cec5SDimitry Andric // and 4), so we report that any time either of those edges is taken (in 5560b57cec5SDimitry Andric // each case when 'b == false'), 'n' is used uninitialized. 5570b57cec5SDimitry Andric SmallVector<const CFGBlock*, 32> Queue; 5580b57cec5SDimitry Andric SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0); 5590b57cec5SDimitry Andric Queue.push_back(block); 5600b57cec5SDimitry Andric // Specify that we've already visited all successors of the starting block. 5610b57cec5SDimitry Andric // This has the dual purpose of ensuring we never add it to the queue, and 5620b57cec5SDimitry Andric // of marking it as not being a candidate element of the frontier. 5630b57cec5SDimitry Andric SuccsVisited[block->getBlockID()] = block->succ_size(); 5640b57cec5SDimitry Andric while (!Queue.empty()) { 5650b57cec5SDimitry Andric const CFGBlock *B = Queue.pop_back_val(); 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric // If the use is always reached from the entry block, make a note of that. 5680b57cec5SDimitry Andric if (B == &cfg.getEntry()) 5690b57cec5SDimitry Andric Use.setUninitAfterCall(); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); 5720b57cec5SDimitry Andric I != E; ++I) { 5730b57cec5SDimitry Andric const CFGBlock *Pred = *I; 5740b57cec5SDimitry Andric if (!Pred) 5750b57cec5SDimitry Andric continue; 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric Value AtPredExit = vals.getValue(Pred, B, vd); 5780b57cec5SDimitry Andric if (AtPredExit == Initialized) 5790b57cec5SDimitry Andric // This block initializes the variable. 5800b57cec5SDimitry Andric continue; 5810b57cec5SDimitry Andric if (AtPredExit == MayUninitialized && 5820b57cec5SDimitry Andric vals.getValue(B, nullptr, vd) == Uninitialized) { 5830b57cec5SDimitry Andric // This block declares the variable (uninitialized), and is reachable 5840b57cec5SDimitry Andric // from a block that initializes the variable. We can't guarantee to 5850b57cec5SDimitry Andric // give an earlier location for the diagnostic (and it appears that 5860b57cec5SDimitry Andric // this code is intended to be reachable) so give a diagnostic here 5870b57cec5SDimitry Andric // and go no further down this path. 5880b57cec5SDimitry Andric Use.setUninitAfterDecl(); 5890b57cec5SDimitry Andric continue; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 592*5ffd83dbSDimitry Andric if (AtPredExit == MayUninitialized) { 593*5ffd83dbSDimitry Andric // If the predecessor's terminator is an "asm goto" that initializes 594*5ffd83dbSDimitry Andric // the variable, then it won't be counted as "initialized" on the 595*5ffd83dbSDimitry Andric // non-fallthrough paths. 596*5ffd83dbSDimitry Andric CFGTerminator term = Pred->getTerminator(); 597*5ffd83dbSDimitry Andric if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) { 598*5ffd83dbSDimitry Andric const CFGBlock *fallthrough = *Pred->succ_begin(); 599*5ffd83dbSDimitry Andric if (as->isAsmGoto() && 600*5ffd83dbSDimitry Andric llvm::any_of(as->outputs(), [&](const Expr *output) { 601*5ffd83dbSDimitry Andric return vd == findVar(output).getDecl() && 602*5ffd83dbSDimitry Andric llvm::any_of(as->labels(), 603*5ffd83dbSDimitry Andric [&](const AddrLabelExpr *label) { 604*5ffd83dbSDimitry Andric return label->getLabel()->getStmt() == B->Label && 605*5ffd83dbSDimitry Andric B != fallthrough; 606*5ffd83dbSDimitry Andric }); 607*5ffd83dbSDimitry Andric })) { 608*5ffd83dbSDimitry Andric Use.setUninitAfterDecl(); 609*5ffd83dbSDimitry Andric continue; 610*5ffd83dbSDimitry Andric } 611*5ffd83dbSDimitry Andric } 612*5ffd83dbSDimitry Andric } 613*5ffd83dbSDimitry Andric 6140b57cec5SDimitry Andric unsigned &SV = SuccsVisited[Pred->getBlockID()]; 6150b57cec5SDimitry Andric if (!SV) { 6160b57cec5SDimitry Andric // When visiting the first successor of a block, mark all NULL 6170b57cec5SDimitry Andric // successors as having been visited. 6180b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(), 6190b57cec5SDimitry Andric SE = Pred->succ_end(); 6200b57cec5SDimitry Andric SI != SE; ++SI) 6210b57cec5SDimitry Andric if (!*SI) 6220b57cec5SDimitry Andric ++SV; 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric if (++SV == Pred->succ_size()) 6260b57cec5SDimitry Andric // All paths from this block lead to the use and don't initialize the 6270b57cec5SDimitry Andric // variable. 6280b57cec5SDimitry Andric Queue.push_back(Pred); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric } 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric // Scan the frontier, looking for blocks where the variable was 6330b57cec5SDimitry Andric // uninitialized. 6340b57cec5SDimitry Andric for (const auto *Block : cfg) { 6350b57cec5SDimitry Andric unsigned BlockID = Block->getBlockID(); 6360b57cec5SDimitry Andric const Stmt *Term = Block->getTerminatorStmt(); 6370b57cec5SDimitry Andric if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() && 6380b57cec5SDimitry Andric Term) { 6390b57cec5SDimitry Andric // This block inevitably leads to the use. If we have an edge from here 6400b57cec5SDimitry Andric // to a post-dominator block, and the variable is uninitialized on that 6410b57cec5SDimitry Andric // edge, we have found a bug. 6420b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator I = Block->succ_begin(), 6430b57cec5SDimitry Andric E = Block->succ_end(); I != E; ++I) { 6440b57cec5SDimitry Andric const CFGBlock *Succ = *I; 6450b57cec5SDimitry Andric if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() && 6460b57cec5SDimitry Andric vals.getValue(Block, Succ, vd) == Uninitialized) { 6470b57cec5SDimitry Andric // Switch cases are a special case: report the label to the caller 6480b57cec5SDimitry Andric // as the 'terminator', not the switch statement itself. Suppress 6490b57cec5SDimitry Andric // situations where no label matched: we can't be sure that's 6500b57cec5SDimitry Andric // possible. 6510b57cec5SDimitry Andric if (isa<SwitchStmt>(Term)) { 6520b57cec5SDimitry Andric const Stmt *Label = Succ->getLabel(); 6530b57cec5SDimitry Andric if (!Label || !isa<SwitchCase>(Label)) 6540b57cec5SDimitry Andric // Might not be possible. 6550b57cec5SDimitry Andric continue; 6560b57cec5SDimitry Andric UninitUse::Branch Branch; 6570b57cec5SDimitry Andric Branch.Terminator = Label; 6580b57cec5SDimitry Andric Branch.Output = 0; // Ignored. 6590b57cec5SDimitry Andric Use.addUninitBranch(Branch); 6600b57cec5SDimitry Andric } else { 6610b57cec5SDimitry Andric UninitUse::Branch Branch; 6620b57cec5SDimitry Andric Branch.Terminator = Term; 6630b57cec5SDimitry Andric Branch.Output = I - Block->succ_begin(); 6640b57cec5SDimitry Andric Use.addUninitBranch(Branch); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric return Use; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric }; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric } // namespace 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) { 6780b57cec5SDimitry Andric Value v = vals[vd]; 6790b57cec5SDimitry Andric if (isUninitialized(v)) 6800b57cec5SDimitry Andric handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 683*5ffd83dbSDimitry Andric void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) { 684*5ffd83dbSDimitry Andric Value v = vals[vd]; 685*5ffd83dbSDimitry Andric if (isAlwaysUninit(v)) 686*5ffd83dbSDimitry Andric handler.handleConstRefUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 687*5ffd83dbSDimitry Andric } 688*5ffd83dbSDimitry Andric 6890b57cec5SDimitry Andric void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) { 6900b57cec5SDimitry Andric // This represents an initialization of the 'element' value. 6910b57cec5SDimitry Andric if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) { 6920b57cec5SDimitry Andric const auto *VD = cast<VarDecl>(DS->getSingleDecl()); 6930b57cec5SDimitry Andric if (isTrackedVar(VD)) 6940b57cec5SDimitry Andric vals[VD] = Initialized; 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric } 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric void TransferFunctions::VisitOMPExecutableDirective( 6990b57cec5SDimitry Andric OMPExecutableDirective *ED) { 7000b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) { 7010b57cec5SDimitry Andric assert(S && "Expected non-null used-in-clause child."); 7020b57cec5SDimitry Andric Visit(S); 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric if (!ED->isStandaloneDirective()) 7050b57cec5SDimitry Andric Visit(ED->getStructuredBlock()); 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric void TransferFunctions::VisitBlockExpr(BlockExpr *be) { 7090b57cec5SDimitry Andric const BlockDecl *bd = be->getBlockDecl(); 7100b57cec5SDimitry Andric for (const auto &I : bd->captures()) { 7110b57cec5SDimitry Andric const VarDecl *vd = I.getVariable(); 7120b57cec5SDimitry Andric if (!isTrackedVar(vd)) 7130b57cec5SDimitry Andric continue; 7140b57cec5SDimitry Andric if (I.isByRef()) { 7150b57cec5SDimitry Andric vals[vd] = Initialized; 7160b57cec5SDimitry Andric continue; 7170b57cec5SDimitry Andric } 7180b57cec5SDimitry Andric reportUse(be, vd); 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric void TransferFunctions::VisitCallExpr(CallExpr *ce) { 7230b57cec5SDimitry Andric if (Decl *Callee = ce->getCalleeDecl()) { 7240b57cec5SDimitry Andric if (Callee->hasAttr<ReturnsTwiceAttr>()) { 7250b57cec5SDimitry Andric // After a call to a function like setjmp or vfork, any variable which is 7260b57cec5SDimitry Andric // initialized anywhere within this function may now be initialized. For 7270b57cec5SDimitry Andric // now, just assume such a call initializes all variables. FIXME: Only 7280b57cec5SDimitry Andric // mark variables as initialized if they have an initializer which is 7290b57cec5SDimitry Andric // reachable from here. 7300b57cec5SDimitry Andric vals.setAllScratchValues(Initialized); 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) { 7330b57cec5SDimitry Andric // Functions labeled like "analyzer_noreturn" are often used to denote 7340b57cec5SDimitry Andric // "panic" functions that in special debug situations can still return, 7350b57cec5SDimitry Andric // but for the most part should not be treated as returning. This is a 7360b57cec5SDimitry Andric // useful annotation borrowed from the static analyzer that is useful for 7370b57cec5SDimitry Andric // suppressing branch-specific false positives when we call one of these 7380b57cec5SDimitry Andric // functions but keep pretending the path continues (when in reality the 7390b57cec5SDimitry Andric // user doesn't care). 7400b57cec5SDimitry Andric vals.setAllScratchValues(Unknown); 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric } 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { 7460b57cec5SDimitry Andric switch (classification.get(dr)) { 7470b57cec5SDimitry Andric case ClassifyRefs::Ignore: 7480b57cec5SDimitry Andric break; 7490b57cec5SDimitry Andric case ClassifyRefs::Use: 7500b57cec5SDimitry Andric reportUse(dr, cast<VarDecl>(dr->getDecl())); 7510b57cec5SDimitry Andric break; 7520b57cec5SDimitry Andric case ClassifyRefs::Init: 7530b57cec5SDimitry Andric vals[cast<VarDecl>(dr->getDecl())] = Initialized; 7540b57cec5SDimitry Andric break; 7550b57cec5SDimitry Andric case ClassifyRefs::SelfInit: 7560b57cec5SDimitry Andric handler.handleSelfInit(cast<VarDecl>(dr->getDecl())); 7570b57cec5SDimitry Andric break; 758*5ffd83dbSDimitry Andric case ClassifyRefs::ConstRefUse: 759*5ffd83dbSDimitry Andric reportConstRefUse(dr, cast<VarDecl>(dr->getDecl())); 760*5ffd83dbSDimitry Andric break; 7610b57cec5SDimitry Andric } 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) { 7650b57cec5SDimitry Andric if (BO->getOpcode() == BO_Assign) { 7660b57cec5SDimitry Andric FindVarResult Var = findVar(BO->getLHS()); 7670b57cec5SDimitry Andric if (const VarDecl *VD = Var.getDecl()) 7680b57cec5SDimitry Andric vals[VD] = Initialized; 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric } 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { 7730b57cec5SDimitry Andric for (auto *DI : DS->decls()) { 7740b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI); 7750b57cec5SDimitry Andric if (VD && isTrackedVar(VD)) { 7760b57cec5SDimitry Andric if (getSelfInitExpr(VD)) { 7770b57cec5SDimitry Andric // If the initializer consists solely of a reference to itself, we 7780b57cec5SDimitry Andric // explicitly mark the variable as uninitialized. This allows code 7790b57cec5SDimitry Andric // like the following: 7800b57cec5SDimitry Andric // 7810b57cec5SDimitry Andric // int x = x; 7820b57cec5SDimitry Andric // 7830b57cec5SDimitry Andric // to deliberately leave a variable uninitialized. Different analysis 7840b57cec5SDimitry Andric // clients can detect this pattern and adjust their reporting 7850b57cec5SDimitry Andric // appropriately, but we need to continue to analyze subsequent uses 7860b57cec5SDimitry Andric // of the variable. 7870b57cec5SDimitry Andric vals[VD] = Uninitialized; 7880b57cec5SDimitry Andric } else if (VD->getInit()) { 7890b57cec5SDimitry Andric // Treat the new variable as initialized. 7900b57cec5SDimitry Andric vals[VD] = Initialized; 7910b57cec5SDimitry Andric } else { 7920b57cec5SDimitry Andric // No initializer: the variable is now uninitialized. This matters 7930b57cec5SDimitry Andric // for cases like: 7940b57cec5SDimitry Andric // while (...) { 7950b57cec5SDimitry Andric // int n; 7960b57cec5SDimitry Andric // use(n); 7970b57cec5SDimitry Andric // n = 0; 7980b57cec5SDimitry Andric // } 7990b57cec5SDimitry Andric // FIXME: Mark the variable as uninitialized whenever its scope is 8000b57cec5SDimitry Andric // left, since its scope could be re-entered by a jump over the 8010b57cec5SDimitry Andric // declaration. 8020b57cec5SDimitry Andric vals[VD] = Uninitialized; 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric } 8060b57cec5SDimitry Andric } 8070b57cec5SDimitry Andric 808*5ffd83dbSDimitry Andric void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) { 809*5ffd83dbSDimitry Andric // An "asm goto" statement is a terminator that may initialize some variables. 810*5ffd83dbSDimitry Andric if (!as->isAsmGoto()) 811*5ffd83dbSDimitry Andric return; 812*5ffd83dbSDimitry Andric 813*5ffd83dbSDimitry Andric for (const Expr *o : as->outputs()) 814*5ffd83dbSDimitry Andric if (const VarDecl *VD = findVar(o).getDecl()) 815*5ffd83dbSDimitry Andric if (vals[VD] != Initialized) 816*5ffd83dbSDimitry Andric // If the variable isn't initialized by the time we get here, then we 817*5ffd83dbSDimitry Andric // mark it as potentially uninitialized for those cases where it's used 818*5ffd83dbSDimitry Andric // on an indirect path, where it's not guaranteed to be defined. 819*5ffd83dbSDimitry Andric vals[VD] = MayUninitialized; 820*5ffd83dbSDimitry Andric } 821*5ffd83dbSDimitry Andric 8220b57cec5SDimitry Andric void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) { 8230b57cec5SDimitry Andric // If the Objective-C message expression is an implicit no-return that 8240b57cec5SDimitry Andric // is not modeled in the CFG, set the tracked dataflow values to Unknown. 8250b57cec5SDimitry Andric if (objCNoRet.isImplicitNoReturn(ME)) { 8260b57cec5SDimitry Andric vals.setAllScratchValues(Unknown); 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric } 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric //------------------------------------------------------------------------====// 8310b57cec5SDimitry Andric // High-level "driver" logic for uninitialized values analysis. 8320b57cec5SDimitry Andric //====------------------------------------------------------------------------// 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric static bool runOnBlock(const CFGBlock *block, const CFG &cfg, 8350b57cec5SDimitry Andric AnalysisDeclContext &ac, CFGBlockValues &vals, 8360b57cec5SDimitry Andric const ClassifyRefs &classification, 8370b57cec5SDimitry Andric llvm::BitVector &wasAnalyzed, 8380b57cec5SDimitry Andric UninitVariablesHandler &handler) { 8390b57cec5SDimitry Andric wasAnalyzed[block->getBlockID()] = true; 8400b57cec5SDimitry Andric vals.resetScratch(); 8410b57cec5SDimitry Andric // Merge in values of predecessor blocks. 8420b57cec5SDimitry Andric bool isFirst = true; 8430b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = block->pred_begin(), 8440b57cec5SDimitry Andric E = block->pred_end(); I != E; ++I) { 8450b57cec5SDimitry Andric const CFGBlock *pred = *I; 8460b57cec5SDimitry Andric if (!pred) 8470b57cec5SDimitry Andric continue; 8480b57cec5SDimitry Andric if (wasAnalyzed[pred->getBlockID()]) { 8490b57cec5SDimitry Andric vals.mergeIntoScratch(vals.getValueVector(pred), isFirst); 8500b57cec5SDimitry Andric isFirst = false; 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric // Apply the transfer function. 8540b57cec5SDimitry Andric TransferFunctions tf(vals, cfg, block, ac, classification, handler); 8550b57cec5SDimitry Andric for (const auto &I : *block) { 8560b57cec5SDimitry Andric if (Optional<CFGStmt> cs = I.getAs<CFGStmt>()) 8570b57cec5SDimitry Andric tf.Visit(const_cast<Stmt *>(cs->getStmt())); 8580b57cec5SDimitry Andric } 859*5ffd83dbSDimitry Andric CFGTerminator terminator = block->getTerminator(); 860*5ffd83dbSDimitry Andric if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt())) 861*5ffd83dbSDimitry Andric if (as->isAsmGoto()) 862*5ffd83dbSDimitry Andric tf.Visit(as); 8630b57cec5SDimitry Andric return vals.updateValueVectorWithScratch(block); 8640b57cec5SDimitry Andric } 8650b57cec5SDimitry Andric 8660b57cec5SDimitry Andric namespace { 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric /// PruneBlocksHandler is a special UninitVariablesHandler that is used 8690b57cec5SDimitry Andric /// to detect when a CFGBlock has any *potential* use of an uninitialized 8700b57cec5SDimitry Andric /// variable. It is mainly used to prune out work during the final 8710b57cec5SDimitry Andric /// reporting pass. 8720b57cec5SDimitry Andric struct PruneBlocksHandler : public UninitVariablesHandler { 8730b57cec5SDimitry Andric /// Records if a CFGBlock had a potential use of an uninitialized variable. 8740b57cec5SDimitry Andric llvm::BitVector hadUse; 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric /// Records if any CFGBlock had a potential use of an uninitialized variable. 8770b57cec5SDimitry Andric bool hadAnyUse = false; 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric /// The current block to scribble use information. 8800b57cec5SDimitry Andric unsigned currentBlock = 0; 8810b57cec5SDimitry Andric 8820b57cec5SDimitry Andric PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {} 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric ~PruneBlocksHandler() override = default; 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric void handleUseOfUninitVariable(const VarDecl *vd, 8870b57cec5SDimitry Andric const UninitUse &use) override { 8880b57cec5SDimitry Andric hadUse[currentBlock] = true; 8890b57cec5SDimitry Andric hadAnyUse = true; 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric 892*5ffd83dbSDimitry Andric void handleConstRefUseOfUninitVariable(const VarDecl *vd, 893*5ffd83dbSDimitry Andric const UninitUse &use) override { 894*5ffd83dbSDimitry Andric hadUse[currentBlock] = true; 895*5ffd83dbSDimitry Andric hadAnyUse = true; 896*5ffd83dbSDimitry Andric } 897*5ffd83dbSDimitry Andric 8980b57cec5SDimitry Andric /// Called when the uninitialized variable analysis detects the 8990b57cec5SDimitry Andric /// idiom 'int x = x'. All other uses of 'x' within the initializer 9000b57cec5SDimitry Andric /// are handled by handleUseOfUninitVariable. 9010b57cec5SDimitry Andric void handleSelfInit(const VarDecl *vd) override { 9020b57cec5SDimitry Andric hadUse[currentBlock] = true; 9030b57cec5SDimitry Andric hadAnyUse = true; 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric }; 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric } // namespace 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric void clang::runUninitializedVariablesAnalysis( 9100b57cec5SDimitry Andric const DeclContext &dc, 9110b57cec5SDimitry Andric const CFG &cfg, 9120b57cec5SDimitry Andric AnalysisDeclContext &ac, 9130b57cec5SDimitry Andric UninitVariablesHandler &handler, 9140b57cec5SDimitry Andric UninitVariablesAnalysisStats &stats) { 9150b57cec5SDimitry Andric CFGBlockValues vals(cfg); 9160b57cec5SDimitry Andric vals.computeSetOfDeclarations(dc); 9170b57cec5SDimitry Andric if (vals.hasNoDeclarations()) 9180b57cec5SDimitry Andric return; 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric stats.NumVariablesAnalyzed = vals.getNumEntries(); 9210b57cec5SDimitry Andric 9220b57cec5SDimitry Andric // Precompute which expressions are uses and which are initializations. 9230b57cec5SDimitry Andric ClassifyRefs classification(ac); 9240b57cec5SDimitry Andric cfg.VisitBlockStmts(classification); 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric // Mark all variables uninitialized at the entry. 9270b57cec5SDimitry Andric const CFGBlock &entry = cfg.getEntry(); 9280b57cec5SDimitry Andric ValueVector &vec = vals.getValueVector(&entry); 9290b57cec5SDimitry Andric const unsigned n = vals.getNumEntries(); 9300b57cec5SDimitry Andric for (unsigned j = 0; j < n; ++j) { 9310b57cec5SDimitry Andric vec[j] = Uninitialized; 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric // Proceed with the workist. 935*5ffd83dbSDimitry Andric ForwardDataflowWorklist worklist(cfg, ac); 9360b57cec5SDimitry Andric llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); 9370b57cec5SDimitry Andric worklist.enqueueSuccessors(&cfg.getEntry()); 9380b57cec5SDimitry Andric llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); 9390b57cec5SDimitry Andric wasAnalyzed[cfg.getEntry().getBlockID()] = true; 9400b57cec5SDimitry Andric PruneBlocksHandler PBH(cfg.getNumBlockIDs()); 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric while (const CFGBlock *block = worklist.dequeue()) { 9430b57cec5SDimitry Andric PBH.currentBlock = block->getBlockID(); 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric // Did the block change? 9460b57cec5SDimitry Andric bool changed = runOnBlock(block, cfg, ac, vals, 9470b57cec5SDimitry Andric classification, wasAnalyzed, PBH); 9480b57cec5SDimitry Andric ++stats.NumBlockVisits; 9490b57cec5SDimitry Andric if (changed || !previouslyVisited[block->getBlockID()]) 9500b57cec5SDimitry Andric worklist.enqueueSuccessors(block); 9510b57cec5SDimitry Andric previouslyVisited[block->getBlockID()] = true; 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric if (!PBH.hadAnyUse) 9550b57cec5SDimitry Andric return; 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // Run through the blocks one more time, and report uninitialized variables. 9580b57cec5SDimitry Andric for (const auto *block : cfg) 9590b57cec5SDimitry Andric if (PBH.hadUse[block->getBlockID()]) { 9600b57cec5SDimitry Andric runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler); 9610b57cec5SDimitry Andric ++stats.NumBlockVisits; 9620b57cec5SDimitry Andric } 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric UninitVariablesHandler::~UninitVariablesHandler() = default; 966