clang-tools  5.0.0
RedundantStringCStrCheck.cpp
Go to the documentation of this file.
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 
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 = cxxRecordDecl(hasName("::std::basic_string"));
81  const auto StringExpr =
82  expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
83 
84  // Match string constructor.
85  const auto StringConstructorExpr = expr(anyOf(
86  cxxConstructExpr(argumentCountIs(1),
87  hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
88  cxxConstructExpr(
89  argumentCountIs(2),
90  hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
91  // If present, the second argument is the alloc object which must not
92  // be present explicitly.
93  hasArgument(1, cxxDefaultArgExpr()))));
94 
95  // Match a call to the string 'c_str()' method.
96  const auto StringCStrCallExpr =
97  cxxMemberCallExpr(on(StringExpr.bind("arg")),
98  callee(memberExpr().bind("member")),
99  callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
100  .bind("call");
101 
102  // Detect redundant 'c_str()' calls through a string constructor.
103  Finder->addMatcher(cxxConstructExpr(StringConstructorExpr,
104  hasArgument(0, StringCStrCallExpr)),
105  this);
106 
107  // Detect: 's == str.c_str()' -> 's == str'
108  Finder->addMatcher(
109  cxxOperatorCallExpr(
110  anyOf(
111  hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"),
112  hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="),
113  hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="),
114  hasOverloadedOperatorName("+")),
115  anyOf(allOf(hasArgument(0, StringExpr),
116  hasArgument(1, StringCStrCallExpr)),
117  allOf(hasArgument(0, StringCStrCallExpr),
118  hasArgument(1, StringExpr)))),
119  this);
120 
121  // Detect: 'dst += str.c_str()' -> 'dst += str'
122  // Detect: 's = str.c_str()' -> 's = str'
123  Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="),
124  hasOverloadedOperatorName("+=")),
125  hasArgument(0, StringExpr),
126  hasArgument(1, StringCStrCallExpr)),
127  this);
128 
129  // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
130  Finder->addMatcher(
131  cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
132  "append", "assign", "compare")))),
133  argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
134  this);
135 
136  // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
137  Finder->addMatcher(
138  cxxMemberCallExpr(on(StringExpr),
139  callee(decl(cxxMethodDecl(hasName("compare")))),
140  argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
141  this);
142 
143  // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
144  Finder->addMatcher(
145  cxxMemberCallExpr(on(StringExpr),
146  callee(decl(cxxMethodDecl(hasAnyName(
147  "find", "find_first_not_of", "find_first_of",
148  "find_last_not_of", "find_last_of", "rfind")))),
149  anyOf(argumentCountIs(1), argumentCountIs(2)),
150  hasArgument(0, StringCStrCallExpr)),
151  this);
152 
153  // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
154  Finder->addMatcher(
155  cxxMemberCallExpr(on(StringExpr),
156  callee(decl(cxxMethodDecl(hasName("insert")))),
157  argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
158  this);
159 
160  // Detect redundant 'c_str()' calls through a StringRef constructor.
161  Finder->addMatcher(
162  cxxConstructExpr(
163  // Implicit constructors of these classes are overloaded
164  // wrt. string types and they internally make a StringRef
165  // referring to the argument. Passing a string directly to
166  // them is preferred to passing a char pointer.
167  hasDeclaration(cxxMethodDecl(hasAnyName(
168  "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
169  argumentCountIs(1),
170  // The only argument must have the form x.c_str() or p->c_str()
171  // where the method is string::c_str(). StringRef also has
172  // a constructor from string which is more efficient (avoids
173  // strlen), so we can construct StringRef from the string
174  // directly.
175  hasArgument(0, StringCStrCallExpr)),
176  this);
177 }
178 
179 void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
180  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
181  const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
182  const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
183  bool Arrow = Member->isArrow();
184  // Replace the "call" node with the "arg" node, prefixed with '*'
185  // if the call was using '->' rather than '.'.
186  std::string ArgText =
187  Arrow ? formatDereference(Result, *Arg) : getText(Result, *Arg).str();
188  if (ArgText.empty())
189  return;
190 
191  diag(Call->getLocStart(), "redundant call to %0")
192  << Member->getMemberDecl()
193  << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
194 }
195 
196 } // namespace readability
197 } // namespace tidy
198 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:275