Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/performance/StringViewConversionsCheck.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

116 lines
4.7 KiB
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
//
//===----------------------------------------------------------------------===//
#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[_view] families.
const auto IsStdString = getStringTypeMatcher("::std::basic_string");
const auto IsStdStringView = getStringTypeMatcher("::std::basic_string_view");
// Matches pointer to any character type (char*, etc.) or array of any
// character type (char[], etc.).
const auto IsCharPointerOrArray =
anyOf(hasType(pointerType(pointee(isAnyCharacter()))),
hasType(arrayType(hasElementType(isAnyCharacter()))));
const auto ImplicitlyConvertibleToStringView =
expr(anyOf(hasType(IsStdStringView), IsCharPointerOrArray,
hasType(IsStdString)))
.bind("originalStringView");
// Matches std::string construction from a string_view-convertible expression:
// - Direct construction: std::string{sv}, std::string{s}
// - Copy from existing string: std::string(s) where s is std::string
const auto RedundantStringConstruction = cxxConstructExpr(
hasType(IsStdString),
hasArgument(0, ignoringImplicit(ImplicitlyConvertibleToStringView)),
unless(hasArgument(1, unless(cxxDefaultArgExpr()))));
// Matches functional cast syntax: std::string(expr):
// std::string(sv), std::string("literal")
const auto RedundantFunctionalCast = cxxFunctionalCastExpr(
hasType(IsStdString), hasDescendant(RedundantStringConstruction));
const auto RedundantTemporaryString =
expr(anyOf(RedundantStringConstruction, RedundantFunctionalCast));
// Matches std::string(...).[c_str()|.data()]
const auto RedundantStringWithCStr =
cxxMemberCallExpr(callee(cxxMethodDecl(hasAnyName("c_str", "data"))),
on(ignoringParenImpCasts(RedundantTemporaryString)));
// Main matcher: finds cases where an expression convertible to
// std::string_view is first converted to std::string unnecessarily.
Finder->addMatcher(
cxxMemberCallExpr(
callee(memberExpr(member(cxxConversionDecl(returns(IsStdStringView))),
has(ignoringImplicit(RedundantTemporaryString.bind(
"redundantExpr"))))))
.bind("stringView"),
this);
Finder->addMatcher(
cxxConstructExpr(
argumentCountIs(1),
hasArgument(0, RedundantStringWithCStr.bind("redundantExpr")))
.bind("stringView"),
this);
}
void StringViewConversionsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *StringView = Result.Nodes.getNodeAs<Expr>("stringView");
const auto *RedundantExpr = Result.Nodes.getNodeAs<Expr>("redundantExpr");
const auto *OriginalExpr = Result.Nodes.getNodeAs<Expr>("originalStringView");
assert(StringView && RedundantExpr && OriginalExpr);
bool IsCStrPattern = false;
StringRef MethodName;
const auto *CStrCall = dyn_cast<CXXMemberCallExpr>(RedundantExpr);
if (CStrCall && CStrCall->getMethodDecl()) {
MethodName = CStrCall->getMethodDecl()->getName();
if (MethodName == "c_str" || MethodName == "data")
IsCStrPattern = true;
}
const StringRef OriginalText = Lexer::getSourceText(
CharSourceRange::getTokenRange(OriginalExpr->getSourceRange()),
*Result.SourceManager, getLangOpts());
if (OriginalText.empty())
return;
const FixItHint FixRedundantConversion = FixItHint::CreateReplacement(
RedundantExpr->getSourceRange(), OriginalText);
if (IsCStrPattern && CStrCall) {
// Handle std::string(sv).c_str() or std::string(sv).data() pattern
diag(RedundantExpr->getBeginLoc(),
"redundant conversion to %0 and calling .%1() and then back to %2")
<< CStrCall->getImplicitObjectArgument()->getType() << MethodName
<< StringView->getType() << FixRedundantConversion;
} else {
// Handle direct std::string(sv) pattern
diag(RedundantExpr->getBeginLoc(),
"redundant conversion to %0 and then back to %1")
<< RedundantExpr->getType() << StringView->getType()
<< FixRedundantConversion;
}
}
} // namespace clang::tidy::performance