diff --git a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp index 2c7a644b7a793..5e7392b09fb33 100644 --- a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp +++ b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp @@ -57,23 +57,24 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const { // Single range algorithms AddStdToLLVM(llvm::makeIntrusiveRefCnt(SingleSig), - {"all_of", "any_of", - "none_of", "for_each", - "find", "find_if", - "find_if_not", "fill", - "count", "count_if", - "copy", "copy_if", - "transform", "replace", - "remove_if", "stable_sort", - "partition", "partition_point", - "is_sorted", "min_element", - "max_element", "binary_search", - "lower_bound", "upper_bound", - "unique", "uninitialized_copy"}); + {"all_of", "any_of", + "none_of", "for_each", + "find", "find_if", + "find_if_not", "fill", + "count", "count_if", + "copy", "copy_if", + "transform", "replace", + "remove_if", "stable_sort", + "partition", "partition_point", + "is_sorted", "min_element", + "max_element", "binary_search", + "lower_bound", "upper_bound", + "unique", "uninitialized_copy", + "adjacent_find"}); // Two range algorithms AddStdToLLVM(llvm::makeIntrusiveRefCnt(TwoSig), - {"equal", "mismatch", "includes"}); + {"equal", "mismatch", "includes", "search"}); return Results; } diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 409cd7b60fa00..dd71533364869 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1790,6 +1790,30 @@ OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) { return std::copy_if(adl_begin(Range), adl_end(Range), Out, P); } +/// Wrapper for std::search. +template auto search(R1 &&Range1, R2 &&Range2) { + return std::search(adl_begin(Range1), adl_end(Range1), adl_begin(Range2), + adl_end(Range2)); +} + +/// Wrapper for std::search. +template +auto search(R1 &&Range1, R2 &&Range2, BinaryPredicate P) { + return std::search(adl_begin(Range1), adl_end(Range1), adl_begin(Range2), + adl_end(Range2), P); +} + +/// Wrapper for std::adjacent_find. +template auto adjacent_find(R &&Range) { + return std::adjacent_find(adl_begin(Range), adl_end(Range)); +} + +/// Wrapper for std::adjacent_find. +template +auto adjacent_find(R &&Range, BinaryPredicate P) { + return std::adjacent_find(adl_begin(Range), adl_end(Range), P); +} + /// Return the single value in \p Range that satisfies /// \p P( *, AllowRepeats)->T * returning nullptr /// when no values or multiple values were found. diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index dbf439b8d63a0..96031276b4716 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -1758,4 +1758,161 @@ struct Bar {}; static_assert(is_incomplete_v, "Foo is incomplete"); static_assert(!is_incomplete_v, "Bar is defined"); +TEST(STLExtrasTest, Search) { + // Test finding a subsequence in the middle + std::vector Haystack = {1, 2, 3, 4, 5, 6, 7, 8}; + std::vector Needle = {4, 5, 6}; + auto It = llvm::search(Haystack, Needle); + EXPECT_NE(It, Haystack.end()); + EXPECT_EQ(It, Haystack.begin() + 3); + EXPECT_THAT(std::vector(It, It + 3), ElementsAre(4, 5, 6)); + + // Test finding at the beginning + std::vector Needle2 = {1, 2, 3}; + auto It2 = llvm::search(Haystack, Needle2); + EXPECT_NE(It2, Haystack.end()); + EXPECT_EQ(It2, Haystack.begin()); + EXPECT_THAT(std::vector(It2, It2 + 3), ElementsAre(1, 2, 3)); + + // Test finding at the end + std::vector Needle3 = {6, 7, 8}; + auto It3 = llvm::search(Haystack, Needle3); + EXPECT_NE(It3, Haystack.end()); + EXPECT_EQ(It3, Haystack.begin() + 5); + EXPECT_THAT(std::vector(It3, It3 + 3), ElementsAre(6, 7, 8)); + + // Test not finding a subsequence + std::vector Needle4 = {9, 10, 11}; + auto It4 = llvm::search(Haystack, Needle4); + EXPECT_EQ(It4, Haystack.end()); + + // Test with empty needle (should find at beginning) + std::vector EmptyNeedle; + auto It5 = llvm::search(Haystack, EmptyNeedle); + EXPECT_NE(It5, Haystack.end()); + EXPECT_EQ(It5, Haystack.begin()); + + // Test with empty haystack + std::vector EmptyHaystack; + auto It6 = llvm::search(EmptyHaystack, Needle); + EXPECT_EQ(It6, EmptyHaystack.end()); + // Test with both empty + auto It7 = llvm::search(EmptyHaystack, EmptyNeedle); + EXPECT_EQ(It7, EmptyHaystack.end()); + EXPECT_EQ(It7, EmptyHaystack.begin()); + + // Test with predicate version + std::vector Haystack2 = {10, 20, 30, 40, 50}; + std::vector Needle5 = {20, 30}; + std::vector Needle6 = {200, 300}; + auto It8 = + llvm::search(Haystack2, Needle5, [](int a, int b) { return a == b; }); + EXPECT_NE(It8, Haystack2.end()); + EXPECT_EQ(It8, Haystack2.begin() + 1); + + // Test with predicate that doesn't match + auto It9 = + llvm::search(Haystack2, Needle6, [](int a, int b) { return a == b; }); + EXPECT_EQ(It9, Haystack2.end()); + + // Test with StringRef + StringRef Str = "Hello, World!"; + StringRef Sub = "World"; + auto It10 = llvm::search(Str, Sub); + EXPECT_NE(It10, Str.end()); + EXPECT_EQ(*It10, 'W'); + + // Test with ArrayRef + ArrayRef ArrRef = Haystack; + ArrayRef NeedleRef = Needle; + auto It11 = llvm::search(ArrRef, NeedleRef); + EXPECT_NE(It11, ArrRef.end()); + EXPECT_EQ(It11, ArrRef.begin() + 3); +} + +TEST(STLExtrasTest, AdjacentFind) { + // Test finding adjacent equal elements + std::vector V = {1, 2, 3, 3, 4, 5}; + auto It = llvm::adjacent_find(V); + EXPECT_NE(It, V.end()); + EXPECT_EQ(It, V.begin() + 2); + EXPECT_EQ(*It, 3); + EXPECT_EQ(*(It + 1), 3); + + // Test not finding adjacent equal elements + std::vector V2 = {1, 2, 3, 4, 5}; + auto It2 = llvm::adjacent_find(V2); + EXPECT_EQ(It2, V2.end()); + + // Test finding at the beginning + std::vector V3 = {1, 1, 2, 3, 4}; + auto It3 = llvm::adjacent_find(V3); + EXPECT_NE(It3, V3.end()); + EXPECT_EQ(It3, V3.begin()); + EXPECT_EQ(*It3, 1); + EXPECT_EQ(*(It3 + 1), 1); + + // Test finding at the end + std::vector V4 = {1, 2, 3, 4, 5, 5}; + auto It4 = llvm::adjacent_find(V4); + EXPECT_NE(It4, V4.end()); + EXPECT_EQ(It4, V4.begin() + 4); + EXPECT_EQ(*It4, 5); + EXPECT_EQ(*(It4 + 1), 5); + + // Test with empty range + std::vector Empty; + auto It5 = llvm::adjacent_find(Empty); + EXPECT_EQ(It5, Empty.end()); + + // Test with single element + std::vector Single = {42}; + auto It6 = llvm::adjacent_find(Single); + EXPECT_EQ(It6, Single.end()); + + // Test with predicate version - finding adjacent elements that satisfy + // predicate + std::vector V5 = {1, 2, 4, 3, 5, 6}; + auto It7 = llvm::adjacent_find(V5, [](int a, int b) { return a > b; }); + EXPECT_NE(It7, V5.end()); + EXPECT_EQ(It7, V5.begin() + 2); + EXPECT_EQ(*It7, 4); + EXPECT_EQ(*(It7 + 1), 3); + + // Test with predicate that doesn't match + std::vector V6 = {1, 2, 3, 4, 5}; + auto It8 = llvm::adjacent_find(V6, [](int a, int b) { return a > b; }); + EXPECT_EQ(It8, V6.end()); + + // Test with predicate finding equal elements + std::vector V7 = {1, 2, 3, 3, 4}; + auto It9 = llvm::adjacent_find(V7, [](int a, int b) { return a == b; }); + EXPECT_NE(It9, V7.end()); + EXPECT_EQ(It9, V7.begin() + 2); + + // Test with StringRef + StringRef Str = "Helo"; + auto It10 = llvm::adjacent_find(Str); + EXPECT_EQ(It10, Str.end()); + + StringRef Str2 = "Helllo"; + auto It11 = llvm::adjacent_find(Str2); + EXPECT_NE(It11, Str2.end()); + EXPECT_EQ(*It11, 'l'); + EXPECT_EQ(*(It11 + 1), 'l'); + + // Test with ArrayRef + ArrayRef ArrRef = V; + auto It12 = llvm::adjacent_find(ArrRef); + EXPECT_NE(It12, ArrRef.end()); + EXPECT_EQ(It12, ArrRef.begin() + 2); + + // Test with list (non-random access iterator) + std::list L = {1, 2, 3, 3, 4, 5}; + auto It13 = llvm::adjacent_find(L); + EXPECT_NE(It13, L.end()); + EXPECT_EQ(*It13, 3); + EXPECT_EQ(*std::next(It13), 3); +} + } // namespace