[llvm] r342045 - [ADT] Made numerous methods of ImmutableList const

Kristof Umann via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 12 04:20:16 PDT 2018


Author: szelethus
Date: Wed Sep 12 04:20:15 2018
New Revision: 342045

URL: http://llvm.org/viewvc/llvm-project?rev=342045&view=rev
Log:
[ADT] Made numerous methods of ImmutableList const

Also added ImmutableList<T>::iterator::operator->.

Differential Revision: https://reviews.llvm.org/D51881

Modified:
    llvm/trunk/include/llvm/ADT/ImmutableList.h
    llvm/trunk/unittests/ADT/ImmutableListTest.cpp

Modified: llvm/trunk/include/llvm/ADT/ImmutableList.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ImmutableList.h?rev=342045&r1=342044&r2=342045&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ImmutableList.h (original)
+++ llvm/trunk/include/llvm/ADT/ImmutableList.h Wed Sep 12 04:20:15 2018
@@ -94,6 +94,9 @@ public:
     bool operator==(const iterator& I) const { return L == I.L; }
     bool operator!=(const iterator& I) const { return L != I.L; }
     const value_type& operator*() const { return L->getHead(); }
+    const typename std::remove_reference<value_type>::type* operator->() const {
+      return &L->getHead();
+    }
 
     ImmutableList getList() const { return L; }
   };
@@ -127,14 +130,14 @@ public:
   bool operator==(const ImmutableList& L) const { return isEqual(L); }
 
   /// getHead - Returns the head of the list.
-  const T& getHead() {
+  const T& getHead() const {
     assert(!isEmpty() && "Cannot get the head of an empty list.");
     return X->getHead();
   }
 
   /// getTail - Returns the tail of the list, which is another (possibly empty)
   ///  ImmutableList.
-  ImmutableList getTail() {
+  ImmutableList getTail() const {
     return X ? X->getTail() : nullptr;
   }
 

Modified: llvm/trunk/unittests/ADT/ImmutableListTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ImmutableListTest.cpp?rev=342045&r1=342044&r2=342045&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ImmutableListTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ImmutableListTest.cpp Wed Sep 12 04:20:15 2018
@@ -80,6 +80,36 @@ TEST_F(ImmutableListTest, OneElemIntList
   EXPECT_FALSE(L2.contains(2));
 }
 
+// We'll store references to objects of this type.
+struct Unmodifiable {
+  Unmodifiable() = default;
+
+  // We'll delete all of these special member functions to make sure no copy or
+  // move happens during insertation.
+  Unmodifiable(const Unmodifiable &) = delete;
+  Unmodifiable(const Unmodifiable &&) = delete;
+  Unmodifiable &operator=(const Unmodifiable &) = delete;
+  Unmodifiable &operator=(const Unmodifiable &&) = delete;
+
+  void doNothing() const {}
+
+  void Profile(FoldingSetNodeID &ID) const { ID.AddPointer(this); }
+};
+
+// Mostly just a check whether ImmutableList::iterator can be instantiated
+// with a reference type as a template argument.
+TEST_F(ImmutableListTest, ReferenceStoringTest) {
+  ImmutableList<const Unmodifiable &>::Factory f;
+
+  Unmodifiable N;
+  ImmutableList<const Unmodifiable &> L = f.create(N);
+  for (ImmutableList<const Unmodifiable &>::iterator It = L.begin(),
+                                                     E = L.end();
+       It != E; ++It) {
+    It->doNothing();
+  }
+}
+
 TEST_F(ImmutableListTest, CreatingIntListTest) {
   ImmutableList<Wrapper<int>>::Factory f;
 




More information about the llvm-commits mailing list