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
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/Support/raw_ostream.h"
13#include <optional>
14
15using namespace clang::ast_matchers;
16
18
19static std::optional<std::string>
20makeCharacterLiteral(const StringLiteral *Literal) {
21 std::string Result;
22 {
23 llvm::raw_string_ostream OS(Result);
24 Literal->outputString(OS);
25 }
26 // Now replace the " with '.
27 auto OpenPos = Result.find_first_of('"');
28 if (OpenPos == std::string::npos)
29 return std::nullopt;
30 Result[OpenPos] = '\'';
31
32 auto ClosePos = Result.find_last_of('"');
33 if (ClosePos == std::string::npos)
34 return std::nullopt;
35 Result[ClosePos] = '\'';
36
37 // "'" is OK, but ''' is not, so add a backslash
38 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] == '\'')
39 Result.replace(OpenPos + 1, 1, "\\'");
40
41 return Result;
42}
43
45 StringRef Name, ClangTidyContext *Context)
46 : ClangTidyCheck(Name, Context),
47 StringLikeClasses(utils::options::parseStringList(
48 Options.get("StringLikeClasses",
49 "::std::basic_string;::std::basic_string_view"))) {}
50
53 Options.store(Opts, "StringLikeClasses",
54 utils::options::serializeStringList(StringLikeClasses));
55}
56
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
82 const MatchFinder::MatchResult &Result) {
83 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>("literal");
84 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>("func");
85
86 auto Replacement = makeCharacterLiteral(Literal);
87 if (!Replacement)
88 return;
89
90 diag(Literal->getBeginLoc(), "%0 called with a string literal consisting of "
91 "a single character; consider using the more "
92 "efficient overload accepting a character")
93 << FindFunc
94 << FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement);
95}
96
97} // 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.
llvm::StringMap< ClangTidyValue > OptionMap