[clang] [clang][Sema] Propagate qualifiers during derived-to-base conversion (PR #127824)
Antonio Frighetto via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 10 08:49:31 PDT 2025
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/127824
>From 47fc0c9fb963cef24a32078bc52232e69a93c211 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Wed, 19 Feb 2025 16:47:18 +0100
Subject: [PATCH] [clang][Sema] Propagate qualifiers during derived-to-base
conversion
When accessing a field member through a derived-to-base conversion,
ensure qualifiers are propagated to the base class subobject.
Fixes: https://github.com/llvm/llvm-project/issues/127683.
---
clang/docs/ReleaseNotes.rst | 2 +
clang/lib/Sema/SemaExpr.cpp | 11 ++++-
clang/test/CodeGenCXX/derived-to-base.cpp | 25 ++++++++++
.../derived-to-base-propagate-qualifiers.cpp | 46 +++++++++++++++++++
4 files changed, 82 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 01b018857a7fc..034e6c691e05e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -299,6 +299,8 @@ Bug Fixes to C++ Support
- Correctly diagnoses template template paramters which have a pack parameter
not in the last position.
- Clang now correctly parses ``if constexpr`` expressions in immediate function context. (#GH123524)
+- Clang was previously coalescing volatile writes to members of volatile base class subobjects.
+ The issue has been addressed by propagating qualifiers during derived-to-base conversions in the AST. (#GH127824)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index a36a2f563739e..ce3e726e560b2 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3113,8 +3113,15 @@ Sema::PerformObjectMemberConversion(Expr *From,
/*IgnoreAccess=*/true))
return ExprError();
- return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase,
- VK, &BasePath);
+ // 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);
+
+ return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, VK,
+ &BasePath);
}
bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
diff --git a/clang/test/CodeGenCXX/derived-to-base.cpp b/clang/test/CodeGenCXX/derived-to-base.cpp
index c8dbd5bf5cb05..c09a12b728bbb 100644
--- a/clang/test/CodeGenCXX/derived-to-base.cpp
+++ b/clang/test/CodeGenCXX/derived-to-base.cpp
@@ -46,4 +46,29 @@ namespace test3 {
}
}
+// Ensure volatile is preserved during derived-to-base conversion.
+namespace PR127683 {
+
+struct Base {
+ int Val;
+};
+
+struct Derived : Base { };
+
+volatile Derived Obj;
+
+// CHECK-LABEL: define void @_ZN8PR12768319test_volatile_storeEv()
+// CHECK: store volatile i32 0, ptr @_ZN8PR1276833ObjE, align 4
+void test_volatile_store() {
+ Obj.Val = 0;
+}
+
+// CHECK-LABEL: define void @_ZN8PR12768318test_volatile_loadEv()
+// CHECK: %0 = load volatile i32, ptr @_ZN8PR1276833ObjE, align 4
+void test_volatile_load() {
+ [[maybe_unused]] int Val = Obj.Val;
+}
+
+}
+
// 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
new file mode 100644
index 0000000000000..8a3b944ab1cb6
--- /dev/null
+++ b/clang/test/SemaCXX/derived-to-base-propagate-qualifiers.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -ast-dump -verify %s | FileCheck %s
+
+// Ensure qualifiers are preserved during derived-to-base conversion.
+namespace PR127683 {
+
+struct Base {
+ int Val;
+};
+
+struct Derived : Base { };
+
+// Value-initialize base class subobjects with type qualifiers.
+volatile Derived VObj;
+const Derived CObj{}; // expected-note{{variable 'CObj' declared const here}}
+const volatile Derived CVObj{}; // expected-note{{variable 'CVObj' declared const here}}
+__attribute__((address_space(1))) Derived AddrSpaceObj{};
+
+void test_store() {
+ // CHECK: `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ VObj.Val = 0;
+
+ // CHECK: `-ImplicitCastExpr {{.*}} 'const PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ CObj.Val = 1; // expected-error {{cannot assign to variable 'CObj' with const-qualified type 'const Derived'}}
+
+ // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ CVObj.Val = 1; // expected-error {{cannot assign to variable 'CVObj' with const-qualified type 'const volatile Derived'}}
+
+ // CHECK: `-ImplicitCastExpr {{.*}} '__attribute__((address_space(1))) PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ AddrSpaceObj.Val = 1;
+}
+
+void test_load() {
+ // CHECK: `-ImplicitCastExpr {{.*}} <col:30> 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ [[maybe_unused]] int Val = VObj.Val;
+
+ // CHECK: `-ImplicitCastExpr {{.*}} 'const PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ Val = CObj.Val;
+
+ // CHECK: `-ImplicitCastExpr {{.*}} 'const volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ Val = CVObj.Val;
+
+ // CHECK: `-ImplicitCastExpr {{.*}} '__attribute__((address_space(1))) PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+ Val = AddrSpaceObj.Val;
+}
+
+}
More information about the cfe-commits
mailing list