[llvm] 3f48e22 - [IList] Fix getNodeParent() interaction with const qualifiers (#228362)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 02:44:05 PDT 2026
Author: Nikita Popov
Date: 2026-10-02T09:43:58Z
New Revision: 3f48e22a1f321d5d3341bd812803694cea588785
URL: https://github.com/llvm/llvm-project/commit/3f48e22a1f321d5d3341bd812803694cea588785
DIFF: https://github.com/llvm/llvm-project/commit/3f48e22a1f321d5d3341bd812803694cea588785.diff
LOG: [IList] Fix getNodeParent() interaction with const qualifiers (#228362)
In the old implementation getNodeParent() did not work at all on
`const_iterator` (because the body would fail to compile) and `const
iterator` (because the method was not marked const).
Fix this and add more unit tests for various combinations of constness.
It should be noted that `const iterator` behaves just like plain
`iterator` in that it returns a non-const parent pointer. Only
`const_iterator` returns a const parent pointer.
Added:
Modified:
llvm/include/llvm/ADT/ilist_iterator.h
llvm/unittests/ADT/IListIteratorTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/ilist_iterator.h b/llvm/include/llvm/ADT/ilist_iterator.h
index 882df9d7e767f..99b86a6edda59 100644
--- a/llvm/include/llvm/ADT/ilist_iterator.h
+++ b/llvm/include/llvm/ADT/ilist_iterator.h
@@ -59,14 +59,14 @@ template <class IteratorTy, class ParentTy>
class iterator_parent_access<IteratorTy, ParentTy, true> {
public:
inline const ParentTy *getNodeParent() const {
- return static_cast<IteratorTy *>(this)->NodePtr->getParent();
+ return static_cast<const IteratorTy *>(this)->NodePtr->getParent();
}
};
template <class IteratorTy, class ParentTy>
class iterator_parent_access<IteratorTy, ParentTy, false> {
public:
- inline ParentTy *getNodeParent() {
- return static_cast<IteratorTy *>(this)->NodePtr->getParent();
+ inline ParentTy *getNodeParent() const {
+ return static_cast<const IteratorTy *>(this)->NodePtr->getParent();
}
};
template <class IteratorTy>
diff --git a/llvm/unittests/ADT/IListIteratorTest.cpp b/llvm/unittests/ADT/IListIteratorTest.cpp
index 54a4258246e9b..257b7b93f7c2b 100644
--- a/llvm/unittests/ADT/IListIteratorTest.cpp
+++ b/llvm/unittests/ADT/IListIteratorTest.cpp
@@ -189,10 +189,31 @@ TEST(IListIteratorTest, GetParent) {
EXPECT_EQ(&P, L.rbegin().getNodeParent());
EXPECT_EQ(&P, L.rend().getNodeParent());
+ // Check we can get the node parent from "const_iterator".
+ EXPECT_EQ(&P, CL.begin().getNodeParent());
+ EXPECT_EQ(&P, CL.end().getNodeParent());
+ EXPECT_EQ(&P, CL.rbegin().getNodeParent());
+ EXPECT_EQ(&P, CL.rend().getNodeParent());
+
+ // Check we can get the node parent from "const iterator".
+ const auto ConstBegin = L.begin();
+ EXPECT_EQ(&P, ConstBegin.getNodeParent());
+ const auto ConstEnd = L.end();
+ EXPECT_EQ(&P, ConstEnd.getNodeParent());
+ const auto ConstRBegin = L.rbegin();
+ EXPECT_EQ(&P, ConstRBegin.getNodeParent());
+ const auto ConstREnd = L.rend();
+ EXPECT_EQ(&P, ConstREnd.getNodeParent());
+
using VarParentTy =
std::remove_pointer_t<decltype(L.begin().getNodeParent())>;
+ using ConstVarParentTy =
+ std::remove_pointer_t<decltype(ConstBegin.getNodeParent())>;
using ConstParentTy =
std::remove_pointer_t<decltype(CL.begin().getNodeParent())>;
+ static_assert(
+ std::is_same_v<VarParentTy, ConstVarParentTy>,
+ "const iterator should behave like iterator, not const_iterator");
static_assert(
std::is_const_v<ConstParentTy> &&
std::is_same_v<VarParentTy, std::remove_const_t<ConstParentTy>>,
More information about the llvm-commits
mailing list