[llvm] Fix getParent / getNodeBaseParent to return pointer to const (#94224) (PR #96053)

Danial Klimkin via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 03:55:12 PDT 2024


https://github.com/dklimkin created https://github.com/llvm/llvm-project/pull/96053

Fix getParent / getNodeBaseParent to return pointer to const as opposed to const pointer.

This is particularly problematic as the returned object iterators get const bit unset.

>From 0a9639f4f219ab0b263d9d7e657f64bff5897954 Mon Sep 17 00:00:00 2001
From: Danial Klimkin <dklimkin at users.noreply.github.com>
Date: Wed, 19 Jun 2024 12:46:45 +0200
Subject: [PATCH] Fix getParent / getNodeBaseParent to return pointer to const

---
 llvm/include/llvm/ADT/ilist_node.h      | 5 ++++-
 llvm/include/llvm/ADT/ilist_node_base.h | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ADT/ilist_node.h b/llvm/include/llvm/ADT/ilist_node.h
index 209fd209167e8..db0319d487433 100644
--- a/llvm/include/llvm/ADT/ilist_node.h
+++ b/llvm/include/llvm/ADT/ilist_node.h
@@ -17,6 +17,7 @@
 
 #include "llvm/ADT/ilist_node_base.h"
 #include "llvm/ADT/ilist_node_options.h"
+#include <type_traits>
 
 namespace llvm {
 
@@ -29,7 +30,9 @@ struct NodeAccess;
 /// has been set in the list options.
 template <class NodeTy, class ParentPtrTy> class node_parent_access {
 public:
-  inline const ParentPtrTy getParent() const {
+  using ConstParentPtrTy =
+      std::add_pointer_t<std::add_const_t<std::remove_pointer_t<ParentPtrTy>>>;
+  inline ConstParentPtrTy getParent() const {
     return static_cast<const NodeTy *>(this)->getNodeBaseParent();
   }
   inline ParentPtrTy getParent() {
diff --git a/llvm/include/llvm/ADT/ilist_node_base.h b/llvm/include/llvm/ADT/ilist_node_base.h
index caad87cdd4d71..5a8244336188f 100644
--- a/llvm/include/llvm/ADT/ilist_node_base.h
+++ b/llvm/include/llvm/ADT/ilist_node_base.h
@@ -10,6 +10,7 @@
 #define LLVM_ADT_ILIST_NODE_BASE_H
 
 #include "llvm/ADT/PointerIntPair.h"
+#include <type_traits>
 
 namespace llvm {
 
@@ -48,10 +49,12 @@ template <class NodeBase> class node_base_prevnext<NodeBase, true> {
 
 template <class ParentPtrTy> class node_base_parent {
   ParentPtrTy Parent = nullptr;
+  using ConstParentPtrTy =
+      std::add_pointer_t<std::add_const_t<std::remove_pointer_t<ParentPtrTy>>>;
 
 public:
   void setNodeBaseParent(ParentPtrTy Parent) { this->Parent = Parent; }
-  inline const ParentPtrTy getNodeBaseParent() const { return Parent; }
+  inline ConstParentPtrTy getNodeBaseParent() const { return Parent; }
   inline ParentPtrTy getNodeBaseParent() { return Parent; }
 };
 template <> class node_base_parent<void> {};



More information about the llvm-commits mailing list