clang-tools  8.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 #include "clang/Tooling/FixIt.h"
17 
18 using namespace clang::ast_matchers;
19 
20 namespace clang {
21 namespace tidy {
22 namespace readability {
23 
24 namespace {
25 
26 // 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.
29 bool 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.
44 std::string
45 formatDereference(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 '&'.
50  return tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(),
51  *Result.Context);
52  }
53  }
54  StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context);
55 
56  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 
65 } // end namespace
66 
67 void RedundantStringCStrCheck::registerMatchers(
68  ast_matchers::MatchFinder *Finder) {
69  // 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 
74  // Match expressions of type 'string' or 'string*'.
75  const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
76  hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
77  const auto StringExpr =
78  expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
79 
80  // Match string constructor.
81  const auto StringConstructorExpr = expr(anyOf(
82  cxxConstructExpr(argumentCountIs(1),
83  hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
84  cxxConstructExpr(
85  argumentCountIs(2),
86  hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
87  // 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.
92  const auto StringCStrCallExpr =
93  cxxMemberCallExpr(on(StringExpr.bind("arg")),
94  callee(memberExpr().bind("member")),
95  callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
96  .bind("call");
97 
98  // Detect redundant 'c_str()' calls through a string constructor.
99  Finder->addMatcher(cxxConstructExpr(StringConstructorExpr,
100  hasArgument(0, StringCStrCallExpr)),
101  this);
102 
103  // Detect: 's == str.c_str()' -> 's == str'
104  Finder->addMatcher(
105  cxxOperatorCallExpr(
106  anyOf(
107  hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"),
108  hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="),
109  hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="),
110  hasOverloadedOperatorName("+")),
111  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'
119  Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="),
120  hasOverloadedOperatorName("+=")),
121  hasArgument(0, StringExpr),
122  hasArgument(1, StringCStrCallExpr)),
123  this);
124 
125  // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
126  Finder->addMatcher(
127  cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
128  "append", "assign", "compare")))),
129  argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
130  this);
131 
132  // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
133  Finder->addMatcher(
134  cxxMemberCallExpr(on(StringExpr),
135  callee(decl(cxxMethodDecl(hasName("compare")))),
136  argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
137  this);
138 
139  // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
140  Finder->addMatcher(
141  cxxMemberCallExpr(on(StringExpr),
142  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)),
147  this);
148 
149  // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
150  Finder->addMatcher(
151  cxxMemberCallExpr(on(StringExpr),
152  callee(decl(cxxMethodDecl(hasName("insert")))),
153  argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
154  this);
155 
156  // Detect redundant 'c_str()' calls through a StringRef constructor.
157  Finder->addMatcher(
158  cxxConstructExpr(
159  // 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.
163  hasDeclaration(cxxMethodDecl(hasAnyName(
164  "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
165  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.
171  hasArgument(0, StringCStrCallExpr)),
172  this);
173 }
174 
175 void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
176  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");
179  bool Arrow = Member->isArrow();
180  // Replace the "call" node with the "arg" node, prefixed with '*'
181  // if the call was using '->' rather than '.'.
182  std::string ArgText =
183  Arrow ? formatDereference(Result, *Arg)
184  : tooling::fixit::getText(*Arg, *Result.Context).str();
185  if (ArgText.empty())
186  return;
187 
188  diag(Call->getBeginLoc(), "redundant call to %0")
189  << Member->getMemberDecl()
190  << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
191 }
192 
193 } // namespace readability
194 } // namespace tidy
195 } // namespace clang
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//