[clang] [clang][NFC] `getAsVoidPointer` and `getFromVoidPointer` should deal in pointers to `const` (PR #172572)
David Stone via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 16 14:48:42 PST 2025
https://github.com/davidstone created https://github.com/llvm/llvm-project/pull/172572
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`.
>From 134b372fd6b72b65e1b2276a42b73589208a2a66 Mon Sep 17 00:00:00 2001
From: David Stone <davidfromonline at gmail.com>
Date: Tue, 16 Dec 2025 15:48:01 -0700
Subject: [PATCH] [clang][NFC] `getAsVoidPointer` and `getFromVoidPointer`
should deal 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`.
---
clang/include/clang/AST/APValue.h | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
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 =
More information about the cfe-commits
mailing list