Index: clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h =================================================================== --- clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h +++ clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h @@ -438,6 +438,10 @@ /// other functions that handle specific kinds of statements. void Visit(const Stmt *S, ExplodedNode *Pred, ExplodedNodeSet &Dst); + /// VisitArrayInitLoopExpr - Transfer function for array init loop. + void VisitArrayInitLoopExpr(const ArrayInitLoopExpr *Ex, ExplodedNode *Pred, + ExplodedNodeSet &Dst); + /// VisitArraySubscriptExpr - Transfer function for array accesses. void VisitArraySubscriptExpr(const ArraySubscriptExpr *Ex, ExplodedNode *Pred, Index: clang/lib/StaticAnalyzer/Core/ExprEngine.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -1375,10 +1375,14 @@ break; } + case Stmt::ArrayInitLoopExprClass: + Bldr.takeNodes(Pred); + VisitArrayInitLoopExpr(cast(S), Pred, Dst); + Bldr.addNodes(Dst); + break; // Cases not handled yet; but will handle some day. case Stmt::DesignatedInitExprClass: case Stmt::DesignatedInitUpdateExprClass: - case Stmt::ArrayInitLoopExprClass: case Stmt::ArrayInitIndexExprClass: case Stmt::ExtVectorElementExprClass: case Stmt::ImaginaryLiteralClass: @@ -2603,15 +2607,109 @@ // operator&. return; } - if (isa(D)) { - // FIXME: proper support for bound declarations. - // For now, let's just prevent crashing. + if (const auto *BD = dyn_cast(D)) { + const DecompositionDecl *DD = + dyn_cast(BD->getDecomposedDecl()); + + // Handle binding to arrays + if (const auto *ASE = dyn_cast(BD->getBinding())) { + SVal Base = state->getLValue(DD, LCtx); + + if (DD->getType()->isReferenceType()) { + Base = state->getSVal(Base.getAsRegion()); + } + + SVal V = state->getLValue( + BD->getType(), state->getSVal(ASE->getIdx()->IgnoreParens(), LCtx), + Base); + + Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V), nullptr, + ProgramPoint::PostLValueKind); + return; + } + return; } llvm_unreachable("Support for this Decl not implemented."); } +/// VisitArrayInitLoopExpr - Transfer function for array init loop. +void ExprEngine::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *Ex, + ExplodedNode *Pred, + ExplodedNodeSet &Dst) { + ExplodedNodeSet CheckerPreStmt; + getCheckerManager().runCheckersForPreStmt(CheckerPreStmt, Pred, Ex, *this); + + ExplodedNodeSet EvalSet; + StmtNodeBuilder Bldr(CheckerPreStmt, EvalSet, *currBldrCtx); + + const Expr *Arr = Ex->getCommonExpr()->getSourceExpr(); + const Expr *Init = Ex->getSubExpr(); + std::size_t ArrSize = Ex->getArraySize().getLimitedValue(); + + for (auto *Node : CheckerPreStmt) { + const LocationContext *LCtx = Node->getLocationContext(); + ProgramStateRef state = Node->getState(); + + SVal Base; + + // Case of implicit copy or move ctor of object with array member + // Note: ExprEngine::VisitMemberExpr is not able to bind the array to the + // environment. + if (const auto *ME = dyn_cast(Arr)) { + Expr *MEBase = ME->getBase(); + + // Move ctor + if (auto CXXSCE = dyn_cast(MEBase)) { + MEBase = CXXSCE->getSubExpr(); + } + + auto ObjDeclExpr = cast(MEBase); + SVal Obj = state->getLValue(cast(ObjDeclExpr->getDecl()), LCtx); + + Base = state->getLValue(cast(ME->getMemberDecl()), Obj); + } + + // Case of lambda capture and decomposition declaration + if (const DeclRefExpr *DRE = dyn_cast(Arr)) + Base = state->getLValue(cast(DRE->getDecl()), LCtx); + + // Note: It is guaranteed to be a value with index 0. If this value is + // undefined, the array is not initialized, otherwise it is. + SVal Fst = + state->getLValue(Init->getType(), svalBuilder.makeArrayIndex(0), Base); + + if (const MemRegion *R = Fst.getAsRegion()) + Fst = state->getSVal(R); + + if (Fst.isUnknownOrUndef()) { + llvm::ImmutableList vals = getBasicVals().getEmptySValList(); + + for (std::size_t i = ArrSize; i != 1; --i) { + vals = getBasicVals().prependSVal(SVal{}, vals); + } + + vals = getBasicVals().prependSVal(Fst, vals); + + Bldr.generateNode( + Ex, Pred, + state->BindExpr(Ex, LCtx, + svalBuilder.makeCompoundVal(Ex->getType(), vals))); + } else { + // Create lazy compound value to the original array + if (Optional L = Base.getAs()) + Base = state->getSVal(*L); + else + assert(Base.isUnknownOrUndef()); + + Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, Base)); + } + } + + getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this); +} + /// VisitArraySubscriptExpr - Transfer function for array accesses void ExprEngine::VisitArraySubscriptExpr(const ArraySubscriptExpr *A, ExplodedNode *Pred, Index: clang/test/Analysis/uninit-structured-bindings.cpp =================================================================== --- /dev/null +++ clang/test/Analysis/uninit-structured-bindings.cpp @@ -0,0 +1,73 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core -std=c++17 -verify -analyzer-output=text %s + +void array_value_a(void) { + int arr[2]; + auto [a, b] = arr; + arr[0] = 0; + int x = a; + // expected-warning@-1{{Assigned value is garbage or undefined}} + // expected-note@-2{{Assigned value is garbage or undefined}} +} + +void array_value_b(void) { + int arr[] = {1, 2}; + auto [a, b] = arr; + int x = a; // no-warning + (void)x; +} + +void array_lref_a(void) { + int arr[2]; + auto &[a, b] = arr; + int x = a; + // expected-warning@-1{{Assigned value is garbage or undefined}} + // expected-note@-2{{Assigned value is garbage or undefined}} +} + +void array_lref_b(void) { + int arr[] = {1, 2}; + auto &[a, b] = arr; + int x = a; // no-warning + (void)x; +} + +void array_lref_c(void) { + int arr[2]; + auto &[a, b] = arr; + + arr[0] = 1; + + int x = a; // no-warning + int y = b; + // expected-warning@-1{{Assigned value is garbage or undefined}} + // expected-note@-2{{Assigned value is garbage or undefined}} + (void)x; +} + +void array_rref_a(void) { + int arr[2]; + auto &&[a, b] = arr; + int x = a; + // expected-warning@-1{{Assigned value is garbage or undefined}} + // expected-note@-2{{Assigned value is garbage or undefined}} +} + +void array_rref_b(void) { + int arr[] = {1, 2}; + auto &&[a, b] = arr; + int x = a; // no-warning + (void)x; +} + +void array_rref_c(void) { + int arr[2]; + auto &&[a, b] = arr; + + arr[0] = 1; + + int x = a; // no-warning + int y = b; + // expected-warning@-1{{Assigned value is garbage or undefined}} + // expected-note@-2{{Assigned value is garbage or undefined}} + (void)x; +} \ No newline at end of file