Files
TheRock/compiler/amd-llvm/clang-tools-extra/clang-tidy/performance/PreferSingleCharOverloadsCheck.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

112 lines
3.9 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 "PreferSingleCharOverloadsCheck.h"
#include "../utils/CheckUtils.h"
#include "../utils/OptionsUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
using namespace clang::ast_matchers;
namespace clang::tidy::performance {
namespace {
constexpr llvm::StringLiteral DeprecatedCheckName =
"performance-faster-string-find";
constexpr llvm::StringLiteral CanonicalCheckName =
"performance-prefer-single-char-overloads";
} // namespace
static std::optional<std::string>
makeCharacterLiteral(const StringLiteral *Literal) {
std::string Result;
{
llvm::raw_string_ostream OS(Result);
Literal->outputString(OS);
}
// Now replace the " with '.
auto OpenPos = Result.find_first_of('"');
if (OpenPos == std::string::npos)
return std::nullopt;
Result[OpenPos] = '\'';
auto ClosePos = Result.find_last_of('"');
if (ClosePos == std::string::npos)
return std::nullopt;
Result[ClosePos] = '\'';
// "'" is OK, but ''' is not, so add a backslash
if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
Result.replace(OpenPos + 1, 1, "\\'");
return Result;
}
PreferSingleCharOverloadsCheck::PreferSingleCharOverloadsCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
StringLikeClasses(utils::options::parseStringList(
Options.get("StringLikeClasses",
"::std::basic_string;::std::basic_string_view"))) {
if (Name == DeprecatedCheckName)
utils::diagDeprecatedCheckAlias(*this, *Context, DeprecatedCheckName,
CanonicalCheckName);
}
void PreferSingleCharOverloadsCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "StringLikeClasses",
utils::options::serializeStringList(StringLikeClasses));
}
void PreferSingleCharOverloadsCheck::registerMatchers(MatchFinder *Finder) {
const auto SingleChar =
ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal"));
const auto StringExpr = expr(hasType(hasUnqualifiedDesugaredType(
recordType(hasDeclaration(recordDecl(hasAnyName(StringLikeClasses)))))));
const auto InterestingStringFunction = hasAnyName(
"find", "rfind", "find_first_of", "find_first_not_of", "find_last_of",
"find_last_not_of", "starts_with", "ends_with", "contains", "operator+=");
Finder->addMatcher(
cxxMemberCallExpr(
callee(functionDecl(InterestingStringFunction).bind("func")),
anyOf(argumentCountIs(1), argumentCountIs(2)),
hasArgument(0, SingleChar), on(StringExpr)),
this);
Finder->addMatcher(cxxOperatorCallExpr(hasOperatorName("+="),
hasLHS(StringExpr), hasRHS(SingleChar),
callee(functionDecl().bind("func"))),
this);
}
void PreferSingleCharOverloadsCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
auto Replacement = makeCharacterLiteral(Literal);
if (!Replacement)
return;
diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
"a single character; consider using the more "
"efficient overload accepting a character")
<< FindFunc
<< FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement);
}
} // namespace clang::tidy::performance