[clang] [clang][CodeGen] Propagate `volatile` qualifier in derived-to-base conversion (PR #127824)

Antonio Frighetto via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 20 03:24:19 PST 2025


https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/127824

>From 1c7357a3279322ba469c1293d49bfba67b0565b5 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 `volatile` during derived-to-base
 conversion

When accessing a field member through a derived-to-base conversion,
ensure the `volatile` qualifier is propagated to the base type.

Fixes: https://github.com/llvm/llvm-project/issues/127683.
---
 clang/lib/Sema/SemaExpr.cpp                   |  7 ++++--
 .../SemaCXX/derived-to-base-volatile-qual.cpp | 23 +++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/derived-to-base-volatile-qual.cpp

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fad15bf95c415..f9b9a05ce3da6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -3107,8 +3107,11 @@ Sema::PerformObjectMemberConversion(Expr *From,
                                    /*IgnoreAccess=*/true))
     return ExprError();
 
-  return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase,
-                           VK, &BasePath);
+  if (FromType.isVolatileQualified())
+    DestType.addVolatile();
+
+  return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, VK,
+                           &BasePath);
 }
 
 bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
diff --git a/clang/test/SemaCXX/derived-to-base-volatile-qual.cpp b/clang/test/SemaCXX/derived-to-base-volatile-qual.cpp
new file mode 100644
index 0000000000000..67df441b99a2a
--- /dev/null
+++ b/clang/test/SemaCXX/derived-to-base-volatile-qual.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -ast-dump %s | FileCheck %s
+
+// Ensure volatile is preserved during derived-to-base conversion. 
+namespace PR127683 {
+
+struct Base {
+  int Val;
+};
+
+struct Derived : Base { };
+
+volatile Derived Obj;
+
+// CHECK:      `-FunctionDecl {{.*}} test_volatile_store 'void ()'
+// CHECK-NEXT:   `-CompoundStmt {{.*}}
+// CHECK-NEXT:     `-BinaryOperator {{.*}} 'volatile int' lvalue '='
+// CHECK-NEXT:       |-MemberExpr {{.*}} 'volatile int' lvalue .Val
+// CHECK-NEXT:       | `-ImplicitCastExpr {{.*}} 'volatile PR127683::Base' lvalue <UncheckedDerivedToBase (Base)>
+void test_volatile_store() {
+  Obj.Val = 0;
+}
+
+}



More information about the cfe-commits mailing list