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