|
| 1 | +//===--- UseRangesCheck.cpp - clang-tidy ----------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "UseRangesCheck.h" |
| 10 | +#include "clang/AST/Decl.h" |
| 11 | +#include "llvm/ADT/ArrayRef.h" |
| 12 | +#include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 13 | +#include "llvm/ADT/StringRef.h" |
| 14 | + |
| 15 | +namespace clang::tidy::modernize { |
| 16 | + |
| 17 | +utils::UseRangesCheck::ReplacerMap UseRangesCheck::GetReplacerMap() const { |
| 18 | + class StdReplacer : public utils::UseRangesCheck::Replacer { |
| 19 | + public: |
| 20 | + explicit StdReplacer(ArrayRef<ArrayRef<Indexes>> Indexes) |
| 21 | + : Indexes(Indexes) {} |
| 22 | + std::string getReplaceName(const NamedDecl &OriginalName) const override { |
| 23 | + return ("std::ranges::" + OriginalName.getName()).str(); |
| 24 | + } |
| 25 | + ArrayRef<ArrayRef<Indexes>> getReplacementSignatures() const override { |
| 26 | + return Indexes; |
| 27 | + } |
| 28 | + std::optional<std::string> |
| 29 | + getHeaderInclusion(const NamedDecl &OriginalName) const override { |
| 30 | + return "<algorithm>"; |
| 31 | + } |
| 32 | + |
| 33 | + private: |
| 34 | + ArrayRef<ArrayRef<Indexes>> Indexes; |
| 35 | + }; |
| 36 | + using Indexes = UseRangesCheck::Replacer::Indexes; |
| 37 | + // using Signatures = Signature[]; |
| 38 | + utils::UseRangesCheck::ReplacerMap Result; |
| 39 | + // template<typename Iter> Func(Iter first, Iter last,...). |
| 40 | + static const Indexes SingleRangeArgs[] = {{0}}; |
| 41 | + // template<typename Policy, typename Iter> |
| 42 | + // Func(Policy policy, Iter first, // Iter last,...). |
| 43 | + static const Indexes SingleRangeExecPolicy[] = {{1}}; |
| 44 | + // template<typename Iter1, typename Iter2> |
| 45 | + // Func(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2,...). |
| 46 | + static const Indexes TwoRangeArgs[] = {{0}, {2}}; |
| 47 | + // template<typename Policy, typename Iter1, typename Iter2> |
| 48 | + // Func(Policy policy, Iter1 first1, Iter1 last1, Iter2 first2, Iter2 |
| 49 | + // last2,...). |
| 50 | + static const Indexes TwoRangeExecPolicy[] = {{1}, {3}}; |
| 51 | + |
| 52 | + static const ArrayRef<Indexes> SingleRangeFunc[] = {SingleRangeArgs}; |
| 53 | + |
| 54 | + static const ArrayRef<Indexes> SingleRangeExecFunc[] = { |
| 55 | + SingleRangeArgs, SingleRangeExecPolicy}; |
| 56 | + static const ArrayRef<Indexes> TwoRangeExecFunc[] = {TwoRangeArgs, |
| 57 | + TwoRangeExecPolicy}; |
| 58 | + static const ArrayRef<Indexes> OneOrTwoFunc[] = {SingleRangeArgs, |
| 59 | + TwoRangeArgs}; |
| 60 | + static const ArrayRef<Indexes> OneOrTwoExecFunc[] = { |
| 61 | + SingleRangeArgs, SingleRangeExecPolicy, TwoRangeArgs, TwoRangeExecPolicy}; |
| 62 | + |
| 63 | + static const std::pair<ArrayRef<ArrayRef<Indexes>>, ArrayRef<StringRef>> |
| 64 | + Names[] = { |
| 65 | + {SingleRangeFunc, |
| 66 | + {"all_of", |
| 67 | + "any_of", |
| 68 | + "none_of", |
| 69 | + "for_each", |
| 70 | + "find", |
| 71 | + "find_if", |
| 72 | + "find_if_not", |
| 73 | + "adjacent_find", |
| 74 | + "copy", |
| 75 | + "copy_if", |
| 76 | + "copy_backward", |
| 77 | + "move", |
| 78 | + "move_backward", |
| 79 | + "fill", |
| 80 | + "transform", |
| 81 | + "replace", |
| 82 | + "replace_if", |
| 83 | + "generate", |
| 84 | + "remove", |
| 85 | + "remove_if", |
| 86 | + "remove_copy", |
| 87 | + "remove_copy_if", |
| 88 | + "unique", |
| 89 | + "unique_copy", |
| 90 | + "sample", |
| 91 | + "partition_point", |
| 92 | + "lower_bound", |
| 93 | + "upper_bound", |
| 94 | + "equal_range", |
| 95 | + "binary_search", |
| 96 | + "push_heap", |
| 97 | + "pop_heap", |
| 98 | + "make_heap", |
| 99 | + "sort_heap", |
| 100 | + "next_permutation", |
| 101 | + "prev_permutation", |
| 102 | + "iota"}}, |
| 103 | + {SingleRangeExecFunc, |
| 104 | + {"reverse", |
| 105 | + "reverse_copy", |
| 106 | + "shift_left", |
| 107 | + "shift_right", |
| 108 | + "is_partitioned", |
| 109 | + "partition", |
| 110 | + "partition_copy", |
| 111 | + "stable_partition", |
| 112 | + "sort", |
| 113 | + "stable_sort", |
| 114 | + "is_sorted", |
| 115 | + "is_sorted_until", |
| 116 | + "is_heap", |
| 117 | + "is_heap_until", |
| 118 | + "max_element", |
| 119 | + "min_element", |
| 120 | + "minmax_element", |
| 121 | + "uninitialized_copy", |
| 122 | + "uninitialized_fill", |
| 123 | + "uninitialized_move", |
| 124 | + "uninitialized_default_construct", |
| 125 | + "uninitialized_value_construct", |
| 126 | + "destroy"}}, |
| 127 | + {TwoRangeExecFunc, |
| 128 | + {"partial_sort_copy", "includes", "set_union", "set_intersection", |
| 129 | + "set_difference", "set_symmetric_difference", "merge", |
| 130 | + "lexicographical_compare", "find_end", "search"}}, |
| 131 | + {OneOrTwoFunc, {"is_permutation"}}, |
| 132 | + {OneOrTwoExecFunc, {"equal", "mismatch"}}}; |
| 133 | + SmallString<64> Buff; |
| 134 | + for (const auto &[Signature, Values] : Names) { |
| 135 | + auto Replacer = llvm::makeIntrusiveRefCnt<StdReplacer>(Signature); |
| 136 | + for (const auto &Name : Values) { |
| 137 | + Buff.clear(); |
| 138 | + Result.try_emplace(("::std::" + Name).toStringRef(Buff), Replacer); |
| 139 | + } |
| 140 | + } |
| 141 | + return Result; |
| 142 | +} |
| 143 | + |
| 144 | +bool UseRangesCheck::isLanguageVersionSupported( |
| 145 | + const LangOptions &LangOpts) const { |
| 146 | + return LangOpts.CPlusPlus20; |
| 147 | +} |
| 148 | +} // namespace clang::tidy::modernize |
0 commit comments