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" 275ffd83dbSDimitry 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/PackedVector.h" 320b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 330b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 340b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 350b57cec5SDimitry Andric #include <algorithm> 360b57cec5SDimitry Andric #include <cassert> 37*bdd1243dSDimitry Andric #include <optional> 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric using namespace clang; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric #define DEBUG_LOGGING 0 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) { 440b57cec5SDimitry Andric if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() && 450b57cec5SDimitry Andric !vd->isExceptionVariable() && !vd->isInitCapture() && 460b57cec5SDimitry Andric !vd->isImplicit() && vd->getDeclContext() == dc) { 470b57cec5SDimitry Andric QualType ty = vd->getType(); 48*bdd1243dSDimitry Andric return ty->isScalarType() || ty->isVectorType() || ty->isRecordType() || 49*bdd1243dSDimitry Andric ty->isRVVType(); 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. 73*bdd1243dSDimitry Andric std::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 89*bdd1243dSDimitry Andric std::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()) 92*bdd1243dSDimitry Andric return std::nullopt; 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) { 150*bdd1243dSDimitry Andric std::optional<unsigned> idx = declToIndex.getValueIndex(vd); 151*bdd1243dSDimitry Andric return getValueVector(block)[*idx]; 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric }; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric } // namespace 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {} 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) { 1600b57cec5SDimitry Andric declToIndex.computeMap(dc); 1610b57cec5SDimitry Andric unsigned decls = declToIndex.size(); 1620b57cec5SDimitry Andric scratch.resize(decls); 1630b57cec5SDimitry Andric unsigned n = cfg.getNumBlockIDs(); 1640b57cec5SDimitry Andric if (!n) 1650b57cec5SDimitry Andric return; 1660b57cec5SDimitry Andric vals.resize(n); 1670b57cec5SDimitry Andric for (auto &val : vals) 1680b57cec5SDimitry Andric val.resize(decls); 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric #if DEBUG_LOGGING 1720b57cec5SDimitry Andric static void printVector(const CFGBlock *block, ValueVector &bv, 1730b57cec5SDimitry Andric unsigned num) { 1740b57cec5SDimitry Andric llvm::errs() << block->getBlockID() << " :"; 1750b57cec5SDimitry Andric for (const auto &i : bv) 1760b57cec5SDimitry Andric llvm::errs() << ' ' << i; 1770b57cec5SDimitry Andric llvm::errs() << " : " << num << '\n'; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric #endif 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric void CFGBlockValues::setAllScratchValues(Value V) { 1820b57cec5SDimitry Andric for (unsigned I = 0, E = scratch.size(); I != E; ++I) 1830b57cec5SDimitry Andric scratch[I] = V; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric void CFGBlockValues::mergeIntoScratch(ValueVector const &source, 1870b57cec5SDimitry Andric bool isFirst) { 1880b57cec5SDimitry Andric if (isFirst) 1890b57cec5SDimitry Andric scratch = source; 1900b57cec5SDimitry Andric else 1910b57cec5SDimitry Andric scratch |= source; 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) { 1950b57cec5SDimitry Andric ValueVector &dst = getValueVector(block); 1960b57cec5SDimitry Andric bool changed = (dst != scratch); 1970b57cec5SDimitry Andric if (changed) 1980b57cec5SDimitry Andric dst = scratch; 1990b57cec5SDimitry Andric #if DEBUG_LOGGING 2000b57cec5SDimitry Andric printVector(block, scratch, 0); 2010b57cec5SDimitry Andric #endif 2020b57cec5SDimitry Andric return changed; 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric void CFGBlockValues::resetScratch() { 2060b57cec5SDimitry Andric scratch.reset(); 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) { 210*bdd1243dSDimitry Andric return scratch[*declToIndex.getValueIndex(vd)]; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric //------------------------------------------------------------------------====// 2140b57cec5SDimitry Andric // Classification of DeclRefExprs as use or initialization. 2150b57cec5SDimitry Andric //====------------------------------------------------------------------------// 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric namespace { 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric class FindVarResult { 2200b57cec5SDimitry Andric const VarDecl *vd; 2210b57cec5SDimitry Andric const DeclRefExpr *dr; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric public: 2240b57cec5SDimitry Andric FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {} 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric const DeclRefExpr *getDeclRefExpr() const { return dr; } 2270b57cec5SDimitry Andric const VarDecl *getDecl() const { return vd; } 2280b57cec5SDimitry Andric }; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric } // namespace 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric static const Expr *stripCasts(ASTContext &C, const Expr *Ex) { 2330b57cec5SDimitry Andric while (Ex) { 2340b57cec5SDimitry Andric Ex = Ex->IgnoreParenNoopCasts(C); 2350b57cec5SDimitry Andric if (const auto *CE = dyn_cast<CastExpr>(Ex)) { 2360b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueBitCast) { 2370b57cec5SDimitry Andric Ex = CE->getSubExpr(); 2380b57cec5SDimitry Andric continue; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric break; 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric return Ex; 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric /// If E is an expression comprising a reference to a single variable, find that 2470b57cec5SDimitry Andric /// variable. 2480b57cec5SDimitry Andric static FindVarResult findVar(const Expr *E, const DeclContext *DC) { 2490b57cec5SDimitry Andric if (const auto *DRE = 2500b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E))) 2510b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) 2520b57cec5SDimitry Andric if (isTrackedVar(VD, DC)) 2530b57cec5SDimitry Andric return FindVarResult(VD, DRE); 2540b57cec5SDimitry Andric return FindVarResult(nullptr, nullptr); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric namespace { 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric /// Classify each DeclRefExpr as an initialization or a use. Any 2600b57cec5SDimitry Andric /// DeclRefExpr which isn't explicitly classified will be assumed to have 2610b57cec5SDimitry Andric /// escaped the analysis and will be treated as an initialization. 2620b57cec5SDimitry Andric class ClassifyRefs : public StmtVisitor<ClassifyRefs> { 2630b57cec5SDimitry Andric public: 2640b57cec5SDimitry Andric enum Class { 2650b57cec5SDimitry Andric Init, 2660b57cec5SDimitry Andric Use, 2670b57cec5SDimitry Andric SelfInit, 2685ffd83dbSDimitry Andric ConstRefUse, 2690b57cec5SDimitry Andric Ignore 2700b57cec5SDimitry Andric }; 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric private: 2730b57cec5SDimitry Andric const DeclContext *DC; 2740b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr *, Class> Classification; 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *VD) const { 2770b57cec5SDimitry Andric return ::isTrackedVar(VD, DC); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric void classify(const Expr *E, Class C); 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric public: 2830b57cec5SDimitry Andric ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {} 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *DS); 2860b57cec5SDimitry Andric void VisitUnaryOperator(UnaryOperator *UO); 2870b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *BO); 2880b57cec5SDimitry Andric void VisitCallExpr(CallExpr *CE); 2890b57cec5SDimitry Andric void VisitCastExpr(CastExpr *CE); 2900b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric void operator()(Stmt *S) { Visit(S); } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric Class get(const DeclRefExpr *DRE) const { 2950b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I 2960b57cec5SDimitry Andric = Classification.find(DRE); 2970b57cec5SDimitry Andric if (I != Classification.end()) 2980b57cec5SDimitry Andric return I->second; 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); 3010b57cec5SDimitry Andric if (!VD || !isTrackedVar(VD)) 3020b57cec5SDimitry Andric return Ignore; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric return Init; 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric }; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric } // namespace 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) { 3110b57cec5SDimitry Andric if (VD->getType()->isRecordType()) 3120b57cec5SDimitry Andric return nullptr; 3130b57cec5SDimitry Andric if (Expr *Init = VD->getInit()) { 3140b57cec5SDimitry Andric const auto *DRE = 3150b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init)); 3160b57cec5SDimitry Andric if (DRE && DRE->getDecl() == VD) 3170b57cec5SDimitry Andric return DRE; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric return nullptr; 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric void ClassifyRefs::classify(const Expr *E, Class C) { 3230b57cec5SDimitry Andric // The result of a ?: could also be an lvalue. 3240b57cec5SDimitry Andric E = E->IgnoreParens(); 3250b57cec5SDimitry Andric if (const auto *CO = dyn_cast<ConditionalOperator>(E)) { 3260b57cec5SDimitry Andric classify(CO->getTrueExpr(), C); 3270b57cec5SDimitry Andric classify(CO->getFalseExpr(), C); 3280b57cec5SDimitry Andric return; 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) { 3320b57cec5SDimitry Andric classify(BCO->getFalseExpr(), C); 3330b57cec5SDimitry Andric return; 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) { 3370b57cec5SDimitry Andric classify(OVE->getSourceExpr(), C); 3380b57cec5SDimitry Andric return; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric if (const auto *ME = dyn_cast<MemberExpr>(E)) { 3420b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { 3430b57cec5SDimitry Andric if (!VD->isStaticDataMember()) 3440b57cec5SDimitry Andric classify(ME->getBase(), C); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric return; 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric if (const auto *BO = dyn_cast<BinaryOperator>(E)) { 3500b57cec5SDimitry Andric switch (BO->getOpcode()) { 3510b57cec5SDimitry Andric case BO_PtrMemD: 3520b57cec5SDimitry Andric case BO_PtrMemI: 3530b57cec5SDimitry Andric classify(BO->getLHS(), C); 3540b57cec5SDimitry Andric return; 3550b57cec5SDimitry Andric case BO_Comma: 3560b57cec5SDimitry Andric classify(BO->getRHS(), C); 3570b57cec5SDimitry Andric return; 3580b57cec5SDimitry Andric default: 3590b57cec5SDimitry Andric return; 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric FindVarResult Var = findVar(E, DC); 3640b57cec5SDimitry Andric if (const DeclRefExpr *DRE = Var.getDeclRefExpr()) 3650b57cec5SDimitry Andric Classification[DRE] = std::max(Classification[DRE], C); 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) { 3690b57cec5SDimitry Andric for (auto *DI : DS->decls()) { 3700b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI); 3710b57cec5SDimitry Andric if (VD && isTrackedVar(VD)) 3720b57cec5SDimitry Andric if (const DeclRefExpr *DRE = getSelfInitExpr(VD)) 3730b57cec5SDimitry Andric Classification[DRE] = SelfInit; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) { 3780b57cec5SDimitry Andric // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this 3790b57cec5SDimitry Andric // is not a compound-assignment, we will treat it as initializing the variable 3800b57cec5SDimitry Andric // when TransferFunctions visits it. A compound-assignment does not affect 3810b57cec5SDimitry Andric // whether a variable is uninitialized, and there's no point counting it as a 3820b57cec5SDimitry Andric // use. 3830b57cec5SDimitry Andric if (BO->isCompoundAssignmentOp()) 3840b57cec5SDimitry Andric classify(BO->getLHS(), Use); 3850b57cec5SDimitry Andric else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma) 3860b57cec5SDimitry Andric classify(BO->getLHS(), Ignore); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) { 3900b57cec5SDimitry Andric // Increment and decrement are uses despite there being no lvalue-to-rvalue 3910b57cec5SDimitry Andric // conversion. 3920b57cec5SDimitry Andric if (UO->isIncrementDecrementOp()) 3930b57cec5SDimitry Andric classify(UO->getSubExpr(), Use); 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) { 3970b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) 3980b57cec5SDimitry Andric classify(cast<Expr>(S), Use); 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric static bool isPointerToConst(const QualType &QT) { 4020b57cec5SDimitry Andric return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified(); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4055ffd83dbSDimitry Andric static bool hasTrivialBody(CallExpr *CE) { 4065ffd83dbSDimitry Andric if (FunctionDecl *FD = CE->getDirectCallee()) { 4075ffd83dbSDimitry Andric if (FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) 4085ffd83dbSDimitry Andric return FTD->getTemplatedDecl()->hasTrivialBody(); 4095ffd83dbSDimitry Andric return FD->hasTrivialBody(); 4105ffd83dbSDimitry Andric } 4115ffd83dbSDimitry Andric return false; 4125ffd83dbSDimitry Andric } 4135ffd83dbSDimitry Andric 4140b57cec5SDimitry Andric void ClassifyRefs::VisitCallExpr(CallExpr *CE) { 4150b57cec5SDimitry Andric // Classify arguments to std::move as used. 4160b57cec5SDimitry Andric if (CE->isCallToStdMove()) { 4170b57cec5SDimitry Andric // RecordTypes are handled in SemaDeclCXX.cpp. 4180b57cec5SDimitry Andric if (!CE->getArg(0)->getType()->isRecordType()) 4190b57cec5SDimitry Andric classify(CE->getArg(0), Use); 4200b57cec5SDimitry Andric return; 4210b57cec5SDimitry Andric } 4225ffd83dbSDimitry Andric bool isTrivialBody = hasTrivialBody(CE); 4235ffd83dbSDimitry Andric // If a value is passed by const pointer to a function, 4240b57cec5SDimitry Andric // we should not assume that it is initialized by the call, and we 4250b57cec5SDimitry Andric // conservatively do not assume that it is used. 4265ffd83dbSDimitry Andric // If a value is passed by const reference to a function, 4275ffd83dbSDimitry Andric // it should already be initialized. 4280b57cec5SDimitry Andric for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end(); 4290b57cec5SDimitry Andric I != E; ++I) { 4300b57cec5SDimitry Andric if ((*I)->isGLValue()) { 4310b57cec5SDimitry Andric if ((*I)->getType().isConstQualified()) 4325ffd83dbSDimitry Andric classify((*I), isTrivialBody ? Ignore : ConstRefUse); 4330b57cec5SDimitry Andric } else if (isPointerToConst((*I)->getType())) { 4340b57cec5SDimitry Andric const Expr *Ex = stripCasts(DC->getParentASTContext(), *I); 4350b57cec5SDimitry Andric const auto *UO = dyn_cast<UnaryOperator>(Ex); 4360b57cec5SDimitry Andric if (UO && UO->getOpcode() == UO_AddrOf) 4370b57cec5SDimitry Andric Ex = UO->getSubExpr(); 4380b57cec5SDimitry Andric classify(Ex, Ignore); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric void ClassifyRefs::VisitCastExpr(CastExpr *CE) { 4440b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueToRValue) 4450b57cec5SDimitry Andric classify(CE->getSubExpr(), Use); 4460b57cec5SDimitry Andric else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) { 4470b57cec5SDimitry Andric if (CSE->getType()->isVoidType()) { 4480b57cec5SDimitry Andric // Squelch any detected load of an uninitialized value if 4490b57cec5SDimitry Andric // we cast it to void. 4500b57cec5SDimitry Andric // e.g. (void) x; 4510b57cec5SDimitry Andric classify(CSE->getSubExpr(), Ignore); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric //------------------------------------------------------------------------====// 4570b57cec5SDimitry Andric // Transfer function for uninitialized values analysis. 4580b57cec5SDimitry Andric //====------------------------------------------------------------------------// 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric namespace { 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric class TransferFunctions : public StmtVisitor<TransferFunctions> { 4630b57cec5SDimitry Andric CFGBlockValues &vals; 4640b57cec5SDimitry Andric const CFG &cfg; 4650b57cec5SDimitry Andric const CFGBlock *block; 4660b57cec5SDimitry Andric AnalysisDeclContext ∾ 4670b57cec5SDimitry Andric const ClassifyRefs &classification; 4680b57cec5SDimitry Andric ObjCNoReturn objCNoRet; 4690b57cec5SDimitry Andric UninitVariablesHandler &handler; 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric public: 4720b57cec5SDimitry Andric TransferFunctions(CFGBlockValues &vals, const CFG &cfg, 4730b57cec5SDimitry Andric const CFGBlock *block, AnalysisDeclContext &ac, 4740b57cec5SDimitry Andric const ClassifyRefs &classification, 4750b57cec5SDimitry Andric UninitVariablesHandler &handler) 4760b57cec5SDimitry Andric : vals(vals), cfg(cfg), block(block), ac(ac), 4770b57cec5SDimitry Andric classification(classification), objCNoRet(ac.getASTContext()), 4780b57cec5SDimitry Andric handler(handler) {} 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric void reportUse(const Expr *ex, const VarDecl *vd); 4815ffd83dbSDimitry Andric void reportConstRefUse(const Expr *ex, const VarDecl *vd); 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *bo); 4840b57cec5SDimitry Andric void VisitBlockExpr(BlockExpr *be); 4850b57cec5SDimitry Andric void VisitCallExpr(CallExpr *ce); 4860b57cec5SDimitry Andric void VisitDeclRefExpr(DeclRefExpr *dr); 4870b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *ds); 4885ffd83dbSDimitry Andric void VisitGCCAsmStmt(GCCAsmStmt *as); 4890b57cec5SDimitry Andric void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS); 4900b57cec5SDimitry Andric void VisitObjCMessageExpr(ObjCMessageExpr *ME); 4910b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED); 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *vd) { 4940b57cec5SDimitry Andric return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl())); 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric FindVarResult findVar(const Expr *ex) { 4980b57cec5SDimitry Andric return ::findVar(ex, cast<DeclContext>(ac.getDecl())); 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) { 5020b57cec5SDimitry Andric UninitUse Use(ex, isAlwaysUninit(v)); 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric assert(isUninitialized(v)); 5050b57cec5SDimitry Andric if (Use.getKind() == UninitUse::Always) 5060b57cec5SDimitry Andric return Use; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric // If an edge which leads unconditionally to this use did not initialize 5090b57cec5SDimitry Andric // the variable, we can say something stronger than 'may be uninitialized': 5100b57cec5SDimitry Andric // we can say 'either it's used uninitialized or you have dead code'. 5110b57cec5SDimitry Andric // 5120b57cec5SDimitry Andric // We track the number of successors of a node which have been visited, and 5130b57cec5SDimitry Andric // visit a node once we have visited all of its successors. Only edges where 5140b57cec5SDimitry Andric // the variable might still be uninitialized are followed. Since a variable 5150b57cec5SDimitry Andric // can't transfer from being initialized to being uninitialized, this will 5160b57cec5SDimitry Andric // trace out the subgraph which inevitably leads to the use and does not 5170b57cec5SDimitry Andric // initialize the variable. We do not want to skip past loops, since their 5180b57cec5SDimitry Andric // non-termination might be correlated with the initialization condition. 5190b57cec5SDimitry Andric // 5200b57cec5SDimitry Andric // For example: 5210b57cec5SDimitry Andric // 5220b57cec5SDimitry Andric // void f(bool a, bool b) { 5230b57cec5SDimitry Andric // block1: int n; 5240b57cec5SDimitry Andric // if (a) { 5250b57cec5SDimitry Andric // block2: if (b) 5260b57cec5SDimitry Andric // block3: n = 1; 5270b57cec5SDimitry Andric // block4: } else if (b) { 5280b57cec5SDimitry Andric // block5: while (!a) { 5290b57cec5SDimitry Andric // block6: do_work(&a); 5300b57cec5SDimitry Andric // n = 2; 5310b57cec5SDimitry Andric // } 5320b57cec5SDimitry Andric // } 5330b57cec5SDimitry Andric // block7: if (a) 5340b57cec5SDimitry Andric // block8: g(); 5350b57cec5SDimitry Andric // block9: return n; 5360b57cec5SDimitry Andric // } 5370b57cec5SDimitry Andric // 5380b57cec5SDimitry Andric // Starting from the maybe-uninitialized use in block 9: 5390b57cec5SDimitry Andric // * Block 7 is not visited because we have only visited one of its two 5400b57cec5SDimitry Andric // successors. 5410b57cec5SDimitry Andric // * Block 8 is visited because we've visited its only successor. 5420b57cec5SDimitry Andric // From block 8: 5430b57cec5SDimitry Andric // * Block 7 is visited because we've now visited both of its successors. 5440b57cec5SDimitry Andric // From block 7: 5450b57cec5SDimitry Andric // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all 5460b57cec5SDimitry Andric // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively). 5470b57cec5SDimitry Andric // * Block 3 is not visited because it initializes 'n'. 5480b57cec5SDimitry Andric // Now the algorithm terminates, having visited blocks 7 and 8, and having 5490b57cec5SDimitry Andric // found the frontier is blocks 2, 4, and 5. 5500b57cec5SDimitry Andric // 5510b57cec5SDimitry Andric // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2 5520b57cec5SDimitry Andric // and 4), so we report that any time either of those edges is taken (in 5530b57cec5SDimitry Andric // each case when 'b == false'), 'n' is used uninitialized. 5540b57cec5SDimitry Andric SmallVector<const CFGBlock*, 32> Queue; 5550b57cec5SDimitry Andric SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0); 5560b57cec5SDimitry Andric Queue.push_back(block); 5570b57cec5SDimitry Andric // Specify that we've already visited all successors of the starting block. 5580b57cec5SDimitry Andric // This has the dual purpose of ensuring we never add it to the queue, and 5590b57cec5SDimitry Andric // of marking it as not being a candidate element of the frontier. 5600b57cec5SDimitry Andric SuccsVisited[block->getBlockID()] = block->succ_size(); 5610b57cec5SDimitry Andric while (!Queue.empty()) { 5620b57cec5SDimitry Andric const CFGBlock *B = Queue.pop_back_val(); 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric // If the use is always reached from the entry block, make a note of that. 5650b57cec5SDimitry Andric if (B == &cfg.getEntry()) 5660b57cec5SDimitry Andric Use.setUninitAfterCall(); 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end(); 5690b57cec5SDimitry Andric I != E; ++I) { 5700b57cec5SDimitry Andric const CFGBlock *Pred = *I; 5710b57cec5SDimitry Andric if (!Pred) 5720b57cec5SDimitry Andric continue; 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric Value AtPredExit = vals.getValue(Pred, B, vd); 5750b57cec5SDimitry Andric if (AtPredExit == Initialized) 5760b57cec5SDimitry Andric // This block initializes the variable. 5770b57cec5SDimitry Andric continue; 5780b57cec5SDimitry Andric if (AtPredExit == MayUninitialized && 5790b57cec5SDimitry Andric vals.getValue(B, nullptr, vd) == Uninitialized) { 5800b57cec5SDimitry Andric // This block declares the variable (uninitialized), and is reachable 5810b57cec5SDimitry Andric // from a block that initializes the variable. We can't guarantee to 5820b57cec5SDimitry Andric // give an earlier location for the diagnostic (and it appears that 5830b57cec5SDimitry Andric // this code is intended to be reachable) so give a diagnostic here 5840b57cec5SDimitry Andric // and go no further down this path. 5850b57cec5SDimitry Andric Use.setUninitAfterDecl(); 5860b57cec5SDimitry Andric continue; 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5895ffd83dbSDimitry Andric if (AtPredExit == MayUninitialized) { 5905ffd83dbSDimitry Andric // If the predecessor's terminator is an "asm goto" that initializes 5910eae32dcSDimitry Andric // the variable, then don't count it as "initialized" on the indirect 5920eae32dcSDimitry Andric // paths. 5935ffd83dbSDimitry Andric CFGTerminator term = Pred->getTerminator(); 5945ffd83dbSDimitry Andric if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) { 5955ffd83dbSDimitry Andric const CFGBlock *fallthrough = *Pred->succ_begin(); 5965ffd83dbSDimitry Andric if (as->isAsmGoto() && 5975ffd83dbSDimitry Andric llvm::any_of(as->outputs(), [&](const Expr *output) { 5985ffd83dbSDimitry Andric return vd == findVar(output).getDecl() && 5995ffd83dbSDimitry Andric llvm::any_of(as->labels(), 6005ffd83dbSDimitry Andric [&](const AddrLabelExpr *label) { 6015ffd83dbSDimitry Andric return label->getLabel()->getStmt() == B->Label && 6025ffd83dbSDimitry Andric B != fallthrough; 6035ffd83dbSDimitry Andric }); 6045ffd83dbSDimitry Andric })) { 6055ffd83dbSDimitry Andric Use.setUninitAfterDecl(); 6065ffd83dbSDimitry Andric continue; 6075ffd83dbSDimitry Andric } 6085ffd83dbSDimitry Andric } 6095ffd83dbSDimitry Andric } 6105ffd83dbSDimitry Andric 6110b57cec5SDimitry Andric unsigned &SV = SuccsVisited[Pred->getBlockID()]; 6120b57cec5SDimitry Andric if (!SV) { 6130b57cec5SDimitry Andric // When visiting the first successor of a block, mark all NULL 6140b57cec5SDimitry Andric // successors as having been visited. 6150b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(), 6160b57cec5SDimitry Andric SE = Pred->succ_end(); 6170b57cec5SDimitry Andric SI != SE; ++SI) 6180b57cec5SDimitry Andric if (!*SI) 6190b57cec5SDimitry Andric ++SV; 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric if (++SV == Pred->succ_size()) 6230b57cec5SDimitry Andric // All paths from this block lead to the use and don't initialize the 6240b57cec5SDimitry Andric // variable. 6250b57cec5SDimitry Andric Queue.push_back(Pred); 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric // Scan the frontier, looking for blocks where the variable was 6300b57cec5SDimitry Andric // uninitialized. 6310b57cec5SDimitry Andric for (const auto *Block : cfg) { 6320b57cec5SDimitry Andric unsigned BlockID = Block->getBlockID(); 6330b57cec5SDimitry Andric const Stmt *Term = Block->getTerminatorStmt(); 6340b57cec5SDimitry Andric if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() && 6350b57cec5SDimitry Andric Term) { 6360b57cec5SDimitry Andric // This block inevitably leads to the use. If we have an edge from here 6370b57cec5SDimitry Andric // to a post-dominator block, and the variable is uninitialized on that 6380b57cec5SDimitry Andric // edge, we have found a bug. 6390b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator I = Block->succ_begin(), 6400b57cec5SDimitry Andric E = Block->succ_end(); I != E; ++I) { 6410b57cec5SDimitry Andric const CFGBlock *Succ = *I; 6420b57cec5SDimitry Andric if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() && 6430b57cec5SDimitry Andric vals.getValue(Block, Succ, vd) == Uninitialized) { 6440b57cec5SDimitry Andric // Switch cases are a special case: report the label to the caller 6450b57cec5SDimitry Andric // as the 'terminator', not the switch statement itself. Suppress 6460b57cec5SDimitry Andric // situations where no label matched: we can't be sure that's 6470b57cec5SDimitry Andric // possible. 6480b57cec5SDimitry Andric if (isa<SwitchStmt>(Term)) { 6490b57cec5SDimitry Andric const Stmt *Label = Succ->getLabel(); 6500b57cec5SDimitry Andric if (!Label || !isa<SwitchCase>(Label)) 6510b57cec5SDimitry Andric // Might not be possible. 6520b57cec5SDimitry Andric continue; 6530b57cec5SDimitry Andric UninitUse::Branch Branch; 6540b57cec5SDimitry Andric Branch.Terminator = Label; 6550b57cec5SDimitry Andric Branch.Output = 0; // Ignored. 6560b57cec5SDimitry Andric Use.addUninitBranch(Branch); 6570b57cec5SDimitry Andric } else { 6580b57cec5SDimitry Andric UninitUse::Branch Branch; 6590b57cec5SDimitry Andric Branch.Terminator = Term; 6600b57cec5SDimitry Andric Branch.Output = I - Block->succ_begin(); 6610b57cec5SDimitry Andric Use.addUninitBranch(Branch); 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric return Use; 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric }; 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric } // namespace 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) { 6750b57cec5SDimitry Andric Value v = vals[vd]; 6760b57cec5SDimitry Andric if (isUninitialized(v)) 6770b57cec5SDimitry Andric handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 6780b57cec5SDimitry Andric } 6790b57cec5SDimitry Andric 6805ffd83dbSDimitry Andric void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) { 6815ffd83dbSDimitry Andric Value v = vals[vd]; 6825ffd83dbSDimitry Andric if (isAlwaysUninit(v)) 6835ffd83dbSDimitry Andric handler.handleConstRefUseOfUninitVariable(vd, getUninitUse(ex, vd, v)); 6845ffd83dbSDimitry Andric } 6855ffd83dbSDimitry Andric 6860b57cec5SDimitry Andric void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) { 6870b57cec5SDimitry Andric // This represents an initialization of the 'element' value. 6880b57cec5SDimitry Andric if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) { 6890b57cec5SDimitry Andric const auto *VD = cast<VarDecl>(DS->getSingleDecl()); 6900b57cec5SDimitry Andric if (isTrackedVar(VD)) 6910b57cec5SDimitry Andric vals[VD] = Initialized; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric void TransferFunctions::VisitOMPExecutableDirective( 6960b57cec5SDimitry Andric OMPExecutableDirective *ED) { 6970b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) { 6980b57cec5SDimitry Andric assert(S && "Expected non-null used-in-clause child."); 6990b57cec5SDimitry Andric Visit(S); 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric if (!ED->isStandaloneDirective()) 7020b57cec5SDimitry Andric Visit(ED->getStructuredBlock()); 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric void TransferFunctions::VisitBlockExpr(BlockExpr *be) { 7060b57cec5SDimitry Andric const BlockDecl *bd = be->getBlockDecl(); 7070b57cec5SDimitry Andric for (const auto &I : bd->captures()) { 7080b57cec5SDimitry Andric const VarDecl *vd = I.getVariable(); 7090b57cec5SDimitry Andric if (!isTrackedVar(vd)) 7100b57cec5SDimitry Andric continue; 7110b57cec5SDimitry Andric if (I.isByRef()) { 7120b57cec5SDimitry Andric vals[vd] = Initialized; 7130b57cec5SDimitry Andric continue; 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric reportUse(be, vd); 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric } 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric void TransferFunctions::VisitCallExpr(CallExpr *ce) { 7200b57cec5SDimitry Andric if (Decl *Callee = ce->getCalleeDecl()) { 7210b57cec5SDimitry Andric if (Callee->hasAttr<ReturnsTwiceAttr>()) { 7220b57cec5SDimitry Andric // After a call to a function like setjmp or vfork, any variable which is 7230b57cec5SDimitry Andric // initialized anywhere within this function may now be initialized. For 7240b57cec5SDimitry Andric // now, just assume such a call initializes all variables. FIXME: Only 7250b57cec5SDimitry Andric // mark variables as initialized if they have an initializer which is 7260b57cec5SDimitry Andric // reachable from here. 7270b57cec5SDimitry Andric vals.setAllScratchValues(Initialized); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) { 7300b57cec5SDimitry Andric // Functions labeled like "analyzer_noreturn" are often used to denote 7310b57cec5SDimitry Andric // "panic" functions that in special debug situations can still return, 7320b57cec5SDimitry Andric // but for the most part should not be treated as returning. This is a 7330b57cec5SDimitry Andric // useful annotation borrowed from the static analyzer that is useful for 7340b57cec5SDimitry Andric // suppressing branch-specific false positives when we call one of these 7350b57cec5SDimitry Andric // functions but keep pretending the path continues (when in reality the 7360b57cec5SDimitry Andric // user doesn't care). 7370b57cec5SDimitry Andric vals.setAllScratchValues(Unknown); 7380b57cec5SDimitry Andric } 7390b57cec5SDimitry Andric } 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) { 7430b57cec5SDimitry Andric switch (classification.get(dr)) { 7440b57cec5SDimitry Andric case ClassifyRefs::Ignore: 7450b57cec5SDimitry Andric break; 7460b57cec5SDimitry Andric case ClassifyRefs::Use: 7470b57cec5SDimitry Andric reportUse(dr, cast<VarDecl>(dr->getDecl())); 7480b57cec5SDimitry Andric break; 7490b57cec5SDimitry Andric case ClassifyRefs::Init: 7500b57cec5SDimitry Andric vals[cast<VarDecl>(dr->getDecl())] = Initialized; 7510b57cec5SDimitry Andric break; 7520b57cec5SDimitry Andric case ClassifyRefs::SelfInit: 7530b57cec5SDimitry Andric handler.handleSelfInit(cast<VarDecl>(dr->getDecl())); 7540b57cec5SDimitry Andric break; 7555ffd83dbSDimitry Andric case ClassifyRefs::ConstRefUse: 7565ffd83dbSDimitry Andric reportConstRefUse(dr, cast<VarDecl>(dr->getDecl())); 7575ffd83dbSDimitry Andric break; 7580b57cec5SDimitry Andric } 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) { 7620b57cec5SDimitry Andric if (BO->getOpcode() == BO_Assign) { 7630b57cec5SDimitry Andric FindVarResult Var = findVar(BO->getLHS()); 7640b57cec5SDimitry Andric if (const VarDecl *VD = Var.getDecl()) 7650b57cec5SDimitry Andric vals[VD] = Initialized; 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric void TransferFunctions::VisitDeclStmt(DeclStmt *DS) { 7700b57cec5SDimitry Andric for (auto *DI : DS->decls()) { 7710b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI); 7720b57cec5SDimitry Andric if (VD && isTrackedVar(VD)) { 7730b57cec5SDimitry Andric if (getSelfInitExpr(VD)) { 7740b57cec5SDimitry Andric // If the initializer consists solely of a reference to itself, we 7750b57cec5SDimitry Andric // explicitly mark the variable as uninitialized. This allows code 7760b57cec5SDimitry Andric // like the following: 7770b57cec5SDimitry Andric // 7780b57cec5SDimitry Andric // int x = x; 7790b57cec5SDimitry Andric // 7800b57cec5SDimitry Andric // to deliberately leave a variable uninitialized. Different analysis 7810b57cec5SDimitry Andric // clients can detect this pattern and adjust their reporting 7820b57cec5SDimitry Andric // appropriately, but we need to continue to analyze subsequent uses 7830b57cec5SDimitry Andric // of the variable. 7840b57cec5SDimitry Andric vals[VD] = Uninitialized; 7850b57cec5SDimitry Andric } else if (VD->getInit()) { 7860b57cec5SDimitry Andric // Treat the new variable as initialized. 7870b57cec5SDimitry Andric vals[VD] = Initialized; 7880b57cec5SDimitry Andric } else { 7890b57cec5SDimitry Andric // No initializer: the variable is now uninitialized. This matters 7900b57cec5SDimitry Andric // for cases like: 7910b57cec5SDimitry Andric // while (...) { 7920b57cec5SDimitry Andric // int n; 7930b57cec5SDimitry Andric // use(n); 7940b57cec5SDimitry Andric // n = 0; 7950b57cec5SDimitry Andric // } 7960b57cec5SDimitry Andric // FIXME: Mark the variable as uninitialized whenever its scope is 7970b57cec5SDimitry Andric // left, since its scope could be re-entered by a jump over the 7980b57cec5SDimitry Andric // declaration. 7990b57cec5SDimitry Andric vals[VD] = Uninitialized; 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric } 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 8055ffd83dbSDimitry Andric void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) { 8065ffd83dbSDimitry Andric // An "asm goto" statement is a terminator that may initialize some variables. 8075ffd83dbSDimitry Andric if (!as->isAsmGoto()) 8085ffd83dbSDimitry Andric return; 8095ffd83dbSDimitry Andric 8100eae32dcSDimitry Andric ASTContext &C = ac.getASTContext(); 8110eae32dcSDimitry Andric for (const Expr *O : as->outputs()) { 8120eae32dcSDimitry Andric const Expr *Ex = stripCasts(C, O); 8130eae32dcSDimitry Andric 8140eae32dcSDimitry Andric // Strip away any unary operators. Invalid l-values are reported by other 8150eae32dcSDimitry Andric // semantic analysis passes. 8160eae32dcSDimitry Andric while (const auto *UO = dyn_cast<UnaryOperator>(Ex)) 8170eae32dcSDimitry Andric Ex = stripCasts(C, UO->getSubExpr()); 8180eae32dcSDimitry Andric 81904eeddc0SDimitry Andric // Mark the variable as potentially uninitialized for those cases where 82004eeddc0SDimitry Andric // it's used on an indirect path, where it's not guaranteed to be 82104eeddc0SDimitry Andric // defined. 8220eae32dcSDimitry Andric if (const VarDecl *VD = findVar(Ex).getDecl()) 8235ffd83dbSDimitry Andric vals[VD] = MayUninitialized; 8245ffd83dbSDimitry Andric } 8250eae32dcSDimitry Andric } 8265ffd83dbSDimitry Andric 8270b57cec5SDimitry Andric void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) { 8280b57cec5SDimitry Andric // If the Objective-C message expression is an implicit no-return that 8290b57cec5SDimitry Andric // is not modeled in the CFG, set the tracked dataflow values to Unknown. 8300b57cec5SDimitry Andric if (objCNoRet.isImplicitNoReturn(ME)) { 8310b57cec5SDimitry Andric vals.setAllScratchValues(Unknown); 8320b57cec5SDimitry Andric } 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric //------------------------------------------------------------------------====// 8360b57cec5SDimitry Andric // High-level "driver" logic for uninitialized values analysis. 8370b57cec5SDimitry Andric //====------------------------------------------------------------------------// 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric static bool runOnBlock(const CFGBlock *block, const CFG &cfg, 8400b57cec5SDimitry Andric AnalysisDeclContext &ac, CFGBlockValues &vals, 8410b57cec5SDimitry Andric const ClassifyRefs &classification, 8420b57cec5SDimitry Andric llvm::BitVector &wasAnalyzed, 8430b57cec5SDimitry Andric UninitVariablesHandler &handler) { 8440b57cec5SDimitry Andric wasAnalyzed[block->getBlockID()] = true; 8450b57cec5SDimitry Andric vals.resetScratch(); 8460b57cec5SDimitry Andric // Merge in values of predecessor blocks. 8470b57cec5SDimitry Andric bool isFirst = true; 8480b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = block->pred_begin(), 8490b57cec5SDimitry Andric E = block->pred_end(); I != E; ++I) { 8500b57cec5SDimitry Andric const CFGBlock *pred = *I; 8510b57cec5SDimitry Andric if (!pred) 8520b57cec5SDimitry Andric continue; 8530b57cec5SDimitry Andric if (wasAnalyzed[pred->getBlockID()]) { 8540b57cec5SDimitry Andric vals.mergeIntoScratch(vals.getValueVector(pred), isFirst); 8550b57cec5SDimitry Andric isFirst = false; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric // Apply the transfer function. 8590b57cec5SDimitry Andric TransferFunctions tf(vals, cfg, block, ac, classification, handler); 8600b57cec5SDimitry Andric for (const auto &I : *block) { 861*bdd1243dSDimitry Andric if (std::optional<CFGStmt> cs = I.getAs<CFGStmt>()) 8620b57cec5SDimitry Andric tf.Visit(const_cast<Stmt *>(cs->getStmt())); 8630b57cec5SDimitry Andric } 8645ffd83dbSDimitry Andric CFGTerminator terminator = block->getTerminator(); 8655ffd83dbSDimitry Andric if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt())) 8665ffd83dbSDimitry Andric if (as->isAsmGoto()) 8675ffd83dbSDimitry Andric tf.Visit(as); 8680b57cec5SDimitry Andric return vals.updateValueVectorWithScratch(block); 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric namespace { 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric /// PruneBlocksHandler is a special UninitVariablesHandler that is used 8740b57cec5SDimitry Andric /// to detect when a CFGBlock has any *potential* use of an uninitialized 8750b57cec5SDimitry Andric /// variable. It is mainly used to prune out work during the final 8760b57cec5SDimitry Andric /// reporting pass. 8770b57cec5SDimitry Andric struct PruneBlocksHandler : public UninitVariablesHandler { 8780b57cec5SDimitry Andric /// Records if a CFGBlock had a potential use of an uninitialized variable. 8790b57cec5SDimitry Andric llvm::BitVector hadUse; 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric /// Records if any CFGBlock had a potential use of an uninitialized variable. 8820b57cec5SDimitry Andric bool hadAnyUse = false; 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric /// The current block to scribble use information. 8850b57cec5SDimitry Andric unsigned currentBlock = 0; 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {} 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric ~PruneBlocksHandler() override = default; 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric void handleUseOfUninitVariable(const VarDecl *vd, 8920b57cec5SDimitry Andric const UninitUse &use) override { 8930b57cec5SDimitry Andric hadUse[currentBlock] = true; 8940b57cec5SDimitry Andric hadAnyUse = true; 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric 8975ffd83dbSDimitry Andric void handleConstRefUseOfUninitVariable(const VarDecl *vd, 8985ffd83dbSDimitry Andric const UninitUse &use) override { 8995ffd83dbSDimitry Andric hadUse[currentBlock] = true; 9005ffd83dbSDimitry Andric hadAnyUse = true; 9015ffd83dbSDimitry Andric } 9025ffd83dbSDimitry Andric 9030b57cec5SDimitry Andric /// Called when the uninitialized variable analysis detects the 9040b57cec5SDimitry Andric /// idiom 'int x = x'. All other uses of 'x' within the initializer 9050b57cec5SDimitry Andric /// are handled by handleUseOfUninitVariable. 9060b57cec5SDimitry Andric void handleSelfInit(const VarDecl *vd) override { 9070b57cec5SDimitry Andric hadUse[currentBlock] = true; 9080b57cec5SDimitry Andric hadAnyUse = true; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric }; 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric } // namespace 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric void clang::runUninitializedVariablesAnalysis( 9150b57cec5SDimitry Andric const DeclContext &dc, 9160b57cec5SDimitry Andric const CFG &cfg, 9170b57cec5SDimitry Andric AnalysisDeclContext &ac, 9180b57cec5SDimitry Andric UninitVariablesHandler &handler, 9190b57cec5SDimitry Andric UninitVariablesAnalysisStats &stats) { 9200b57cec5SDimitry Andric CFGBlockValues vals(cfg); 9210b57cec5SDimitry Andric vals.computeSetOfDeclarations(dc); 9220b57cec5SDimitry Andric if (vals.hasNoDeclarations()) 9230b57cec5SDimitry Andric return; 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric stats.NumVariablesAnalyzed = vals.getNumEntries(); 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric // Precompute which expressions are uses and which are initializations. 9280b57cec5SDimitry Andric ClassifyRefs classification(ac); 9290b57cec5SDimitry Andric cfg.VisitBlockStmts(classification); 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric // Mark all variables uninitialized at the entry. 9320b57cec5SDimitry Andric const CFGBlock &entry = cfg.getEntry(); 9330b57cec5SDimitry Andric ValueVector &vec = vals.getValueVector(&entry); 9340b57cec5SDimitry Andric const unsigned n = vals.getNumEntries(); 9350b57cec5SDimitry Andric for (unsigned j = 0; j < n; ++j) { 9360b57cec5SDimitry Andric vec[j] = Uninitialized; 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // Proceed with the workist. 9405ffd83dbSDimitry Andric ForwardDataflowWorklist worklist(cfg, ac); 9410b57cec5SDimitry Andric llvm::BitVector previouslyVisited(cfg.getNumBlockIDs()); 9420b57cec5SDimitry Andric worklist.enqueueSuccessors(&cfg.getEntry()); 9430b57cec5SDimitry Andric llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false); 9440b57cec5SDimitry Andric wasAnalyzed[cfg.getEntry().getBlockID()] = true; 9450b57cec5SDimitry Andric PruneBlocksHandler PBH(cfg.getNumBlockIDs()); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric while (const CFGBlock *block = worklist.dequeue()) { 9480b57cec5SDimitry Andric PBH.currentBlock = block->getBlockID(); 9490b57cec5SDimitry Andric 9500b57cec5SDimitry Andric // Did the block change? 9510b57cec5SDimitry Andric bool changed = runOnBlock(block, cfg, ac, vals, 9520b57cec5SDimitry Andric classification, wasAnalyzed, PBH); 9530b57cec5SDimitry Andric ++stats.NumBlockVisits; 9540b57cec5SDimitry Andric if (changed || !previouslyVisited[block->getBlockID()]) 9550b57cec5SDimitry Andric worklist.enqueueSuccessors(block); 9560b57cec5SDimitry Andric previouslyVisited[block->getBlockID()] = true; 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric if (!PBH.hadAnyUse) 9600b57cec5SDimitry Andric return; 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric // Run through the blocks one more time, and report uninitialized variables. 9630b57cec5SDimitry Andric for (const auto *block : cfg) 9640b57cec5SDimitry Andric if (PBH.hadUse[block->getBlockID()]) { 9650b57cec5SDimitry Andric runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler); 9660b57cec5SDimitry Andric ++stats.NumBlockVisits; 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric 9700b57cec5SDimitry Andric UninitVariablesHandler::~UninitVariablesHandler() = default; 971