https://github.com/irishrover updated https://github.com/llvm/llvm-project/pull/174288
>From 8c37c3fb18a852bb5f4fa30eb0ce18accaac0f1d Mon Sep 17 00:00:00 2001 From: Zinovy Nis <[email protected]> Date: Sat, 3 Jan 2026 17:48:40 +0300 Subject: [PATCH] [clang-tidy] Add a new check 'performance-string-view-conversions' Looks for redundant conversions from ``std::[w|u8|u16|u32]string_view`` to ``std::[...]string`` in call expressions expecting ``std::[...]string_view``. And fixes them. --- .../clang-tidy/performance/CMakeLists.txt | 1 + .../performance/PerformanceTidyModule.cpp | 3 + .../StringViewConversionsCheck.cpp | 145 ++++++++++++++++++ .../performance/StringViewConversionsCheck.h | 34 ++++ clang-tools-extra/docs/ReleaseNotes.rst | 9 +- .../docs/clang-tidy/checks/list.rst | 1 + .../performance/string-view-conversions.rst | 32 ++++ .../clang-tidy/checkers/Inputs/Headers/string | 24 +++ .../string-view-conversions-cxx20.cpp | 27 ++++ .../performance/string-view-conversions.cpp | 80 ++++++++++ 10 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt index 9a2f90069edbf..4dba117e1ee54 100644 --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -21,6 +21,7 @@ add_clang_library(clangTidyPerformanceModule STATIC NoexceptMoveConstructorCheck.cpp NoexceptSwapCheck.cpp PerformanceTidyModule.cpp + StringViewConversionsCheck.cpp TriviallyDestructibleCheck.cpp TypePromotionInMathFnCheck.cpp UnnecessaryCopyInitializationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp index 6bab1a46d18db..294a209e4c602 100644 --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -23,6 +23,7 @@ #include "NoexceptDestructorCheck.h" #include "NoexceptMoveConstructorCheck.h" #include "NoexceptSwapCheck.h" +#include "StringViewConversionsCheck.h" #include "TriviallyDestructibleCheck.h" #include "TypePromotionInMathFnCheck.h" #include "UnnecessaryCopyInitializationCheck.h" @@ -62,6 +63,8 @@ class PerformanceModule : public ClangTidyModule { "performance-noexcept-move-constructor"); CheckFactories.registerCheck<NoexceptSwapCheck>( "performance-noexcept-swap"); + CheckFactories.registerCheck<StringViewConversionsCheck>( + "performance-string-view-conversions"); CheckFactories.registerCheck<TriviallyDestructibleCheck>( "performance-trivially-destructible"); CheckFactories.registerCheck<TypePromotionInMathFnCheck>( diff --git a/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp new file mode 100644 index 0000000000000..d7ececa414132 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp @@ -0,0 +1,145 @@ +//===----------------------------------------------------------------------===// +// +// 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 "StringViewConversionsCheck.h" +#include "clang/AST/Expr.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::performance { + +static auto getStringTypeMatcher(StringRef CharType) { + return hasCanonicalType(hasDeclaration(cxxRecordDecl(hasName(CharType)))); +} + +void StringViewConversionsCheck::registerMatchers(MatchFinder *Finder) { + // Matchers for std::basic_[w|u8|u16|u32]string and + // std::basic_[w|u8|u16|u32]string_view families. + const auto IsStdString = getStringTypeMatcher("::std::basic_string"); + const auto IsStdStringView = getStringTypeMatcher("::std::basic_string_view"); + + // Matches pointer to any character type (char*, const char*, wchar_t*, etc.) + // or array of any character type (char[], char[N], const char[N], etc.). + const auto IsCharPointerOrArray = + anyOf(hasType(pointerType(pointee(isAnyCharacter()))), + hasType(arrayType(hasElementType(isAnyCharacter())))); + + // Matches expressions that can be implicitly converted to string_view + // without going through std::string: + // - string_view itself (no conversion needed) + // - string literals ("hello", L"wide", u8"utf8", etc.) + // - character pointers (const char*, char*) + // - character arrays (char arr[], char arr[N]) + // These are the expressions we want to preserve after removing + // the redundant std::string conversion. + const auto ImplicitlyConvertibleToStringView = + expr(anyOf(hasType(IsStdStringView), stringLiteral(), + IsCharPointerOrArray)) + .bind("originalStringView"); + + // Matches direct std::string construction from a string_view-convertible + // expression. This handles cases like: + // - Implicit construction in certain contexts + // - Brace initialization: std::string{sv} + // Excludes copy/move constructors to avoid false positives when + // an existing std::string is being copied or moved. + const auto RedundantStringConstruction = cxxConstructExpr( + hasType(IsStdString), + hasArgument(0, ignoringImplicit(ImplicitlyConvertibleToStringView)), + unless(hasDeclaration(cxxConstructorDecl(isCopyConstructor()))), + unless(hasDeclaration(cxxConstructorDecl(isMoveConstructor())))); + + // Matches functional cast syntax: std::string(expr) + // In the AST, this appears as CXXFunctionalCastExpr containing + // a CXXConstructExpr. Example: std::string(sv), std::string("literal") + const auto RedundantFunctionalCast = cxxFunctionalCastExpr( + hasType(IsStdString), hasDescendant(RedundantStringConstruction)); + + // Main matcher: finds function calls where: + // 1. A parameter has type string_view + // 2. The corresponding argument contains a redundant std::string construction + // (either functional cast syntax or direct construction/brace init) + // 3. The argument does NOT involve string concatenation with operator+ + // (since string_view doesn't support operator+, such conversions + // are necessary and not redundant) + // + // Detected patterns (will be flagged): + // void foo(std::string_view sv); + // foo(std::string(sv)); // sv -> string -> string_view + // foo(std::string{"literal"}); // literal -> string -> string_view + // foo(std::string(ptr)); // const char* -> string -> string_view + // + // Excluded patterns (will NOT be flagged): + // foo(std::string(sv) + "suffix"); // operator+ requires std::string + // foo("prefix" + std::string(sv)); // operator+ requires std::string + Finder->addMatcher( + callExpr( + forEachArgumentWithParam( + expr(hasType(IsStdStringView), + // Match either syntax for std::string construction + hasDescendant(expr(anyOf(RedundantFunctionalCast, + RedundantStringConstruction)) + .bind("redundantExpr")), + // Exclude expressions containing string concatenation, + // as the std::string is actually needed for operator+ + unless(hasDescendant(cxxOperatorCallExpr( + hasOverloadedOperatorName("+"), hasType(IsStdString))))) + .bind("expr"), + parmVarDecl(hasType(IsStdStringView)))) + .bind("call"), + this); +} + +void StringViewConversionsCheck::check(const MatchFinder::MatchResult &Result) { + // Get the full argument expression passed to the function. + // This has type string_view after implicit conversions. + const auto *ParamExpr = Result.Nodes.getNodeAs<Expr>("expr"); + if (!ParamExpr) + return; + + // Get the redundant std::string construction expression. + // This is either CXXFunctionalCastExpr for std::string(x) syntax + // or CXXTemporaryObjectExpr for std::string{x} syntax. + const auto *RedundantExpr = Result.Nodes.getNodeAs<Expr>("redundantExpr"); + if (!RedundantExpr) + return; + + // Get the original expression that was passed to std::string constructor. + // This is what we want to use as the replacement. + const auto *OriginalExpr = Result.Nodes.getNodeAs<Expr>("originalStringView"); + if (!OriginalExpr) + return; + + // Sanity check. Verify that the redundant expression is the direct source of + // the argument, not part of a larger expression (e.g., std::string(sv) + + // "bar"). If source ranges don't match, there's something between the string + // construction and the function argument, so we shouldn't transform. + assert(ParamExpr->getSourceRange() == RedundantExpr->getSourceRange()); + + // Extract the source text of the original expression to use as replacement. + // For example, if the code is std::string(sv), we extract "sv". + const StringRef OriginalText = Lexer::getSourceText( + CharSourceRange::getTokenRange(OriginalExpr->getSourceRange()), + *Result.SourceManager, getLangOpts()); + + // Skip if we couldn't extract the source text (e.g., macro expansion issues). + if (OriginalText.empty()) + return; + + diag(RedundantExpr->getBeginLoc(), + "redundant conversion to %0 and then back to %1") + << RedundantExpr->getType().getAsString() + << ParamExpr->getType().getAsString() + << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(RedundantExpr->getSourceRange()), + OriginalText); +} + +} // namespace clang::tidy::performance diff --git a/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h new file mode 100644 index 0000000000000..b2b013d5086b7 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.h @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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_PERFORMANCE_STRINGVIEWCONVERSIONSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_STRINGVIEWCONVERSIONSCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::performance { + +/// Looks for redundant conversions from std::string_view to std::string in +/// call expressions expecting std::string_view. And fixes them. +/// +/// For the user-facing documentation see: +/// https://clang.llvm.org/extra/clang-tidy/checks/performance/string-view-conversions.html +class StringViewConversionsCheck : public ClangTidyCheck { +public: + StringViewConversionsCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus17; + } +}; + +} // namespace clang::tidy::performance + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_STRINGVIEWCONVERSIONSCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index c7ec5ae66499b..816763d85249e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -265,6 +265,13 @@ New checks Finds virtual function overrides with different visibility than the function in the base class. +- New :doc:`performance-string-view-conversions + <clang-tidy/checks/performance/string-view-conversions>` check. + + Looks for redundant conversions from ``std::[w|u8|u16|u32]string_view`` to + ``std::[...]string`` in call expressions expecting ``std::[...]string_view``. + And fixes them. + - New :doc:`readability-inconsistent-ifelse-braces <clang-tidy/checks/readability/inconsistent-ifelse-braces>` check. @@ -364,7 +371,7 @@ New check aliases keeping initial check as an alias to the new one. - Renamed :doc:`google-build-namespaces <clang-tidy/checks/google/build-namespaces>` to - :doc:`misc-anonymous-namespace-in-header + :doc:`misc-anonymous-namespace-in-header <clang-tidy/checks/misc/anonymous-namespace-in-header>` keeping initial check as an alias to the new one. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index e0de2b9c2dada..d79566485b59f 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -362,6 +362,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-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" :doc:`performance-unnecessary-copy-initialization <performance/unnecessary-copy-initialization>`, "Yes" diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst new file mode 100644 index 0000000000000..347c3a7597689 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/string-view-conversions.rst @@ -0,0 +1,32 @@ +.. title:: clang-tidy - performance-string-view-conversions + +performance-string-view-conversions +=================================== + +Finds and removes redundant conversions from ``std::[w|u8|u16|u32]string_view`` +to ``std::[...]string`` in call expressions expecting ``std::[...]string_view``. + + +Before: + +.. code-block:: c++ + + void foo(int p1, std::string_view p2, double p3); + void bar(std::string_view sv) { + foo(42, std::string(sv), 3.14); // conversion to std::string is + // redundant as std::string_view + // is expected + foo(42, std::string("foo"), 3.14); // conversion to std::string is + // redundant as std::string_view + // is expected + } + +After: + +.. code-block:: c++ + + void foo(int p1, std::string_view p2, double p3); + void bar(std::string_view sv) { + foo(42, sv, 3.14); + foo(42, "foo", 3.14); + } diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string index 6cedda4202f14..c3fd2cf6c1ff7 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string @@ -22,9 +22,11 @@ struct basic_string { typedef size_t size_type; typedef basic_string<C, T, A> _Type; basic_string(); + basic_string(basic_string_view<C, T>); basic_string(const C *p, const A &a = A()); basic_string(const C *p, size_type count); basic_string(const C *b, const C *e); + basic_string(size_t, C); ~basic_string(); @@ -90,8 +92,14 @@ struct basic_string { typedef basic_string<char> string; typedef basic_string<wchar_t> wstring; +#if __cplusplus >= 202002L +typedef basic_string<char8_t> u8string; +typedef basic_string<char16_t> u16string; +typedef basic_string<char32_t> u32string; +#else typedef basic_string<char16> u16string; typedef basic_string<char32> u32string; +#endif template <typename C, typename T> struct basic_string_view { @@ -100,6 +108,7 @@ struct basic_string_view { const C *str; constexpr basic_string_view(const C* s) : str(s) {} + basic_string_view(const basic_string<C, T>&) {} const C *data() const; @@ -136,8 +145,14 @@ struct basic_string_view { typedef basic_string_view<char> string_view; typedef basic_string_view<wchar_t> wstring_view; +#if __cplusplus >= 202002L +typedef basic_string_view<char8_t> u8string_view; +typedef basic_string_view<char16_t> u16string_view; +typedef basic_string_view<char32_t> u32string_view; +#else typedef basic_string_view<char16> u16string_view; typedef basic_string_view<char32> u32string_view; +#endif std::string operator+(const std::string&, const std::string&); std::string operator+(const std::string&, const char*); @@ -166,6 +181,15 @@ bool operator!=(const char*, const std::string_view&); #endif size_t strlen(const char* str); + +namespace literals { +namespace string_literals { + string operator""s(const char *, size_t); +} +namespace string_view_literals { + string_view operator""sv(const char *, size_t); +} +} } #endif // _STRING_ diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp new file mode 100644 index 0000000000000..3ebc60231074e --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions-cxx20.cpp @@ -0,0 +1,27 @@ +// RUN: %check_clang_tidy -std=c++20 %s performance-string-view-conversions %t -- \ +// RUN: -- -isystem %clang_tidy_headers + +#include <string> + +using namespace std::literals::string_literals; +using namespace std::literals::string_view_literals; + +void foo_u8sv(int p1, std::u8string_view p2, double p3); +void foo_u16sv(int p1, std::u16string_view p2, double p3); +void foo_u32sv(int p1, std::u32string_view p2, double p3); + +void positive(std::string_view sv, std::wstring_view wsv) { + // [u8|u16|32]string([u8|u16|32]string_view) + // + foo_u8sv(42, std::u8string(u8"Hello, world"), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant conversion to std::u8string and then back to std::u8string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_u8sv(42, u8"Hello, world", 3.14); + + foo_u16sv(42, std::u16string(u"Hello, world"), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant conversion to std::u16string and then back to std::u16string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_u16sv(42, u"Hello, world", 3.14); + + foo_u32sv(42, std::u32string(U"Hello, world"), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: redundant conversion to std::u32string and then back to std::u32string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_u32sv(42, U"Hello, world", 3.14); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp new file mode 100644 index 0000000000000..c5304cc3a4090 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/string-view-conversions.cpp @@ -0,0 +1,80 @@ +// RUN: %check_clang_tidy -std=c++17 %s performance-string-view-conversions %t -- \ +// RUN: -- -isystem %clang_tidy_headers + +#include <string> + +using namespace std::literals::string_literals; +using namespace std::literals::string_view_literals; + +void foo_sv(int p1, std::string_view p2, double p3); +void foo_wsv(int p1, std::wstring_view p2, double p3); +void foo_str(int p1, const std::string& p2, double p3); +void foo_wstr(int p1, const std::wstring& p2, double p3); +std::string foo_str(int p1); +std::string_view foo_sv(int p1); + +void positive(std::string_view sv, std::wstring_view wsv) { + // string(string_view) + // + foo_sv(42, std::string(sv), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to std::string and then back to std::string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_sv(42, sv, 3.14); + + foo_sv(42, std::string("Hello, world"), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to std::string and then back to std::string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_sv(42, "Hello, world", 3.14); + + // TODO: support for ""sv literals + foo_sv(42, "Hello, world"s, 3.14); + + foo_sv(42, std::string{"Hello, world"}, 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to std::string and then back to std::string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_sv(42, "Hello, world", 3.14); + + const char *ptr = "Hello, world"; + foo_sv(42, std::string(ptr), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to std::string and then back to std::string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_sv(42, ptr, 3.14); + + char arr[] = "Hello, world"; + foo_sv(42, std::string(arr), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant conversion to std::string and then back to std::string_view [performance-string-view-conversions] + // CHECK-FIXES: foo_sv(42, arr, 3.14); + + // wstring(wstring_view) + // + foo_wsv(42, std::wstring(wsv), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant conversion to std::wstring and then back to std::wstring_view [performance-string-view-conversions] + // CHECK-FIXES: foo_wsv(42, wsv, 3.14); + + const wchar_t *wptr = L"Hello, world"; + foo_wsv(42, std::wstring(wptr), 3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant conversion to std::wstring and then back to std::wstring_view [performance-string-view-conversions] + // CHECK-FIXES: foo_wsv(42, wptr, 3.14); +} + +void negative(std::string_view sv, std::wstring_view wsv) { + // No warnings expected: already string_view + foo_sv(42, sv, 3.14); + foo_sv(42, "Hello, world", 3.14); + // No warnings expected: complex expression + foo_sv(42, std::string(sv) + "bar", 3.14); + foo_sv(42, + std::string( sv ) + + ("foo" "bar") , + 3.14); + foo_sv(42, "foo" + std::string(sv), 3.14); + foo_sv(42, "foo" + std::string(sv) + "bar", 3.14); + foo_sv(42, std::string(sv) + std::string(sv), 3.14); + foo_sv(42, std::string("foo") + std::string("bar"), 3.14); + foo_sv(42, std::string(5, 'a'), 3.14); + + // No warnings expected: string parameter, not string-view + foo_str(42, std::string(sv), 3.14); + foo_str(42, std::string("Hello, world"), 3.14); + foo_wstr(42, std::wstring(wsv), 3.14); + foo_wstr(42, std::wstring(L"Hello, world"), 3.14); + + foo_sv(42, foo_str(42), 3.14); + foo_sv(42, foo_sv(42), 3.14); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
