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>
37bdd1243dSDimitry Andric #include <optional>
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric using namespace clang;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric #define DEBUG_LOGGING 0
420b57cec5SDimitry Andric
recordIsNotEmpty(const RecordDecl * RD)4306c3fb27SDimitry Andric static bool recordIsNotEmpty(const RecordDecl *RD) {
4406c3fb27SDimitry Andric // We consider a record decl to be empty if it contains only unnamed bit-
4506c3fb27SDimitry Andric // fields, zero-width fields, and fields of empty record type.
4606c3fb27SDimitry Andric for (const auto *FD : RD->fields()) {
47*0fca6ea1SDimitry Andric if (FD->isUnnamedBitField())
4806c3fb27SDimitry Andric continue;
4906c3fb27SDimitry Andric if (FD->isZeroSize(FD->getASTContext()))
5006c3fb27SDimitry Andric continue;
5106c3fb27SDimitry Andric // The only case remaining to check is for a field declaration of record
5206c3fb27SDimitry Andric // type and whether that record itself is empty.
5306c3fb27SDimitry Andric if (const auto *FieldRD = FD->getType()->getAsRecordDecl();
5406c3fb27SDimitry Andric !FieldRD || recordIsNotEmpty(FieldRD))
5506c3fb27SDimitry Andric return true;
5606c3fb27SDimitry Andric }
5706c3fb27SDimitry Andric return false;
5806c3fb27SDimitry Andric }
5906c3fb27SDimitry Andric
isTrackedVar(const VarDecl * vd,const DeclContext * dc)600b57cec5SDimitry Andric static bool isTrackedVar(const VarDecl *vd, const DeclContext *dc) {
610b57cec5SDimitry Andric if (vd->isLocalVarDecl() && !vd->hasGlobalStorage() &&
6206c3fb27SDimitry Andric !vd->isExceptionVariable() && !vd->isInitCapture() && !vd->isImplicit() &&
6306c3fb27SDimitry Andric vd->getDeclContext() == dc) {
640b57cec5SDimitry Andric QualType ty = vd->getType();
6506c3fb27SDimitry Andric if (const auto *RD = ty->getAsRecordDecl())
6606c3fb27SDimitry Andric return recordIsNotEmpty(RD);
675f757f3fSDimitry Andric return ty->isScalarType() || ty->isVectorType() || ty->isRVVSizelessBuiltinType();
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric return false;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric //------------------------------------------------------------------------====//
730b57cec5SDimitry Andric // DeclToIndex: a mapping from Decls we track to value indices.
740b57cec5SDimitry Andric //====------------------------------------------------------------------------//
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric namespace {
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric class DeclToIndex {
790b57cec5SDimitry Andric llvm::DenseMap<const VarDecl *, unsigned> map;
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric public:
820b57cec5SDimitry Andric DeclToIndex() = default;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric /// Compute the actual mapping from declarations to bits.
850b57cec5SDimitry Andric void computeMap(const DeclContext &dc);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric /// Return the number of declarations in the map.
size() const880b57cec5SDimitry Andric unsigned size() const { return map.size(); }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric /// Returns the bit vector index for a given declaration.
91bdd1243dSDimitry Andric std::optional<unsigned> getValueIndex(const VarDecl *d) const;
920b57cec5SDimitry Andric };
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric } // namespace
950b57cec5SDimitry Andric
computeMap(const DeclContext & dc)960b57cec5SDimitry Andric void DeclToIndex::computeMap(const DeclContext &dc) {
970b57cec5SDimitry Andric unsigned count = 0;
980b57cec5SDimitry Andric DeclContext::specific_decl_iterator<VarDecl> I(dc.decls_begin()),
990b57cec5SDimitry Andric E(dc.decls_end());
1000b57cec5SDimitry Andric for ( ; I != E; ++I) {
1010b57cec5SDimitry Andric const VarDecl *vd = *I;
1020b57cec5SDimitry Andric if (isTrackedVar(vd, &dc))
1030b57cec5SDimitry Andric map[vd] = count++;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
getValueIndex(const VarDecl * d) const107bdd1243dSDimitry Andric std::optional<unsigned> DeclToIndex::getValueIndex(const VarDecl *d) const {
1080b57cec5SDimitry Andric llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = map.find(d);
1090b57cec5SDimitry Andric if (I == map.end())
110bdd1243dSDimitry Andric return std::nullopt;
1110b57cec5SDimitry Andric return I->second;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric //------------------------------------------------------------------------====//
1150b57cec5SDimitry Andric // CFGBlockValues: dataflow values for CFG blocks.
1160b57cec5SDimitry Andric //====------------------------------------------------------------------------//
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric // These values are defined in such a way that a merge can be done using
1190b57cec5SDimitry Andric // a bitwise OR.
1200b57cec5SDimitry Andric enum Value { Unknown = 0x0, /* 00 */
1210b57cec5SDimitry Andric Initialized = 0x1, /* 01 */
1220b57cec5SDimitry Andric Uninitialized = 0x2, /* 10 */
1230b57cec5SDimitry Andric MayUninitialized = 0x3 /* 11 */ };
1240b57cec5SDimitry Andric
isUninitialized(const Value v)1250b57cec5SDimitry Andric static bool isUninitialized(const Value v) {
1260b57cec5SDimitry Andric return v >= Uninitialized;
1270b57cec5SDimitry Andric }
1280b57cec5SDimitry Andric
isAlwaysUninit(const Value v)1290b57cec5SDimitry Andric static bool isAlwaysUninit(const Value v) {
1300b57cec5SDimitry Andric return v == Uninitialized;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andric namespace {
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric using ValueVector = llvm::PackedVector<Value, 2, llvm::SmallBitVector>;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric class CFGBlockValues {
1380b57cec5SDimitry Andric const CFG &cfg;
1390b57cec5SDimitry Andric SmallVector<ValueVector, 8> vals;
1400b57cec5SDimitry Andric ValueVector scratch;
1410b57cec5SDimitry Andric DeclToIndex declToIndex;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric public:
1440b57cec5SDimitry Andric CFGBlockValues(const CFG &cfg);
1450b57cec5SDimitry Andric
getNumEntries() const1460b57cec5SDimitry Andric unsigned getNumEntries() const { return declToIndex.size(); }
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric void computeSetOfDeclarations(const DeclContext &dc);
1490b57cec5SDimitry Andric
getValueVector(const CFGBlock * block)1500b57cec5SDimitry Andric ValueVector &getValueVector(const CFGBlock *block) {
1510b57cec5SDimitry Andric return vals[block->getBlockID()];
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric void setAllScratchValues(Value V);
1550b57cec5SDimitry Andric void mergeIntoScratch(ValueVector const &source, bool isFirst);
1560b57cec5SDimitry Andric bool updateValueVectorWithScratch(const CFGBlock *block);
1570b57cec5SDimitry Andric
hasNoDeclarations() const1580b57cec5SDimitry Andric bool hasNoDeclarations() const {
1590b57cec5SDimitry Andric return declToIndex.size() == 0;
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric void resetScratch();
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric ValueVector::reference operator[](const VarDecl *vd);
1650b57cec5SDimitry Andric
getValue(const CFGBlock * block,const CFGBlock * dstBlock,const VarDecl * vd)1660b57cec5SDimitry Andric Value getValue(const CFGBlock *block, const CFGBlock *dstBlock,
1670b57cec5SDimitry Andric const VarDecl *vd) {
168bdd1243dSDimitry Andric std::optional<unsigned> idx = declToIndex.getValueIndex(vd);
169bdd1243dSDimitry Andric return getValueVector(block)[*idx];
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric };
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric } // namespace
1740b57cec5SDimitry Andric
CFGBlockValues(const CFG & c)1750b57cec5SDimitry Andric CFGBlockValues::CFGBlockValues(const CFG &c) : cfg(c), vals(0) {}
1760b57cec5SDimitry Andric
computeSetOfDeclarations(const DeclContext & dc)1770b57cec5SDimitry Andric void CFGBlockValues::computeSetOfDeclarations(const DeclContext &dc) {
1780b57cec5SDimitry Andric declToIndex.computeMap(dc);
1790b57cec5SDimitry Andric unsigned decls = declToIndex.size();
1800b57cec5SDimitry Andric scratch.resize(decls);
1810b57cec5SDimitry Andric unsigned n = cfg.getNumBlockIDs();
1820b57cec5SDimitry Andric if (!n)
1830b57cec5SDimitry Andric return;
1840b57cec5SDimitry Andric vals.resize(n);
1850b57cec5SDimitry Andric for (auto &val : vals)
1860b57cec5SDimitry Andric val.resize(decls);
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric #if DEBUG_LOGGING
printVector(const CFGBlock * block,ValueVector & bv,unsigned num)1900b57cec5SDimitry Andric static void printVector(const CFGBlock *block, ValueVector &bv,
1910b57cec5SDimitry Andric unsigned num) {
1920b57cec5SDimitry Andric llvm::errs() << block->getBlockID() << " :";
1930b57cec5SDimitry Andric for (const auto &i : bv)
1940b57cec5SDimitry Andric llvm::errs() << ' ' << i;
1950b57cec5SDimitry Andric llvm::errs() << " : " << num << '\n';
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric #endif
1980b57cec5SDimitry Andric
setAllScratchValues(Value V)1990b57cec5SDimitry Andric void CFGBlockValues::setAllScratchValues(Value V) {
2000b57cec5SDimitry Andric for (unsigned I = 0, E = scratch.size(); I != E; ++I)
2010b57cec5SDimitry Andric scratch[I] = V;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric
mergeIntoScratch(ValueVector const & source,bool isFirst)2040b57cec5SDimitry Andric void CFGBlockValues::mergeIntoScratch(ValueVector const &source,
2050b57cec5SDimitry Andric bool isFirst) {
2060b57cec5SDimitry Andric if (isFirst)
2070b57cec5SDimitry Andric scratch = source;
2080b57cec5SDimitry Andric else
2090b57cec5SDimitry Andric scratch |= source;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric
updateValueVectorWithScratch(const CFGBlock * block)2120b57cec5SDimitry Andric bool CFGBlockValues::updateValueVectorWithScratch(const CFGBlock *block) {
2130b57cec5SDimitry Andric ValueVector &dst = getValueVector(block);
2140b57cec5SDimitry Andric bool changed = (dst != scratch);
2150b57cec5SDimitry Andric if (changed)
2160b57cec5SDimitry Andric dst = scratch;
2170b57cec5SDimitry Andric #if DEBUG_LOGGING
2180b57cec5SDimitry Andric printVector(block, scratch, 0);
2190b57cec5SDimitry Andric #endif
2200b57cec5SDimitry Andric return changed;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric
resetScratch()2230b57cec5SDimitry Andric void CFGBlockValues::resetScratch() {
2240b57cec5SDimitry Andric scratch.reset();
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric
operator [](const VarDecl * vd)2270b57cec5SDimitry Andric ValueVector::reference CFGBlockValues::operator[](const VarDecl *vd) {
228bdd1243dSDimitry Andric return scratch[*declToIndex.getValueIndex(vd)];
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric //------------------------------------------------------------------------====//
2320b57cec5SDimitry Andric // Classification of DeclRefExprs as use or initialization.
2330b57cec5SDimitry Andric //====------------------------------------------------------------------------//
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric namespace {
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric class FindVarResult {
2380b57cec5SDimitry Andric const VarDecl *vd;
2390b57cec5SDimitry Andric const DeclRefExpr *dr;
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric public:
FindVarResult(const VarDecl * vd,const DeclRefExpr * dr)2420b57cec5SDimitry Andric FindVarResult(const VarDecl *vd, const DeclRefExpr *dr) : vd(vd), dr(dr) {}
2430b57cec5SDimitry Andric
getDeclRefExpr() const2440b57cec5SDimitry Andric const DeclRefExpr *getDeclRefExpr() const { return dr; }
getDecl() const2450b57cec5SDimitry Andric const VarDecl *getDecl() const { return vd; }
2460b57cec5SDimitry Andric };
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric } // namespace
2490b57cec5SDimitry Andric
stripCasts(ASTContext & C,const Expr * Ex)2500b57cec5SDimitry Andric static const Expr *stripCasts(ASTContext &C, const Expr *Ex) {
2510b57cec5SDimitry Andric while (Ex) {
2520b57cec5SDimitry Andric Ex = Ex->IgnoreParenNoopCasts(C);
2530b57cec5SDimitry Andric if (const auto *CE = dyn_cast<CastExpr>(Ex)) {
2540b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueBitCast) {
2550b57cec5SDimitry Andric Ex = CE->getSubExpr();
2560b57cec5SDimitry Andric continue;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric break;
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric return Ex;
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric /// If E is an expression comprising a reference to a single variable, find that
2650b57cec5SDimitry Andric /// variable.
findVar(const Expr * E,const DeclContext * DC)2660b57cec5SDimitry Andric static FindVarResult findVar(const Expr *E, const DeclContext *DC) {
2670b57cec5SDimitry Andric if (const auto *DRE =
2680b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(DC->getParentASTContext(), E)))
2690b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
2700b57cec5SDimitry Andric if (isTrackedVar(VD, DC))
2710b57cec5SDimitry Andric return FindVarResult(VD, DRE);
2720b57cec5SDimitry Andric return FindVarResult(nullptr, nullptr);
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric namespace {
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric /// Classify each DeclRefExpr as an initialization or a use. Any
2780b57cec5SDimitry Andric /// DeclRefExpr which isn't explicitly classified will be assumed to have
2790b57cec5SDimitry Andric /// escaped the analysis and will be treated as an initialization.
2800b57cec5SDimitry Andric class ClassifyRefs : public StmtVisitor<ClassifyRefs> {
2810b57cec5SDimitry Andric public:
2820b57cec5SDimitry Andric enum Class {
2830b57cec5SDimitry Andric Init,
2840b57cec5SDimitry Andric Use,
2850b57cec5SDimitry Andric SelfInit,
2865ffd83dbSDimitry Andric ConstRefUse,
2870b57cec5SDimitry Andric Ignore
2880b57cec5SDimitry Andric };
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric private:
2910b57cec5SDimitry Andric const DeclContext *DC;
2920b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr *, Class> Classification;
2930b57cec5SDimitry Andric
isTrackedVar(const VarDecl * VD) const2940b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *VD) const {
2950b57cec5SDimitry Andric return ::isTrackedVar(VD, DC);
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andric void classify(const Expr *E, Class C);
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andric public:
ClassifyRefs(AnalysisDeclContext & AC)3010b57cec5SDimitry Andric ClassifyRefs(AnalysisDeclContext &AC) : DC(cast<DeclContext>(AC.getDecl())) {}
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *DS);
3040b57cec5SDimitry Andric void VisitUnaryOperator(UnaryOperator *UO);
3050b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *BO);
3060b57cec5SDimitry Andric void VisitCallExpr(CallExpr *CE);
3070b57cec5SDimitry Andric void VisitCastExpr(CastExpr *CE);
3080b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
3090b57cec5SDimitry Andric
operator ()(Stmt * S)3100b57cec5SDimitry Andric void operator()(Stmt *S) { Visit(S); }
3110b57cec5SDimitry Andric
get(const DeclRefExpr * DRE) const3120b57cec5SDimitry Andric Class get(const DeclRefExpr *DRE) const {
3130b57cec5SDimitry Andric llvm::DenseMap<const DeclRefExpr*, Class>::const_iterator I
3140b57cec5SDimitry Andric = Classification.find(DRE);
3150b57cec5SDimitry Andric if (I != Classification.end())
3160b57cec5SDimitry Andric return I->second;
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
3190b57cec5SDimitry Andric if (!VD || !isTrackedVar(VD))
3200b57cec5SDimitry Andric return Ignore;
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric return Init;
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric };
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric } // namespace
3270b57cec5SDimitry Andric
getSelfInitExpr(VarDecl * VD)3280b57cec5SDimitry Andric static const DeclRefExpr *getSelfInitExpr(VarDecl *VD) {
3290b57cec5SDimitry Andric if (VD->getType()->isRecordType())
3300b57cec5SDimitry Andric return nullptr;
3310b57cec5SDimitry Andric if (Expr *Init = VD->getInit()) {
3320b57cec5SDimitry Andric const auto *DRE =
3330b57cec5SDimitry Andric dyn_cast<DeclRefExpr>(stripCasts(VD->getASTContext(), Init));
3340b57cec5SDimitry Andric if (DRE && DRE->getDecl() == VD)
3350b57cec5SDimitry Andric return DRE;
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric return nullptr;
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric
classify(const Expr * E,Class C)3400b57cec5SDimitry Andric void ClassifyRefs::classify(const Expr *E, Class C) {
3410b57cec5SDimitry Andric // The result of a ?: could also be an lvalue.
3420b57cec5SDimitry Andric E = E->IgnoreParens();
3430b57cec5SDimitry Andric if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
3440b57cec5SDimitry Andric classify(CO->getTrueExpr(), C);
3450b57cec5SDimitry Andric classify(CO->getFalseExpr(), C);
3460b57cec5SDimitry Andric return;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric if (const auto *BCO = dyn_cast<BinaryConditionalOperator>(E)) {
3500b57cec5SDimitry Andric classify(BCO->getFalseExpr(), C);
3510b57cec5SDimitry Andric return;
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric if (const auto *OVE = dyn_cast<OpaqueValueExpr>(E)) {
3550b57cec5SDimitry Andric classify(OVE->getSourceExpr(), C);
3560b57cec5SDimitry Andric return;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric if (const auto *ME = dyn_cast<MemberExpr>(E)) {
3600b57cec5SDimitry Andric if (const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) {
3610b57cec5SDimitry Andric if (!VD->isStaticDataMember())
3620b57cec5SDimitry Andric classify(ME->getBase(), C);
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric return;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
3680b57cec5SDimitry Andric switch (BO->getOpcode()) {
3690b57cec5SDimitry Andric case BO_PtrMemD:
3700b57cec5SDimitry Andric case BO_PtrMemI:
3710b57cec5SDimitry Andric classify(BO->getLHS(), C);
3720b57cec5SDimitry Andric return;
3730b57cec5SDimitry Andric case BO_Comma:
3740b57cec5SDimitry Andric classify(BO->getRHS(), C);
3750b57cec5SDimitry Andric return;
3760b57cec5SDimitry Andric default:
3770b57cec5SDimitry Andric return;
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andric FindVarResult Var = findVar(E, DC);
3820b57cec5SDimitry Andric if (const DeclRefExpr *DRE = Var.getDeclRefExpr())
3830b57cec5SDimitry Andric Classification[DRE] = std::max(Classification[DRE], C);
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
VisitDeclStmt(DeclStmt * DS)3860b57cec5SDimitry Andric void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
3870b57cec5SDimitry Andric for (auto *DI : DS->decls()) {
3880b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI);
3890b57cec5SDimitry Andric if (VD && isTrackedVar(VD))
3900b57cec5SDimitry Andric if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
3910b57cec5SDimitry Andric Classification[DRE] = SelfInit;
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric
VisitBinaryOperator(BinaryOperator * BO)3950b57cec5SDimitry Andric void ClassifyRefs::VisitBinaryOperator(BinaryOperator *BO) {
3960b57cec5SDimitry Andric // Ignore the evaluation of a DeclRefExpr on the LHS of an assignment. If this
3970b57cec5SDimitry Andric // is not a compound-assignment, we will treat it as initializing the variable
3980b57cec5SDimitry Andric // when TransferFunctions visits it. A compound-assignment does not affect
3990b57cec5SDimitry Andric // whether a variable is uninitialized, and there's no point counting it as a
4000b57cec5SDimitry Andric // use.
4010b57cec5SDimitry Andric if (BO->isCompoundAssignmentOp())
4020b57cec5SDimitry Andric classify(BO->getLHS(), Use);
4030b57cec5SDimitry Andric else if (BO->getOpcode() == BO_Assign || BO->getOpcode() == BO_Comma)
4040b57cec5SDimitry Andric classify(BO->getLHS(), Ignore);
4050b57cec5SDimitry Andric }
4060b57cec5SDimitry Andric
VisitUnaryOperator(UnaryOperator * UO)4070b57cec5SDimitry Andric void ClassifyRefs::VisitUnaryOperator(UnaryOperator *UO) {
4080b57cec5SDimitry Andric // Increment and decrement are uses despite there being no lvalue-to-rvalue
4090b57cec5SDimitry Andric // conversion.
4100b57cec5SDimitry Andric if (UO->isIncrementDecrementOp())
4110b57cec5SDimitry Andric classify(UO->getSubExpr(), Use);
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
VisitOMPExecutableDirective(OMPExecutableDirective * ED)4140b57cec5SDimitry Andric void ClassifyRefs::VisitOMPExecutableDirective(OMPExecutableDirective *ED) {
4150b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses()))
4160b57cec5SDimitry Andric classify(cast<Expr>(S), Use);
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric
isPointerToConst(const QualType & QT)4190b57cec5SDimitry Andric static bool isPointerToConst(const QualType &QT) {
4200b57cec5SDimitry Andric return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
hasTrivialBody(CallExpr * CE)4235ffd83dbSDimitry Andric static bool hasTrivialBody(CallExpr *CE) {
4245ffd83dbSDimitry Andric if (FunctionDecl *FD = CE->getDirectCallee()) {
4255ffd83dbSDimitry Andric if (FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
4265ffd83dbSDimitry Andric return FTD->getTemplatedDecl()->hasTrivialBody();
4275ffd83dbSDimitry Andric return FD->hasTrivialBody();
4285ffd83dbSDimitry Andric }
4295ffd83dbSDimitry Andric return false;
4305ffd83dbSDimitry Andric }
4315ffd83dbSDimitry Andric
VisitCallExpr(CallExpr * CE)4320b57cec5SDimitry Andric void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
4330b57cec5SDimitry Andric // Classify arguments to std::move as used.
4340b57cec5SDimitry Andric if (CE->isCallToStdMove()) {
4350b57cec5SDimitry Andric // RecordTypes are handled in SemaDeclCXX.cpp.
4360b57cec5SDimitry Andric if (!CE->getArg(0)->getType()->isRecordType())
4370b57cec5SDimitry Andric classify(CE->getArg(0), Use);
4380b57cec5SDimitry Andric return;
4390b57cec5SDimitry Andric }
4405ffd83dbSDimitry Andric bool isTrivialBody = hasTrivialBody(CE);
4415ffd83dbSDimitry Andric // If a value is passed by const pointer to a function,
4420b57cec5SDimitry Andric // we should not assume that it is initialized by the call, and we
4430b57cec5SDimitry Andric // conservatively do not assume that it is used.
4445ffd83dbSDimitry Andric // If a value is passed by const reference to a function,
4455ffd83dbSDimitry Andric // it should already be initialized.
4460b57cec5SDimitry Andric for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
4470b57cec5SDimitry Andric I != E; ++I) {
4480b57cec5SDimitry Andric if ((*I)->isGLValue()) {
4490b57cec5SDimitry Andric if ((*I)->getType().isConstQualified())
4505ffd83dbSDimitry Andric classify((*I), isTrivialBody ? Ignore : ConstRefUse);
4510b57cec5SDimitry Andric } else if (isPointerToConst((*I)->getType())) {
4520b57cec5SDimitry Andric const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
4530b57cec5SDimitry Andric const auto *UO = dyn_cast<UnaryOperator>(Ex);
4540b57cec5SDimitry Andric if (UO && UO->getOpcode() == UO_AddrOf)
4550b57cec5SDimitry Andric Ex = UO->getSubExpr();
4560b57cec5SDimitry Andric classify(Ex, Ignore);
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric
VisitCastExpr(CastExpr * CE)4610b57cec5SDimitry Andric void ClassifyRefs::VisitCastExpr(CastExpr *CE) {
4620b57cec5SDimitry Andric if (CE->getCastKind() == CK_LValueToRValue)
4630b57cec5SDimitry Andric classify(CE->getSubExpr(), Use);
4640b57cec5SDimitry Andric else if (const auto *CSE = dyn_cast<CStyleCastExpr>(CE)) {
4650b57cec5SDimitry Andric if (CSE->getType()->isVoidType()) {
4660b57cec5SDimitry Andric // Squelch any detected load of an uninitialized value if
4670b57cec5SDimitry Andric // we cast it to void.
4680b57cec5SDimitry Andric // e.g. (void) x;
4690b57cec5SDimitry Andric classify(CSE->getSubExpr(), Ignore);
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric }
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric
4740b57cec5SDimitry Andric //------------------------------------------------------------------------====//
4750b57cec5SDimitry Andric // Transfer function for uninitialized values analysis.
4760b57cec5SDimitry Andric //====------------------------------------------------------------------------//
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andric namespace {
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric class TransferFunctions : public StmtVisitor<TransferFunctions> {
4810b57cec5SDimitry Andric CFGBlockValues &vals;
4820b57cec5SDimitry Andric const CFG &cfg;
4830b57cec5SDimitry Andric const CFGBlock *block;
4840b57cec5SDimitry Andric AnalysisDeclContext ∾
4850b57cec5SDimitry Andric const ClassifyRefs &classification;
4860b57cec5SDimitry Andric ObjCNoReturn objCNoRet;
4870b57cec5SDimitry Andric UninitVariablesHandler &handler;
4880b57cec5SDimitry Andric
4890b57cec5SDimitry Andric public:
TransferFunctions(CFGBlockValues & vals,const CFG & cfg,const CFGBlock * block,AnalysisDeclContext & ac,const ClassifyRefs & classification,UninitVariablesHandler & handler)4900b57cec5SDimitry Andric TransferFunctions(CFGBlockValues &vals, const CFG &cfg,
4910b57cec5SDimitry Andric const CFGBlock *block, AnalysisDeclContext &ac,
4920b57cec5SDimitry Andric const ClassifyRefs &classification,
4930b57cec5SDimitry Andric UninitVariablesHandler &handler)
4940b57cec5SDimitry Andric : vals(vals), cfg(cfg), block(block), ac(ac),
4950b57cec5SDimitry Andric classification(classification), objCNoRet(ac.getASTContext()),
4960b57cec5SDimitry Andric handler(handler) {}
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric void reportUse(const Expr *ex, const VarDecl *vd);
4995ffd83dbSDimitry Andric void reportConstRefUse(const Expr *ex, const VarDecl *vd);
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andric void VisitBinaryOperator(BinaryOperator *bo);
5020b57cec5SDimitry Andric void VisitBlockExpr(BlockExpr *be);
5030b57cec5SDimitry Andric void VisitCallExpr(CallExpr *ce);
5040b57cec5SDimitry Andric void VisitDeclRefExpr(DeclRefExpr *dr);
5050b57cec5SDimitry Andric void VisitDeclStmt(DeclStmt *ds);
5065ffd83dbSDimitry Andric void VisitGCCAsmStmt(GCCAsmStmt *as);
5070b57cec5SDimitry Andric void VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS);
5080b57cec5SDimitry Andric void VisitObjCMessageExpr(ObjCMessageExpr *ME);
5090b57cec5SDimitry Andric void VisitOMPExecutableDirective(OMPExecutableDirective *ED);
5100b57cec5SDimitry Andric
isTrackedVar(const VarDecl * vd)5110b57cec5SDimitry Andric bool isTrackedVar(const VarDecl *vd) {
5120b57cec5SDimitry Andric return ::isTrackedVar(vd, cast<DeclContext>(ac.getDecl()));
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric
findVar(const Expr * ex)5150b57cec5SDimitry Andric FindVarResult findVar(const Expr *ex) {
5160b57cec5SDimitry Andric return ::findVar(ex, cast<DeclContext>(ac.getDecl()));
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric
getUninitUse(const Expr * ex,const VarDecl * vd,Value v)5190b57cec5SDimitry Andric UninitUse getUninitUse(const Expr *ex, const VarDecl *vd, Value v) {
5200b57cec5SDimitry Andric UninitUse Use(ex, isAlwaysUninit(v));
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric assert(isUninitialized(v));
5230b57cec5SDimitry Andric if (Use.getKind() == UninitUse::Always)
5240b57cec5SDimitry Andric return Use;
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric // If an edge which leads unconditionally to this use did not initialize
5270b57cec5SDimitry Andric // the variable, we can say something stronger than 'may be uninitialized':
5280b57cec5SDimitry Andric // we can say 'either it's used uninitialized or you have dead code'.
5290b57cec5SDimitry Andric //
5300b57cec5SDimitry Andric // We track the number of successors of a node which have been visited, and
5310b57cec5SDimitry Andric // visit a node once we have visited all of its successors. Only edges where
5320b57cec5SDimitry Andric // the variable might still be uninitialized are followed. Since a variable
5330b57cec5SDimitry Andric // can't transfer from being initialized to being uninitialized, this will
5340b57cec5SDimitry Andric // trace out the subgraph which inevitably leads to the use and does not
5350b57cec5SDimitry Andric // initialize the variable. We do not want to skip past loops, since their
5360b57cec5SDimitry Andric // non-termination might be correlated with the initialization condition.
5370b57cec5SDimitry Andric //
5380b57cec5SDimitry Andric // For example:
5390b57cec5SDimitry Andric //
5400b57cec5SDimitry Andric // void f(bool a, bool b) {
5410b57cec5SDimitry Andric // block1: int n;
5420b57cec5SDimitry Andric // if (a) {
5430b57cec5SDimitry Andric // block2: if (b)
5440b57cec5SDimitry Andric // block3: n = 1;
5450b57cec5SDimitry Andric // block4: } else if (b) {
5460b57cec5SDimitry Andric // block5: while (!a) {
5470b57cec5SDimitry Andric // block6: do_work(&a);
5480b57cec5SDimitry Andric // n = 2;
5490b57cec5SDimitry Andric // }
5500b57cec5SDimitry Andric // }
5510b57cec5SDimitry Andric // block7: if (a)
5520b57cec5SDimitry Andric // block8: g();
5530b57cec5SDimitry Andric // block9: return n;
5540b57cec5SDimitry Andric // }
5550b57cec5SDimitry Andric //
5560b57cec5SDimitry Andric // Starting from the maybe-uninitialized use in block 9:
5570b57cec5SDimitry Andric // * Block 7 is not visited because we have only visited one of its two
5580b57cec5SDimitry Andric // successors.
5590b57cec5SDimitry Andric // * Block 8 is visited because we've visited its only successor.
5600b57cec5SDimitry Andric // From block 8:
5610b57cec5SDimitry Andric // * Block 7 is visited because we've now visited both of its successors.
5620b57cec5SDimitry Andric // From block 7:
5630b57cec5SDimitry Andric // * Blocks 1, 2, 4, 5, and 6 are not visited because we didn't visit all
5640b57cec5SDimitry Andric // of their successors (we didn't visit 4, 3, 5, 6, and 5, respectively).
5650b57cec5SDimitry Andric // * Block 3 is not visited because it initializes 'n'.
5660b57cec5SDimitry Andric // Now the algorithm terminates, having visited blocks 7 and 8, and having
5670b57cec5SDimitry Andric // found the frontier is blocks 2, 4, and 5.
5680b57cec5SDimitry Andric //
5690b57cec5SDimitry Andric // 'n' is definitely uninitialized for two edges into block 7 (from blocks 2
5700b57cec5SDimitry Andric // and 4), so we report that any time either of those edges is taken (in
5710b57cec5SDimitry Andric // each case when 'b == false'), 'n' is used uninitialized.
5720b57cec5SDimitry Andric SmallVector<const CFGBlock*, 32> Queue;
5730b57cec5SDimitry Andric SmallVector<unsigned, 32> SuccsVisited(cfg.getNumBlockIDs(), 0);
5740b57cec5SDimitry Andric Queue.push_back(block);
5750b57cec5SDimitry Andric // Specify that we've already visited all successors of the starting block.
5760b57cec5SDimitry Andric // This has the dual purpose of ensuring we never add it to the queue, and
5770b57cec5SDimitry Andric // of marking it as not being a candidate element of the frontier.
5780b57cec5SDimitry Andric SuccsVisited[block->getBlockID()] = block->succ_size();
5790b57cec5SDimitry Andric while (!Queue.empty()) {
5800b57cec5SDimitry Andric const CFGBlock *B = Queue.pop_back_val();
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric // If the use is always reached from the entry block, make a note of that.
5830b57cec5SDimitry Andric if (B == &cfg.getEntry())
5840b57cec5SDimitry Andric Use.setUninitAfterCall();
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = B->pred_begin(), E = B->pred_end();
5870b57cec5SDimitry Andric I != E; ++I) {
5880b57cec5SDimitry Andric const CFGBlock *Pred = *I;
5890b57cec5SDimitry Andric if (!Pred)
5900b57cec5SDimitry Andric continue;
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric Value AtPredExit = vals.getValue(Pred, B, vd);
5930b57cec5SDimitry Andric if (AtPredExit == Initialized)
5940b57cec5SDimitry Andric // This block initializes the variable.
5950b57cec5SDimitry Andric continue;
5960b57cec5SDimitry Andric if (AtPredExit == MayUninitialized &&
5970b57cec5SDimitry Andric vals.getValue(B, nullptr, vd) == Uninitialized) {
5980b57cec5SDimitry Andric // This block declares the variable (uninitialized), and is reachable
5990b57cec5SDimitry Andric // from a block that initializes the variable. We can't guarantee to
6000b57cec5SDimitry Andric // give an earlier location for the diagnostic (and it appears that
6010b57cec5SDimitry Andric // this code is intended to be reachable) so give a diagnostic here
6020b57cec5SDimitry Andric // and go no further down this path.
6030b57cec5SDimitry Andric Use.setUninitAfterDecl();
6040b57cec5SDimitry Andric continue;
6050b57cec5SDimitry Andric }
6060b57cec5SDimitry Andric
6070b57cec5SDimitry Andric unsigned &SV = SuccsVisited[Pred->getBlockID()];
6080b57cec5SDimitry Andric if (!SV) {
6090b57cec5SDimitry Andric // When visiting the first successor of a block, mark all NULL
6100b57cec5SDimitry Andric // successors as having been visited.
6110b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator SI = Pred->succ_begin(),
6120b57cec5SDimitry Andric SE = Pred->succ_end();
6130b57cec5SDimitry Andric SI != SE; ++SI)
6140b57cec5SDimitry Andric if (!*SI)
6150b57cec5SDimitry Andric ++SV;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric
6180b57cec5SDimitry Andric if (++SV == Pred->succ_size())
6190b57cec5SDimitry Andric // All paths from this block lead to the use and don't initialize the
6200b57cec5SDimitry Andric // variable.
6210b57cec5SDimitry Andric Queue.push_back(Pred);
6220b57cec5SDimitry Andric }
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric // Scan the frontier, looking for blocks where the variable was
6260b57cec5SDimitry Andric // uninitialized.
6270b57cec5SDimitry Andric for (const auto *Block : cfg) {
6280b57cec5SDimitry Andric unsigned BlockID = Block->getBlockID();
6290b57cec5SDimitry Andric const Stmt *Term = Block->getTerminatorStmt();
6300b57cec5SDimitry Andric if (SuccsVisited[BlockID] && SuccsVisited[BlockID] < Block->succ_size() &&
6310b57cec5SDimitry Andric Term) {
6320b57cec5SDimitry Andric // This block inevitably leads to the use. If we have an edge from here
6330b57cec5SDimitry Andric // to a post-dominator block, and the variable is uninitialized on that
6340b57cec5SDimitry Andric // edge, we have found a bug.
6350b57cec5SDimitry Andric for (CFGBlock::const_succ_iterator I = Block->succ_begin(),
6360b57cec5SDimitry Andric E = Block->succ_end(); I != E; ++I) {
6370b57cec5SDimitry Andric const CFGBlock *Succ = *I;
6380b57cec5SDimitry Andric if (Succ && SuccsVisited[Succ->getBlockID()] >= Succ->succ_size() &&
6390b57cec5SDimitry Andric vals.getValue(Block, Succ, vd) == Uninitialized) {
6400b57cec5SDimitry Andric // Switch cases are a special case: report the label to the caller
6410b57cec5SDimitry Andric // as the 'terminator', not the switch statement itself. Suppress
6420b57cec5SDimitry Andric // situations where no label matched: we can't be sure that's
6430b57cec5SDimitry Andric // possible.
6440b57cec5SDimitry Andric if (isa<SwitchStmt>(Term)) {
6450b57cec5SDimitry Andric const Stmt *Label = Succ->getLabel();
6460b57cec5SDimitry Andric if (!Label || !isa<SwitchCase>(Label))
6470b57cec5SDimitry Andric // Might not be possible.
6480b57cec5SDimitry Andric continue;
6490b57cec5SDimitry Andric UninitUse::Branch Branch;
6500b57cec5SDimitry Andric Branch.Terminator = Label;
6510b57cec5SDimitry Andric Branch.Output = 0; // Ignored.
6520b57cec5SDimitry Andric Use.addUninitBranch(Branch);
6530b57cec5SDimitry Andric } else {
6540b57cec5SDimitry Andric UninitUse::Branch Branch;
6550b57cec5SDimitry Andric Branch.Terminator = Term;
6560b57cec5SDimitry Andric Branch.Output = I - Block->succ_begin();
6570b57cec5SDimitry Andric Use.addUninitBranch(Branch);
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric }
6620b57cec5SDimitry Andric }
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric return Use;
6650b57cec5SDimitry Andric }
6660b57cec5SDimitry Andric };
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric } // namespace
6690b57cec5SDimitry Andric
reportUse(const Expr * ex,const VarDecl * vd)6700b57cec5SDimitry Andric void TransferFunctions::reportUse(const Expr *ex, const VarDecl *vd) {
6710b57cec5SDimitry Andric Value v = vals[vd];
6720b57cec5SDimitry Andric if (isUninitialized(v))
6730b57cec5SDimitry Andric handler.handleUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric
reportConstRefUse(const Expr * ex,const VarDecl * vd)6765ffd83dbSDimitry Andric void TransferFunctions::reportConstRefUse(const Expr *ex, const VarDecl *vd) {
6775ffd83dbSDimitry Andric Value v = vals[vd];
6785ffd83dbSDimitry Andric if (isAlwaysUninit(v))
6795ffd83dbSDimitry Andric handler.handleConstRefUseOfUninitVariable(vd, getUninitUse(ex, vd, v));
6805ffd83dbSDimitry Andric }
6815ffd83dbSDimitry Andric
VisitObjCForCollectionStmt(ObjCForCollectionStmt * FS)6820b57cec5SDimitry Andric void TransferFunctions::VisitObjCForCollectionStmt(ObjCForCollectionStmt *FS) {
6830b57cec5SDimitry Andric // This represents an initialization of the 'element' value.
6840b57cec5SDimitry Andric if (const auto *DS = dyn_cast<DeclStmt>(FS->getElement())) {
6850b57cec5SDimitry Andric const auto *VD = cast<VarDecl>(DS->getSingleDecl());
6860b57cec5SDimitry Andric if (isTrackedVar(VD))
6870b57cec5SDimitry Andric vals[VD] = Initialized;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric }
6900b57cec5SDimitry Andric
VisitOMPExecutableDirective(OMPExecutableDirective * ED)6910b57cec5SDimitry Andric void TransferFunctions::VisitOMPExecutableDirective(
6920b57cec5SDimitry Andric OMPExecutableDirective *ED) {
6930b57cec5SDimitry Andric for (Stmt *S : OMPExecutableDirective::used_clauses_children(ED->clauses())) {
6940b57cec5SDimitry Andric assert(S && "Expected non-null used-in-clause child.");
6950b57cec5SDimitry Andric Visit(S);
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric if (!ED->isStandaloneDirective())
6980b57cec5SDimitry Andric Visit(ED->getStructuredBlock());
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric
VisitBlockExpr(BlockExpr * be)7010b57cec5SDimitry Andric void TransferFunctions::VisitBlockExpr(BlockExpr *be) {
7020b57cec5SDimitry Andric const BlockDecl *bd = be->getBlockDecl();
7030b57cec5SDimitry Andric for (const auto &I : bd->captures()) {
7040b57cec5SDimitry Andric const VarDecl *vd = I.getVariable();
7050b57cec5SDimitry Andric if (!isTrackedVar(vd))
7060b57cec5SDimitry Andric continue;
7070b57cec5SDimitry Andric if (I.isByRef()) {
7080b57cec5SDimitry Andric vals[vd] = Initialized;
7090b57cec5SDimitry Andric continue;
7100b57cec5SDimitry Andric }
7110b57cec5SDimitry Andric reportUse(be, vd);
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric }
7140b57cec5SDimitry Andric
VisitCallExpr(CallExpr * ce)7150b57cec5SDimitry Andric void TransferFunctions::VisitCallExpr(CallExpr *ce) {
7160b57cec5SDimitry Andric if (Decl *Callee = ce->getCalleeDecl()) {
7170b57cec5SDimitry Andric if (Callee->hasAttr<ReturnsTwiceAttr>()) {
7180b57cec5SDimitry Andric // After a call to a function like setjmp or vfork, any variable which is
7190b57cec5SDimitry Andric // initialized anywhere within this function may now be initialized. For
7200b57cec5SDimitry Andric // now, just assume such a call initializes all variables. FIXME: Only
7210b57cec5SDimitry Andric // mark variables as initialized if they have an initializer which is
7220b57cec5SDimitry Andric // reachable from here.
7230b57cec5SDimitry Andric vals.setAllScratchValues(Initialized);
7240b57cec5SDimitry Andric }
7250b57cec5SDimitry Andric else if (Callee->hasAttr<AnalyzerNoReturnAttr>()) {
7260b57cec5SDimitry Andric // Functions labeled like "analyzer_noreturn" are often used to denote
7270b57cec5SDimitry Andric // "panic" functions that in special debug situations can still return,
7280b57cec5SDimitry Andric // but for the most part should not be treated as returning. This is a
7290b57cec5SDimitry Andric // useful annotation borrowed from the static analyzer that is useful for
7300b57cec5SDimitry Andric // suppressing branch-specific false positives when we call one of these
7310b57cec5SDimitry Andric // functions but keep pretending the path continues (when in reality the
7320b57cec5SDimitry Andric // user doesn't care).
7330b57cec5SDimitry Andric vals.setAllScratchValues(Unknown);
7340b57cec5SDimitry Andric }
7350b57cec5SDimitry Andric }
7360b57cec5SDimitry Andric }
7370b57cec5SDimitry Andric
VisitDeclRefExpr(DeclRefExpr * dr)7380b57cec5SDimitry Andric void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *dr) {
7390b57cec5SDimitry Andric switch (classification.get(dr)) {
7400b57cec5SDimitry Andric case ClassifyRefs::Ignore:
7410b57cec5SDimitry Andric break;
7420b57cec5SDimitry Andric case ClassifyRefs::Use:
7430b57cec5SDimitry Andric reportUse(dr, cast<VarDecl>(dr->getDecl()));
7440b57cec5SDimitry Andric break;
7450b57cec5SDimitry Andric case ClassifyRefs::Init:
7460b57cec5SDimitry Andric vals[cast<VarDecl>(dr->getDecl())] = Initialized;
7470b57cec5SDimitry Andric break;
7480b57cec5SDimitry Andric case ClassifyRefs::SelfInit:
7490b57cec5SDimitry Andric handler.handleSelfInit(cast<VarDecl>(dr->getDecl()));
7500b57cec5SDimitry Andric break;
7515ffd83dbSDimitry Andric case ClassifyRefs::ConstRefUse:
7525ffd83dbSDimitry Andric reportConstRefUse(dr, cast<VarDecl>(dr->getDecl()));
7535ffd83dbSDimitry Andric break;
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric }
7560b57cec5SDimitry Andric
VisitBinaryOperator(BinaryOperator * BO)7570b57cec5SDimitry Andric void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
7580b57cec5SDimitry Andric if (BO->getOpcode() == BO_Assign) {
7590b57cec5SDimitry Andric FindVarResult Var = findVar(BO->getLHS());
7600b57cec5SDimitry Andric if (const VarDecl *VD = Var.getDecl())
7610b57cec5SDimitry Andric vals[VD] = Initialized;
7620b57cec5SDimitry Andric }
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric
VisitDeclStmt(DeclStmt * DS)7650b57cec5SDimitry Andric void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
7660b57cec5SDimitry Andric for (auto *DI : DS->decls()) {
7670b57cec5SDimitry Andric auto *VD = dyn_cast<VarDecl>(DI);
7680b57cec5SDimitry Andric if (VD && isTrackedVar(VD)) {
7690b57cec5SDimitry Andric if (getSelfInitExpr(VD)) {
7700b57cec5SDimitry Andric // If the initializer consists solely of a reference to itself, we
7710b57cec5SDimitry Andric // explicitly mark the variable as uninitialized. This allows code
7720b57cec5SDimitry Andric // like the following:
7730b57cec5SDimitry Andric //
7740b57cec5SDimitry Andric // int x = x;
7750b57cec5SDimitry Andric //
7760b57cec5SDimitry Andric // to deliberately leave a variable uninitialized. Different analysis
7770b57cec5SDimitry Andric // clients can detect this pattern and adjust their reporting
7780b57cec5SDimitry Andric // appropriately, but we need to continue to analyze subsequent uses
7790b57cec5SDimitry Andric // of the variable.
7800b57cec5SDimitry Andric vals[VD] = Uninitialized;
7810b57cec5SDimitry Andric } else if (VD->getInit()) {
7820b57cec5SDimitry Andric // Treat the new variable as initialized.
7830b57cec5SDimitry Andric vals[VD] = Initialized;
7840b57cec5SDimitry Andric } else {
7850b57cec5SDimitry Andric // No initializer: the variable is now uninitialized. This matters
7860b57cec5SDimitry Andric // for cases like:
7870b57cec5SDimitry Andric // while (...) {
7880b57cec5SDimitry Andric // int n;
7890b57cec5SDimitry Andric // use(n);
7900b57cec5SDimitry Andric // n = 0;
7910b57cec5SDimitry Andric // }
7920b57cec5SDimitry Andric // FIXME: Mark the variable as uninitialized whenever its scope is
7930b57cec5SDimitry Andric // left, since its scope could be re-entered by a jump over the
7940b57cec5SDimitry Andric // declaration.
7950b57cec5SDimitry Andric vals[VD] = Uninitialized;
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric }
7980b57cec5SDimitry Andric }
7990b57cec5SDimitry Andric }
8000b57cec5SDimitry Andric
VisitGCCAsmStmt(GCCAsmStmt * as)8015ffd83dbSDimitry Andric void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) {
8025ffd83dbSDimitry Andric // An "asm goto" statement is a terminator that may initialize some variables.
8035ffd83dbSDimitry Andric if (!as->isAsmGoto())
8045ffd83dbSDimitry Andric return;
8055ffd83dbSDimitry Andric
8060eae32dcSDimitry Andric ASTContext &C = ac.getASTContext();
8070eae32dcSDimitry Andric for (const Expr *O : as->outputs()) {
8080eae32dcSDimitry Andric const Expr *Ex = stripCasts(C, O);
8090eae32dcSDimitry Andric
8100eae32dcSDimitry Andric // Strip away any unary operators. Invalid l-values are reported by other
8110eae32dcSDimitry Andric // semantic analysis passes.
8120eae32dcSDimitry Andric while (const auto *UO = dyn_cast<UnaryOperator>(Ex))
8130eae32dcSDimitry Andric Ex = stripCasts(C, UO->getSubExpr());
8140eae32dcSDimitry Andric
81504eeddc0SDimitry Andric // Mark the variable as potentially uninitialized for those cases where
81604eeddc0SDimitry Andric // it's used on an indirect path, where it's not guaranteed to be
81704eeddc0SDimitry Andric // defined.
8180eae32dcSDimitry Andric if (const VarDecl *VD = findVar(Ex).getDecl())
81906c3fb27SDimitry Andric if (vals[VD] != Initialized)
8205ffd83dbSDimitry Andric vals[VD] = MayUninitialized;
8215ffd83dbSDimitry Andric }
8220eae32dcSDimitry Andric }
8235ffd83dbSDimitry Andric
VisitObjCMessageExpr(ObjCMessageExpr * ME)8240b57cec5SDimitry Andric void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
8250b57cec5SDimitry Andric // If the Objective-C message expression is an implicit no-return that
8260b57cec5SDimitry Andric // is not modeled in the CFG, set the tracked dataflow values to Unknown.
8270b57cec5SDimitry Andric if (objCNoRet.isImplicitNoReturn(ME)) {
8280b57cec5SDimitry Andric vals.setAllScratchValues(Unknown);
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric
8320b57cec5SDimitry Andric //------------------------------------------------------------------------====//
8330b57cec5SDimitry Andric // High-level "driver" logic for uninitialized values analysis.
8340b57cec5SDimitry Andric //====------------------------------------------------------------------------//
8350b57cec5SDimitry Andric
runOnBlock(const CFGBlock * block,const CFG & cfg,AnalysisDeclContext & ac,CFGBlockValues & vals,const ClassifyRefs & classification,llvm::BitVector & wasAnalyzed,UninitVariablesHandler & handler)8360b57cec5SDimitry Andric static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
8370b57cec5SDimitry Andric AnalysisDeclContext &ac, CFGBlockValues &vals,
8380b57cec5SDimitry Andric const ClassifyRefs &classification,
8390b57cec5SDimitry Andric llvm::BitVector &wasAnalyzed,
8400b57cec5SDimitry Andric UninitVariablesHandler &handler) {
8410b57cec5SDimitry Andric wasAnalyzed[block->getBlockID()] = true;
8420b57cec5SDimitry Andric vals.resetScratch();
8430b57cec5SDimitry Andric // Merge in values of predecessor blocks.
8440b57cec5SDimitry Andric bool isFirst = true;
8450b57cec5SDimitry Andric for (CFGBlock::const_pred_iterator I = block->pred_begin(),
8460b57cec5SDimitry Andric E = block->pred_end(); I != E; ++I) {
8470b57cec5SDimitry Andric const CFGBlock *pred = *I;
8480b57cec5SDimitry Andric if (!pred)
8490b57cec5SDimitry Andric continue;
8500b57cec5SDimitry Andric if (wasAnalyzed[pred->getBlockID()]) {
8510b57cec5SDimitry Andric vals.mergeIntoScratch(vals.getValueVector(pred), isFirst);
8520b57cec5SDimitry Andric isFirst = false;
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric }
8550b57cec5SDimitry Andric // Apply the transfer function.
8560b57cec5SDimitry Andric TransferFunctions tf(vals, cfg, block, ac, classification, handler);
8570b57cec5SDimitry Andric for (const auto &I : *block) {
858bdd1243dSDimitry Andric if (std::optional<CFGStmt> cs = I.getAs<CFGStmt>())
8590b57cec5SDimitry Andric tf.Visit(const_cast<Stmt *>(cs->getStmt()));
8600b57cec5SDimitry Andric }
8615ffd83dbSDimitry Andric CFGTerminator terminator = block->getTerminator();
8625ffd83dbSDimitry Andric if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt()))
8635ffd83dbSDimitry Andric if (as->isAsmGoto())
8645ffd83dbSDimitry Andric tf.Visit(as);
8650b57cec5SDimitry Andric return vals.updateValueVectorWithScratch(block);
8660b57cec5SDimitry Andric }
8670b57cec5SDimitry Andric
8680b57cec5SDimitry Andric namespace {
8690b57cec5SDimitry Andric
8700b57cec5SDimitry Andric /// PruneBlocksHandler is a special UninitVariablesHandler that is used
8710b57cec5SDimitry Andric /// to detect when a CFGBlock has any *potential* use of an uninitialized
8720b57cec5SDimitry Andric /// variable. It is mainly used to prune out work during the final
8730b57cec5SDimitry Andric /// reporting pass.
8740b57cec5SDimitry Andric struct PruneBlocksHandler : public UninitVariablesHandler {
8750b57cec5SDimitry Andric /// Records if a CFGBlock had a potential use of an uninitialized variable.
8760b57cec5SDimitry Andric llvm::BitVector hadUse;
8770b57cec5SDimitry Andric
8780b57cec5SDimitry Andric /// Records if any CFGBlock had a potential use of an uninitialized variable.
8790b57cec5SDimitry Andric bool hadAnyUse = false;
8800b57cec5SDimitry Andric
8810b57cec5SDimitry Andric /// The current block to scribble use information.
8820b57cec5SDimitry Andric unsigned currentBlock = 0;
8830b57cec5SDimitry Andric
PruneBlocksHandler__anon2e1962360611::PruneBlocksHandler8840b57cec5SDimitry Andric PruneBlocksHandler(unsigned numBlocks) : hadUse(numBlocks, false) {}
8850b57cec5SDimitry Andric
8860b57cec5SDimitry Andric ~PruneBlocksHandler() override = default;
8870b57cec5SDimitry Andric
handleUseOfUninitVariable__anon2e1962360611::PruneBlocksHandler8880b57cec5SDimitry Andric void handleUseOfUninitVariable(const VarDecl *vd,
8890b57cec5SDimitry Andric const UninitUse &use) override {
8900b57cec5SDimitry Andric hadUse[currentBlock] = true;
8910b57cec5SDimitry Andric hadAnyUse = true;
8920b57cec5SDimitry Andric }
8930b57cec5SDimitry Andric
handleConstRefUseOfUninitVariable__anon2e1962360611::PruneBlocksHandler8945ffd83dbSDimitry Andric void handleConstRefUseOfUninitVariable(const VarDecl *vd,
8955ffd83dbSDimitry Andric const UninitUse &use) override {
8965ffd83dbSDimitry Andric hadUse[currentBlock] = true;
8975ffd83dbSDimitry Andric hadAnyUse = true;
8985ffd83dbSDimitry Andric }
8995ffd83dbSDimitry Andric
9000b57cec5SDimitry Andric /// Called when the uninitialized variable analysis detects the
9010b57cec5SDimitry Andric /// idiom 'int x = x'. All other uses of 'x' within the initializer
9020b57cec5SDimitry Andric /// are handled by handleUseOfUninitVariable.
handleSelfInit__anon2e1962360611::PruneBlocksHandler9030b57cec5SDimitry Andric void handleSelfInit(const VarDecl *vd) override {
9040b57cec5SDimitry Andric hadUse[currentBlock] = true;
9050b57cec5SDimitry Andric hadAnyUse = true;
9060b57cec5SDimitry Andric }
9070b57cec5SDimitry Andric };
9080b57cec5SDimitry Andric
9090b57cec5SDimitry Andric } // namespace
9100b57cec5SDimitry Andric
runUninitializedVariablesAnalysis(const DeclContext & dc,const CFG & cfg,AnalysisDeclContext & ac,UninitVariablesHandler & handler,UninitVariablesAnalysisStats & stats)9110b57cec5SDimitry Andric void clang::runUninitializedVariablesAnalysis(
9120b57cec5SDimitry Andric const DeclContext &dc,
9130b57cec5SDimitry Andric const CFG &cfg,
9140b57cec5SDimitry Andric AnalysisDeclContext &ac,
9150b57cec5SDimitry Andric UninitVariablesHandler &handler,
9160b57cec5SDimitry Andric UninitVariablesAnalysisStats &stats) {
9170b57cec5SDimitry Andric CFGBlockValues vals(cfg);
9180b57cec5SDimitry Andric vals.computeSetOfDeclarations(dc);
9190b57cec5SDimitry Andric if (vals.hasNoDeclarations())
9200b57cec5SDimitry Andric return;
9210b57cec5SDimitry Andric
9220b57cec5SDimitry Andric stats.NumVariablesAnalyzed = vals.getNumEntries();
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric // Precompute which expressions are uses and which are initializations.
9250b57cec5SDimitry Andric ClassifyRefs classification(ac);
9260b57cec5SDimitry Andric cfg.VisitBlockStmts(classification);
9270b57cec5SDimitry Andric
9280b57cec5SDimitry Andric // Mark all variables uninitialized at the entry.
9290b57cec5SDimitry Andric const CFGBlock &entry = cfg.getEntry();
9300b57cec5SDimitry Andric ValueVector &vec = vals.getValueVector(&entry);
9310b57cec5SDimitry Andric const unsigned n = vals.getNumEntries();
9320b57cec5SDimitry Andric for (unsigned j = 0; j < n; ++j) {
9330b57cec5SDimitry Andric vec[j] = Uninitialized;
9340b57cec5SDimitry Andric }
9350b57cec5SDimitry Andric
9360b57cec5SDimitry Andric // Proceed with the workist.
9375ffd83dbSDimitry Andric ForwardDataflowWorklist worklist(cfg, ac);
9380b57cec5SDimitry Andric llvm::BitVector previouslyVisited(cfg.getNumBlockIDs());
9390b57cec5SDimitry Andric worklist.enqueueSuccessors(&cfg.getEntry());
9400b57cec5SDimitry Andric llvm::BitVector wasAnalyzed(cfg.getNumBlockIDs(), false);
9410b57cec5SDimitry Andric wasAnalyzed[cfg.getEntry().getBlockID()] = true;
9420b57cec5SDimitry Andric PruneBlocksHandler PBH(cfg.getNumBlockIDs());
9430b57cec5SDimitry Andric
9440b57cec5SDimitry Andric while (const CFGBlock *block = worklist.dequeue()) {
9450b57cec5SDimitry Andric PBH.currentBlock = block->getBlockID();
9460b57cec5SDimitry Andric
9470b57cec5SDimitry Andric // Did the block change?
9480b57cec5SDimitry Andric bool changed = runOnBlock(block, cfg, ac, vals,
9490b57cec5SDimitry Andric classification, wasAnalyzed, PBH);
9500b57cec5SDimitry Andric ++stats.NumBlockVisits;
9510b57cec5SDimitry Andric if (changed || !previouslyVisited[block->getBlockID()])
9520b57cec5SDimitry Andric worklist.enqueueSuccessors(block);
9530b57cec5SDimitry Andric previouslyVisited[block->getBlockID()] = true;
9540b57cec5SDimitry Andric }
9550b57cec5SDimitry Andric
9560b57cec5SDimitry Andric if (!PBH.hadAnyUse)
9570b57cec5SDimitry Andric return;
9580b57cec5SDimitry Andric
9590b57cec5SDimitry Andric // Run through the blocks one more time, and report uninitialized variables.
9600b57cec5SDimitry Andric for (const auto *block : cfg)
9610b57cec5SDimitry Andric if (PBH.hadUse[block->getBlockID()]) {
9620b57cec5SDimitry Andric runOnBlock(block, cfg, ac, vals, classification, wasAnalyzed, handler);
9630b57cec5SDimitry Andric ++stats.NumBlockVisits;
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric }
9660b57cec5SDimitry Andric
9670b57cec5SDimitry Andric UninitVariablesHandler::~UninitVariablesHandler() = default;
968