Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 1 | //===- RedundantStringCStrCheck.cpp - Check for redundant c_str calls -----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a check for redundant calls of c_str() on strings. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "RedundantStringCStrCheck.h" |
| 14 | #include "clang/Lex/Lexer.h" |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame] | 15 | #include "clang/Tooling/FixIt.h" |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 16 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 17 | using namespace clang::ast_matchers; |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 18 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 19 | namespace clang { |
| 20 | namespace tidy { |
| 21 | namespace readability { |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 25 | // Return true if expr needs to be put in parens when it is an argument of a |
| 26 | // prefix unary operator, e.g. when it is a binary or ternary operator |
| 27 | // syntactically. |
| 28 | bool needParensAfterUnaryOperator(const Expr &ExprNode) { |
| 29 | if (isa<clang::BinaryOperator>(&ExprNode) || |
| 30 | isa<clang::ConditionalOperator>(&ExprNode)) { |
| 31 | return true; |
| 32 | } |
| 33 | if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) { |
| 34 | return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus && |
| 35 | Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call && |
| 36 | Op->getOperator() != OO_Subscript; |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // Format a pointer to an expression: prefix with '*' but simplify |
| 42 | // when it already begins with '&'. Return empty string on failure. |
| 43 | std::string |
| 44 | formatDereference(const ast_matchers::MatchFinder::MatchResult &Result, |
| 45 | const Expr &ExprNode) { |
| 46 | if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) { |
| 47 | if (Op->getOpcode() == UO_AddrOf) { |
| 48 | // Strip leading '&'. |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame] | 49 | return tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(), |
| 50 | *Result.Context); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame] | 53 | StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context); |
| 54 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 55 | if (Text.empty()) |
| 56 | return std::string(); |
| 57 | // Add leading '*'. |
| 58 | if (needParensAfterUnaryOperator(ExprNode)) { |
| 59 | return (llvm::Twine("*(") + Text + ")").str(); |
| 60 | } |
| 61 | return (llvm::Twine("*") + Text).str(); |
| 62 | } |
| 63 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 64 | } // end namespace |
| 65 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 66 | void RedundantStringCStrCheck::registerMatchers( |
| 67 | ast_matchers::MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 68 | // Only register the matchers for C++; the functionality currently does not |
| 69 | // provide any benefit to other languages, despite being benign. |
| 70 | if (!getLangOpts().CPlusPlus) |
| 71 | return; |
| 72 | |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 73 | // Match expressions of type 'string' or 'string*'. |
Manuel Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 74 | const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType( |
| 75 | hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))); |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 76 | const auto StringExpr = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 77 | expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl))))); |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 78 | |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 79 | // Match string constructor. |
| 80 | const auto StringConstructorExpr = expr(anyOf( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 81 | cxxConstructExpr(argumentCountIs(1), |
| 82 | hasDeclaration(cxxMethodDecl(hasName("basic_string")))), |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 83 | cxxConstructExpr( |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 84 | argumentCountIs(2), |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 85 | hasDeclaration(cxxMethodDecl(hasName("basic_string"))), |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 86 | // If present, the second argument is the alloc object which must not |
| 87 | // be present explicitly. |
| 88 | hasArgument(1, cxxDefaultArgExpr())))); |
| 89 | |
| 90 | // Match a call to the string 'c_str()' method. |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 91 | const auto StringCStrCallExpr = |
| 92 | cxxMemberCallExpr(on(StringExpr.bind("arg")), |
| 93 | callee(memberExpr().bind("member")), |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 94 | callee(cxxMethodDecl(hasAnyName("c_str", "data")))) |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 95 | .bind("call"); |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 96 | |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 97 | // Detect redundant 'c_str()' calls through a string constructor. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 98 | Finder->addMatcher(cxxConstructExpr(StringConstructorExpr, |
| 99 | hasArgument(0, StringCStrCallExpr)), |
| 100 | this); |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 101 | |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 102 | // Detect: 's == str.c_str()' -> 's == str' |
| 103 | Finder->addMatcher( |
| 104 | cxxOperatorCallExpr( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 105 | anyOf( |
| 106 | hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"), |
| 107 | hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="), |
| 108 | hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="), |
| 109 | hasOverloadedOperatorName("+")), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 110 | anyOf(allOf(hasArgument(0, StringExpr), |
| 111 | hasArgument(1, StringCStrCallExpr)), |
| 112 | allOf(hasArgument(0, StringCStrCallExpr), |
| 113 | hasArgument(1, StringExpr)))), |
| 114 | this); |
| 115 | |
| 116 | // Detect: 'dst += str.c_str()' -> 'dst += str' |
| 117 | // Detect: 's = str.c_str()' -> 's = str' |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 118 | Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="), |
| 119 | hasOverloadedOperatorName("+=")), |
| 120 | hasArgument(0, StringExpr), |
| 121 | hasArgument(1, StringCStrCallExpr)), |
| 122 | this); |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 123 | |
| 124 | // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)' |
| 125 | Finder->addMatcher( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 126 | cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName( |
| 127 | "append", "assign", "compare")))), |
| 128 | argumentCountIs(1), hasArgument(0, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 129 | this); |
| 130 | |
| 131 | // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)' |
| 132 | Finder->addMatcher( |
| 133 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 134 | callee(decl(cxxMethodDecl(hasName("compare")))), |
| 135 | argumentCountIs(3), hasArgument(2, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 136 | this); |
| 137 | |
| 138 | // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)' |
| 139 | Finder->addMatcher( |
| 140 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 141 | callee(decl(cxxMethodDecl(hasAnyName( |
| 142 | "find", "find_first_not_of", "find_first_of", |
| 143 | "find_last_not_of", "find_last_of", "rfind")))), |
| 144 | anyOf(argumentCountIs(1), argumentCountIs(2)), |
| 145 | hasArgument(0, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 146 | this); |
| 147 | |
| 148 | // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)' |
| 149 | Finder->addMatcher( |
| 150 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 151 | callee(decl(cxxMethodDecl(hasName("insert")))), |
| 152 | argumentCountIs(2), hasArgument(1, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 153 | this); |
| 154 | |
| 155 | // Detect redundant 'c_str()' calls through a StringRef constructor. |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 156 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 157 | cxxConstructExpr( |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 158 | // Implicit constructors of these classes are overloaded |
| 159 | // wrt. string types and they internally make a StringRef |
| 160 | // referring to the argument. Passing a string directly to |
| 161 | // them is preferred to passing a char pointer. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 162 | hasDeclaration(cxxMethodDecl(hasAnyName( |
| 163 | "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))), |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 164 | argumentCountIs(1), |
| 165 | // The only argument must have the form x.c_str() or p->c_str() |
| 166 | // where the method is string::c_str(). StringRef also has |
| 167 | // a constructor from string which is more efficient (avoids |
| 168 | // strlen), so we can construct StringRef from the string |
| 169 | // directly. |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 170 | hasArgument(0, StringCStrCallExpr)), |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 171 | this); |
| 172 | } |
| 173 | |
| 174 | void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 175 | const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call"); |
| 176 | const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg"); |
| 177 | const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member"); |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 178 | bool Arrow = Member->isArrow(); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 179 | // Replace the "call" node with the "arg" node, prefixed with '*' |
| 180 | // if the call was using '->' rather than '.'. |
| 181 | std::string ArgText = |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame] | 182 | Arrow ? formatDereference(Result, *Arg) |
| 183 | : tooling::fixit::getText(*Arg, *Result.Context).str(); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 184 | if (ArgText.empty()) |
| 185 | return; |
| 186 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 187 | diag(Call->getBeginLoc(), "redundant call to %0") |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 188 | << Member->getMemberDecl() |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 189 | << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText); |
| 190 | } |
| 191 | |
| 192 | } // namespace readability |
| 193 | } // namespace tidy |
| 194 | } // namespace clang |