Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 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" |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame^] | 16 | #include "clang/Tooling/FixIt.h" |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 17 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 18 | using namespace clang::ast_matchers; |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 19 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | namespace readability { |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 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 '&'. |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame^] | 50 | return tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(), |
| 51 | *Result.Context); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame^] | 54 | StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context); |
| 55 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 65 | } // end namespace |
| 66 | |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 67 | void RedundantStringCStrCheck::registerMatchers( |
| 68 | ast_matchers::MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 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 | |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 74 | // Match expressions of type 'string' or 'string*'. |
Manuel Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 75 | const auto StringDecl = type(hasUnqualifiedDesugaredType(recordType( |
| 76 | hasDeclaration(cxxRecordDecl(hasName("::std::basic_string")))))); |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 77 | const auto StringExpr = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 78 | expr(anyOf(hasType(StringDecl), hasType(qualType(pointsTo(StringDecl))))); |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 79 | |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 80 | // Match string constructor. |
| 81 | const auto StringConstructorExpr = expr(anyOf( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 82 | cxxConstructExpr(argumentCountIs(1), |
| 83 | hasDeclaration(cxxMethodDecl(hasName("basic_string")))), |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 84 | cxxConstructExpr( |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 85 | argumentCountIs(2), |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 86 | hasDeclaration(cxxMethodDecl(hasName("basic_string"))), |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 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. |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 92 | const auto StringCStrCallExpr = |
| 93 | cxxMemberCallExpr(on(StringExpr.bind("arg")), |
| 94 | callee(memberExpr().bind("member")), |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 95 | callee(cxxMethodDecl(hasAnyName("c_str", "data")))) |
Etienne Bergeron | cb7ce98 | 2016-03-24 19:42:36 +0000 | [diff] [blame] | 96 | .bind("call"); |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 97 | |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 98 | // Detect redundant 'c_str()' calls through a string constructor. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 99 | Finder->addMatcher(cxxConstructExpr(StringConstructorExpr, |
| 100 | hasArgument(0, StringCStrCallExpr)), |
| 101 | this); |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 102 | |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 103 | // Detect: 's == str.c_str()' -> 's == str' |
| 104 | Finder->addMatcher( |
| 105 | cxxOperatorCallExpr( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 106 | anyOf( |
| 107 | hasOverloadedOperatorName("<"), hasOverloadedOperatorName(">"), |
| 108 | hasOverloadedOperatorName(">="), hasOverloadedOperatorName("<="), |
| 109 | hasOverloadedOperatorName("!="), hasOverloadedOperatorName("=="), |
| 110 | hasOverloadedOperatorName("+")), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 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' |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 119 | Finder->addMatcher(cxxOperatorCallExpr(anyOf(hasOverloadedOperatorName("="), |
| 120 | hasOverloadedOperatorName("+=")), |
| 121 | hasArgument(0, StringExpr), |
| 122 | hasArgument(1, StringCStrCallExpr)), |
| 123 | this); |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 124 | |
| 125 | // Detect: 'dst.append(str.c_str())' -> 'dst.append(str)' |
| 126 | Finder->addMatcher( |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 127 | cxxMemberCallExpr(on(StringExpr), callee(decl(cxxMethodDecl(hasAnyName( |
| 128 | "append", "assign", "compare")))), |
| 129 | argumentCountIs(1), hasArgument(0, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 130 | this); |
| 131 | |
| 132 | // Detect: 'dst.compare(p, n, str.c_str())' -> 'dst.compare(p, n, str)' |
| 133 | Finder->addMatcher( |
| 134 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 135 | callee(decl(cxxMethodDecl(hasName("compare")))), |
| 136 | argumentCountIs(3), hasArgument(2, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 137 | this); |
| 138 | |
| 139 | // Detect: 'dst.find(str.c_str())' -> 'dst.find(str)' |
| 140 | Finder->addMatcher( |
| 141 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 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)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 147 | this); |
| 148 | |
| 149 | // Detect: 'dst.insert(pos, str.c_str())' -> 'dst.insert(pos, str)' |
| 150 | Finder->addMatcher( |
| 151 | cxxMemberCallExpr(on(StringExpr), |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 152 | callee(decl(cxxMethodDecl(hasName("insert")))), |
| 153 | argumentCountIs(2), hasArgument(1, StringCStrCallExpr)), |
Etienne Bergeron | 9cfd8ce | 2016-04-15 18:12:06 +0000 | [diff] [blame] | 154 | this); |
| 155 | |
| 156 | // Detect redundant 'c_str()' calls through a StringRef constructor. |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 157 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 158 | cxxConstructExpr( |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 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. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 163 | hasDeclaration(cxxMethodDecl(hasAnyName( |
| 164 | "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))), |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 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. |
Etienne Bergeron | 4c3b55c | 2016-03-22 18:00:13 +0000 | [diff] [blame] | 171 | hasArgument(0, StringCStrCallExpr)), |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 172 | this); |
| 173 | } |
| 174 | |
| 175 | void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 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"); |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 179 | bool Arrow = Member->isArrow(); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 180 | // Replace the "call" node with the "arg" node, prefixed with '*' |
| 181 | // if the call was using '->' rather than '.'. |
| 182 | std::string ArgText = |
Haojian Wu | 75f6cad | 2018-12-07 11:25:37 +0000 | [diff] [blame^] | 183 | Arrow ? formatDereference(Result, *Arg) |
| 184 | : tooling::fixit::getText(*Arg, *Result.Context).str(); |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 185 | if (ArgText.empty()) |
| 186 | return; |
| 187 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 188 | diag(Call->getBeginLoc(), "redundant call to %0") |
Malcolm Parsons | 8b70e26 | 2016-11-03 12:56:48 +0000 | [diff] [blame] | 189 | << Member->getMemberDecl() |
Alexander Kornienko | 57a5c6b | 2015-03-16 00:32:25 +0000 | [diff] [blame] | 190 | << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText); |
| 191 | } |
| 192 | |
| 193 | } // namespace readability |
| 194 | } // namespace tidy |
| 195 | } // namespace clang |