[llvm] Inline operator== and operator!= (PR #67958)

Giulio Eulisse via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 3 00:33:16 PDT 2023


https://github.com/ktf updated https://github.com/llvm/llvm-project/pull/67958

>From 77e9d0c050ecc998ab4c2485e6a284f835689c6e Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Mon, 2 Oct 2023 10:27:50 +0200
Subject: [PATCH 1/2] Inline operator== and operator!=

Avoid triggering -Wnon-template-friend.
---
 llvm/include/llvm/ADT/PagedVector.h | 43 +++++++++++++----------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 667bece6d718385..c96e0b20e2d33d7 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -209,36 +209,31 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
       return PagePtr[ElementIdx % PageSize];
     }
 
+    /// Equality operator.
     friend bool operator==(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS);
+                           MaterializedIterator const &RHS) {
+      assert(LHS.PV == RHS.PV);
+      // Make sure we are comparing either end iterators or iterators pointing
+      // to materialized elements.
+      // It should not be possible to build two iterators pointing to non
+      // materialized elements.
+      assert(LHS.ElementIdx == LHS.PV->Size ||
+             (LHS.ElementIdx < LHS.PV->Size &&
+              LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
+      assert(RHS.ElementIdx == RHS.PV->Size ||
+             (RHS.ElementIdx < RHS.PV->Size &&
+              RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
+      return LHS.ElementIdx == RHS.ElementIdx;
+    }
+
     friend bool operator!=(MaterializedIterator const &LHS,
-                           MaterializedIterator const &RHS);
+                           MaterializedIterator const &RHS) {
+      return !(LHS == RHS);
+    }
 
     [[nodiscard]] size_t getIndex() const { return ElementIdx; }
   };
 
-  /// Equality operator.
-  friend bool operator==(MaterializedIterator const &LHS,
-                         MaterializedIterator const &RHS) {
-    assert(LHS.PV == RHS.PV);
-    // Make sure we are comparing either end iterators or iterators pointing
-    // to materialized elements.
-    // It should not be possible to build two iterators pointing to non
-    // materialized elements.
-    assert(LHS.ElementIdx == LHS.PV->Size ||
-           (LHS.ElementIdx < LHS.PV->Size &&
-            LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
-    assert(RHS.ElementIdx == RHS.PV->Size ||
-           (RHS.ElementIdx < RHS.PV->Size &&
-            RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
-    return LHS.ElementIdx == RHS.ElementIdx;
-  }
-
-  friend bool operator!=(MaterializedIterator const &LHS,
-                         MaterializedIterator const &RHS) {
-    return !(LHS == RHS);
-  }
-
   /// Iterators over the materialized elements of the vector.
   ///
   /// This includes all the elements belonging to allocated pages,

>From 8494fea5ad62645bcdc93e4e886795d9de65379c Mon Sep 17 00:00:00 2001
From: Giulio Eulisse <10544+ktf at users.noreply.github.com>
Date: Tue, 3 Oct 2023 09:33:10 +0200
Subject: [PATCH 2/2] Update llvm/include/llvm/ADT/PagedVector.h

Co-authored-by: Richard Smith <richard at metafoo.co.uk>
---
 llvm/include/llvm/ADT/PagedVector.h | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index c96e0b20e2d33d7..39d569b8df8b49f 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -212,18 +212,21 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
     /// Equality operator.
     friend bool operator==(MaterializedIterator const &LHS,
                            MaterializedIterator const &RHS) {
-      assert(LHS.PV == RHS.PV);
-      // Make sure we are comparing either end iterators or iterators pointing
-      // to materialized elements.
-      // It should not be possible to build two iterators pointing to non
-      // materialized elements.
-      assert(LHS.ElementIdx == LHS.PV->Size ||
-             (LHS.ElementIdx < LHS.PV->Size &&
-              LHS.PV->PageToDataPtrs[LHS.ElementIdx / PageSize]));
-      assert(RHS.ElementIdx == RHS.PV->Size ||
-             (RHS.ElementIdx < RHS.PV->Size &&
-              RHS.PV->PageToDataPtrs[RHS.ElementIdx / PageSize]));
-      return LHS.ElementIdx == RHS.ElementIdx;
+      return LHS.equals(RHS);
+    }
+
+  private:
+    void verify() {
+      assert(ElementIdx == PV->Size ||
+             (ElementIdx < PV->Size &&
+              PV->PageToDataPtrs[ElementIdx / PageSize]));
+    }
+
+    bool equals(const MaterializedIterator &Other) const {
+      assert(PV == Other.PV);
+      verify();
+      Other.verify();
+      return ElementIdx == Other.ElementIdx;
     }
 
     friend bool operator!=(MaterializedIterator const &LHS,



More information about the llvm-commits mailing list