| Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 1 | //===--- RedundantSmartptrGetCheck.cpp - clang-tidy -----------------------===// |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 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 |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 9 | #include "RedundantSmartptrGetCheck.h" |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | #include "clang/Lex/Lexer.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| Alexander Kornienko | 35ddae4 | 2014-10-15 10:51:57 +0000 | [diff] [blame] | 17 | namespace readability { |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 18 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 19 | namespace { |
| Benjamin Kramer | 51a9cc9 | 2016-06-15 15:46:10 +0000 | [diff] [blame] | 20 | internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) { |
| Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 21 | return cxxMemberCallExpr( |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 22 | on(expr(anyOf(hasType(OnClass), |
| Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 23 | hasType(qualType( |
| 24 | pointsTo(decl(OnClass).bind("ptr_to_ptr")))))) |
| 25 | .bind("smart_pointer")), |
| 26 | unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))), |
| Samuel Benzaquen | 1fb8bc7 | 2016-02-17 16:13:14 +0000 | [diff] [blame] | 27 | callee(cxxMethodDecl( |
| 28 | hasName("get"), |
| 29 | returns(qualType(pointsTo(type().bind("getType"))))))) |
| Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 30 | .bind("redundant_get"); |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 33 | internal::Matcher<Decl> knownSmartptr() { |
| 34 | return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr")); |
| 35 | } |
| 36 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 37 | void registerMatchersForGetArrowStart(MatchFinder *Finder, |
| 38 | MatchFinder::MatchCallback *Callback) { |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 39 | const auto QuacksLikeASmartptr = recordDecl( |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 40 | recordDecl().bind("duck_typing"), |
| Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 41 | has(cxxMethodDecl(hasName("operator->"), |
| 42 | returns(qualType(pointsTo(type().bind("op->Type")))))), |
| Samuel Benzaquen | 1fb8bc7 | 2016-02-17 16:13:14 +0000 | [diff] [blame] | 43 | has(cxxMethodDecl(hasName("operator*"), returns(qualType(references( |
| 44 | type().bind("op*Type"))))))); |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 45 | |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 46 | // Make sure we are not missing the known standard types. |
| 47 | const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr); |
| 48 | |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 49 | // Catch 'ptr.get()->Foo()' |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 50 | Finder->addMatcher( |
| 51 | memberExpr(expr().bind("memberExpr"), isArrow(), |
| 52 | hasObjectExpression(ignoringImpCasts(callToGet(Smartptr)))), |
| 53 | Callback); |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 54 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 55 | // Catch '*ptr.get()' or '*ptr->get()' |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 56 | Finder->addMatcher( |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 57 | unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))), |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 58 | Callback); |
| Samuel Benzaquen | c814872 | 2018-01-15 18:03:20 +0000 | [diff] [blame] | 59 | |
| 60 | // Catch '!ptr.get()' |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 61 | const auto CallToGetAsBool = ignoringParenImpCasts(callToGet( |
| 62 | recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType())))))); |
| Samuel Benzaquen | c814872 | 2018-01-15 18:03:20 +0000 | [diff] [blame] | 63 | Finder->addMatcher( |
| 64 | unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)), |
| 65 | Callback); |
| 66 | |
| 67 | // Catch 'if(ptr.get())' |
| 68 | Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback); |
| 69 | |
| 70 | // Catch 'ptr.get() ? X : Y' |
| 71 | Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)), |
| 72 | Callback); |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 73 | } |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 74 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 75 | void registerMatchersForGetEquals(MatchFinder *Finder, |
| 76 | MatchFinder::MatchCallback *Callback) { |
| 77 | // This one is harder to do with duck typing. |
| 78 | // The operator==/!= that we are looking for might be member or non-member, |
| 79 | // might be on global namespace or found by ADL, might be a template, etc. |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 80 | // For now, lets keep it to the known standard types. |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 81 | |
| 82 | // Matches against nullptr. |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 83 | Finder->addMatcher( |
| Nathan James | 97572fa | 2020-03-10 00:42:21 +0000 | [diff] [blame^] | 84 | binaryOperator(hasAnyOperatorName("==", "!="), |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 85 | hasEitherOperand(ignoringImpCasts( |
| 86 | anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(), |
| 87 | integerLiteral(equals(0))))), |
| Florian Gross | e25a0e9 | 2019-05-02 16:41:28 +0000 | [diff] [blame] | 88 | hasEitherOperand(callToGet(knownSmartptr()))), |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 89 | Callback); |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 90 | |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 91 | // FIXME: Match and fix if (l.get() == r.get()). |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 94 | } // namespace |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 95 | |
| Miklos Vajna | e967a12 | 2018-10-21 19:16:25 +0000 | [diff] [blame] | 96 | void RedundantSmartptrGetCheck::storeOptions( |
| 97 | ClangTidyOptions::OptionMap &Opts) { |
| 98 | Options.store(Opts, "IgnoreMacros", IgnoreMacros); |
| 99 | } |
| 100 | |
| Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 101 | void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) { |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 102 | registerMatchersForGetArrowStart(Finder, this); |
| 103 | registerMatchersForGetEquals(Finder, this); |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | namespace { |
| 107 | bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) { |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 108 | if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr) |
| 109 | return true; |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 110 | // Verify that the types match. |
| 111 | // We can't do this on the matcher because the type nodes can be different, |
| 112 | // even though they represent the same type. This difference comes from how |
| 113 | // the type is referenced (eg. through a typedef, a type trait, etc). |
| 114 | const Type *OpArrowType = |
| 115 | Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType(); |
| 116 | const Type *OpStarType = |
| 117 | Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType(); |
| 118 | const Type *GetType = |
| 119 | Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType(); |
| 120 | return OpArrowType == OpStarType && OpArrowType == GetType; |
| 121 | } |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 122 | } // namespace |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 123 | |
| Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 124 | void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) { |
| Kirill Bobyrev | 9559f04 | 2016-09-26 07:22:37 +0000 | [diff] [blame] | 125 | if (!allReturnTypesMatch(Result)) |
| 126 | return; |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 127 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 128 | bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr; |
| 129 | bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr; |
| Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 130 | const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get"); |
| Miklos Vajna | e967a12 | 2018-10-21 19:16:25 +0000 | [diff] [blame] | 131 | if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros) |
| 132 | return; |
| 133 | |
| Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 134 | const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer"); |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 135 | |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 136 | if (IsPtrToPtr && IsMemberExpr) { |
| 137 | // Ignore this case (eg. Foo->get()->DoSomething()); |
| 138 | return; |
| 139 | } |
| 140 | |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 141 | StringRef SmartptrText = Lexer::getSourceText( |
| 142 | CharSourceRange::getTokenRange(Smartptr->getSourceRange()), |
| Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 143 | *Result.SourceManager, getLangOpts()); |
| Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 144 | // Replace foo->get() with *foo, and foo.get() with foo. |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 145 | std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str(); |
| Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 146 | diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer") |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 147 | << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement); |
| 148 | } |
| 149 | |
| Alexander Kornienko | 35ddae4 | 2014-10-15 10:51:57 +0000 | [diff] [blame] | 150 | } // namespace readability |
| Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 151 | } // namespace tidy |
| 152 | } // namespace clang |