[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
nicovank wrote: Addressed feedback, renamed to `performance-use-starts-ends-with`, relaxed use of `auto`. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From fd3e09fddae97549c327cf224d66de144221fe83 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/performance/CMakeLists.txt | 1 + .../performance/PerformanceTidyModule.cpp | 3 + .../performance/UseStartsEndsWithCheck.cpp| 106 ++ .../performance/UseStartsEndsWithCheck.h | 44 clang-tools-extra/docs/ReleaseNotes.rst | 7 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../performance/use-starts-ends-with.rst | 31 + .../abseil/string-find-startswith.cpp | 2 +- .../performance/use-starts-ends-with.cpp | 77 + 11 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/use-starts-ends-with.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/use-starts-ends-with.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..09773139daa1d66 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer performance-use-starts-ends-with when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 81128ff086021ed..fc88156d8c5f395 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyPerformanceModule TypePromotionInMathFnCheck.cpp UnnecessaryCopyInitialization.cpp UnnecessaryValueParamCheck.cpp + UseStartsEndsWithCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 9e0fa6f88b36a00..3405b5514cbce44 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -28,6 +28,7 @@ #include "TypePromotionInMathFnCheck.h" #include "UnnecessaryCopyInitialization.h" #include "UnnecessaryValueParamCheck.h" +#include "UseStartsEndsWithCheck.h" namespace clang::tidy { namespace performance { @@ -70,6 +71,8 @@ class PerformanceModule : public ClangTidyModule { "performance-unnecessary-copy-initialization"); CheckFactories.registerCheck( "performance-unnecessary-value-param"); +CheckFactories.registerCheck( +"performance-use-starts-ends-with"); } }; diff --git a/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp new file mode 100644 index 000..988eb7da008fb71 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp @@ -0,0 +1,106 @@ +//===--- UseStartsEndsWithCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseStartsEndsWithCheck.h" + +#include "../utils/OptionsUtils.h" +#include
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From 5d66e404d9b6536814f004a64e1c7f0d27bd1f03 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/performance/CMakeLists.txt | 1 + .../performance/PerformanceTidyModule.cpp | 3 + .../performance/UseStartsEndsWithCheck.cpp| 107 ++ .../performance/UseStartsEndsWithCheck.h | 44 +++ clang-tools-extra/docs/ReleaseNotes.rst | 7 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../performance/use-starts-ends-with.rst | 31 + .../abseil/string-find-startswith.cpp | 2 +- .../performance/use-starts-ends-with.cpp | 77 + 11 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/use-starts-ends-with.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/use-starts-ends-with.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..09773139daa1d66 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer performance-use-starts-ends-with when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 81128ff086021ed..fc88156d8c5f395 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyPerformanceModule TypePromotionInMathFnCheck.cpp UnnecessaryCopyInitialization.cpp UnnecessaryValueParamCheck.cpp + UseStartsEndsWithCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 9e0fa6f88b36a00..3405b5514cbce44 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -28,6 +28,7 @@ #include "TypePromotionInMathFnCheck.h" #include "UnnecessaryCopyInitialization.h" #include "UnnecessaryValueParamCheck.h" +#include "UseStartsEndsWithCheck.h" namespace clang::tidy { namespace performance { @@ -70,6 +71,8 @@ class PerformanceModule : public ClangTidyModule { "performance-unnecessary-copy-initialization"); CheckFactories.registerCheck( "performance-unnecessary-value-param"); +CheckFactories.registerCheck( +"performance-use-starts-ends-with"); } }; diff --git a/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp new file mode 100644 index 000..b8e09984c8f950a --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp @@ -0,0 +1,107 @@ +//===--- UseStartsEndsWithCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseStartsEndsWithCheck.h" + +#include "../utils/OptionsUtils.h" +#include
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From 9c6123e91f6a075a6a4cc7c2aba8c76152539ae2 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/performance/CMakeLists.txt | 1 + .../performance/PerformanceTidyModule.cpp | 3 + .../performance/UseStartsEndsWithCheck.cpp| 107 ++ .../performance/UseStartsEndsWithCheck.h | 44 +++ clang-tools-extra/docs/ReleaseNotes.rst | 7 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../performance/use-starts-ends-with.rst | 30 + .../abseil/string-find-startswith.cpp | 2 +- .../performance/use-starts-ends-with.cpp | 77 + 11 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/use-starts-ends-with.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/use-starts-ends-with.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..09773139daa1d66 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer performance-use-starts-ends-with when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 81128ff086021ed..fc88156d8c5f395 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyPerformanceModule TypePromotionInMathFnCheck.cpp UnnecessaryCopyInitialization.cpp UnnecessaryValueParamCheck.cpp + UseStartsEndsWithCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 9e0fa6f88b36a00..3405b5514cbce44 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -28,6 +28,7 @@ #include "TypePromotionInMathFnCheck.h" #include "UnnecessaryCopyInitialization.h" #include "UnnecessaryValueParamCheck.h" +#include "UseStartsEndsWithCheck.h" namespace clang::tidy { namespace performance { @@ -70,6 +71,8 @@ class PerformanceModule : public ClangTidyModule { "performance-unnecessary-copy-initialization"); CheckFactories.registerCheck( "performance-unnecessary-value-param"); +CheckFactories.registerCheck( +"performance-use-starts-ends-with"); } }; diff --git a/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp new file mode 100644 index 000..b8e09984c8f950a --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp @@ -0,0 +1,107 @@ +//===--- UseStartsEndsWithCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseStartsEndsWithCheck.h" + +#include "../utils/OptionsUtils.h" +#include
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From 35a78d8fa7d16c60f7b40b3f0b53cc38c26b58b9 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/performance/CMakeLists.txt | 1 + .../performance/PerformanceTidyModule.cpp | 3 + .../performance/UseStartsEndsWithCheck.cpp| 107 ++ .../performance/UseStartsEndsWithCheck.h | 44 +++ clang-tools-extra/docs/ReleaseNotes.rst | 7 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../performance/use-starts-ends-with.rst | 6 + .../abseil/string-find-startswith.cpp | 2 +- .../performance/use-starts-ends-with.cpp | 77 + 11 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/use-starts-ends-with.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/use-starts-ends-with.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..09773139daa1d66 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer performance-use-starts-ends-with when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 81128ff086021ed..fc88156d8c5f395 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyPerformanceModule TypePromotionInMathFnCheck.cpp UnnecessaryCopyInitialization.cpp UnnecessaryValueParamCheck.cpp + UseStartsEndsWithCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 9e0fa6f88b36a00..3405b5514cbce44 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -28,6 +28,7 @@ #include "TypePromotionInMathFnCheck.h" #include "UnnecessaryCopyInitialization.h" #include "UnnecessaryValueParamCheck.h" +#include "UseStartsEndsWithCheck.h" namespace clang::tidy { namespace performance { @@ -70,6 +71,8 @@ class PerformanceModule : public ClangTidyModule { "performance-unnecessary-copy-initialization"); CheckFactories.registerCheck( "performance-unnecessary-value-param"); +CheckFactories.registerCheck( +"performance-use-starts-ends-with"); } }; diff --git a/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp new file mode 100644 index 000..b8e09984c8f950a --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/UseStartsEndsWithCheck.cpp @@ -0,0 +1,107 @@ +//===--- UseStartsEndsWithCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseStartsEndsWithCheck.h" + +#include "../utils/OptionsUtils.h" +#include
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
PiotrZSL wrote: Performance is fine, go ahead. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +const auto DefaultStringLikeClasses = +"::std::basic_string;::std::basic_string_view"; + +StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + StringLikeClasses(utils::options::parseStringList( + Options.get("StringLikeClasses", DefaultStringLikeClasses))) {} + +void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) { + const auto ZeroLiteral = integerLiteral(equals(0)); + const auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses)); + const auto StringType = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(StringClassMatcher))); + + const auto StringFind = cxxMemberCallExpr( + // .find()-call on a string... + callee(cxxMethodDecl(hasName("find")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and either "0" as second argument or the default argument (also 0). + anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr(; + + const auto StringRFind = cxxMemberCallExpr( + // .rfind()-call on a string... + callee(cxxMethodDecl(hasName("rfind")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and "0" as second argument. + hasArgument(1, ZeroLiteral)); + + Finder->addMatcher( + // Match [=!]= with a zero on one side and a string.(r?)find on the other. + binaryOperator( + hasAnyOperatorName("==", "!="), + hasOperands(ignoringParenImpCasts(ZeroLiteral), + ignoringParenImpCasts( + cxxMemberCallExpr(anyOf(StringFind, StringRFind)) + .bind("findexpr" + .bind("expr"), + this); +} + +void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { + const auto = *Result.Context; EugeneZelenko wrote: Please don't use `auto` unless type is explicitly spelled in same statement or iterator. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/EugeneZelenko edited https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/EugeneZelenko requested changes to this pull request. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
nicovank wrote: I like `XXX-use-starts-ends-with`. I would rather move it to `performance` than `readability` if not `modernize`. @PiotrZSL thoughts before I go ahead and do the renaming? Also move it somewhere else? https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
PiotrZSL wrote: > contains We already got one check for contains, its readability-container-contains, not sure how it apply for std::string, but maybe instead of calling this check modernize it could be readability, otherwise all checks would be modernize. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
PiotrZSL wrote: Maybe modernize-use-starts-ends-with https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
firewave wrote: > Any thoughts on open-ended check name instead? `modernize-string-find-affix` > (affix = prefix | suffix)? `modernize-string-startswith-endswith` is the first what popped into my head but it would not have been my first choice. Would this also be the check you would implement the suggested use of `contains()` in? https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,41 @@ +//===--- StringFindStartswithCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_STRINGFINDSTARTSWITHCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_STRINGFINDSTARTSWITHCHECK_H + +#include "../ClangTidyCheck.h" + +#include + +namespace clang::tidy::modernize { + +/// Checks whether a ``std::string::find()`` or ``std::string::rfind()`` (and +/// corresponding ``std::string_view`` methods) result is compared with 0, and +/// suggests replacing with ``starts_with()``. This is both a readability and a +/// performance issue. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/string-find-startswith.html +class StringFindStartswithCheck : public ClangTidyCheck { +public: + StringFindStartswithCheck(StringRef Name, ClangTidyContext *Context); + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult ) override; + void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +return LangOpts.CPlusPlus20; + } + nicovank wrote: Was this always recommended? Maybe it should be added to the `add_new_check` template... Along with `isLanguageVersionSupported`. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
nicovank wrote: > Detect the `str.compare("marker", 0, 6) == 0` pattern. This is actually in my notes for making this check / codemod. No performance impact so not tackling yet, maybe in the future. > Support `ends_with` in same check. Similar as above, matching for that length argument is a bit trickier than just looking for 0. Not tackling for now. Any thoughts on open-ended check name instead? `modernize-string-find-affix` (affix = prefix | suffix)? https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
firewave wrote: > would be to support also endswith in same check +1 On a side note: I will be looking into the related patterns and their performance soon as I am getting very strange code/performance when they are used outside of a benchmark - especially with Clang. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +const auto DefaultStringLikeClasses = +"::std::basic_string;::std::basic_string_view"; + +StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + StringLikeClasses(utils::options::parseStringList( + Options.get("StringLikeClasses", DefaultStringLikeClasses))) {} + +void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) { + const auto ZeroLiteral = integerLiteral(equals(0)); + const auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses)); + const auto StringType = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(StringClassMatcher))); + + const auto StringFind = cxxMemberCallExpr( + // .find()-call on a string... + callee(cxxMethodDecl(hasName("find")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and either "0" as second argument or the default argument (also 0). + anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr(; + + const auto StringRFind = cxxMemberCallExpr( + // .rfind()-call on a string... + callee(cxxMethodDecl(hasName("rfind")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and "0" as second argument. + hasArgument(1, ZeroLiteral)); + + Finder->addMatcher( + // Match [=!]= with a zero on one side and a string.(r?)find on the other. + binaryOperator( + hasAnyOperatorName("==", "!="), + hasOperands(ignoringParenImpCasts(ZeroLiteral), + ignoringParenImpCasts( + cxxMemberCallExpr(anyOf(StringFind, StringRFind)) + .bind("findexpr" + .bind("expr"), + this); +} + +void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { + const auto = *Result.Context; + const auto = Context.getSourceManager(); + + const auto *ComparisonExpr = Result.Nodes.getNodeAs("expr"); + const auto *Needle = Result.Nodes.getNodeAs("needle"); + const auto *Haystack = Result.Nodes.getNodeAs("findexpr") + ->getImplicitObjectArgument(); + const auto *FindFun = Result.Nodes.getNodeAs("findfun"); + + if (ComparisonExpr->getBeginLoc().isMacroID()) { +return; + } + + const auto Rev = FindFun->getName().contains("rfind"); + const auto Neg = ComparisonExpr->getOpcode() == BO_NE; + + const auto NeedleExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Needle->getSourceRange()), Source, + Context.getLangOpts()); + const auto HaystackExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Haystack->getSourceRange()), Source, nicovank wrote: I thought that lines 73-75 were checking and ignoring macro expansions? ``` if (ComparisonExpr->getBeginLoc().isMacroID()) { return; } ``` https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -181,6 +181,14 @@ New checks points in a coroutine. Such hostile types include scoped-lockable types and types belonging to a configurable denylist. +- New :doc:`modernize-string-find-startswith + ` check. + + Checks whether a ``std::string::find()`` or ``std::string::rfind()`` (and + corresponding ``std::string_view`` methods) result is compared with 0, and + suggests replacing with ``starts_with()``. This is both a readability and a PiotrZSL wrote: "This is both a readability and a performance issue." no need for that in release notes https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,41 @@ +//===--- StringFindStartswithCheck.h - clang-tidy ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_STRINGFINDSTARTSWITHCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_STRINGFINDSTARTSWITHCHECK_H + +#include "../ClangTidyCheck.h" + +#include + +namespace clang::tidy::modernize { + +/// Checks whether a ``std::string::find()`` or ``std::string::rfind()`` (and +/// corresponding ``std::string_view`` methods) result is compared with 0, and +/// suggests replacing with ``starts_with()``. This is both a readability and a +/// performance issue. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/string-find-startswith.html +class StringFindStartswithCheck : public ClangTidyCheck { +public: + StringFindStartswithCheck(StringRef Name, ClangTidyContext *Context); + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult ) override; + void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +return LangOpts.CPlusPlus20; + } + PiotrZSL wrote: use TK_IgnoreUnlessSpelledInSource, check other checks... https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +const auto DefaultStringLikeClasses = +"::std::basic_string;::std::basic_string_view"; + +StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + StringLikeClasses(utils::options::parseStringList( + Options.get("StringLikeClasses", DefaultStringLikeClasses))) {} + +void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) { + const auto ZeroLiteral = integerLiteral(equals(0)); + const auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses)); + const auto StringType = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(StringClassMatcher))); + + const auto StringFind = cxxMemberCallExpr( + // .find()-call on a string... + callee(cxxMethodDecl(hasName("find")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and either "0" as second argument or the default argument (also 0). + anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr(; + + const auto StringRFind = cxxMemberCallExpr( + // .rfind()-call on a string... + callee(cxxMethodDecl(hasName("rfind")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and "0" as second argument. + hasArgument(1, ZeroLiteral)); + + Finder->addMatcher( + // Match [=!]= with a zero on one side and a string.(r?)find on the other. + binaryOperator( + hasAnyOperatorName("==", "!="), + hasOperands(ignoringParenImpCasts(ZeroLiteral), + ignoringParenImpCasts( + cxxMemberCallExpr(anyOf(StringFind, StringRFind)) + .bind("findexpr" + .bind("expr"), + this); +} + +void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { + const auto = *Result.Context; + const auto = Context.getSourceManager(); + + const auto *ComparisonExpr = Result.Nodes.getNodeAs("expr"); + const auto *Needle = Result.Nodes.getNodeAs("needle"); + const auto *Haystack = Result.Nodes.getNodeAs("findexpr") + ->getImplicitObjectArgument(); + const auto *FindFun = Result.Nodes.getNodeAs("findfun"); + + if (ComparisonExpr->getBeginLoc().isMacroID()) { +return; + } + + const auto Rev = FindFun->getName().contains("rfind"); + const auto Neg = ComparisonExpr->getOpcode() == BO_NE; + + const auto NeedleExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Needle->getSourceRange()), Source, + Context.getLangOpts()); + const auto HaystackExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Haystack->getSourceRange()), Source, PiotrZSL wrote: Other workaround could be to use inserts instead of replacement https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +const auto DefaultStringLikeClasses = +"::std::basic_string;::std::basic_string_view"; + +StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + StringLikeClasses(utils::options::parseStringList( + Options.get("StringLikeClasses", DefaultStringLikeClasses))) {} + +void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) { + const auto ZeroLiteral = integerLiteral(equals(0)); + const auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses)); + const auto StringType = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(StringClassMatcher))); + + const auto StringFind = cxxMemberCallExpr( + // .find()-call on a string... + callee(cxxMethodDecl(hasName("find")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and either "0" as second argument or the default argument (also 0). + anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr(; + + const auto StringRFind = cxxMemberCallExpr( + // .rfind()-call on a string... + callee(cxxMethodDecl(hasName("rfind")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and "0" as second argument. + hasArgument(1, ZeroLiteral)); + + Finder->addMatcher( + // Match [=!]= with a zero on one side and a string.(r?)find on the other. + binaryOperator( + hasAnyOperatorName("==", "!="), + hasOperands(ignoringParenImpCasts(ZeroLiteral), + ignoringParenImpCasts( + cxxMemberCallExpr(anyOf(StringFind, StringRFind)) + .bind("findexpr" + .bind("expr"), + this); +} + +void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { + const auto = *Result.Context; + const auto = Context.getSourceManager(); + + const auto *ComparisonExpr = Result.Nodes.getNodeAs("expr"); + const auto *Needle = Result.Nodes.getNodeAs("needle"); + const auto *Haystack = Result.Nodes.getNodeAs("findexpr") + ->getImplicitObjectArgument(); + const auto *FindFun = Result.Nodes.getNodeAs("findfun"); + + if (ComparisonExpr->getBeginLoc().isMacroID()) { +return; + } + + const auto Rev = FindFun->getName().contains("rfind"); + const auto Neg = ComparisonExpr->getOpcode() == BO_NE; + + const auto NeedleExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Needle->getSourceRange()), Source, + Context.getLangOpts()); + const auto HaystackExprCode = Lexer::getSourceText( + CharSourceRange::getTokenRange(Haystack->getSourceRange()), Source, PiotrZSL wrote: Wont work with macros, you may want to get expansion location https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank edited https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
@@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +const auto DefaultStringLikeClasses = +"::std::basic_string;::std::basic_string_view"; + +StringFindStartswithCheck::StringFindStartswithCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + StringLikeClasses(utils::options::parseStringList( + Options.get("StringLikeClasses", DefaultStringLikeClasses))) {} + +void StringFindStartswithCheck::registerMatchers(MatchFinder *Finder) { + const auto ZeroLiteral = integerLiteral(equals(0)); + const auto StringClassMatcher = cxxRecordDecl(hasAnyName(StringLikeClasses)); + const auto StringType = hasUnqualifiedDesugaredType( + recordType(hasDeclaration(StringClassMatcher))); + + const auto StringFind = cxxMemberCallExpr( + // .find()-call on a string... + callee(cxxMethodDecl(hasName("find")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and either "0" as second argument or the default argument (also 0). + anyOf(hasArgument(1, ZeroLiteral), hasArgument(1, cxxDefaultArgExpr(; + + const auto StringRFind = cxxMemberCallExpr( + // .rfind()-call on a string... + callee(cxxMethodDecl(hasName("rfind")).bind("findfun")), + on(hasType(StringType)), + // ... with some search expression ... + hasArgument(0, expr().bind("needle")), + // ... and "0" as second argument. + hasArgument(1, ZeroLiteral)); + + Finder->addMatcher( + // Match [=!]= with a zero on one side and a string.(r?)find on the other. + binaryOperator( + hasAnyOperatorName("==", "!="), + hasOperands(ignoringParenImpCasts(ZeroLiteral), + ignoringParenImpCasts( + cxxMemberCallExpr(anyOf(StringFind, StringRFind)) + .bind("findexpr" + .bind("expr"), + this); +} + +void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { + const auto = *Result.Context; + const auto = Context.getSourceManager(); + + const auto *ComparisonExpr = Result.Nodes.getNodeAs("expr"); + const auto *Needle = Result.Nodes.getNodeAs("needle"); + const auto *Haystack = Result.Nodes.getNodeAs("findexpr") + ->getImplicitObjectArgument(); + const auto *FindFun = Result.Nodes.getNodeAs("findfun"); + + if (ComparisonExpr->getBeginLoc().isMacroID()) { +return; + } + + const auto Rev = FindFun->getName().contains("rfind"); PiotrZSL wrote: use diffrent binding instead, simplyone bind to findfun, other to rfindfun https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
PiotrZSL wrote: would be to support also endswith in same check, so i'm not so sure about check name. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From d31148a9d99004f1a64dc6dc1bce85a58985aa0f Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/StringFindStartswithCheck.cpp | 105 ++ .../modernize/StringFindStartswithCheck.h | 41 +++ clang-tools-extra/docs/ReleaseNotes.rst | 8 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../modernize/string-find-startswith.rst | 32 ++ .../abseil/string-find-startswith.cpp | 2 +- .../modernize/string-find-startswith.cpp | 77 + 11 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..ea569f924416dff --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,105 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier:
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
firewave wrote: I wonder if this should also detect the `str.compare("marker", 0, 6) == 0` pattern. There is possibly some kind of pattern involving `std::equal()` as well. Could as well be a different check though. Not sure if it would have a performance impact to use `starts_with()` instead though. https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From d9995b339e787374452b65fcffa8b3acf4aade3d Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/StringFindStartswithCheck.cpp | 112 ++ .../modernize/StringFindStartswithCheck.h | 41 +++ clang-tools-extra/docs/ReleaseNotes.rst | 8 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../modernize/string-find-startswith.rst | 32 + .../abseil/string-find-startswith.cpp | 2 +- .../modernize/string-find-startswith.cpp | 77 11 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..f9a7ce7479099dc --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,112 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier:
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From 2b01f9d8cb945d8332a271947492ec0fe7a6f7c0 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/StringFindStartswithCheck.cpp | 110 ++ .../modernize/StringFindStartswithCheck.h | 41 +++ clang-tools-extra/docs/ReleaseNotes.rst | 8 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../modernize/string-find-startswith.rst | 32 + .../abseil/string-find-startswith.cpp | 2 +- .../modernize/string-find-startswith.cpp | 77 11 files changed, 282 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..1d9e3ac25c785a9 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,110 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier:
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
llvmbot wrote: @llvm/pr-subscribers-clang-tidy Author: Nicolas van Kempen (nicovank) Changes Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- Full diff: https://github.com/llvm/llvm-project/pull/72385.diff 11 Files Affected: - (modified) clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h (+4-1) - (modified) clang-tools-extra/clang-tidy/modernize/CMakeLists.txt (+1) - (modified) clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp (+3) - (added) clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp (+111) - (added) clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h (+41) - (modified) clang-tools-extra/docs/ReleaseNotes.rst (+8) - (modified) clang-tools-extra/docs/clang-tidy/checks/abseil/string-find-startswith.rst (+4) - (modified) clang-tools-extra/docs/clang-tidy/checks/list.rst (+1) - (added) clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst (+32) - (modified) clang-tools-extra/test/clang-tidy/checkers/abseil/string-find-startswith.cpp (+1-1) - (added) clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp (+77) ``diff diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..74f59b9fcea1982 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,111 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "StringFindStartswithCheck.h" + +#include "../utils/OptionsUtils.h" +#include "clang/Lex/Lexer.h" + +using
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank ready_for_review https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank updated https://github.com/llvm/llvm-project/pull/72385 >From 58e1392e140f0d5b932fce683a07fb799ef6b89f Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/StringFindStartswithCheck.cpp | 111 ++ .../modernize/StringFindStartswithCheck.h | 41 +++ clang-tools-extra/docs/ReleaseNotes.rst | 8 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../modernize/string-find-startswith.rst | 32 + .../abseil/string-find-startswith.cpp | 2 +- .../modernize/string-find-startswith.cpp | 77 11 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..74f59b9fcea1982 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,111 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy ---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier:
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 2060bfcdc7eb704647c64bf925cdceb94c1f535f 5e5a2bac0f7373e6b1830fddc609e97dc61df9d4 -- clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp clang-tools-extra/test/clang-tidy/checkers/abseil/string-find-startswith.cpp `` View the diff from clang-format here. ``diff diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp index 7b992d77d0..74f59b9fce 100644 --- a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -92,7 +92,7 @@ void StringFindStartswithCheck::check(const MatchFinder::MatchResult ) { Context.getLangOpts()); const auto ReplacementCode = (Neg ? "!" : "") + HaystackExprCode + - ".starts_with(" + NeedleExprCode + ")"; + ".starts_with(" + NeedleExprCode + ")"; diag(ComparisonExpr->getBeginLoc(), "use starts_with " `` https://github.com/llvm/llvm-project/pull/72385 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add new modernize-string-find-startswith check (PR #72385)
https://github.com/nicovank created https://github.com/llvm/llvm-project/pull/72385 Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. >From 5e5a2bac0f7373e6b1830fddc609e97dc61df9d4 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Wed, 15 Nov 2023 01:13:10 -0800 Subject: [PATCH] [clang-tidy] Add new modernize-string-find-startswith check Matchers are copied over from abseil-string-find-startswith, only the error message is different and suggests `std::{string|string_view}::starts_with` instead of the Abseil equivalent. --- .../abseil/StringFindStartswithCheck.h| 5 +- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/StringFindStartswithCheck.cpp | 111 ++ .../modernize/StringFindStartswithCheck.h | 41 +++ clang-tools-extra/docs/ReleaseNotes.rst | 8 ++ .../checks/abseil/string-find-startswith.rst | 4 + .../docs/clang-tidy/checks/list.rst | 1 + .../modernize/string-find-startswith.rst | 32 + .../abseil/string-find-startswith.cpp | 2 +- .../modernize/string-find-startswith.cpp | 77 11 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/string-find-startswith.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/string-find-startswith.cpp diff --git a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h index 923b5caece5439b..5019b7000f1d062 100644 --- a/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h +++ b/clang-tools-extra/clang-tidy/abseil/StringFindStartswithCheck.h @@ -21,7 +21,6 @@ namespace clang::tidy::abseil { // Find string.find(...) == 0 comparisons and suggest replacing with StartsWith. // FIXME(niko): Add similar check for EndsWith -// FIXME(niko): Add equivalent modernize checks for C++20's std::starts_With class StringFindStartswithCheck : public ClangTidyCheck { public: using ClangTidyCheck::ClangTidyCheck; @@ -31,6 +30,10 @@ class StringFindStartswithCheck : public ClangTidyCheck { void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult ) override; void storeOptions(ClangTidyOptions::OptionMap ) override; + bool isLanguageVersionSupported(const LangOptions ) const override { +// Prefer modernize-string-find-startswith when C++20 is available. +return LangOpts.CPlusPlus && !LangOpts.CPlusPlus20; + } private: const std::vector StringLikeClasses; diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index 717c400c4790330..541f58304119856 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -25,6 +25,7 @@ add_clang_library(clangTidyModernizeModule ReplaceRandomShuffleCheck.cpp ReturnBracedInitListCheck.cpp ShrinkToFitCheck.cpp + StringFindStartswithCheck.cpp TypeTraitsCheck.cpp UnaryStaticAssertCheck.cpp UseAutoCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index 73751cf2705068d..1fcbff79ddc6f96 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -26,6 +26,7 @@ #include "ReplaceRandomShuffleCheck.h" #include "ReturnBracedInitListCheck.h" #include "ShrinkToFitCheck.h" +#include "StringFindStartswithCheck.h" #include "TypeTraitsCheck.h" #include "UnaryStaticAssertCheck.h" #include "UseAutoCheck.h" @@ -66,6 +67,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck("modernize-make-shared"); CheckFactories.registerCheck("modernize-make-unique"); CheckFactories.registerCheck("modernize-pass-by-value"); +CheckFactories.registerCheck( +"modernize-string-find-startswith"); CheckFactories.registerCheck("modernize-use-std-print"); CheckFactories.registerCheck( "modernize-raw-string-literal"); diff --git a/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp new file mode 100644 index 000..7b992d77d0e7bb5 --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/StringFindStartswithCheck.cpp @@ -0,0 +1,111 @@ +//===--- StringFindStartswithCheck.cpp - clang-tidy