https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/244274
>From 41ff9a71f55cd5625b05dbd580ace2ab8a99da69 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Wed, 18 Nov 2026 23:54:24 +0530 Subject: [PATCH 1/6] [Clang][CodeGen] Fix crash on a compound literal in a VLA bound Fixes #237441 Since #219045 a C compound literal queues its `llvm.lifetime.end` as a cleanup for the end of the enclosing full-expression. An array bound never gets an `ExprWithCleanups`, so a literal in a VLA bound left the cleanup queued and `FinishFunction` asserted, for parameters and local VLAs alike. `EmitVariablyModifiedType` now evaluates each bound in its own `RunCleanupsScope`, so the cleanup reaches the enclosing scope and the literal lives to the end of its block, the function body for a parameter. A C++ temporary in a bound is now destroyed at the end of the declaration as well. LLM tools were used for this contribution. --- clang/docs/ReleaseNotes.md | 1 + clang/lib/CodeGen/CodeGenFunction.cpp | 11 +++- .../test/CodeGen/compound-literal-lifetime.c | 57 +++++++++++++++++++ clang/test/CodeGenCXX/vla.cpp | 16 +++++- 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index a72577e6b33d71..fc337b98c86c7c 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -878,6 +878,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and `declare mapper` directive. (#GH223366) - Fixed an assertion failure when a variable implicitly mapped by an OpenMP `target` directive has a class type (such as `std::map`) whose mapper lookup instantiates a class template specialization. (#GH154704) +- Fixed a crash when a C compound literal appears in the bound of a variable length array. (#GH237441) - Fixed a bug where repeated #imports of modular headers in non-modular compilation were translated to #pragma clang module import. (#GH216924) - Fixed an assertion when `#pragma omp declare simd` or `#pragma omp declare variant` is followed by another OpenMP declarative directive containing a qualified identifier. (#GH217204) - Fixed crashes on an OpenMP `target` region inside a lambda or block at namespace scope, including when the region used a global reference; such a region in a lambda is now also emitted for the offloading device. (#GH223397, #GH136652) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 33434526d86325..742c429411664b 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -2653,7 +2653,16 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType type) { // e.g. with a typedef and a pointer to it. llvm::Value *&entry = VLASizeMap[sizeExpr]; if (!entry) { - llvm::Value *size = EmitScalarExpr(sizeExpr); + // The bound is never wrapped in an ExprWithCleanups, so provide the + // full-expression scope here: cleanups queued while evaluating it, + // such as the lifetime end of a C compound literal, must be handed + // to the enclosing scope rather than left queued. + llvm::Value *size; + { + RunCleanupsScope BoundScope(*this); + size = EmitScalarExpr(sizeExpr); + BoundScope.ForceCleanup({&size}); + } // C11 6.7.6.2p5: // If the size is an expression that is not an integer constant diff --git a/clang/test/CodeGen/compound-literal-lifetime.c b/clang/test/CodeGen/compound-literal-lifetime.c index c61b8189de6125..cef10702dcb411 100644 --- a/clang/test/CodeGen/compound-literal-lifetime.c +++ b/clang/test/CodeGen/compound-literal-lifetime.c @@ -103,3 +103,60 @@ int test_musttail(int cond) { s = cond ? (struct foo){.x = 1} : (struct foo){.x = 2}; [[clang::musttail]] return callee(s.x); } + +// GH237441: a compound literal in a VLA bound used to leave its lifetime cleanup +// queued and FinishFunction asserted. It lives until the end of the enclosing +// block, which for a parameter is the whole function. +int size(int *); +void use(char *); +void other(void); + +// CHECK-LABEL: define dso_local void @GH237441_param( +// CHECK: call void @llvm.lifetime.start.p0(ptr nonnull %[[CL:.*]]) +// CHECK: call i32 @size(ptr noundef nonnull %[[CL]]) +// CHECK: call void @llvm.lifetime.end.p0(ptr nonnull %[[CL]]) +// CHECK-NEXT: ret void +void GH237441_param(char *c[size((int[]){})]) {} + +// CHECK-LABEL: define dso_local void @GH237441_init( +// CHECK: call void @llvm.lifetime.start.p0(ptr nonnull %[[CL:.*]]) +// CHECK: call i32 @side_effect() +// CHECK: call i32 @size(ptr noundef nonnull %[[CL]]) +// CHECK: call void @llvm.lifetime.end.p0(ptr nonnull %[[CL]]) +// CHECK-NEXT: ret void +void GH237441_init(char (*p)[size((int[]){side_effect(), 2})]) {} + +// CHECK-LABEL: define dso_local void @GH237441_local( +// CHECK: call void @llvm.lifetime.start.p0(ptr nonnull %[[CL:.*]]) +// CHECK: call i32 @size(ptr noundef nonnull %[[CL]]) +// CHECK: call void @use( +// CHECK: call void @llvm.lifetime.end.p0(ptr nonnull %[[CL]]) +// CHECK: ret void +void GH237441_local(void) { + char c[size((int[]){1})]; + use(c); +} + +// The literal's lifetime ends with its block, before the next statement. +// CHECK-LABEL: define dso_local void @GH237441_nested( +// CHECK: call void @llvm.lifetime.start.p0(ptr nonnull %[[CL:.*]]) +// CHECK: call i32 @size(ptr noundef nonnull %[[CL]]) +// CHECK: call void @use( +// CHECK: call void @llvm.lifetime.end.p0(ptr nonnull %[[CL]]) +// CHECK: call void @other() +// CHECK: ret void +void GH237441_nested(void) { + { + char c[size((int[]){1})]; + use(c); + } + other(); +} + +// A bound without a compound literal gets no lifetime markers. +// CHECK-LABEL: define dso_local void @GH237441_control( +// CHECK-NOT: llvm.lifetime +// CHECK: call i32 @size( +// CHECK-NOT: llvm.lifetime +// CHECK: ret void +void GH237441_control(int n, char *c[size(&n)]) {} diff --git a/clang/test/CodeGenCXX/vla.cpp b/clang/test/CodeGenCXX/vla.cpp index f96412cbe68243..9216634740ce3f 100644 --- a/clang/test/CodeGenCXX/vla.cpp +++ b/clang/test/CodeGenCXX/vla.cpp @@ -119,8 +119,22 @@ void test3(int b, int c) { //CHECK-NEXT: [[VLA_END_PTR:%.*]] = getelementptr inbounds nuw i32, ptr {{%.*}}, i64 [[VLA_END_INDEX]] //X64-NEXT: store ptr [[VLA_END_PTR]], ptr %__end //AMDGCN-NEXT: store ptr [[VLA_END_PTR]], ptr [[END]] - + for (auto &d : varr) 0; } +// GH237441: a temporary in a VLA bound is destroyed at the end of the +// declaration, before the array is allocated. +struct GH237441 { int x; GH237441(); ~GH237441(); }; +void gh237441_use(char *); +void gh237441(int n) { + // CHECK-LABEL: define{{.*}} void @_Z8gh237441i( + // CHECK: call void @_ZN8GH237441C1Ev( + // CHECK: call void @_ZN8GH237441D1Ev( + // CHECK: @llvm.stacksave + // CHECK: call void @_Z12gh237441_usePc( + char c[n + GH237441().x]; + gh237441_use(c); +} + >From f44d949652ce0e503fcca01e87249afd9dbd70de Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Fri, 20 Nov 2026 00:05:01 +0530 Subject: [PATCH 2/6] [Clang][Parser] Wrap a C VLA bound in an ExprWithCleanups Move the fix from CodeGen into the parser, as asked in review. The bound is wrapped while its constant-evaluated context is still open, since leaving the context drops the cleanup, and after the lvalue conversion so the load stays inside the wrapper. C++ bounds are unchanged. --- clang/lib/CodeGen/CodeGenFunction.cpp | 11 +---------- clang/lib/Parse/ParseDecl.cpp | 6 ++++++ clang/test/AST/ast-dump-expr.c | 11 +++++++++++ clang/test/CodeGenCXX/vla.cpp | 14 -------------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 742c429411664b..33434526d86325 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -2653,16 +2653,7 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType type) { // e.g. with a typedef and a pointer to it. llvm::Value *&entry = VLASizeMap[sizeExpr]; if (!entry) { - // The bound is never wrapped in an ExprWithCleanups, so provide the - // full-expression scope here: cleanups queued while evaluating it, - // such as the lifetime end of a C compound literal, must be handed - // to the enclosing scope rather than left queued. - llvm::Value *size; - { - RunCleanupsScope BoundScope(*this); - size = EmitScalarExpr(sizeExpr); - BoundScope.ForceCleanup({&size}); - } + llvm::Value *size = EmitScalarExpr(sizeExpr); // C11 6.7.6.2p5: // If the size is an expression that is not an integer constant diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index cdff89a6e6897b..6c6f444b489ea8 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -8067,6 +8067,12 @@ void Parser::ParseBracketDeclarator(Declarator &D) { EnterExpressionEvaluationContext Unevaluated( Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); NumElements = ParseAssignmentExpression(); + // A non-constant bound is evaluated at run time, so a compound literal + // in it needs the ExprWithCleanups made here, while the context above + // still holds the cleanup; convert first so the load stays inside it. + if (NumElements.isUsable()) + NumElements = Actions.MaybeCreateExprWithCleanups( + Actions.DefaultLvalueConversion(NumElements.get())); } } else { if (StaticLoc.isValid()) { diff --git a/clang/test/AST/ast-dump-expr.c b/clang/test/AST/ast-dump-expr.c index e7aba39be8f685..71394b7105544e 100644 --- a/clang/test/AST/ast-dump-expr.c +++ b/clang/test/AST/ast-dump-expr.c @@ -346,3 +346,14 @@ void PrimaryExpressions(int a) { // Generic selection expressions are covered elsewhere. } + +void GH237441(void) { + typedef char T[(int){3}]; + // CHECK: TypedefDecl 0x{{[^ ]*}} <col:3, col:26> col:16 T 'char[(int){3}]' + // CHECK-NEXT: VariableArrayType 0x{{[^ ]*}} 'char[(int){3}]' variably_modified + // CHECK-NEXT: BuiltinType 0x{{[^ ]*}} 'char' + // CHECK-NEXT: ExprWithCleanups 0x{{[^ ]*}} <col:18, col:25> 'int' + // CHECK-NEXT: cleanup CompoundLiteralExpr 0x{{[^ ]*}} + // CHECK-NEXT: ImplicitCastExpr 0x{{[^ ]*}} <col:18, col:25> 'int' <LValueToRValue> + // CHECK-NEXT: CompoundLiteralExpr 0x{{[^ ]*}} <col:18, col:25> 'int' lvalue +} diff --git a/clang/test/CodeGenCXX/vla.cpp b/clang/test/CodeGenCXX/vla.cpp index 9216634740ce3f..d08640b1bdc8f8 100644 --- a/clang/test/CodeGenCXX/vla.cpp +++ b/clang/test/CodeGenCXX/vla.cpp @@ -123,18 +123,4 @@ void test3(int b, int c) { for (auto &d : varr) 0; } -// GH237441: a temporary in a VLA bound is destroyed at the end of the -// declaration, before the array is allocated. -struct GH237441 { int x; GH237441(); ~GH237441(); }; -void gh237441_use(char *); -void gh237441(int n) { - // CHECK-LABEL: define{{.*}} void @_Z8gh237441i( - // CHECK: call void @_ZN8GH237441C1Ev( - // CHECK: call void @_ZN8GH237441D1Ev( - // CHECK: @llvm.stacksave - // CHECK: call void @_Z12gh237441_usePc( - char c[n + GH237441().x]; - gh237441_use(c); -} - >From 03c8ece3fe2731fedfb5f6221b9829efafe46ddc Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Fri, 20 Nov 2026 09:45:17 +0530 Subject: [PATCH 3/6] [Clang][Sema] Finish C++ array bounds as full-expressions too Review follow-up: a temporary in a C++ bound was left undestroyed during constant evaluation, so `int a[S().a]` with a `constexpr` destructor became a folded VLA. Both parsers and `TransformDependentSizedArrayType` now go through `Sema::ActOnArrayBound`, which applies the `lvalue` conversion and wraps the bound in an `ExprWithCleanups`; class-typed bounds are left to `BuildArrayType`. --- clang/docs/ReleaseNotes.md | 2 +- clang/include/clang/Sema/Sema.h | 4 ++++ clang/lib/Parse/ParseDecl.cpp | 8 +------- clang/lib/Parse/ParseExpr.cpp | 4 ++-- clang/lib/Sema/SemaExpr.cpp | 9 +++++++++ clang/lib/Sema/TreeTransform.h | 1 + clang/test/CodeGen/compound-literal-lifetime.c | 4 +--- clang/test/CodeGenCXX/vla.cpp | 13 +++++++++++++ clang/test/SemaCXX/constant-expression-cxx2a.cpp | 8 ++++++++ 9 files changed, 40 insertions(+), 13 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index fc337b98c86c7c..ab73f4373218d2 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -878,7 +878,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and `declare mapper` directive. (#GH223366) - Fixed an assertion failure when a variable implicitly mapped by an OpenMP `target` directive has a class type (such as `std::map`) whose mapper lookup instantiates a class template specialization. (#GH154704) -- Fixed a crash when a C compound literal appears in the bound of a variable length array. (#GH237441) +- Fixed a crash when a C compound literal appears in a variable length array bound; array bounds are now full-expressions, so a temporary with a constexpr destructor can also appear in a constant array bound. (#GH237441) - Fixed a bug where repeated #imports of modular headers in non-modular compilation were translated to #pragma clang module import. (#GH216924) - Fixed an assertion when `#pragma omp declare simd` or `#pragma omp declare variant` is followed by another OpenMP declarative directive containing a qualified identifier. (#GH217204) - Fixed crashes on an OpenMP `target` region inside a lambda or block at namespace scope, including when the region used a global reference; such a region in a lambda is now also emitted for the offloading device. (#GH223397, #GH136652) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 0e5548e56f678a..04540938e57ad4 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -7225,6 +7225,10 @@ class Sema final : public SemaBase { ExprResult ActOnConstantExpression(ExprResult Res); + /// Finish an array bound: apply the lvalue conversion and wrap any cleanups + /// it queued in an ExprWithCleanups while its evaluation context is open. + ExprResult ActOnArrayBound(ExprResult Bound); + // Functions for marking a declaration referenced. These functions also // contain the relevant logic for marking if a reference to a function or // variable is an odr-use (in the C++11 sense). There are separate variants diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index 6c6f444b489ea8..f2d24b80f3eece 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -8066,13 +8066,7 @@ void Parser::ParseBracketDeclarator(Declarator &D) { } else { EnterExpressionEvaluationContext Unevaluated( Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); - NumElements = ParseAssignmentExpression(); - // A non-constant bound is evaluated at run time, so a compound literal - // in it needs the ExprWithCleanups made here, while the context above - // still holds the cleanup; convert first so the load stays inside it. - if (NumElements.isUsable()) - NumElements = Actions.MaybeCreateExprWithCleanups( - Actions.DefaultLvalueConversion(NumElements.get())); + NumElements = Actions.ActOnArrayBound(ParseAssignmentExpression()); } } else { if (StaticLoc.isValid()) { diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index 14a55cfecfb8e0..8adf61f17474a2 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -166,8 +166,8 @@ ExprResult Parser::ParseArrayBoundExpression() { break; Iter->InConditionallyConstantEvaluateContext = true; } - return ParseConstantExpressionInExprEvalContext( - TypoCorrectionTypeBehavior::AllowNonTypes); + return Actions.ActOnArrayBound(ParseConstantExpressionInExprEvalContext( + TypoCorrectionTypeBehavior::AllowNonTypes)); } ExprResult Parser::ParseCaseExpression(SourceLocation CaseLoc) { diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 873ad8d8e1cb92..68338d379be03c 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -20968,6 +20968,15 @@ ExprResult Sema::ActOnConstantExpression(ExprResult Res) { return CheckLValueToRValueConversionOperand(Res.get()); } +ExprResult Sema::ActOnArrayBound(ExprResult Bound) { + // Wrap now, before the bound's context drops the cleanups, with the lvalue + // conversion inside the wrapper. A class-typed bound is converted later by + // BuildArrayType and is left alone. + if (!Bound.isUsable() || Bound.get()->getType()->isRecordType()) + return Bound; + return MaybeCreateExprWithCleanups(DefaultLvalueConversion(Bound.get())); +} + void Sema::CleanupVarDeclMarking() { // Iterate through a local copy in case MarkVarDeclODRUsed makes a recursive // call. diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 2b6348b7ec8a6b..1b8fe8f8f4b1b1 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6185,6 +6185,7 @@ TreeTransform<Derived>::TransformDependentSizedArrayType(TypeLocBuilder &TLB, ExprResult sizeResult = getDerived().TransformExpr(origSize); sizeResult = SemaRef.ActOnConstantExpression(sizeResult); + sizeResult = SemaRef.ActOnArrayBound(sizeResult); if (sizeResult.isInvalid()) return QualType(); diff --git a/clang/test/CodeGen/compound-literal-lifetime.c b/clang/test/CodeGen/compound-literal-lifetime.c index cef10702dcb411..80de8504e15954 100644 --- a/clang/test/CodeGen/compound-literal-lifetime.c +++ b/clang/test/CodeGen/compound-literal-lifetime.c @@ -104,9 +104,7 @@ int test_musttail(int cond) { [[clang::musttail]] return callee(s.x); } -// GH237441: a compound literal in a VLA bound used to leave its lifetime cleanup -// queued and FinishFunction asserted. It lives until the end of the enclosing -// block, which for a parameter is the whole function. +// GH237441: a literal in a VLA bound lives to the end of the enclosing block. int size(int *); void use(char *); void other(void); diff --git a/clang/test/CodeGenCXX/vla.cpp b/clang/test/CodeGenCXX/vla.cpp index d08640b1bdc8f8..5779bc2c367b22 100644 --- a/clang/test/CodeGenCXX/vla.cpp +++ b/clang/test/CodeGenCXX/vla.cpp @@ -123,4 +123,17 @@ void test3(int b, int c) { for (auto &d : varr) 0; } +// GH237441: a temporary in a VLA bound is destroyed at the end of the bound. +struct GH237441 { int x; GH237441(); ~GH237441(); }; +void gh237441_use(char *); +void gh237441(int n) { + // CHECK-LABEL: define{{.*}} void @_Z8gh237441i( + // CHECK: call void @_ZN8GH237441C1Ev( + // CHECK: call void @_ZN8GH237441D1Ev( + // CHECK: @llvm.stacksave + // CHECK: call void @_Z12gh237441_usePc( + char c[n + GH237441().x]; + gh237441_use(c); +} + diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp index fcc982182c06ec..37b0d4cbc2920a 100644 --- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp @@ -1543,3 +1543,11 @@ namespace GH197403 { } static_assert(test() == 0); } + +namespace GH237441 { + struct S { int a = 4; constexpr ~S() {} }; + int a[S().a]; + static_assert(sizeof(a) == 4 * sizeof(int)); + template <typename T> constexpr int f() { int b[T().a]; return sizeof(b); } + static_assert(f<S>() == 4 * sizeof(int)); +} >From 962596461d483a385b215b1943818172939eabd2 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Fri, 20 Nov 2026 18:09:24 +0530 Subject: [PATCH 4/6] [Clang][AST] Let CheckICE see through an ExprWithCleanups A C++98 array bound such as `__is_rvalue_expr(C()) ? 1 : -1` is now wrapped in an ExprWithCleanups, which CheckICE classified as never being an ICE, so the bound became a VLA. The wrapper is transparent for the ICE check; temporaries the operand evaluates are still rejected by the operand itself. --- clang/lib/AST/ExprConstant.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 647bb9c45f871a..828bc5b60557b3 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -23096,7 +23096,6 @@ static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { case Expr::CXXInheritedCtorInitExprClass: case Expr::CXXStdInitializerListExprClass: case Expr::CXXBindTemporaryExprClass: - case Expr::ExprWithCleanupsClass: case Expr::CXXTemporaryObjectExprClass: case Expr::CXXUnresolvedConstructExprClass: case Expr::CXXDependentScopeMemberExprClass: @@ -23184,6 +23183,11 @@ static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { case Expr::ConstantExprClass: return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); + case Expr::ExprWithCleanupsClass: + // Temporaries the operand evaluates are rejected by the operand itself; + // the wrapper alone, e.g. around an array bound, does not break an ICE. + return CheckICE(cast<ExprWithCleanups>(E)->getSubExpr(), Ctx); + case Expr::ParenExprClass: return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); case Expr::GenericSelectionExprClass: >From b4e8342a2c412d678b6cab65ecec596d99edad57 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sat, 21 Nov 2026 09:23:55 +0530 Subject: [PATCH 5/6] [Clang][Sema] Wrap every array bound in ActOnArrayBound Drop the record-type exception: the bound's constant-evaluated context is popped before BuildArrayType runs and drops the cleanups, so the wrapper can only be made in ActOnArrayBound. The converted-constant-expression path for class-typed bounds converts the operand of the wrapper, since BuildArrayType keeps the wrapped bound as the array's size expression. --- clang/lib/Sema/SemaExpr.cpp | 5 ++--- clang/lib/Sema/SemaType.cpp | 10 ++++++++-- clang/test/SemaCXX/constant-expression-cxx2a.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 68338d379be03c..cd8c8f22196ff6 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -20970,9 +20970,8 @@ ExprResult Sema::ActOnConstantExpression(ExprResult Res) { ExprResult Sema::ActOnArrayBound(ExprResult Bound) { // Wrap now, before the bound's context drops the cleanups, with the lvalue - // conversion inside the wrapper. A class-typed bound is converted later by - // BuildArrayType and is left alone. - if (!Bound.isUsable() || Bound.get()->getType()->isRecordType()) + // conversion inside the wrapper. + if (!Bound.isUsable()) return Bound; return MaybeCreateExprWithCleanups(DefaultLvalueConversion(Bound.get())); } diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 19d39b58614859..45295207f5cd5b 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2055,8 +2055,14 @@ static ExprResult checkArraySize(Sema &S, Expr *&ArraySize, // allow non-constant expressions and constant-folding. We only need to use // the converted constant expression rules (to properly convert the source) // when the source expression is of class type. - return S.CheckConvertedConstantExpression( - ArraySize, S.Context.getSizeType(), SizeVal, CCEKind::ArrayBound); + // + // Convert the bound itself, inside the ExprWithCleanups ActOnArrayBound + // may have wrapped it in. + Expr *Bound = ArraySize; + if (auto *Cleanups = dyn_cast<ExprWithCleanups>(Bound)) + Bound = Cleanups->getSubExpr(); + return S.CheckConvertedConstantExpression(Bound, S.Context.getSizeType(), + SizeVal, CCEKind::ArrayBound); } // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp index 37b0d4cbc2920a..a11895fc75d00a 100644 --- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp @@ -1550,4 +1550,10 @@ namespace GH237441 { static_assert(sizeof(a) == 4 * sizeof(int)); template <typename T> constexpr int f() { int b[T().a]; return sizeof(b); } static_assert(f<S>() == 4 * sizeof(int)); + + struct C { constexpr operator int() const { return 4; } constexpr ~C() {} }; + int c[C()]; + static_assert(sizeof(c) == 4 * sizeof(int)); + template <typename T> constexpr int g() { int b[T()]; return sizeof(b); } + static_assert(g<C>() == 4 * sizeof(int)); } >From 99fd996195208cfa4adb57e25603c459cb92788f Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sun, 22 Nov 2026 09:00:30 +0530 Subject: [PATCH 6/6] [Clang][Sema] Keep the bound's cleanups around the converted bound checkArraySize converts the operand of the ExprWithCleanups that ActOnArrayBound made; put that wrapper, objects included, back around the converted expression instead of relying on BuildArrayType keeping the original bound. --- clang/lib/Sema/SemaType.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 45295207f5cd5b..c60e2b01a233c7 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2056,13 +2056,17 @@ static ExprResult checkArraySize(Sema &S, Expr *&ArraySize, // the converted constant expression rules (to properly convert the source) // when the source expression is of class type. // - // Convert the bound itself, inside the ExprWithCleanups ActOnArrayBound - // may have wrapped it in. - Expr *Bound = ArraySize; - if (auto *Cleanups = dyn_cast<ExprWithCleanups>(Bound)) - Bound = Cleanups->getSubExpr(); - return S.CheckConvertedConstantExpression(Bound, S.Context.getSizeType(), - SizeVal, CCEKind::ArrayBound); + // Convert the bound itself, then put the ExprWithCleanups ActOnArrayBound + // may have wrapped it in back around the result. + auto *Cleanups = dyn_cast<ExprWithCleanups>(ArraySize); + ExprResult R = S.CheckConvertedConstantExpression( + Cleanups ? Cleanups->getSubExpr() : ArraySize, S.Context.getSizeType(), + SizeVal, CCEKind::ArrayBound); + if (Cleanups && R.isUsable()) + R = ExprWithCleanups::Create(S.Context, R.get(), + Cleanups->cleanupsHaveSideEffects(), + Cleanups->getObjects()); + return R; } // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
