Skip to content

Conversation

@davidstone
Copy link
Contributor

Rather than just blindly assuming we can modify the data pointed to by a const void *, just accept and return const-qualified pointers. This eliminates a const_cast and makes a reinterpret_cast no longer act as a const_cast.

…in pointers to `const`

Rather than just blindly assuming we can modify the data pointed to by a `const void *`, just accept and return `const`-qualified pointers. This eliminates a `const_cast` and makes a `reinterpret_cast` no longer act as a `const_cast`.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2025

@llvm/pr-subscribers-clang

Author: David Stone (davidstone)

Changes

Rather than just blindly assuming we can modify the data pointed to by a const void *, just accept and return const-qualified pointers. This eliminates a const_cast and makes a reinterpret_cast no longer act as a const_cast.


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

1 Files Affected:

  • (modified) clang/include/clang/AST/APValue.h (+10-10)
diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h
index cb942ea865e2d..8a2d6d434792a 100644
--- a/clang/include/clang/AST/APValue.h
+++ b/clang/include/clang/AST/APValue.h
@@ -51,8 +51,8 @@ class TypeInfoLValue {
   const Type *getType() const { return T; }
   explicit operator bool() const { return T; }
 
-  void *getOpaqueValue() { return const_cast<Type*>(T); }
-  static TypeInfoLValue getFromOpaqueValue(void *Value) {
+  const void *getOpaqueValue() const { return T; }
+  static TypeInfoLValue getFromOpaqueValue(const void *Value) {
     TypeInfoLValue V;
     V.T = reinterpret_cast<const Type*>(Value);
     return V;
@@ -72,11 +72,11 @@ class DynamicAllocLValue {
 
   explicit operator bool() const { return Index != 0; }
 
-  void *getOpaqueValue() {
-    return reinterpret_cast<void *>(static_cast<uintptr_t>(Index)
-                                    << NumLowBitsAvailable);
+  const void *getOpaqueValue() const {
+    return reinterpret_cast<const void *>(static_cast<uintptr_t>(Index)
+                                          << NumLowBitsAvailable);
   }
-  static DynamicAllocLValue getFromOpaqueValue(void *Value) {
+  static DynamicAllocLValue getFromOpaqueValue(const void *Value) {
     DynamicAllocLValue V;
     V.Index = reinterpret_cast<uintptr_t>(Value) >> NumLowBitsAvailable;
     return V;
@@ -92,10 +92,10 @@ class DynamicAllocLValue {
 
 namespace llvm {
 template<> struct PointerLikeTypeTraits<clang::TypeInfoLValue> {
-  static void *getAsVoidPointer(clang::TypeInfoLValue V) {
+  static const void *getAsVoidPointer(clang::TypeInfoLValue V) {
     return V.getOpaqueValue();
   }
-  static clang::TypeInfoLValue getFromVoidPointer(void *P) {
+  static clang::TypeInfoLValue getFromVoidPointer(const void *P) {
     return clang::TypeInfoLValue::getFromOpaqueValue(P);
   }
   // Validated by static_assert in APValue.cpp; hardcoded to avoid needing
@@ -104,10 +104,10 @@ template<> struct PointerLikeTypeTraits<clang::TypeInfoLValue> {
 };
 
 template<> struct PointerLikeTypeTraits<clang::DynamicAllocLValue> {
-  static void *getAsVoidPointer(clang::DynamicAllocLValue V) {
+  static const void *getAsVoidPointer(clang::DynamicAllocLValue V) {
     return V.getOpaqueValue();
   }
-  static clang::DynamicAllocLValue getFromVoidPointer(void *P) {
+  static clang::DynamicAllocLValue getFromVoidPointer(const void *P) {
     return clang::DynamicAllocLValue::getFromOpaqueValue(P);
   }
   static constexpr int NumLowBitsAvailable =

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants