aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp
diff options
context:
space:
mode:
authorthegeorg <thegeorg@yandex-team.com>2024-03-13 13:58:24 +0300
committerthegeorg <thegeorg@yandex-team.com>2024-03-13 14:11:53 +0300
commit11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch)
treefabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp
parent9685917341315774aad5733b1793b1e533a88bbb (diff)
downloadydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp')
-rw-r--r--contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp b/contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp
new file mode 100644
index 0000000000..d932b5fb93
--- /dev/null
+++ b/contrib/libs/clang16/tools/extra/clang-tidy/performance/FasterStringFindCheck.cpp
@@ -0,0 +1,96 @@
+//===--- FasterStringFindCheck.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 "FasterStringFindCheck.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/Support/raw_ostream.h"
+#include <optional>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+namespace {
+
+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 Pos = Result.find_first_of('"');
+ if (Pos == Result.npos)
+ return std::nullopt;
+ Result[Pos] = '\'';
+ Pos = Result.find_last_of('"');
+ if (Pos == Result.npos)
+ return std::nullopt;
+ Result[Pos] = '\'';
+ return Result;
+}
+
+AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<Expr>,
+ hasSubstitutedType) {
+ return hasType(qualType(anyOf(substTemplateTypeParmType(),
+ hasDescendant(substTemplateTypeParmType()))));
+}
+
+} // namespace
+
+FasterStringFindCheck::FasterStringFindCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StringLikeClasses(utils::options::parseStringList(
+ Options.get("StringLikeClasses",
+ "::std::basic_string;::std::basic_string_view"))) {}
+
+void FasterStringFindCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "StringLikeClasses",
+ utils::options::serializeStringList(StringLikeClasses));
+}
+
+void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {
+ const auto SingleChar =
+ expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal")));
+ const auto StringFindFunctions =
+ hasAnyName("find", "rfind", "find_first_of", "find_first_not_of",
+ "find_last_of", "find_last_not_of");
+
+ Finder->addMatcher(
+ cxxMemberCallExpr(
+ callee(functionDecl(StringFindFunctions).bind("func")),
+ anyOf(argumentCountIs(1), argumentCountIs(2)),
+ hasArgument(0, SingleChar),
+ on(expr(hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+ recordDecl(hasAnyName(StringLikeClasses)))))),
+ unless(hasSubstitutedType())))),
+ this);
+}
+
+void FasterStringFindCheck::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 "
+ "effective overload accepting a character")
+ << FindFunc
+ << FixItHint::CreateReplacement(
+ CharSourceRange::getTokenRange(Literal->getBeginLoc(),
+ Literal->getEndLoc()),
+ *Replacement);
+}
+
+} // namespace clang::tidy::performance