[clang] [clang][Sema] Propagate pointee and qualified-base qualifiers in PerformObjectMemberConversion (PR #245525)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 23 23:56:37 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: ivan lei (ivan-lei)

<details>
<summary>Changes</summary>

Fixes #<!-- -->245523

Follow-up to #<!-- -->127824 (`e9988c36ed78`). Propagate qualifiers from the derived object/pointee (`FromRecordType`) rather than `FromType` across both the qualified-base (`NestedNameSpecifier::Kind::Type`) and pointer (`PointerConversions`) paths in `Sema::PerformObjectMemberConversion`, while preserving `DestRecordType`'s address space and any top-level pointer qualifiers on `DestType` (such as `__restrict`).

Strips qualifiers from qualified typedef nested-name-specifiers so they do not spuriously qualify the subobject, and adds AST/CodeGen regression tests in `derived-to-base.cpp` and `derived-to-base-propagate-qualifiers.cpp`.


---
Full diff: https://github.com/llvm/llvm-project/pull/245525.diff


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+4) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+18-6) 
- (modified) clang/test/CodeGenCXX/derived-to-base.cpp (+119) 
- (modified) clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp (+141-2) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 785455d8d58c6..c8350a27eda48 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1339,6 +1339,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed bugs where Clang could apply an incorrectly high alignment to a vptr
   load within a virtual function thunk. (#GH242501)
 
+- Fixed a bug where qualifiers (such as `volatile`) were dropped during
+  derived-to-base conversions for pointer (`->`) and qualified (`Base::`)
+  member accesses. (#GH245523)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 96bdaf870540e..0aad29bd964b1 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3256,6 +3256,15 @@ ExprResult Sema::PerformObjectMemberConversion(Expr *From,
 
   ExprValueKind VK = From->getValueKind();
 
+  // Propagate qualifiers from the derived object (or pointee) to base
+  // subobjects as per C++ [basic.type.qualifier]p1.2:
+  //   A volatile object is [...] a subobject of a volatile object.
+  auto PropagateQualifiers = [&](QualType TargetRecordType) {
+    Qualifiers Quals = FromRecordType.getQualifiers();
+    Quals.setAddressSpace(DestRecordType.getAddressSpace());
+    return Context.getQualifiedType(TargetRecordType, Quals);
+  };
+
   // C++ [class.member.lookup]p8:
   //   [...] Ambiguities can often be resolved by qualifying a name with its
   //   class name.
@@ -3289,6 +3298,8 @@ ExprResult Sema::PerformObjectMemberConversion(Expr *From,
                                        FromLoc, FromRange, &BasePath))
         return ExprError();
 
+      QRecordType = PropagateQualifiers(QRecordType);
+      QType = PropagateQualifiers(QType.getUnqualifiedType());
       if (PointerConversions)
         QType = Context.getPointerType(QType);
       From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase,
@@ -3310,12 +3321,13 @@ ExprResult Sema::PerformObjectMemberConversion(Expr *From,
                                    /*IgnoreAccess=*/true))
     return ExprError();
 
-  // Propagate qualifiers to base subobjects as per:
-  // C++ [basic.type.qualifier]p1.2:
-  //   A volatile object is [...] a subobject of a volatile object.
-  Qualifiers FromTypeQuals = FromType.getQualifiers();
-  FromTypeQuals.setAddressSpace(DestType.getAddressSpace());
-  DestType = Context.getQualifiedType(DestType, FromTypeQuals);
+  if (PointerConversions) {
+    QualType DestPointee = PropagateQualifiers(DestType->getPointeeType());
+    DestType = Context.getQualifiedType(Context.getPointerType(DestPointee),
+                                        DestType.getQualifiers());
+  } else {
+    DestType = PropagateQualifiers(DestType);
+  }
 
   return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, VK,
                            &BasePath);
