blob: 2938e2b9c7cd069f731c1be0ba0386e7ee03d060 [file] [log] [blame]
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +00001//===- RedundantStringCStrCheck.cpp - Check for redundant c_str calls -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a check for redundant calls of c_str() on strings.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RedundantStringCStrCheck.h"
14#include "clang/Lex/Lexer.h"
Haojian Wu75f6cad2018-12-07 11:25:37 +000015#include "clang/Tooling/FixIt.h"
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000016
Etienne Bergeron456177b2016-05-02 18:00:29 +000017using namespace clang::ast_matchers;
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000018
Etienne Bergeron456177b2016-05-02 18:00:29 +000019namespace clang {
20namespace tidy {
21namespace readability {
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000022
23namespace {
24
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000025// Return true if expr needs to be put in parens when it is an argument of a
26// prefix unary operator, e.g. when it is a binary or ternary operator
27// syntactically.
28bool needParensAfterUnaryOperator(const Expr &ExprNode) {
29 if (isa<clang::BinaryOperator>(&ExprNode) ||
30 isa<clang::ConditionalOperator>(&ExprNode)) {
31 return true;
32 }
33 if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
34 return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
35 Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
36 Op->getOperator() != OO_Subscript;
37 }
38 return false;
39}
40
41// Format a pointer to an expression: prefix with '*' but simplify
42// when it already begins with '&'. Return empty string on failure.
43std::string
44formatDereference(const ast_matchers::MatchFinder::MatchResult &Result,
45 const Expr &ExprNode) {
46 if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
47 if (Op->getOpcode() == UO_AddrOf) {
48 // Strip leading '&'.
Benjamin Krameradcd0262020-01-28 20:23:46 +010049 return std::string(tooling::fixit::getText(
50 *Op->getSubExpr()->IgnoreParens(), *Result.Context));
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000051 }
52 }
Haojian Wu75f6cad2018-12-07 11:25:37 +000053 StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context);
54
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000055 if (Text.empty())
56 return std::string();
57 // Add leading '*'.
58 if (needParensAfterUnaryOperator(ExprNode)) {
59 return (llvm::Twine("*(") + Text + ")").str();
60 }
61 return (llvm::Twine("*") + Text).str();
62}
63
Karasev Nikita47282b12020-02-18 15:32:03 -050064// Trying to get CallExpr in which CxxConstructExpr is called.
65static const clang::CallExpr *
66tryGetCallExprAncestorForCxxConstructExpr(const Expr *TheExpr,
67 ASTContext &Context) {
68 // We skip nodes such as CXXBindTemporaryExpr, MaterializeTemporaryExpr.
69 for (ast_type_traits::DynTypedNode DynParent : Context.getParents(*TheExpr)) {
70 if (const auto *Parent = DynParent.get<Expr>()) {
71 if (const auto *TheCallExpr = dyn_cast<CallExpr>(Parent))
72 return TheCallExpr;
73
74 if (const clang::CallExpr *TheCallExpr =
75 tryGetCallExprAncestorForCxxConstructExpr(Parent, Context))
76 return TheCallExpr;
77 }
78 }
79
80 return nullptr;
81}
82
83// Check that ParamDecl of CallExprDecl has rvalue type.
84static bool checkParamDeclOfAncestorCallExprHasRValueRefType(
85 const Expr *TheCxxConstructExpr, ASTContext &Context) {
86 if (const clang::CallExpr *TheCallExpr =
87 tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr,
88 Context)) {
Eric Christopher28728bf2020-02-18 17:49:22 -080089 for (unsigned i = 0; i < TheCallExpr->getNumArgs(); ++i) {
Karasev Nikita47282b12020-02-18 15:32:03 -050090 const Expr *Arg = TheCallExpr->getArg(i);
91 if (Arg->getSourceRange() == TheCxxConstructExpr->getSourceRange()) {
92 if (const auto *TheCallExprFuncProto =
93 TheCallExpr->getCallee()
94 ->getType()
95 ->getPointeeType()
96 ->getAs<FunctionProtoType>()) {
97 if (TheCallExprFuncProto->getParamType(i)->isRValueReferenceType())
98 return true;
99 }
100 }
101 }
102 }
103
104 return false;
105}
106
107AST_MATCHER(CXXConstructExpr,
108 matchedParamDeclOfAncestorCallExprHasRValueRefType) {
109 return checkParamDeclOfAncestorCallExprHasRValueRefType(
110 &Node, Finder->getASTContext());
111}
112
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000113} // end namespace
114
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000115void RedundantStringCStrCheck::registerMatchers(
116 ast_matchers::MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +0000117 // Only register the matchers for C++; the functionality currently does not
118 // provide any benefit to other languages, despite being benign.
119 if (!getLangOpts().CPlusPlus)
120 return;
121
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000122 // Match expressions of type 'string' or 'string*'.
Manuel Klimek7b9c1172017-08-02 13:13:11 +0000123 const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
124 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000125 const auto StringExpr =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000126 expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000127
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000128 // Match string constructor.
129 const auto StringConstructorExpr = expr(anyOf(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000130 cxxConstructExpr(argumentCountIs(1),
131 hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000132 cxxConstructExpr(
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000133 argumentCountIs(2),
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000134 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000135 // If present, the second argument is the alloc object which must not
136 // be present explicitly.
137 hasArgument(1, cxxDefaultArgExpr()))));
138
139 // Match a call to the string 'c_str()' method.
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000140 const auto StringCStrCallExpr =
141 cxxMemberCallExpr(on(StringExpr.bind("arg")),
142 callee(memberExpr().bind("member")),
Malcolm Parsons8b70e262016-11-03 12:56:48 +0000143 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
Etienne Bergeroncb7ce982016-03-24 19:42:36 +0000144 .bind("call");
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000145
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000146 // Detect redundant 'c_str()' calls through a string constructor.
Karasev Nikita47282b12020-02-18 15:32:03 -0500147 // If CxxConstructExpr is the part of some CallExpr we need to
148 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.
149 Finder->addMatcher(
150 cxxConstructExpr(
151 StringConstructorExpr, hasArgument(0, StringCStrCallExpr),
152 unless(matchedParamDeclOfAncestorCallExprHasRValueRefType())),
153 this);
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000154
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000155 // Detect: 's == str.c_str()' -> 's == str'
156 Finder->addMatcher(
157 cxxOperatorCallExpr(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000158 anyOf(
159 hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"),
160 hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="),
161 hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="),
162 hasOverloadedOperatorName("+")),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000163 anyOf(allOf(hasArgument(0, StringExpr),
164 hasArgument(1, StringCStrCallExpr)),
165 allOf(hasArgument(0, StringCStrCallExpr),
166 hasArgument(1, StringExpr)))),
167 this);
168
169 // Detect: 'dst += str.c_str()' -> 'dst += str'
170 // Detect: 's = str.c_str()' -> 's = str'
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000171 Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="),
172 hasOverloadedOperatorName("+=")),
173 hasArgument(0, StringExpr),
174 hasArgument(1, StringCStrCallExpr)),
175 this);
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000176
177 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
178 Finder->addMatcher(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000179 cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
180 "append", "assign", "compare")))),
181 argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000182 this);
183
184 // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
185 Finder->addMatcher(
186 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000187 callee(decl(cxxMethodDecl(hasName("compare")))),
188 argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000189 this);
190
191 // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
192 Finder->addMatcher(
193 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000194 callee(decl(cxxMethodDecl(hasAnyName(
195 "find", "find_first_not_of", "find_first_of",
196 "find_last_not_of", "find_last_of", "rfind")))),
197 anyOf(argumentCountIs(1), argumentCountIs(2)),
198 hasArgument(0, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000199 this);
200
201 // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
202 Finder->addMatcher(
203 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000204 callee(decl(cxxMethodDecl(hasName("insert")))),
205 argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000206 this);
207
208 // Detect redundant 'c_str()' calls through a StringRef constructor.
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000209 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000210 cxxConstructExpr(
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000211 // Implicit constructors of these classes are overloaded
212 // wrt. string types and they internally make a StringRef
213 // referring to the argument. Passing a string directly to
214 // them is preferred to passing a char pointer.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000215 hasDeclaration(cxxMethodDecl(hasAnyName(
216 "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000217 argumentCountIs(1),
218 // The only argument must have the form x.c_str() or p->c_str()
219 // where the method is string::c_str(). StringRef also has
220 // a constructor from string which is more efficient (avoids
221 // strlen), so we can construct StringRef from the string
222 // directly.
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000223 hasArgument(0, StringCStrCallExpr)),
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000224 this);
225}
226
227void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko9f58fe02016-12-13 16:19:19 +0000228 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
229 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
230 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
Malcolm Parsons8b70e262016-11-03 12:56:48 +0000231 bool Arrow = Member->isArrow();
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000232 // Replace the "call" node with the "arg" node, prefixed with '*'
233 // if the call was using '->' rather than '.'.
234 std::string ArgText =
Haojian Wu75f6cad2018-12-07 11:25:37 +0000235 Arrow ? formatDereference(Result, *Arg)
236 : tooling::fixit::getText(*Arg, *Result.Context).str();
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000237 if (ArgText.empty())
238 return;
239
Stephen Kelly43465bf2018-08-09 22:42:26 +0000240 diag(Call->getBeginLoc(), "redundant call to %0")
Malcolm Parsons8b70e262016-11-03 12:56:48 +0000241 << Member->getMemberDecl()
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000242 << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
243}
244
245} // namespace readability
246} // namespace tidy
247} // namespace clang