Skip to content

[ADT][NFCI]: Fix iterator category for graph iterators with external … #116403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion llvm/include/llvm/ADT/DepthFirstIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "llvm/ADT/iterator_range.h"
#include <iterator>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -84,7 +85,10 @@ template <class GraphT,
bool ExtStorage = false, class GT = GraphTraits<GraphT>>
class df_iterator : public df_iterator_storage<SetType, ExtStorage> {
public:
using iterator_category = std::forward_iterator_tag;
// When External storage is used we are not multi-pass safe.
using iterator_category =
std::conditional_t<ExtStorage, std::input_iterator_tag,
std::forward_iterator_tag>;
using value_type = typename GT::NodeRef;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
Expand Down
6 changes: 5 additions & 1 deletion llvm/include/llvm/ADT/PostOrderIterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <iterator>
#include <optional>
#include <set>
#include <type_traits>
#include <utility>

namespace llvm {
Expand Down Expand Up @@ -95,7 +96,10 @@ template <class GraphT,
bool ExtStorage = false, class GT = GraphTraits<GraphT>>
class po_iterator : public po_iterator_storage<SetType, ExtStorage> {
public:
using iterator_category = std::forward_iterator_tag;
// When External storage is used we are not multi-pass safe.
using iterator_category =
std::conditional_t<ExtStorage, std::input_iterator_tag,
std::forward_iterator_tag>;
using value_type = typename GT::NodeRef;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
Expand Down
31 changes: 31 additions & 0 deletions llvm/unittests/ADT/BreadthFirstIteratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "TestGraph.h"
#include "gtest/gtest.h"

#include <array>
#include <iterator>
#include <type_traits>

#include <cstddef>

using namespace llvm;

namespace llvm {
Expand Down Expand Up @@ -74,4 +80,29 @@ static_assert(
std::is_convertible_v<decltype(*std::declval<bf_iterator<Graph<3>>>()),
typename bf_iterator<Graph<3>>::reference>);

// bf_iterator should be (at-least) a forward-iterator
static_assert(std::is_base_of_v<std::forward_iterator_tag,
bf_iterator<Graph<4>>::iterator_category>);

TEST(BreadthFristIteratorTest, MultiPassSafeWithInternalSet) {
Graph<4> G;
G.AddEdge(0, 1);
G.AddEdge(1, 2);
G.AddEdge(1, 3);

std::array<decltype(G)::NodeType *, 4> NodesFirstPass, NodesSecondPass;

auto B = bf_begin(G), E = bf_end(G);

std::size_t I = 0;
for (auto It = B; It != E; ++It)
NodesFirstPass[I++] = *It;

I = 0;
for (auto It = B; It != E; ++It)
NodesSecondPass[I++] = *It;

EXPECT_EQ(NodesFirstPass, NodesSecondPass);
}

} // end namespace llvm
35 changes: 35 additions & 0 deletions llvm/unittests/ADT/DepthFirstIteratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "TestGraph.h"
#include "gtest/gtest.h"

#include <array>
#include <iterator>
#include <type_traits>

#include <cstddef>

using namespace llvm;

namespace llvm {
Expand Down Expand Up @@ -55,4 +61,33 @@ static_assert(
std::is_convertible_v<decltype(*std::declval<df_iterator<Graph<3>>>()),
typename df_iterator<Graph<3>>::reference>);

// df_iterator should be (at-least) a forward-iterator
static_assert(std::is_base_of_v<std::forward_iterator_tag,
df_iterator<Graph<4>>::iterator_category>);

// df_ext_iterator cannot provide multi-pass guarantee, therefore its only
// an input-iterator
static_assert(std::is_same_v<df_ext_iterator<Graph<4>>::iterator_category,
std::input_iterator_tag>);

TEST(DepthFirstIteratorTest, MultiPassSafeWithInternalSet) {
Graph<4> G;
G.AddEdge(0, 1);
G.AddEdge(1, 2);
G.AddEdge(1, 3);

std::array<decltype(G)::NodeType *, 4> NodesFirstPass, NodesSecondPass;

auto B = df_begin(G), E = df_end(G);

std::size_t I = 0;
for (auto It = B; It != E; ++It)
NodesFirstPass[I++] = *It;

I = 0;
for (auto It = B; It != E; ++It)
NodesSecondPass[I++] = *It;

EXPECT_EQ(NodesFirstPass, NodesSecondPass);
}
}
36 changes: 36 additions & 0 deletions llvm/unittests/ADT/PostOrderIteratorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include "gtest/gtest.h"
#include "TestGraph.h"

#include <array>
#include <iterator>
#include <type_traits>

#include <cstddef>

using namespace llvm;

namespace {
Expand Down Expand Up @@ -75,4 +81,34 @@ TEST(PostOrderIteratorTest, PostOrderAndReversePostOrderTraverrsal) {
EXPECT_EQ(1, FromIterator[4]);
EXPECT_EQ(4, FromIterator[5]);
}

// po_iterator should be (at-least) a forward-iterator
static_assert(std::is_base_of_v<std::forward_iterator_tag,
po_iterator<Graph<4>>::iterator_category>);

// po_ext_iterator cannot provide multi-pass guarantee, therefore its only
// an input-iterator
static_assert(std::is_same_v<po_ext_iterator<Graph<4>>::iterator_category,
std::input_iterator_tag>);

TEST(PostOrderIteratorTest, MultiPassSafeWithInternalSet) {
Graph<4> G;
G.AddEdge(0, 1);
G.AddEdge(1, 2);
G.AddEdge(1, 3);

std::array<decltype(G)::NodeType *, 4> NodesFirstPass, NodesSecondPass;

auto B = po_begin(G), E = po_end(G);

std::size_t I = 0;
for (auto It = B; It != E; ++It)
NodesFirstPass[I++] = *It;

I = 0;
for (auto It = B; It != E; ++It)
NodesSecondPass[I++] = *It;

EXPECT_EQ(NodesFirstPass, NodesSecondPass);
}
}
Loading