https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/186946
>From e2bf54187e64db0316cbe6f226f85c9533350b27 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Tue, 17 Mar 2026 11:29:49 +0800 Subject: [PATCH 1/3] [clang-tidy] Rename performance-faster-string-find to performance-faster-string-operation --- .../clang-tidy/performance/CMakeLists.txt | 2 +- ...eck.cpp => FasterStringOperationCheck.cpp} | 13 +++---- ...ndCheck.h => FasterStringOperationCheck.h} | 12 +++---- .../performance/PerformanceTidyModule.cpp | 6 ++-- clang-tools-extra/docs/ReleaseNotes.rst | 16 +++++++-- .../docs/clang-tidy/checks/list.rst | 1 + .../checks/performance/faster-string-find.rst | 30 +++------------- .../performance/faster-string-operation.rst | 35 +++++++++++++++++++ ...g-find.cpp => faster-string-operation.cpp} | 8 ++--- 9 files changed, 77 insertions(+), 46 deletions(-) rename clang-tools-extra/clang-tidy/performance/{FasterStringFindCheck.cpp => FasterStringOperationCheck.cpp} (89%) rename clang-tools-extra/clang-tidy/performance/{FasterStringFindCheck.h => FasterStringOperationCheck.h} (87%) create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst rename clang-tools-extra/test/clang-tidy/checkers/performance/{faster-string-find.cpp => faster-string-operation.cpp} (95%) diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 5b3e849fb20a8..1c9c7bab2b46a 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -6,7 +6,7 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyPerformanceModule STATIC AvoidEndlCheck.cpp EnumSizeCheck.cpp - FasterStringFindCheck.cpp + FasterStringOperationCheck.cpp ForRangeCopyCheck.cpp ImplicitConversionInLoopCheck.cpp InefficientAlgorithmCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp similarity index 89% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp rename to clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp index 1d9325166e341..0b94eb09ee0bc 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "FasterStringFindCheck.h" +#include "FasterStringOperationCheck.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -42,19 +42,20 @@ makeCharacterLiteral(const StringLiteral *Literal) { return Result; } -FasterStringFindCheck::FasterStringFindCheck(StringRef Name, - ClangTidyContext *Context) +FasterStringOperationCheck::FasterStringOperationCheck( + StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), StringLikeClasses(utils::options::parseStringList( Options.get("StringLikeClasses", "::std::basic_string;::std::basic_string_view"))) {} -void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { +void FasterStringOperationCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StringLikeClasses", utils::options::serializeStringList(StringLikeClasses)); } -void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { +void FasterStringOperationCheck::registerMatchers(MatchFinder *Finder) { const auto SingleChar = ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")); @@ -78,7 +79,7 @@ void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) { this); } -void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) { +void FasterStringOperationCheck::check(const MatchFinder::MatchResult &Result) { const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal"); const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h similarity index 87% rename from clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h rename to clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h index f5d36d805498e..4569bdf00690d 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringFindCheck.h +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H #include "../ClangTidyCheck.h" @@ -21,10 +21,10 @@ namespace clang::tidy::performance { /// The character literal overload is more efficient. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-find.html -class FasterStringFindCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-operation.html +class FasterStringOperationCheck : public ClangTidyCheck { public: - FasterStringFindCheck(StringRef Name, ClangTidyContext *Context); + FasterStringOperationCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; } @@ -41,4 +41,4 @@ class FasterStringFindCheck : public ClangTidyCheck { } // namespace clang::tidy::performance -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGFINDCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 21274fae43795..0deab21a8ea3a 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -10,7 +10,7 @@ #include "../ClangTidyModule.h" #include "AvoidEndlCheck.h" #include "EnumSizeCheck.h" -#include "FasterStringFindCheck.h" +#include "FasterStringOperationCheck.h" #include "ForRangeCopyCheck.h" #include "ImplicitConversionInLoopCheck.h" #include "InefficientAlgorithmCheck.h" @@ -39,8 +39,10 @@ class PerformanceModule : public ClangTidyModule { void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<AvoidEndlCheck>("performance-avoid-endl"); CheckFactories.registerCheck<EnumSizeCheck>("performance-enum-size"); - CheckFactories.registerCheck<FasterStringFindCheck>( + CheckFactories.registerCheck<FasterStringOperationCheck>( "performance-faster-string-find"); + CheckFactories.registerCheck<FasterStringOperationCheck>( + "performance-faster-string-operation"); CheckFactories.registerCheck<ForRangeCopyCheck>( "performance-for-range-copy"); CheckFactories.registerCheck<ImplicitConversionInLoopCheck>( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 077335c16c331..9e79981fe888a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -49,6 +49,12 @@ Major New Features Potentially Breaking Changes ---------------------------- +- Deprecated the :program:`clang-tidy` check :doc:`performance-faster-string-find + <clang-tidy/checks/performance/faster-string-find>`. It has been renamed to + :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>`. + The original check will be removed in the 25th release. + Improvements to clangd ---------------------- @@ -174,6 +180,12 @@ New check aliases <clang-tidy/checks/portability/no-assembler>`. The `hicpp-no-assembler` name is kept as an alias. +- Renamed :doc:`performance-faster-string-find + <clang-tidy/checks/performance/faster-string-find>` to + :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>`. + The `performance-faster-string-find` name is kept as an alias. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -292,8 +304,8 @@ Changes in existing checks - Improved the ignore list to correctly handle ``typedef`` and ``enum``. -- Improved :doc:`performance-faster-string-find - <clang-tidy/checks/performance/faster-string-find>` check: +- Improved :doc:`performance-faster-string-operation + <clang-tidy/checks/performance/faster-string-operation>` check: - Now analyzes calls to the ``starts_with``, ``ends_with``, ``contains``, and ``operator+=`` string member functions. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 362455796bd50..0854bb5676d6e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -351,6 +351,7 @@ Clang-Tidy Checks :doc:`performance-avoid-endl <performance/avoid-endl>`, "Yes" :doc:`performance-enum-size <performance/enum-size>`, :doc:`performance-faster-string-find <performance/faster-string-find>`, "Yes" + :doc:`performance-faster-string-operation <performance/faster-string-operation>`, "Yes" :doc:`performance-for-range-copy <performance/for-range-copy>`, "Yes" :doc:`performance-implicit-conversion-in-loop <performance/implicit-conversion-in-loop>`, :doc:`performance-inefficient-algorithm <performance/inefficient-algorithm>`, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst index e7ed869acc8ad..3dc2ff04d53e6 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst @@ -1,30 +1,10 @@ .. title:: clang-tidy - performance-faster-string-find +.. meta:: + :http-equiv=refresh: 5;URL=faster-string-operation.html performance-faster-string-find ============================== -Optimize calls to ``std::string::find()`` and friends when the needle passed is -a single character string literal. The character literal overload is more -efficient. - -Examples: - -.. code-block:: c++ - - str.find("A"); - - // becomes - - str.find('A'); - -Options -------- - -.. option:: StringLikeClasses - - Semicolon-separated list of names of string-like classes. By default only - ``::std::basic_string`` and ``::std::basic_string_view`` are considered. - Within these classes, the check will only consider member functions named - ``find``, ``rfind``, ``find_first_of``, ``find_first_not_of``, - ``find_last_of``, ``find_last_not_of``, ``starts_with``, ``ends_with``, - ``contains``, or ``operator+=``. +The `performance-faster-string-find check` is an alias, please see +:doc:`performance-faster-string-operation <faster-string-operation>` +for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst new file mode 100644 index 0000000000000..25907b2f5e6d3 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst @@ -0,0 +1,35 @@ +.. title:: clang-tidy - performance-faster-string-operation + +performance-faster-string-operation +=================================== + +Optimize calls to ``std::string::find()`` and friends when the needle passed is +a single character string literal. The character literal overload is more +efficient. + +Examples: + +.. code-block:: c++ + + str.find("A"); + str += "B"; + + // becomes + + str.find('A'); + str += 'B'; + +This check flags passing strings of size 1 to miscellaneous member functions +as well as ``operator+=``. + +Options +------- + +.. option:: StringLikeClasses + + Semicolon-separated list of names of string-like classes. By default only + ``::std::basic_string`` and ``::std::basic_string_view`` are considered. + Within these classes, the check will only consider member functions named + ``find``, ``rfind``, ``find_first_of``, ``find_first_not_of``, + ``find_last_of``, ``find_last_not_of``, ``starts_with``, ``ends_with``, + ``contains``, or ``operator+=``. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp similarity index 95% rename from clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp rename to clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp index 3999049a10718..1c093afa41ef0 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-find.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp @@ -1,7 +1,7 @@ -// RUN: %check_clang_tidy %s performance-faster-string-find %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-find %t -- \ +// RUN: %check_clang_tidy %s performance-faster-string-operation %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-operation %t -- \ // RUN: -config="{CheckOptions: \ -// RUN: {performance-faster-string-find.StringLikeClasses: \ +// RUN: {performance-faster-string-operation.StringLikeClasses: \ // RUN: '::llvm::StringRef;'}}" -- -fno-delayed-template-parsing #include <string> @@ -23,7 +23,7 @@ void StringFind() { std::string Str; Str.find("a"); - // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more effective overload accepting a character [performance-faster-string-find] + // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more effective overload accepting a character [performance-faster-string-operation] // CHECK-FIXES: Str.find('a'); // Works with the pos argument. >From 7f59a56fa2718f46fb091397d3ce8cbd39c0e7f9 Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Mon, 23 Mar 2026 13:48:13 +0800 Subject: [PATCH 2/3] fix header --- .../clang-tidy/performance/FasterStringOperationCheck.cpp | 1 - .../clang-tidy/performance/FasterStringOperationCheck.h | 1 - 2 files changed, 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp index def8964e40aeb..d64d4ed5c5aac 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp @@ -8,7 +8,6 @@ #include "FasterStringOperationCheck.h" #include "../utils/OptionsUtils.h" -#include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/Support/raw_ostream.h" #include <optional> diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h index 4569bdf00690d..55413171263a4 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h +++ b/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h @@ -11,7 +11,6 @@ #include "../ClangTidyCheck.h" -#include <string> #include <vector> namespace clang::tidy::performance { >From 3862da8f9e65fdd530d86886f4f4eb60ba235d0e Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Sat, 28 Mar 2026 23:09:20 +0800 Subject: [PATCH 3/3] fixup --- .../clang-tidy/performance/CMakeLists.txt | 2 +- .../performance/PerformanceTidyModule.cpp | 8 ++++---- ....cpp => PreferSingleCharOverloadsCheck.cpp} | 18 +++++++++++++----- ...heck.h => PreferSingleCharOverloadsCheck.h} | 12 ++++++------ clang-tools-extra/docs/ReleaseNotes.rst | 6 +++--- .../docs/clang-tidy/checks/list.rst | 2 +- .../checks/performance/faster-string-find.rst | 4 ++-- ...on.rst => prefer-single-char-overloads.rst} | 6 +++--- .../prefer-single-char-overloads-alias.cpp | 11 +++++++++++ ...on.cpp => prefer-single-char-overloads.cpp} | 8 ++++---- 10 files changed, 48 insertions(+), 29 deletions(-) rename clang-tools-extra/clang-tidy/performance/{FasterStringOperationCheck.cpp => PreferSingleCharOverloadsCheck.cpp} (83%) rename clang-tools-extra/clang-tidy/performance/{FasterStringOperationCheck.h => PreferSingleCharOverloadsCheck.h} (71%) rename clang-tools-extra/docs/clang-tidy/checks/performance/{faster-string-operation.rst => prefer-single-char-overloads.rst} (85%) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp rename clang-tools-extra/test/clang-tidy/checkers/performance/{faster-string-operation.cpp => prefer-single-char-overloads.cpp} (94%) diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 1c9c7bab2b46a..0c778b5a9f36b 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyPerformanceModule STATIC AvoidEndlCheck.cpp EnumSizeCheck.cpp - FasterStringOperationCheck.cpp ForRangeCopyCheck.cpp ImplicitConversionInLoopCheck.cpp InefficientAlgorithmCheck.cpp @@ -21,6 +20,7 @@ add_clang_library(clangTidyPerformanceModule STATIC NoexceptMoveConstructorCheck.cpp NoexceptSwapCheck.cpp PerformanceTidyModule.cpp + PreferSingleCharOverloadsCheck.cpp StringViewConversionsCheck.cpp TriviallyDestructibleCheck.cpp TypePromotionInMathFnCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 0deab21a8ea3a..a4c1cdacab496 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -10,7 +10,6 @@ #include "../ClangTidyModule.h" #include "AvoidEndlCheck.h" #include "EnumSizeCheck.h" -#include "FasterStringOperationCheck.h" #include "ForRangeCopyCheck.h" #include "ImplicitConversionInLoopCheck.h" #include "InefficientAlgorithmCheck.h" @@ -23,6 +22,7 @@ #include "NoexceptDestructorCheck.h" #include "NoexceptMoveConstructorCheck.h" #include "NoexceptSwapCheck.h" +#include "PreferSingleCharOverloadsCheck.h" #include "StringViewConversionsCheck.h" #include "TriviallyDestructibleCheck.h" #include "TypePromotionInMathFnCheck.h" @@ -39,10 +39,8 @@ class PerformanceModule : public ClangTidyModule { void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { CheckFactories.registerCheck<AvoidEndlCheck>("performance-avoid-endl"); CheckFactories.registerCheck<EnumSizeCheck>("performance-enum-size"); - CheckFactories.registerCheck<FasterStringOperationCheck>( + CheckFactories.registerCheck<PreferSingleCharOverloadsCheck>( "performance-faster-string-find"); - CheckFactories.registerCheck<FasterStringOperationCheck>( - "performance-faster-string-operation"); CheckFactories.registerCheck<ForRangeCopyCheck>( "performance-for-range-copy"); CheckFactories.registerCheck<ImplicitConversionInLoopCheck>( @@ -66,6 +64,8 @@ class PerformanceModule : public ClangTidyModule { "performance-noexcept-move-constructor"); CheckFactories.registerCheck<NoexceptSwapCheck>( "performance-noexcept-swap"); + CheckFactories.registerCheck<PreferSingleCharOverloadsCheck>( + "performance-prefer-single-char-overloads"); CheckFactories.registerCheck<StringViewConversionsCheck>( "performance-string-view-conversions"); CheckFactories.registerCheck<TriviallyDestructibleCheck>( diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp similarity index 83% rename from clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp rename to clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp index d64d4ed5c5aac..df4f29ad26308 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "FasterStringOperationCheck.h" +#include "PreferSingleCharOverloadsCheck.h" #include "../utils/OptionsUtils.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/Support/raw_ostream.h" @@ -41,20 +41,20 @@ makeCharacterLiteral(const StringLiteral *Literal) { return Result; } -FasterStringOperationCheck::FasterStringOperationCheck( +PreferSingleCharOverloadsCheck::PreferSingleCharOverloadsCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), StringLikeClasses(utils::options::parseStringList( Options.get("StringLikeClasses", "::std::basic_string;::std::basic_string_view"))) {} -void FasterStringOperationCheck::storeOptions( +void PreferSingleCharOverloadsCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "StringLikeClasses", utils::options::serializeStringList(StringLikeClasses)); } -void FasterStringOperationCheck::registerMatchers(MatchFinder *Finder) { +void PreferSingleCharOverloadsCheck::registerMatchers(MatchFinder *Finder) { const auto SingleChar = ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")); @@ -78,7 +78,8 @@ void FasterStringOperationCheck::registerMatchers(MatchFinder *Finder) { this); } -void FasterStringOperationCheck::check(const MatchFinder::MatchResult &Result) { +void PreferSingleCharOverloadsCheck::check( + const MatchFinder::MatchResult &Result) { const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal"); const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); @@ -91,6 +92,13 @@ void FasterStringOperationCheck::check(const MatchFinder::MatchResult &Result) { "efficient overload accepting a character") << FindFunc << FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement); + + if (getID() == "performance-faster-string-find") + diag(Literal->getBeginLoc(), + "performance-faster-string-find is deprecated and will be removed in " + "future release, consider using " + "performance-prefer-single-char-overloads", + DiagnosticIDs::Note); } } // namespace clang::tidy::performance diff --git a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h similarity index 71% rename from clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h rename to clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h index 55413171263a4..848ad0be9724c 100644 --- a/clang-tools-extra/clang-tidy/performance/FasterStringOperationCheck.h +++ b/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.h @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_PREFERSINGLECHAROVERLOADSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_PREFERSINGLECHAROVERLOADSCHECK_H #include "../ClangTidyCheck.h" @@ -20,10 +20,10 @@ namespace clang::tidy::performance { /// The character literal overload is more efficient. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/performance/faster-string-operation.html -class FasterStringOperationCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/performance/prefer-single-char-overloads.html +class PreferSingleCharOverloadsCheck : public ClangTidyCheck { public: - FasterStringOperationCheck(StringRef Name, ClangTidyContext *Context); + PreferSingleCharOverloadsCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; } @@ -40,4 +40,4 @@ class FasterStringOperationCheck : public ClangTidyCheck { } // namespace clang::tidy::performance -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_FASTERSTRINGOPERATIONCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_PREFERSINGLECHAROVERLOADSCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 887ad1f6e16fb..c2b751293aeba 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -51,7 +51,7 @@ Potentially Breaking Changes - Deprecated the :program:`clang-tidy` check :doc:`performance-faster-string-find <clang-tidy/checks/performance/faster-string-find>`. It has been renamed to - :doc:`performance-faster-string-operation + :doc:`performance-prefer-single-char-overloads <clang-tidy/checks/performance/faster-string-operation>`. The original check will be removed in the 25th release. @@ -197,7 +197,7 @@ New check aliases - Renamed :doc:`performance-faster-string-find <clang-tidy/checks/performance/faster-string-find>` to - :doc:`performance-faster-string-operation + :doc:`performance-prefer-single-char-overloads <clang-tidy/checks/performance/faster-string-operation>`. The `performance-faster-string-find` name is kept as an alias. @@ -341,7 +341,7 @@ Changes in existing checks - Improved the ignore list to correctly handle ``typedef`` and ``enum``. -- Improved :doc:`performance-faster-string-operation +- Improved :doc:`performance-prefer-single-char-overloads <clang-tidy/checks/performance/faster-string-operation>` check: - Now analyzes calls to the ``starts_with``, ``ends_with``, ``contains``, diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 1dd1c1c49717a..2b5be931271ec 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -352,7 +352,6 @@ Clang-Tidy Checks :doc:`performance-avoid-endl <performance/avoid-endl>`, "Yes" :doc:`performance-enum-size <performance/enum-size>`, :doc:`performance-faster-string-find <performance/faster-string-find>`, "Yes" - :doc:`performance-faster-string-operation <performance/faster-string-operation>`, "Yes" :doc:`performance-for-range-copy <performance/for-range-copy>`, "Yes" :doc:`performance-implicit-conversion-in-loop <performance/implicit-conversion-in-loop>`, :doc:`performance-inefficient-algorithm <performance/inefficient-algorithm>`, "Yes" @@ -365,6 +364,7 @@ Clang-Tidy Checks :doc:`performance-noexcept-destructor <performance/noexcept-destructor>`, "Yes" :doc:`performance-noexcept-move-constructor <performance/noexcept-move-constructor>`, "Yes" :doc:`performance-noexcept-swap <performance/noexcept-swap>`, "Yes" + :doc:`performance-prefer-single-char-overloads <performance/prefer-single-char-overloads>`, "Yes" :doc:`performance-string-view-conversions <performance/string-view-conversions>`, "Yes" :doc:`performance-trivially-destructible <performance/trivially-destructible>`, "Yes" :doc:`performance-type-promotion-in-math-fn <performance/type-promotion-in-math-fn>`, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst index 3dc2ff04d53e6..39788bf4623e7 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-find.rst @@ -1,10 +1,10 @@ .. title:: clang-tidy - performance-faster-string-find .. meta:: - :http-equiv=refresh: 5;URL=faster-string-operation.html + :http-equiv=refresh: 5;URL=prefer-single-char-overloads.html performance-faster-string-find ============================== The `performance-faster-string-find check` is an alias, please see -:doc:`performance-faster-string-operation <faster-string-operation>` +:doc:`performance-prefer-single-char-overloads <prefer-single-char-overloads>` for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst similarity index 85% rename from clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst rename to clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst index 25907b2f5e6d3..3e6a9d979497e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/faster-string-operation.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/prefer-single-char-overloads.rst @@ -1,7 +1,7 @@ -.. title:: clang-tidy - performance-faster-string-operation +.. title:: clang-tidy - performance-prefer-single-char-overloads -performance-faster-string-operation -=================================== +performance-prefer-single-char-overloads +======================================== Optimize calls to ``std::string::find()`` and friends when the needle passed is a single character string literal. The character literal overload is more diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp new file mode 100644 index 0000000000000..50077d8fbd0a9 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads-alias.cpp @@ -0,0 +1,11 @@ +// RUN: %check_clang_tidy %s performance-faster-string-find %t + +#include <string> + +void StringFind() { + std::string Str; + Str.find("a"); + // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-find] + // CHECK-MESSAGES: [[@LINE-2]]:12: note: performance-faster-string-find is deprecated and will be removed in future release, consider using performance-prefer-single-char-overloads + // CHECK-FIXES: Str.find('a'); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.cpp similarity index 94% rename from clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp rename to clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.cpp index 1ecf76e9d075a..46bbf3c20f8fd 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/faster-string-operation.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/prefer-single-char-overloads.cpp @@ -1,7 +1,7 @@ -// RUN: %check_clang_tidy %s performance-faster-string-operation %t -- -- -fno-delayed-template-parsing -// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-faster-string-operation %t -- \ +// RUN: %check_clang_tidy %s performance-prefer-single-char-overloads %t -- -- -fno-delayed-template-parsing +// RUN: %check_clang_tidy -check-suffix=CUSTOM %s performance-prefer-single-char-overloads %t -- \ // RUN: -config="{CheckOptions: \ -// RUN: {performance-faster-string-operation.StringLikeClasses: \ +// RUN: {performance-prefer-single-char-overloads.StringLikeClasses: \ // RUN: '::llvm::StringRef;'}}" -- -fno-delayed-template-parsing #include <string> @@ -23,7 +23,7 @@ void StringFind() { std::string Str; Str.find("a"); - // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-faster-string-operation] + // CHECK-MESSAGES: [[@LINE-1]]:12: warning: 'find' called with a string literal consisting of a single character; consider using the more efficient overload accepting a character [performance-prefer-single-char-overloads] // CHECK-FIXES: Str.find('a'); // Works with the pos argument. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
