clang-tools 22.0.0git
RedundantStringCStrCheck.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 "../utils/Matchers.h"
13#include "clang/Lex/Lexer.h"
14#include "clang/Tooling/FixIt.h"
15
16using namespace clang::ast_matchers;
17
19
20namespace {
21
22AST_MATCHER(MaterializeTemporaryExpr, isBoundToLValue) {
23 return Node.isBoundToLvalueReference();
24}
25
26} // end namespace
27
29 ClangTidyContext *Context)
30 : ClangTidyCheck(Name, Context),
31 StringParameterFunctions(utils::options::parseStringList(
32 Options.get("StringParameterFunctions", ""))) {
33 if (getLangOpts().CPlusPlus20)
34 StringParameterFunctions.emplace_back("::std::format");
35 if (getLangOpts().CPlusPlus23)
36 StringParameterFunctions.emplace_back("::std::print");
37}
38
40 ast_matchers::MatchFinder *Finder) {
41 // Match expressions of type 'string' or 'string*'.
42 const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
43 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
44 const auto StringExpr =
45 expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
46
47 // Match string constructor.
48 const auto StringConstructorExpr = expr(anyOf(
49 cxxConstructExpr(argumentCountIs(1),
50 hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
51 cxxConstructExpr(argumentCountIs(2),
52 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
53 // If present, the second argument is the alloc object
54 // which must not be present explicitly.
55 hasArgument(1, cxxDefaultArgExpr()))));
56
57 // Match string constructor.
58 const auto StringViewConstructorExpr = cxxConstructExpr(
59 argumentCountIs(1),
60 hasDeclaration(cxxMethodDecl(hasName("basic_string_view"))));
61
62 // Match a call to the string 'c_str()' method.
63 const auto StringCStrCallExpr =
64 cxxMemberCallExpr(on(StringExpr.bind("arg")),
65 callee(memberExpr().bind("member")),
66 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
67 .bind("call");
68 const auto HasRValueTempParent =
69 hasParent(materializeTemporaryExpr(unless(isBoundToLValue())));
70 // Detect redundant 'c_str()' calls through a string constructor.
71 // If CxxConstructExpr is the part of some CallExpr we need to
72 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.
73 Finder->addMatcher(
74 traverse(
75 TK_AsIs,
76 cxxConstructExpr(
77 anyOf(StringConstructorExpr, StringViewConstructorExpr),
78 hasArgument(0, StringCStrCallExpr),
79 unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
80 HasRValueTempParent)))))),
81 this);
82
83 // Detect: 's == str.c_str()' -> 's == str'
84 Finder->addMatcher(
85 cxxOperatorCallExpr(
86 hasAnyOverloadedOperatorName("<", ">", ">=", "<=", "!=", "==", "+"),
87 anyOf(allOf(hasArgument(0, StringExpr),
88 hasArgument(1, StringCStrCallExpr)),
89 allOf(hasArgument(0, StringCStrCallExpr),
90 hasArgument(1, StringExpr)))),
91 this);
92
93 // Detect: 'dst += str.c_str()' -> 'dst += str'
94 // Detect: 's = str.c_str()' -> 's = str'
95 Finder->addMatcher(
96 cxxOperatorCallExpr(hasAnyOverloadedOperatorName("=", "+="),
97 hasArgument(0, StringExpr),
98 hasArgument(1, StringCStrCallExpr)),
99 this);
100
101 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
102 Finder->addMatcher(
103 cxxMemberCallExpr(on(StringExpr),
104 callee(decl(cxxMethodDecl(
105 hasAnyName("append", "assign", "compare")))),
106 argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
107 this);
108
109 // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
110 Finder->addMatcher(
111 cxxMemberCallExpr(on(StringExpr),
112 callee(decl(cxxMethodDecl(hasName("compare")))),
113 argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
114 this);
115
116 // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
117 Finder->addMatcher(
118 cxxMemberCallExpr(on(StringExpr),
119 callee(decl(cxxMethodDecl(hasAnyName(
120 "find", "find_first_not_of", "find_first_of",
121 "find_last_not_of", "find_last_of", "rfind")))),
122 anyOf(argumentCountIs(1), argumentCountIs(2)),
123 hasArgument(0, StringCStrCallExpr)),
124 this);
125
126 // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
127 Finder->addMatcher(
128 cxxMemberCallExpr(on(StringExpr),
129 callee(decl(cxxMethodDecl(hasName("insert")))),
130 argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
131 this);
132
133 // Detect redundant 'c_str()' calls through a StringRef constructor.
134 Finder->addMatcher(
135 traverse(
136 TK_AsIs,
137 cxxConstructExpr(
138 // Implicit constructors of these classes are overloaded
139 // wrt. string types and they internally make a StringRef
140 // referring to the argument. Passing a string directly to
141 // them is preferred to passing a char pointer.
142 hasDeclaration(cxxMethodDecl(hasAnyName(
143 "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
144 argumentCountIs(1),
145 // The only argument must have the form x.c_str() or p->c_str()
146 // where the method is string::c_str(). StringRef also has
147 // a constructor from string which is more efficient (avoids
148 // strlen), so we can construct StringRef from the string
149 // directly.
150 hasArgument(0, StringCStrCallExpr))),
151 this);
152
153 if (!StringParameterFunctions.empty()) {
154 // Detect redundant 'c_str()' calls in parameters passed to std::format in
155 // C++20 onwards and std::print in C++23 onwards.
156 Finder->addMatcher(
157 traverse(TK_AsIs,
158 callExpr(callee(functionDecl(matchers::matchesAnyListedName(
159 StringParameterFunctions))),
160 forEachArgumentWithParam(StringCStrCallExpr,
161 parmVarDecl()))),
162 this);
163 }
164}
165
166void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
167 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
168 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
169 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
170 const bool Arrow = Member->isArrow();
171 // Replace the "call" node with the "arg" node, prefixed with '*'
172 // if the call was using '->' rather than '.'.
173 const std::string ArgText =
174 Arrow ? utils::fixit::formatDereference(*Arg, *Result.Context)
175 : tooling::fixit::getText(*Arg, *Result.Context).str();
176 if (ArgText.empty())
177 return;
178
179 diag(Call->getBeginLoc(), "redundant call to %0")
180 << Member->getMemberDecl()
181 << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
182}
183
184} // namespace clang::tidy::readability
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
RedundantStringCStrCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string formatDereference(const Expr &ExprNode, const ASTContext &Context)