https://github.com/AltriaSuki created https://github.com/llvm/llvm-project/pull/196260
For non-static data member copy initialization, Clang used the location of the '=' token as the construction location for the synthesized CXXConstructExpr. This caused the expression source range for cases like 'D d = d0;' to include '= d0', unlike the corresponding VarDecl case. Use an invalid equal location for member default initializer copy-initialization so the constructed expression range begins at the initializer expression itself. Fixes #190816. >From 870c7dc99a12499194aff7692ccff691d90f5776 Mon Sep 17 00:00:00 2001 From: yuki <[email protected]> Date: Thu, 7 May 2026 16:37:55 +0800 Subject: [PATCH] [Clang][Sema] Fix source range for FieldDecl copy initialization For non-static data member copy initialization, Clang used the location of the '=' token as the construction location for the synthesized CXXConstructExpr. This caused the expression source range for cases like 'D d = d0;' to include '= d0', unlike the corresponding VarDecl case. Use an invalid equal location for member default initializer copy-initialization so the constructed expression range begins at the initializer expression itself. Fixes #190816. --- clang/lib/Sema/SemaDeclCXX.cpp | 3 ++- clang/test/AST/sourceranges.cpp | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 1e339fee29ab8..19c13e5540f39 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -4210,7 +4210,8 @@ ExprResult Sema::ConvertMemberDefaultInitExpression(FieldDecl *FD, ? InitializationKind::CreateDirectList(InitExpr->getBeginLoc(), InitExpr->getBeginLoc(), InitExpr->getEndLoc()) - : InitializationKind::CreateCopy(InitExpr->getBeginLoc(), InitLoc); + : InitializationKind::CreateCopy(InitExpr->getBeginLoc(), + SourceLocation()); InitializationSequence Seq(*this, Entity, Kind, InitExpr); return Seq.Perform(*this, Entity, Kind, InitExpr); } diff --git a/clang/test/AST/sourceranges.cpp b/clang/test/AST/sourceranges.cpp index 598a28bddad60..1126e4d2ed408 100644 --- a/clang/test/AST/sourceranges.cpp +++ b/clang/test/AST/sourceranges.cpp @@ -180,6 +180,53 @@ namespace in_class_init { }; } +// CHECK-1Z: NamespaceDecl {{.*}} field_copy_init +namespace field_copy_init { + struct D {}; + + // CHECK-1Z: CXXRecordDecl {{.*}} struct S definition + struct S { + D d0; + + // CHECK-1Z: FieldDecl {{.*}} d1 'D' + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:12> 'D' + D d1 = d0; + + // CHECK-1Z: FieldDecl {{.*}} d2 'D' + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:12, col:15> 'D' + D d2 = {d0}; + + // CHECK-1Z: FieldDecl {{.*}} d3 'D' + // CHECK-1Z-NEXT: CXXTemporaryObjectExpr {{.*}} <col:12, col:16> 'D' + D d3 = D{d0}; + + // CHECK-1Z: FieldDecl {{.*}} d4 'D' + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:9, col:12> 'D' + D d4{d0}; + }; + + // CHECK-1Z: FunctionDecl {{.*}} fn 'void ()' + void fn() { + D d0; + + // CHECK-1Z: VarDecl {{.*}} d1 'D' cinit + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:12> 'D' + D d1 = d0; + + // CHECK-1Z: VarDecl {{.*}} d2 'D' cinit + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:12, col:15> 'D' + D d2 = {d0}; + + // CHECK-1Z: VarDecl {{.*}} d3 'D' cinit + // CHECK-1Z-NEXT: CXXTemporaryObjectExpr {{.*}} <col:12, col:16> 'D' + D d3 = D{d0}; + + // CHECK-1Z: VarDecl {{.*}} d4 'D' listinit + // CHECK-1Z-NEXT: CXXConstructExpr {{.*}} <col:7, col:12> 'D' + D d4{d0}; + } +} + // CHECK-1Z: NamespaceDecl {{.*}} delegating_constructor_init namespace delegating_constructor_init { struct A {}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
