annotate clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- RedundantStringCStrCheck.cpp - Check for redundant c_str calls -----===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements a check for redundant calls of c_str() on strings.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "RedundantStringCStrCheck.h"
anatofuz
parents:
diff changeset
14 #include "clang/Lex/Lexer.h"
anatofuz
parents:
diff changeset
15 #include "clang/Tooling/FixIt.h"
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 namespace clang {
anatofuz
parents:
diff changeset
20 namespace tidy {
anatofuz
parents:
diff changeset
21 namespace readability {
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 namespace {
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 // Return true if expr needs to be put in parens when it is an argument of a
anatofuz
parents:
diff changeset
26 // prefix unary operator, e.g. when it is a binary or ternary operator
anatofuz
parents:
diff changeset
27 // syntactically.
anatofuz
parents:
diff changeset
28 bool needParensAfterUnaryOperator(const Expr &ExprNode) {
anatofuz
parents:
diff changeset
29 if (isa<clang::BinaryOperator>(&ExprNode) ||
anatofuz
parents:
diff changeset
30 isa<clang::ConditionalOperator>(&ExprNode)) {
anatofuz
parents:
diff changeset
31 return true;
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33 if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
anatofuz
parents:
diff changeset
34 return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
anatofuz
parents:
diff changeset
35 Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
anatofuz
parents:
diff changeset
36 Op->getOperator() != OO_Subscript;
anatofuz
parents:
diff changeset
37 }
anatofuz
parents:
diff changeset
38 return false;
anatofuz
parents:
diff changeset
39 }
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 // Format a pointer to an expression: prefix with '*' but simplify
anatofuz
parents:
diff changeset
42 // when it already begins with '&'. Return empty string on failure.
anatofuz
parents:
diff changeset
43 std::string
anatofuz
parents:
diff changeset
44 formatDereference(const ast_matchers::MatchFinder::MatchResult &Result,
anatofuz
parents:
diff changeset
45 const Expr &ExprNode) {
anatofuz
parents:
diff changeset
46 if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
anatofuz
parents:
diff changeset
47 if (Op->getOpcode() == UO_AddrOf) {
anatofuz
parents:
diff changeset
48 // Strip leading '&'.
anatofuz
parents:
diff changeset
49 return std::string(tooling::fixit::getText(
anatofuz
parents:
diff changeset
50 *Op->getSubExpr()->IgnoreParens(), *Result.Context));
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52 }
anatofuz
parents:
diff changeset
53 StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context);
anatofuz
parents:
diff changeset
54
anatofuz
parents:
diff changeset
55 if (Text.empty())
anatofuz
parents:
diff changeset
56 return std::string();
anatofuz
parents:
diff changeset
57 // Add leading '*'.
anatofuz
parents:
diff changeset
58 if (needParensAfterUnaryOperator(ExprNode)) {
anatofuz
parents:
diff changeset
59 return (llvm::Twine("*(") + Text + ")").str();
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61 return (llvm::Twine("*") + Text).str();
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
64 AST_MATCHER(MaterializeTemporaryExpr, isBoundToLValue) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
65 return Node.isBoundToLvalueReference();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
66 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
67
150
anatofuz
parents:
diff changeset
68 } // end namespace
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 void RedundantStringCStrCheck::registerMatchers(
anatofuz
parents:
diff changeset
71 ast_matchers::MatchFinder *Finder) {
anatofuz
parents:
diff changeset
72 // Match expressions of type 'string' or 'string*'.
anatofuz
parents:
diff changeset
73 const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType(
anatofuz
parents:
diff changeset
74 hasDeclaration(cxxRecordDecl(hasName("::std::basic_string"))))));
anatofuz
parents:
diff changeset
75 const auto StringExpr =
anatofuz
parents:
diff changeset
76 expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl)))));
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 // Match string constructor.
anatofuz
parents:
diff changeset
79 const auto StringConstructorExpr = expr(anyOf(
anatofuz
parents:
diff changeset
80 cxxConstructExpr(argumentCountIs(1),
anatofuz
parents:
diff changeset
81 hasDeclaration(cxxMethodDecl(hasName("basic_string")))),
anatofuz
parents:
diff changeset
82 cxxConstructExpr(
anatofuz
parents:
diff changeset
83 argumentCountIs(2),
anatofuz
parents:
diff changeset
84 hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
anatofuz
parents:
diff changeset
85 // If present, the second argument is the alloc object which must not
anatofuz
parents:
diff changeset
86 // be present explicitly.
anatofuz
parents:
diff changeset
87 hasArgument(1, cxxDefaultArgExpr()))));
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 // Match a call to the string 'c_str()' method.
anatofuz
parents:
diff changeset
90 const auto StringCStrCallExpr =
anatofuz
parents:
diff changeset
91 cxxMemberCallExpr(on(StringExpr.bind("arg")),
anatofuz
parents:
diff changeset
92 callee(memberExpr().bind("member")),
anatofuz
parents:
diff changeset
93 callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
anatofuz
parents:
diff changeset
94 .bind("call");
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
95 const auto HasRValueTempParent =
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
96 hasParent(materializeTemporaryExpr(unless(isBoundToLValue())));
150
anatofuz
parents:
diff changeset
97 // Detect redundant 'c_str()' calls through a string constructor.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
98 // If CxxConstructExpr is the part of some CallExpr we need to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
99 // check that matched ParamDecl of the ancestor CallExpr is not rvalue.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
100 Finder->addMatcher(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 traverse(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
102 TK_AsIs,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103 cxxConstructExpr(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 StringConstructorExpr, hasArgument(0, StringCStrCallExpr),
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 HasRValueTempParent)))))),
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 this);
150
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 // Detect: 's == str.c_str()' -> 's == str'
anatofuz
parents:
diff changeset
110 Finder->addMatcher(
anatofuz
parents:
diff changeset
111 cxxOperatorCallExpr(
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
112 hasAnyOverloadedOperatorName("<", ">", ">=", "<=", "!=", "==", "+"),
150
anatofuz
parents:
diff changeset
113 anyOf(allOf(hasArgument(0, StringExpr),
anatofuz
parents:
diff changeset
114 hasArgument(1, StringCStrCallExpr)),
anatofuz
parents:
diff changeset
115 allOf(hasArgument(0, StringCStrCallExpr),
anatofuz
parents:
diff changeset
116 hasArgument(1, StringExpr)))),
anatofuz
parents:
diff changeset
117 this);
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 // Detect: 'dst += str.c_str()' -> 'dst += str'
anatofuz
parents:
diff changeset
120 // Detect: 's = str.c_str()' -> 's = str'
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
121 Finder->addMatcher(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
122 cxxOperatorCallExpr(hasAnyOverloadedOperatorName("=", "+="),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
123 hasArgument(0, StringExpr),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
124 hasArgument(1, StringCStrCallExpr)),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
125 this);
150
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)'
anatofuz
parents:
diff changeset
128 Finder->addMatcher(
anatofuz
parents:
diff changeset
129 cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName(
anatofuz
parents:
diff changeset
130 "append", "assign", "compare")))),
anatofuz
parents:
diff changeset
131 argumentCountIs(1), hasArgument(0, StringCStrCallExpr)),
anatofuz
parents:
diff changeset
132 this);
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)'
anatofuz
parents:
diff changeset
135 Finder->addMatcher(
anatofuz
parents:
diff changeset
136 cxxMemberCallExpr(on(StringExpr),
anatofuz
parents:
diff changeset
137 callee(decl(cxxMethodDecl(hasName("compare")))),
anatofuz
parents:
diff changeset
138 argumentCountIs(3), hasArgument(2, StringCStrCallExpr)),
anatofuz
parents:
diff changeset
139 this);
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)'
anatofuz
parents:
diff changeset
142 Finder->addMatcher(
anatofuz
parents:
diff changeset
143 cxxMemberCallExpr(on(StringExpr),
anatofuz
parents:
diff changeset
144 callee(decl(cxxMethodDecl(hasAnyName(
anatofuz
parents:
diff changeset
145 "find", "find_first_not_of", "find_first_of",
anatofuz
parents:
diff changeset
146 "find_last_not_of", "find_last_of", "rfind")))),
anatofuz
parents:
diff changeset
147 anyOf(argumentCountIs(1), argumentCountIs(2)),
anatofuz
parents:
diff changeset
148 hasArgument(0, StringCStrCallExpr)),
anatofuz
parents:
diff changeset
149 this);
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)'
anatofuz
parents:
diff changeset
152 Finder->addMatcher(
anatofuz
parents:
diff changeset
153 cxxMemberCallExpr(on(StringExpr),
anatofuz
parents:
diff changeset
154 callee(decl(cxxMethodDecl(hasName("insert")))),
anatofuz
parents:
diff changeset
155 argumentCountIs(2), hasArgument(1, StringCStrCallExpr)),
anatofuz
parents:
diff changeset
156 this);
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 // Detect redundant 'c_str()' calls through a StringRef constructor.
anatofuz
parents:
diff changeset
159 Finder->addMatcher(
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
160 traverse(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 TK_AsIs,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
162 cxxConstructExpr(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
163 // Implicit constructors of these classes are overloaded
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
164 // wrt. string types and they internally make a StringRef
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
165 // referring to the argument. Passing a string directly to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
166 // them is preferred to passing a char pointer.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167 hasDeclaration(cxxMethodDecl(hasAnyName(
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
168 "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
169 argumentCountIs(1),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
170 // The only argument must have the form x.c_str() or p->c_str()
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
171 // where the method is string::c_str(). StringRef also has
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
172 // a constructor from string which is more efficient (avoids
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
173 // strlen), so we can construct StringRef from the string
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
174 // directly.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
175 hasArgument(0, StringCStrCallExpr))),
150
anatofuz
parents:
diff changeset
176 this);
anatofuz
parents:
diff changeset
177 }
anatofuz
parents:
diff changeset
178
anatofuz
parents:
diff changeset
179 void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
180 const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
anatofuz
parents:
diff changeset
181 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
anatofuz
parents:
diff changeset
182 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
anatofuz
parents:
diff changeset
183 bool Arrow = Member->isArrow();
anatofuz
parents:
diff changeset
184 // Replace the "call" node with the "arg" node, prefixed with '*'
anatofuz
parents:
diff changeset
185 // if the call was using '->' rather than '.'.
anatofuz
parents:
diff changeset
186 std::string ArgText =
anatofuz
parents:
diff changeset
187 Arrow ? formatDereference(Result, *Arg)
anatofuz
parents:
diff changeset
188 : tooling::fixit::getText(*Arg, *Result.Context).str();
anatofuz
parents:
diff changeset
189 if (ArgText.empty())
anatofuz
parents:
diff changeset
190 return;
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 diag(Call->getBeginLoc(), "redundant call to %0")
anatofuz
parents:
diff changeset
193 << Member->getMemberDecl()
anatofuz
parents:
diff changeset
194 << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 } // namespace readability
anatofuz
parents:
diff changeset
198 } // namespace tidy
anatofuz
parents:
diff changeset
199 } // namespace clang