|
| 1 | +// RUN: %check_clang_tidy %s llvm-use-ranges %t |
| 2 | + |
| 3 | +// Test that the header is included |
| 4 | +// CHECK-FIXES: #include "llvm/ADT/STLExtras.h" |
| 5 | + |
| 6 | +namespace std { |
| 7 | + |
| 8 | +template <typename T> class vector { |
| 9 | +public: |
| 10 | + using iterator = T *; |
| 11 | + using const_iterator = const T *; |
| 12 | + |
| 13 | + iterator begin(); |
| 14 | + iterator end(); |
| 15 | + const_iterator begin() const; |
| 16 | + const_iterator end() const; |
| 17 | + const_iterator cbegin() const; |
| 18 | + const_iterator cend() const; |
| 19 | +}; |
| 20 | + |
| 21 | +template <typename T> T* begin(T (&arr)[5]); |
| 22 | +template <typename T> T* end(T (&arr)[5]); |
| 23 | + |
| 24 | +template <class InputIt, class T> |
| 25 | +InputIt find(InputIt first, InputIt last, const T &value); |
| 26 | + |
| 27 | +template <class RandomIt> |
| 28 | +void sort(RandomIt first, RandomIt last); |
| 29 | + |
| 30 | +template <class RandomIt> |
| 31 | +void stable_sort(RandomIt first, RandomIt last); |
| 32 | + |
| 33 | +template <class InputIt, class UnaryPredicate> |
| 34 | +bool all_of(InputIt first, InputIt last, UnaryPredicate p); |
| 35 | + |
| 36 | +template <class InputIt, class UnaryFunction> |
| 37 | +UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f); |
| 38 | + |
| 39 | +template <class ForwardIt, class T> |
| 40 | +ForwardIt remove(ForwardIt first, ForwardIt last, const T& value); |
| 41 | + |
| 42 | +template <class ForwardIt> |
| 43 | +ForwardIt min_element(ForwardIt first, ForwardIt last); |
| 44 | + |
| 45 | +template <class InputIt1, class InputIt2> |
| 46 | +bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2); |
| 47 | + |
| 48 | +template <class InputIt1, class InputIt2> |
| 49 | +bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2); |
| 50 | + |
| 51 | +template <class InputIt, class OutputIt> |
| 52 | +OutputIt copy(InputIt first, InputIt last, OutputIt d_first); |
| 53 | + |
| 54 | +template <class ForwardIt, class T> |
| 55 | +void fill(ForwardIt first, ForwardIt last, const T& value); |
| 56 | + |
| 57 | +template <class BidirIt> |
| 58 | +void reverse(BidirIt first, BidirIt last); |
| 59 | + |
| 60 | +template <class ForwardIt> |
| 61 | +ForwardIt unique(ForwardIt first, ForwardIt last); |
| 62 | + |
| 63 | +template <class ForwardIt> |
| 64 | +bool is_sorted(ForwardIt first, ForwardIt last); |
| 65 | + |
| 66 | +template <class InputIt1, class InputIt2> |
| 67 | +bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2); |
| 68 | + |
| 69 | +} // namespace std |
| 70 | + |
| 71 | +bool is_even(int x); |
| 72 | +void double_ref(int& x); |
| 73 | + |
| 74 | +void test_positive() { |
| 75 | + std::vector<int> vec; |
| 76 | + int arr[5] = {1, 2, 3, 4, 5}; |
| 77 | + |
| 78 | + auto it1 = std::find(vec.begin(), vec.end(), 3); |
| 79 | + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm |
| 80 | + // CHECK-FIXES: auto it1 = llvm::find(vec, 3); |
| 81 | + |
| 82 | + auto it2 = std::find(std::begin(arr), std::end(arr), 3); |
| 83 | + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm |
| 84 | + // CHECK-FIXES: auto it2 = llvm::find(arr, 3); |
| 85 | + |
| 86 | + std::stable_sort(vec.begin(), vec.end()); |
| 87 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm |
| 88 | + // CHECK-FIXES: llvm::stable_sort(vec); |
| 89 | + |
| 90 | + bool all = std::all_of(vec.begin(), vec.end(), is_even); |
| 91 | + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm |
| 92 | + // CHECK-FIXES: bool all = llvm::all_of(vec, is_even); |
| 93 | + |
| 94 | + std::for_each(vec.begin(), vec.end(), double_ref); |
| 95 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm |
| 96 | + // CHECK-FIXES: llvm::for_each(vec, double_ref); |
| 97 | + |
| 98 | + auto min_it = std::min_element(vec.begin(), vec.end()); |
| 99 | + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use an LLVM range-based algorithm |
| 100 | + // CHECK-FIXES: auto min_it = llvm::min_element(vec); |
| 101 | + |
| 102 | + std::vector<int> vec2; |
| 103 | + bool eq = std::equal(vec.begin(), vec.end(), vec2.begin(), vec2.end()); |
| 104 | + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use an LLVM range-based algorithm |
| 105 | + // CHECK-FIXES: bool eq = llvm::equal(vec, vec2); |
| 106 | + |
| 107 | + std::copy(vec.begin(), vec.end(), vec2.begin()); |
| 108 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm |
| 109 | + // CHECK-FIXES: llvm::copy(vec, vec2.begin()); |
| 110 | + |
| 111 | + std::fill(vec.begin(), vec.end(), 0); |
| 112 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm |
| 113 | + // CHECK-FIXES: llvm::fill(vec, 0); |
| 114 | + |
| 115 | + auto last = std::unique(vec.begin(), vec.end()); |
| 116 | + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use an LLVM range-based algorithm |
| 117 | + // CHECK-FIXES: auto last = llvm::unique(vec); |
| 118 | + |
| 119 | + bool sorted = std::is_sorted(vec.begin(), vec.end()); |
| 120 | + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use an LLVM range-based algorithm |
| 121 | + // CHECK-FIXES: bool sorted = llvm::is_sorted(vec); |
| 122 | + |
| 123 | + std::includes(vec.begin(), vec.end(), std::begin(arr), std::end(arr)); |
| 124 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use an LLVM range-based algorithm |
| 125 | + // CHECK-FIXES: llvm::includes(vec, arr); |
| 126 | +} |
| 127 | + |
| 128 | +void test_negative() { |
| 129 | + std::vector<int> v; |
| 130 | + |
| 131 | + // can not use `llvm::sort` because of potential different ordering from `std::sort`. |
| 132 | + std::sort(v.begin(), v.end()); |
| 133 | + |
| 134 | + //non-begin/end iterators |
| 135 | + auto it1 = std::find(v.begin() + 1, v.end(), 2); |
| 136 | + auto it2 = std::find(v.begin(), v.end() - 1, 2); |
| 137 | + |
| 138 | + // Using different containers (3-arg equal) |
| 139 | + std::vector<int> v2; |
| 140 | + bool eq = std::equal(v.begin(), v.end(), v2.begin()); |
| 141 | +} |
0 commit comments