|
| 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 | + |
| 11 | +namespace clang::tidy::llvm_check { |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +class StdToLLVMReplacer : public utils::UseRangesCheck::Replacer { |
| 16 | +public: |
| 17 | + explicit StdToLLVMReplacer( |
| 18 | + ArrayRef<utils::UseRangesCheck::Signature> Signatures) |
| 19 | + : Signatures(Signatures) {} |
| 20 | + |
| 21 | + ArrayRef<utils::UseRangesCheck::Signature> |
| 22 | + getReplacementSignatures() const override { |
| 23 | + return Signatures; |
| 24 | + } |
| 25 | + |
| 26 | + std::optional<std::string> |
| 27 | + getReplaceName(const NamedDecl &OriginalName) const override { |
| 28 | + return ("llvm::" + OriginalName.getName()).str(); |
| 29 | + } |
| 30 | + |
| 31 | + std::optional<std::string> |
| 32 | + getHeaderInclusion(const NamedDecl &) const override { |
| 33 | + return "llvm/ADT/STLExtras.h"; |
| 34 | + } |
| 35 | + |
| 36 | +private: |
| 37 | + ArrayRef<utils::UseRangesCheck::Signature> Signatures; |
| 38 | +}; |
| 39 | + |
| 40 | +} // namespace |
| 41 | + |
| 42 | +utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const { |
| 43 | + ReplacerMap Results; |
| 44 | + |
| 45 | + static const Signature SingleSig = {{0}}; |
| 46 | + static const Signature TwoSig = {{0}, {2}}; |
| 47 | + |
| 48 | + const auto AddStdToLLVM = |
| 49 | + [&Results](llvm::IntrusiveRefCntPtr<Replacer> Replacer, |
| 50 | + std::initializer_list<StringRef> Names) { |
| 51 | + for (const auto &Name : Names) { |
| 52 | + Results.try_emplace(("::std::" + Name).str(), Replacer); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + // Single range algorithms |
| 57 | + AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig), |
| 58 | + {"all_of", "any_of", |
| 59 | + "none_of", "for_each", |
| 60 | + "find", "find_if", |
| 61 | + "find_if_not", "fill", |
| 62 | + "count", "count_if", |
| 63 | + "copy", "copy_if", |
| 64 | + "transform", "replace", |
| 65 | + "remove_if", "stable_sort", |
| 66 | + "partition", "partition_point", |
| 67 | + "is_sorted", "min_element", |
| 68 | + "max_element", "binary_search", |
| 69 | + "lower_bound", "upper_bound", |
| 70 | + "unique", "uninitialized_copy"}); |
| 71 | + |
| 72 | + // Two range algorithms |
| 73 | + AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig), |
| 74 | + {"equal", "mismatch", "includes"}); |
| 75 | + |
| 76 | + return Results; |
| 77 | +} |
| 78 | + |
| 79 | +UseRangesCheck::UseRangesCheck(StringRef Name, ClangTidyContext *Context) |
| 80 | + : utils::UseRangesCheck(Name, Context) {} |
| 81 | + |
| 82 | +DiagnosticBuilder UseRangesCheck::createDiag(const CallExpr &Call) { |
| 83 | + return diag(Call.getBeginLoc(), "use an LLVM range-based algorithm"); |
| 84 | +} |
| 85 | + |
| 86 | +ArrayRef<std::pair<StringRef, StringRef>> |
| 87 | +UseRangesCheck::getFreeBeginEndMethods() const { |
| 88 | + static constexpr std::pair<StringRef, StringRef> Refs[] = { |
| 89 | + {"::std::begin", "::std::end"}, |
| 90 | + {"::std::cbegin", "::std::cend"}, |
| 91 | + {"::std::rbegin", "::std::rend"}, |
| 92 | + {"::std::crbegin", "::std::crend"}, |
| 93 | + }; |
| 94 | + return Refs; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace clang::tidy::llvm_check |
0 commit comments