blob: 903a0ed1d43176dbdff444ffafad8ad5683da7cf [file] [log] [blame]
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +00001//===- RedundantStringCStrCheck.cpp - Check for redundant c_str calls -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a check for redundant calls of c_str() on strings.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RedundantStringCStrCheck.h"
15#include "clang/Lex/Lexer.h"
Haojian Wu75f6cad2018-12-07 11:25:37 +000016#include "clang/Tooling/FixIt.h"
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000017
Etienne Bergeron456177b2016-05-02 18:00:29 +000018using namespace clang::ast_matchers;
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000019
Etienne Bergeron456177b2016-05-02 18:00:29 +000020namespace clang {
21namespace tidy {
22namespace readability {
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000023
24namespace {
25
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000026// Return true if expr needs to be put in parens when it is an argument of a
27// prefix unary operator, e.g. when it is a binary or ternary operator
28// syntactically.
29bool needParensAfterUnaryOperator(const Expr &ExprNode) {
30 if (isa<clang::BinaryOperator>(&ExprNode) ||
31 isa<clang::ConditionalOperator>(&ExprNode)) {
32 return true;
33 }
34 if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
35 return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
36 Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
37 Op->getOperator() != OO_Subscript;
38 }
39 return false;
40}
41
42// Format a pointer to an expression: prefix with '*' but simplify
43// when it already begins with '&'. Return empty string on failure.
44std::string
45formatDereference(const ast_matchers::MatchFinder::MatchResult &Result,
46 const Expr &ExprNode) {
47 if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
48 if (Op->getOpcode() == UO_AddrOf) {
49 // Strip leading '&'.
Haojian Wu75f6cad2018-12-07 11:25:37 +000050 return tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(),
51 *Result.Context);
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000052 }
53 }
Haojian Wu75f6cad2018-12-07 11:25:37 +000054 StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context);
55
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000056 if (Text.empty())
57 return std::string();
58 // Add leading '*'.
59 if (needParensAfterUnaryOperator(ExprNode)) {
60 return (llvm::Twine("*(") + Text + ")").str();
61 }
62 return (llvm::Twine("*") + Text).str();
63}
64
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000065} // end namespace
66
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000067void RedundantStringCStrCheck::registerMatchers(
68 ast_matchers::MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000069 // Only register the matchers for C++; the functionality currently does not
70 // provide any benefit to other languages, despite being benign.
71 if (!getLangOpts().CPlusPlus)
72 return;
73
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000074 // Match expressions of type 'string' or 'string*'.
Manuel Klimek7b9c1172017-08-02 13:13:11 +000075 const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
76 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000077 const auto StringExpr =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000078 expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000079
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +000080 // Match string constructor.
81 const auto StringConstructorExpr = expr(anyOf(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000082 cxxConstructExpr(argumentCountIs(1),
83 hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +000084 cxxConstructExpr(
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +000085 argumentCountIs(2),
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000086 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +000087 // If present, the second argument is the alloc object which must not
88 // be present explicitly.
89 hasArgument(1, cxxDefaultArgExpr()))));
90
91 // Match a call to the string 'c_str()' method.
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000092 const auto StringCStrCallExpr =
93 cxxMemberCallExpr(on(StringExpr.bind("arg")),
94 callee(memberExpr().bind("member")),
Malcolm Parsons8b70e262016-11-03 12:56:48 +000095 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
Etienne Bergeroncb7ce982016-03-24 19:42:36 +000096 .bind("call");
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +000097
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +000098 // Detect redundant 'c_str()' calls through a string constructor.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000099 Finder->addMatcher(cxxConstructExpr(StringConstructorExpr,
100 hasArgument(0, StringCStrCallExpr)),
101 this);
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000102
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000103 // Detect: 's == str.c_str()' -> 's == str'
104 Finder->addMatcher(
105 cxxOperatorCallExpr(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000106 anyOf(
107 hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"),
108 hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="),
109 hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="),
110 hasOverloadedOperatorName("+")),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000111 anyOf(allOf(hasArgument(0, StringExpr),
112 hasArgument(1, StringCStrCallExpr)),
113 allOf(hasArgument(0, StringCStrCallExpr),
114 hasArgument(1, StringExpr)))),
115 this);
116
117 // Detect: 'dst += str.c_str()' -> 'dst += str'
118 // Detect: 's = str.c_str()' -> 's = str'
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000119 Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="),
120 hasOverloadedOperatorName("+=")),
121 hasArgument(0, StringExpr),
122 hasArgument(1, StringCStrCallExpr)),
123 this);
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000124
125 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
126 Finder->addMatcher(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000127 cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
128 "append", "assign", "compare")))),
129 argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000130 this);
131
132 // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
133 Finder->addMatcher(
134 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000135 callee(decl(cxxMethodDecl(hasName("compare")))),
136 argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000137 this);
138
139 // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
140 Finder->addMatcher(
141 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000142 callee(decl(cxxMethodDecl(hasAnyName(
143 "find", "find_first_not_of", "find_first_of",
144 "find_last_not_of", "find_last_of", "rfind")))),
145 anyOf(argumentCountIs(1), argumentCountIs(2)),
146 hasArgument(0, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000147 this);
148
149 // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
150 Finder->addMatcher(
151 cxxMemberCallExpr(on(StringExpr),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000152 callee(decl(cxxMethodDecl(hasName("insert")))),
153 argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
Etienne Bergeron9cfd8ce2016-04-15 18:12:06 +0000154 this);
155
156 // Detect redundant 'c_str()' calls through a StringRef constructor.
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000157 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000158 cxxConstructExpr(
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000159 // Implicit constructors of these classes are overloaded
160 // wrt. string types and they internally make a StringRef
161 // referring to the argument. Passing a string directly to
162 // them is preferred to passing a char pointer.
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000163 hasDeclaration(cxxMethodDecl(hasAnyName(
164 "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000165 argumentCountIs(1),
166 // The only argument must have the form x.c_str() or p->c_str()
167 // where the method is string::c_str(). StringRef also has
168 // a constructor from string which is more efficient (avoids
169 // strlen), so we can construct StringRef from the string
170 // directly.
Etienne Bergeron4c3b55c2016-03-22 18:00:13 +0000171 hasArgument(0, StringCStrCallExpr)),
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000172 this);
173}
174
175void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko9f58fe02016-12-13 16:19:19 +0000176 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
177 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
178 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
Malcolm Parsons8b70e262016-11-03 12:56:48 +0000179 bool Arrow = Member->isArrow();
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000180 // Replace the "call" node with the "arg" node, prefixed with '*'
181 // if the call was using '->' rather than '.'.
182 std::string ArgText =
Haojian Wu75f6cad2018-12-07 11:25:37 +0000183 Arrow ? formatDereference(Result, *Arg)
184 : tooling::fixit::getText(*Arg, *Result.Context).str();
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000185 if (ArgText.empty())
186 return;
187
Stephen Kelly43465bf2018-08-09 22:42:26 +0000188 diag(Call->getBeginLoc(), "redundant call to %0")
Malcolm Parsons8b70e262016-11-03 12:56:48 +0000189 << Member->getMemberDecl()
Alexander Kornienko57a5c6b2015-03-16 00:32:25 +0000190 << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
191}
192
193} // namespace readability
194} // namespace tidy
195} // namespace clang