diff --git a/clang/test/CodeGenCXX/derived-to-base.cpp b/clang/test/CodeGenCXX/derived-to-base.cpp
index c09a12b728bbb..4c0ff93a35ad2 100644
--- a/clang/test/CodeGenCXX/derived-to-base.cpp
+++ b/clang/test/CodeGenCXX/derived-to-base.cpp
@@ -51,11 +51,15 @@ namespace PR127683 {
 
 struct Base {
   int Val;
+  mutable int MutVal;
 };
   
 struct Derived : Base { };
   
 volatile Derived Obj;
+volatile Derived *Ptr;
+Derived *volatile VolPtr;
+volatile Derived *volatile VPtrToV;
 
 // CHECK-LABEL: define void @_ZN8PR12768319test_volatile_storeEv()
 // CHECK:         store volatile i32 0, ptr @_ZN8PR1276833ObjE, align 4
@@ -69,6 +73,121 @@ void test_volatile_load() {
   [[maybe_unused]] int Val = Obj.Val;
 }
 
+// CHECK-LABEL: define void @_ZN8PR12768323test_volatile_ptr_storeEv()
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+void test_volatile_ptr_store() {
+  Ptr->Val = 0;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768322test_volatile_ptr_loadEv()
+// CHECK:         load volatile i32, ptr %{{.*}}, align 4
+void test_volatile_ptr_load() {
+  [[maybe_unused]] int Val = Ptr->Val;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768324test_volatile_qual_storeEv()
+// CHECK:         store volatile i32 0, ptr @_ZN8PR1276833ObjE, align 4
+void test_volatile_qual_store() {
+  Obj.Base::Val = 0;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768323test_volatile_qual_loadEv()
+// CHECK:         load volatile i32, ptr @_ZN8PR1276833ObjE, align 4
+void test_volatile_qual_load() {
+  [[maybe_unused]] int Val = Obj.Base::Val;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768328test_volatile_ptr_qual_storeEv()
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+void test_volatile_ptr_qual_store() {
+  Ptr->Base::Val = 0;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768327test_volatile_ptr_qual_loadEv()
+// CHECK:         load volatile i32, ptr %{{.*}}, align 4
+void test_volatile_ptr_qual_load() {
+  [[maybe_unused]] int Val = Ptr->Base::Val;
+}
+
+struct DerivedMethod : Base {
+  void test_this_volatile() volatile;
+};
+
+// CHECK-LABEL: define void @_ZNV8PR12768313DerivedMethod18test_this_volatileEv(
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 1, ptr %{{.*}}, align 4
+void DerivedMethod::test_this_volatile() volatile {
+  Val = 0;
+  Base::Val = 1;
+}
+
+// Ensure volatile on the pointer itself does not make the pointee access volatile.
+// CHECK-LABEL: define void @_ZN8PR12768330test_volatile_ptr_itself_storeEv()
+// CHECK:         load volatile ptr, ptr @_ZN8PR1276836VolPtrE, align 8
+// CHECK:         store i32 0, ptr %{{.*}}, align 4
+// CHECK:         load volatile ptr, ptr @_ZN8PR1276836VolPtrE, align 8
+// CHECK:         store i32 1, ptr %{{.*}}, align 4
+void test_volatile_ptr_itself_store() {
+  VolPtr->Val = 0;
+  VolPtr->Base::Val = 1;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768335test_volatile_ptr_and_pointee_storeEv()
+// CHECK:         load volatile ptr, ptr @_ZN8PR1276837VPtrToVE, align 8
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+void test_volatile_ptr_and_pointee_store() {
+  VPtrToV->Val = 0;
+}
+
+struct Derived1 : Base {};
+struct Derived2 : Base {};
+struct VeryDerived : Derived1, Derived2 {};
+
+// CHECK-LABEL: define void @_ZN8PR12768318test_diamond_storeERVNS_11VeryDerivedEPS1_(
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 1, ptr %{{.*}}, align 4
+void test_diamond_store(volatile VeryDerived &R, volatile VeryDerived *P) {
+  R.Derived1::Val = 0;
+  P->Derived1::Val = 1;
+}
+
+struct VDerived : virtual Base {};
+
+// CHECK-LABEL: define void @_ZN8PR12768323test_virtual_base_storeERVNS_8VDerivedEPS1_(
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 1, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 2, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 3, ptr %{{.*}}, align 4
+void test_virtual_base_store(volatile VDerived &R, volatile VDerived *P) {
+  R.Val = 0;
+  R.Base::Val = 1;
+  P->Val = 2;
+  P->Base::Val = 3;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768327test_const_volatile_mutableERVKNS_7DerivedEPS1_(
+// CHECK:         store volatile i32 0, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 1, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 2, ptr %{{.*}}, align 4
+// CHECK:         store volatile i32 3, ptr %{{.*}}, align 4
+void test_const_volatile_mutable(const volatile Derived &R,
+                                 const volatile Derived *P) {
+  R.MutVal = 0;
+  R.Base::MutVal = 1;
+  P->MutVal = 2;
+  P->Base::MutVal = 3;
+}
+
+using VolBase = volatile Base;
+
+// CHECK-LABEL: define void @_ZN8PR12768326test_qualified_typedef_nnsERNS_7DerivedEPS0_(
+// CHECK:         store i32 0, ptr %{{.*}}, align 4
+// CHECK:         store i32 1, ptr %{{.*}}, align 4
+void test_qualified_typedef_nns(Derived &R, Derived *P) {
+  R.VolBase::Val = 0;
+  P->VolBase::Val = 1;
+}
+
 }
 
 // CHECK: attributes [[NUW]] = { mustprogress noinline nounwind{{.*}} }
diff --git a/clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp b/clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp
index 8a3b944ab1cb6..a5511c9f500c6 100644
--- a/clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp
+++ b/clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp
@@ -5,16 +5,38 @@ namespace PR127683 {
 
 struct Base {
   int Val;
+  mutable int MutVal;
+  void f_const() const;
+  void f_vol() volatile;
+  void f_restrict() const __restrict;
 };
 
 struct Derived : Base { };
+struct Derived1 : Base { };
+struct Derived2 : Base { };
+struct VeryDerived : Derived1, Derived2 { };
+struct VDerived : virtual Base { };
 
 // Value-initialize base class subobjects with type qualifiers.
+Derived PlainObj{};
 volatile Derived VObj;
-const Derived CObj{}; // expected-note{{variable 'CObj' declared const here}}
-const volatile Derived CVObj{}; // expected-note{{variable 'CVObj' declared const here}}
+const Derived CObj{}; // expected-note 2{{variable 'CObj' declared const here}}
+const volatile Derived CVObj{}; // expected-note 2{{variable 'CVObj' declared const here}}
 __attribute__((address_space(1))) Derived AddrSpaceObj{};
 
+Derived *PlainPtr;
+volatile Derived *VPtr;
+const Derived *CPtr; // expected-note 2{{variable 'CPtr' declared const here}}
+const volatile Derived *CVPtr; // expected-note 2{{variable 'CVPtr' declared const here}}
+Derived *volatile VolPtr;
+__attribute__((address_space(1))) Derived *ASPtr;
+volatile __attribute__((address_space(1))) Derived *VASPtr;
+
+volatile VeryDerived VVeryObj;
+volatile VeryDerived *VVeryPtr;
+volatile VDerived VVObj;
+volatile VDerived *VVPtr;
+
 void test_store() {
   // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
   VObj.Val = 0;
@@ -43,4 +65,121 @@ void test_load() {
   Val = AddrSpaceObj.Val;
 }
 
+void test_ptr_and_qualified() {
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  VPtr->Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  CPtr->Val = 1; // expected-error {{cannot assign to variable 'CPtr' with const-qualified type 'const Derived *'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  CVPtr->Val = 1; // expected-error {{cannot assign to variable 'CVPtr' with const-qualified type 'const volatile Derived *'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} '__attribute__((address_space(1))) PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  ASPtr->Val = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile __attribute__((address_space(1))) PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  VASPtr->Val = 1;
+
+  // Pointer itself is volatile, pointee is not.
+  // CHECK: `-ImplicitCastExpr {{.*}} 'PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  VolPtr->Val = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'Base *' <UncheckedDerivedToBase (Base)>
+  VolPtr->Base::Val = 1;
+
+  // Qualified member access on objects.
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile Base' lvalue <UncheckedDerivedToBase (Base)>
+  VObj.Base::Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const Base' lvalue <UncheckedDerivedToBase (Base)>
+  CObj.Base::Val = 1; // expected-error {{cannot assign to variable 'CObj' with const-qualified type 'const Derived'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile Base' lvalue <UncheckedDerivedToBase (Base)>
+  CVObj.Base::Val = 1; // expected-error {{cannot assign to variable 'CVObj' with const-qualified type 'const volatile Derived'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} '__attribute__((address_space(1))) Base' lvalue <UncheckedDerivedToBase (Base)>
+  AddrSpaceObj.Base::Val = 1;
+
+  // Qualified member access on pointers.
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile Base *' <UncheckedDerivedToBase (Base)>
+  VPtr->Base::Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const Base *' <UncheckedDerivedToBase (Base)>
+  CPtr->Base::Val = 1; // expected-error {{cannot assign to variable 'CPtr' with const-qualified type 'const Derived *'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile Base *' <UncheckedDerivedToBase (Base)>
+  CVPtr->Base::Val = 1; // expected-error {{cannot assign to variable 'CVPtr' with const-qualified type 'const volatile Derived *'}}
+
+  // CHECK: `-ImplicitCastExpr {{.*}} '__attribute__((address_space(1))) Base *' <UncheckedDerivedToBase (Base)>
+  ASPtr->Base::Val = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile __attribute__((address_space(1))) Base *' <UncheckedDerivedToBase (Base)>
+  VASPtr->Base::Val = 1;
+}
+
+void test_diamond_and_virtual() {
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+  // CHECK:   `-ImplicitCastExpr {{.*}} 'volatile Derived1' lvalue <UncheckedDerivedToBase (Derived1)>
+  VVeryObj.Derived1::Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  // CHECK:   `-ImplicitCastExpr {{.*}} 'volatile Derived1 *' <UncheckedDerivedToBase (Derived1)>
+  VVeryPtr->Derived1::Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (virtual Base)>
+  VVObj.Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile Base' lvalue <UncheckedDerivedToBase (virtual Base)>
+  VVObj.Base::Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base *' <UncheckedDerivedToBase (virtual Base)>
+  VVPtr->Val = 0;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'volatile Base *' <UncheckedDerivedToBase (virtual Base)>
+  VVPtr->Base::Val = 0;
+}
+
+void test_mutable_and_methods() {
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+  CVObj.MutVal = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile Base' lvalue <UncheckedDerivedToBase (Base)>
+  CVObj.Base::MutVal = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  CVPtr->MutVal = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile Base *' <UncheckedDerivedToBase (Base)>
+  CVPtr->Base::MutVal = 1;
+
+  // Method calls preserve method pointer qualifiers (including __restrict).
+  // CHECK: ImplicitCastExpr {{.*}} 'const PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  PlainPtr->f_const();
+  // CHECK: ImplicitCastExpr {{.*}} 'Base *' <UncheckedDerivedToBase (Base)>
+  PlainPtr->Base::f_const();
+  // CHECK: ImplicitCastExpr {{.*}} 'volatile PR127683::Base *' <UncheckedDerivedToBase (Base)>
+  VPtr->f_vol();
+  // CHECK: ImplicitCastExpr {{.*}} 'volatile Base *' <UncheckedDerivedToBase (Base)>
+  VPtr->Base::f_vol();
+  // CHECK: ImplicitCastExpr {{.*}} 'const PR127683::Base *__restrict' <UncheckedDerivedToBase (Base)>
+  PlainPtr->f_restrict();
+  // CHECK: ImplicitCastExpr {{.*}} 'Base *' <UncheckedDerivedToBase (Base)>
+  PlainPtr->Base::f_restrict();
+}
+
+using VolBase = volatile Base;
+using ConstBase = const Base;
+
+void test_qualified_typedef_nns() {
+  // CHECK: `-ImplicitCastExpr {{.*}} 'Base' lvalue <UncheckedDerivedToBase (Base)>
+  PlainObj.VolBase::Val = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'Base' lvalue <UncheckedDerivedToBase (Base)>
+  PlainObj.ConstBase::Val = 1;
+
+  // CHECK: `-ImplicitCastExpr {{.*}} 'Base *' <UncheckedDerivedToBase (Base)>
+  PlainPtr->VolBase::Val = 1;
+}
+
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/245525


More information about the cfe-commits mailing list