clang-tools 23.0.0git
PreferSingleCharOverloadsCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/CheckUtils.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/Support/raw_ostream.h"
14#include <optional>
15
16using namespace clang::ast_matchers;
17
19
20namespace {
21
22constexpr llvm::StringLiteral DeprecatedCheckName =
23 "performance-faster-string-find";
24constexpr llvm::StringLiteral CanonicalCheckName =
25 "performance-prefer-single-char-overloads";
26
27} // namespace
28
29static std::optional<std::string>
30makeCharacterLiteral(const StringLiteral *Literal) {
31 std::string Result;
32 {
33 llvm::raw_string_ostream OS(Result);
34 Literal->outputString(OS);
35 }
36 // Now replace the " with '.
37 auto OpenPos = Result.find_first_of('"');
38 if (OpenPos == std::string::npos)
39 return std::nullopt;
40 Result[OpenPos] = '\'';
41
42 auto ClosePos = Result.find_last_of('"');
43 if (ClosePos == std::string::npos)
44 return std::nullopt;
45 Result[ClosePos] = '\'';
46
47 // "'" is OK, but ''' is not, so add a backslash
48 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
49 Result.replace(OpenPos + 1, 1, "\\'");
50
51 return Result;
52}
53
55 StringRef Name, ClangTidyContext *Context)
56 : ClangTidyCheck(Name, Context),
57 StringLikeClasses(utils::options::parseStringList(
58 Options.get("StringLikeClasses",
59 "::std::basic_string;::std::basic_string_view"))) {
60 if (Name == DeprecatedCheckName)
61 utils::diagDeprecatedCheckAlias(*this, *Context, DeprecatedCheckName,
62 CanonicalCheckName);
63}
64
67 Options.store(Opts, "StringLikeClasses",
68 utils::options::serializeStringList(StringLikeClasses));
69}
70
72 const auto SingleChar =
73 ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal"));
74
75 const auto StringExpr = expr(hasType(hasUnqualifiedDesugaredType(
76 recordType(hasDeclaration(recordDecl(hasAnyName(StringLikeClasses)))))));
77
78 const auto InterestingStringFunction = hasAnyName(
79 "find", "rfind", "find_first_of", "find_first_not_of", "find_last_of",
80 "find_last_not_of", "starts_with", "ends_with", "contains", "operator+=");
81
82 Finder->addMatcher(
83 cxxMemberCallExpr(
84 callee(functionDecl(InterestingStringFunction).bind("func")),
85 anyOf(argumentCountIs(1), argumentCountIs(2)),
86 hasArgument(0, SingleChar), on(StringExpr)),
87 this);
88
89 Finder->addMatcher(cxxOperatorCallExpr(hasOperatorName("+="),
90 hasLHS(StringExpr), hasRHS(SingleChar),
91 callee(functionDecl().bind("func"))),
92 this);
93}
94
96 const MatchFinder::MatchResult &Result) {
97 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
98 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
99
100 auto Replacement = makeCharacterLiteral(Literal);
101 if (!Replacement)
102 return;
103
104 diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
105 "a single character; consider using the more "
106 "efficient overload accepting a character")
107 << FindFunc
108 << FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement);
109}
110
111} // namespace clang::tidy::performance
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
PreferSingleCharOverloadsCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static std::optional< std::string > makeCharacterLiteral(const StringLiteral *Literal)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
void diagDeprecatedCheckAlias(ClangTidyCheck &Check, const ClangTidyContext &Context, StringRef DeprecatedName, StringRef CanonicalName)
Emits a configuration diagnostic when a deprecated check alias is enabled and the canonical check nam...
Definition CheckUtils.h:10
llvm::StringMap< ClangTidyValue > OptionMap