clang-tools 23.0.0git
FasterStringFindCheck.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
11#include "clang/AST/ASTContext.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
20static std::optional<std::string>
21makeCharacterLiteral(const StringLiteral *Literal) {
22 std::string Result;
23 {
24 llvm::raw_string_ostream OS(Result);
25 Literal->outputString(OS);
26 }
27 // Now replace the " with '.
28 auto OpenPos = Result.find_first_of('"');
29 if (OpenPos == std::string::npos)
30 return std::nullopt;
31 Result[OpenPos] = '\'';
32
33 auto ClosePos = Result.find_last_of('"');
34 if (ClosePos == std::string::npos)
35 return std::nullopt;
36 Result[ClosePos] = '\'';
37
38 // "'" is OK, but ''' is not, so add a backslash
39 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
40 Result.replace(OpenPos + 1, 1, "\\'");
41
42 return Result;
43}
44
46 ClangTidyContext *Context)
47 : ClangTidyCheck(Name, Context),
48 StringLikeClasses(utils::options::parseStringList(
49 Options.get("StringLikeClasses",
50 "::std::basic_string;::std::basic_string_view"))) {}
51
53 Options.store(Opts, "StringLikeClasses",
54 utils::options::serializeStringList(StringLikeClasses));
55}
56
57void FasterStringFindCheck::registerMatchers(MatchFinder *Finder) {
58 const auto SingleChar =
59 ignoringParenCasts(stringLiteral(hasSize(1)).bind("literal"));
60
61 const auto StringExpr = expr(hasType(hasUnqualifiedDesugaredType(
62 recordType(hasDeclaration(recordDecl(hasAnyName(StringLikeClasses)))))));
63
64 const auto InterestingStringFunction = hasAnyName(
65 "find", "rfind", "find_first_of", "find_first_not_of", "find_last_of",
66 "find_last_not_of", "starts_with", "ends_with", "contains", "operator+=");
67
68 Finder->addMatcher(
69 cxxMemberCallExpr(
70 callee(functionDecl(InterestingStringFunction).bind("func")),
71 anyOf(argumentCountIs(1), argumentCountIs(2)),
72 hasArgument(0, SingleChar), on(StringExpr)),
73 this);
74
75 Finder->addMatcher(cxxOperatorCallExpr(hasOperatorName("+="),
76 hasLHS(StringExpr), hasRHS(SingleChar),
77 callee(functionDecl().bind("func"))),
78 this);
79}
80
81void FasterStringFindCheck::check(const MatchFinder::MatchResult &Result) {
82 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
83 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
84
85 auto Replacement = makeCharacterLiteral(Literal);
86 if (!Replacement)
87 return;
88
89 diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
90 "a single character; consider using the more "
91 "effective overload accepting a character")
92 << FindFunc
93 << FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement);
94}
95
96} // namespace clang::tidy::performance
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
FasterStringFindCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) 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.
llvm::StringMap< ClangTidyValue > OptionMap