1 //===- 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"
16 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace readability {
22 
23 namespace {
24 
25 template <typename T>
26 StringRef getText(const ast_matchers::MatchFinder::MatchResult &Result,
27                   T const &Node) {
28   return Lexer::getSourceText(
29       CharSourceRange::getTokenRange(Node.getSourceRange()),
30       *Result.SourceManager, Result.Context->getLangOpts());
31 }
32 
33 // Return true if expr needs to be put in parens when it is an argument of a
34 // prefix unary operator, e.g. when it is a binary or ternary operator
35 // syntactically.
36 bool needParensAfterUnaryOperator(const Expr &ExprNode) {
37   if (isa<clang::BinaryOperator>(&ExprNode) ||
38       isa<clang::ConditionalOperator>(&ExprNode)) {
39     return true;
40   }
41   if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
42     return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
43            Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
44            Op->getOperator() != OO_Subscript;
45   }
46   return false;
47 }
48 
49 // Format a pointer to an expression: prefix with '*' but simplify
50 // when it already begins with '&'.  Return empty string on failure.
51 std::string
52 formatDereference(const ast_matchers::MatchFinder::MatchResult &Result,
53                   const Expr &ExprNode) {
54   if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
55     if (Op->getOpcode() == UO_AddrOf) {
56       // Strip leading '&'.
57       return getText(Result, *Op->getSubExpr()->IgnoreParens());
58     }
59   }
60   StringRef Text = getText(Result, ExprNode);
61   if (Text.empty())
62     return std::string();
63   // Add leading '*'.
64   if (needParensAfterUnaryOperator(ExprNode)) {
65     return (llvm::Twine("*(") + Text + ")").str();
66   }
67   return (llvm::Twine("*") + Text).str();
68 }
69 
70 } // end namespace
71 
72 void RedundantStringCStrCheck::registerMatchers(
73     ast_matchers::MatchFinder *Finder) {
74   // Only register the matchers for C++; the functionality currently does not
75   // provide any benefit to other languages, despite being benign.
76   if (!getLangOpts().CPlusPlus)
77     return;
78 
79   // Match expressions of type 'string' or 'string*'.
80   const auto StringDecl =
81       cxxRecordDecl(hasName("::std::basic_string"));
82   const auto StringExpr =
83       expr(anyOf(hasType(StringDecl),
84                  hasType(qualType(pointsTo(StringDecl)))));
85 
86   // Match string constructor.
87   const auto StringConstructorExpr = expr(anyOf(
88       cxxConstructExpr(
89           argumentCountIs(1),
90           hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
91       cxxConstructExpr(
92           argumentCountIs(2),
93           hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
94           // If present, the second argument is the alloc object which must not
95           // be present explicitly.
96           hasArgument(1, cxxDefaultArgExpr()))));
97 
98   // Match a call to the string 'c_str()' method.
99   const auto StringCStrCallExpr =
100       cxxMemberCallExpr(on(StringExpr.bind("arg")),
101                         callee(memberExpr().bind("member")),
102                         callee(cxxMethodDecl(hasName("c_str"))))
103           .bind("call");
104 
105   // Detect redundant 'c_str()' calls through a string constructor.
106   Finder->addMatcher(
107       cxxConstructExpr(StringConstructorExpr,
108                        hasArgument(0, StringCStrCallExpr)),
109       this);
110 
111   // Detect: 's == str.c_str()'  ->  's == str'
112   Finder->addMatcher(
113       cxxOperatorCallExpr(
114           anyOf(hasOverloadedOperatorName("<"),
115                 hasOverloadedOperatorName(">"),
116                 hasOverloadedOperatorName(">="),
117                 hasOverloadedOperatorName("<="),
118                 hasOverloadedOperatorName("!="),
119                 hasOverloadedOperatorName("=="),
120                 hasOverloadedOperatorName("+")),
121           anyOf(allOf(hasArgument(0, StringExpr),
122                       hasArgument(1, StringCStrCallExpr)),
123                 allOf(hasArgument(0, StringCStrCallExpr),
124                       hasArgument(1, StringExpr)))),
125       this);
126 
127   // Detect: 'dst += str.c_str()'  ->  'dst += str'
128   // Detect: 's = str.c_str()'  ->  's = str'
129   Finder->addMatcher(
130       cxxOperatorCallExpr(
131           anyOf(hasOverloadedOperatorName("="),
132                 hasOverloadedOperatorName("+=")),
133           hasArgument(0, StringExpr),
134           hasArgument(1, StringCStrCallExpr)),
135       this);
136 
137   // Detect: 'dst.append(str.c_str())'  ->  'dst.append(str)'
138   Finder->addMatcher(
139       cxxMemberCallExpr(on(StringExpr),
140           callee(decl(cxxMethodDecl(
141               hasAnyName("append", "assign", "compare")))),
142           argumentCountIs(1),
143           hasArgument(0, StringCStrCallExpr)),
144       this);
145 
146   // Detect: 'dst.compare(p, n, str.c_str())'  ->  'dst.compare(p, n, str)'
147   Finder->addMatcher(
148       cxxMemberCallExpr(on(StringExpr),
149           callee(decl(cxxMethodDecl(hasName("compare")))),
150           argumentCountIs(3),
151           hasArgument(2, StringCStrCallExpr)),
152       this);
153 
154   // Detect: 'dst.find(str.c_str())'  ->  'dst.find(str)'
155   Finder->addMatcher(
156       cxxMemberCallExpr(on(StringExpr),
157           callee(decl(cxxMethodDecl(
158               hasAnyName("find", "find_first_not_of", "find_first_of",
159                          "find_last_not_of", "find_last_of", "rfind")))),
160           anyOf(argumentCountIs(1), argumentCountIs(2)),
161           hasArgument(0, StringCStrCallExpr)),
162       this);
163 
164   // Detect: 'dst.insert(pos, str.c_str())'  ->  'dst.insert(pos, str)'
165   Finder->addMatcher(
166       cxxMemberCallExpr(on(StringExpr),
167           callee(decl(cxxMethodDecl(hasName("insert")))),
168           argumentCountIs(2),
169           hasArgument(1, StringCStrCallExpr)),
170       this);
171 
172   // Detect redundant 'c_str()' calls through a StringRef constructor.
173   Finder->addMatcher(
174       cxxConstructExpr(
175           // Implicit constructors of these classes are overloaded
176           // wrt. string types and they internally make a StringRef
177           // referring to the argument.  Passing a string directly to
178           // them is preferred to passing a char pointer.
179           hasDeclaration(
180               cxxMethodDecl(hasAnyName("::llvm::StringRef::StringRef",
181                                        "::llvm::Twine::Twine"))),
182           argumentCountIs(1),
183           // The only argument must have the form x.c_str() or p->c_str()
184           // where the method is string::c_str().  StringRef also has
185           // a constructor from string which is more efficient (avoids
186           // strlen), so we can construct StringRef from the string
187           // directly.
188           hasArgument(0, StringCStrCallExpr)),
189       this);
190 }
191 
192 void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
193   const auto *Call = Result.Nodes.getStmtAs<CallExpr>("call");
194   const auto *Arg = Result.Nodes.getStmtAs<Expr>("arg");
195   bool Arrow = Result.Nodes.getStmtAs<MemberExpr>("member")->isArrow();
196   // Replace the "call" node with the "arg" node, prefixed with '*'
197   // if the call was using '->' rather than '.'.
198   std::string ArgText =
199       Arrow ? formatDereference(Result, *Arg) : getText(Result, *Arg).str();
200   if (ArgText.empty())
201     return;
202 
203   diag(Call->getLocStart(), "redundant call to `c_str()`")
204       << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
205 }
206 
207 } // namespace readability
208 } // namespace tidy
209 } // namespace clang
210