[llvm] [llvm][Object] Add missing const qualifier for value_type in content_iterator (PR #124106)

Bushev Dmitry via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 27 09:22:20 PST 2025


https://github.com/dybv-sc updated https://github.com/llvm/llvm-project/pull/124106

>From cc28e552ec79eeb5fce4f0d1bbc3733c378e3aac Mon Sep 17 00:00:00 2001
From: Dmitry Bushev <dmitry.bushev at syntacore.com>
Date: Thu, 23 Jan 2025 15:01:30 +0300
Subject: [PATCH 1/3] [llvm][Object] Add missing const qualifier for value_type
 in content_iterator.

value_type was defined as non-const for content_iterator, although it's methods
returned a const pointers/references. This prevented it from using in some
algorithms from STLExtras.h
---
 llvm/include/llvm/Object/SymbolicFile.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Object/SymbolicFile.h b/llvm/include/llvm/Object/SymbolicFile.h
index b13588c147d9b2..2c857e72c3e5a6 100644
--- a/llvm/include/llvm/Object/SymbolicFile.h
+++ b/llvm/include/llvm/Object/SymbolicFile.h
@@ -71,7 +71,7 @@ template <class content_type> class content_iterator {
 
 public:
   using iterator_category = std::forward_iterator_tag;
-  using value_type = content_type;
+  using value_type = const content_type;
   using difference_type = std::ptrdiff_t;
   using pointer = value_type *;
   using reference = value_type &;

>From 27d2197cc7e46dc124b2a16649892f4b31b2611e Mon Sep 17 00:00:00 2001
From: Dmitry Bushev <dmitry.bushev at syntacore.com>
Date: Fri, 24 Jan 2025 22:18:27 +0300
Subject: [PATCH 2/3] added unit test

---
 llvm/unittests/Object/SymbolicFileTest.cpp | 23 ++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/llvm/unittests/Object/SymbolicFileTest.cpp b/llvm/unittests/Object/SymbolicFileTest.cpp
index 38875ce7b8cd99..bdbddcd0e95922 100644
--- a/llvm/unittests/Object/SymbolicFileTest.cpp
+++ b/llvm/unittests/Object/SymbolicFileTest.cpp
@@ -7,8 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Object/SymbolicFile.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Host.h"
+
 #include "gtest/gtest.h"
 #include <sstream>
 
@@ -38,3 +40,24 @@ TEST(Object, DataRefImplOstream) {
 
   EXPECT_EQ(Expected, s);
 }
+
+struct ProxyContent {
+  unsigned Index = 0;
+  ProxyContent(unsigned Index) : Index(Index) {};
+  void moveNext() { Index++; }
+
+  bool operator==(const ProxyContent &Another) const {
+    return Index == Another.Index;
+  }
+};
+
+TEST(Object, ContentIterator) {
+  using Iter = llvm::object::content_iterator<ProxyContent>;
+  auto Sequence = llvm::make_range(Iter(0u), Iter(10u));
+  auto EvenSequence = llvm::make_filter_range(
+      Sequence, [](auto &&PC) { return PC.Index % 2 == 0; });
+
+  for (auto &&[I, Value] : llvm::enumerate(EvenSequence)) {
+    EXPECT_EQ(I * 2u, Value.Index);
+  }
+}

>From ec01c39fcb1b106e40991038f23a6afaee387ed2 Mon Sep 17 00:00:00 2001
From: Dmitry Bushev <dmitry.bushev at syntacore.com>
Date: Mon, 27 Jan 2025 20:15:58 +0300
Subject: [PATCH 3/3] Addressing kuhar's and jh7370's comments

---
 llvm/unittests/Object/SymbolicFileTest.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/unittests/Object/SymbolicFileTest.cpp b/llvm/unittests/Object/SymbolicFileTest.cpp
index bdbddcd0e95922..c3813b12b4476d 100644
--- a/llvm/unittests/Object/SymbolicFileTest.cpp
+++ b/llvm/unittests/Object/SymbolicFileTest.cpp
@@ -10,7 +10,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Host.h"
-
+#include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <sstream>
 
@@ -44,7 +44,7 @@ TEST(Object, DataRefImplOstream) {
 struct ProxyContent {
   unsigned Index = 0;
   ProxyContent(unsigned Index) : Index(Index) {};
-  void moveNext() { Index++; }
+  void moveNext() { ++Index; }
 
   bool operator==(const ProxyContent &Another) const {
     return Index == Another.Index;
@@ -57,7 +57,5 @@ TEST(Object, ContentIterator) {
   auto EvenSequence = llvm::make_filter_range(
       Sequence, [](auto &&PC) { return PC.Index % 2 == 0; });
 
-  for (auto &&[I, Value] : llvm::enumerate(EvenSequence)) {
-    EXPECT_EQ(I * 2u, Value.Index);
-  }
+  EXPECT_THAT(EvenSequence, testing::ElementsAre(0u, 2u, 4u, 6u, 8u));
 }



More information about the llvm-commits mailing list