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 | // |
| 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 | |
Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 10 | #include "RedundantSmartptrGetCheck.h" |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
Alexander Kornienko | 35ddae4 | 2014-10-15 10:51:57 +0000 | [diff] [blame] | 18 | namespace readability { |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 19 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | internal::Matcher<Expr> callToGet(internal::Matcher<Decl> OnClass) { |
| 22 | return memberCallExpr( |
| 23 | on(expr(anyOf(hasType(OnClass), |
| 24 | hasType(qualType(pointsTo(decl(OnClass).bind( |
| 25 | "ptr_to_ptr")))))).bind("smart_pointer")), |
Samuel Benzaquen | 20e93f3 | 2014-04-29 13:41:23 +0000 | [diff] [blame] | 26 | unless(callee(memberExpr(hasObjectExpression(thisExpr())))), |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 27 | callee(methodDecl(hasName("get")))).bind("redundant_get"); |
| 28 | } |
| 29 | |
| 30 | void registerMatchersForGetArrowStart(MatchFinder *Finder, |
| 31 | MatchFinder::MatchCallback *Callback) { |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 32 | const auto QuacksLikeASmartptr = recordDecl( |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 33 | recordDecl().bind("duck_typing"), |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 34 | has(methodDecl(hasName("operator->"), |
| 35 | returns(qualType(pointsTo(type().bind("op->Type")))))), |
| 36 | has(methodDecl(hasName("operator*"), |
| 37 | returns(qualType(references(type().bind("op*Type")))))), |
| 38 | has(methodDecl(hasName("get"), |
| 39 | returns(qualType(pointsTo(type().bind("getType"))))))); |
| 40 | |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 41 | // Catch 'ptr.get()->Foo()' |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 42 | Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(), |
| 43 | hasObjectExpression(ignoringImpCasts( |
| 44 | callToGet(QuacksLikeASmartptr)))), |
| 45 | Callback); |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 46 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 47 | // Catch '*ptr.get()' or '*ptr->get()' |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 48 | Finder->addMatcher( |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 49 | unaryOperator(hasOperatorName("*"), |
| 50 | hasUnaryOperand(callToGet(QuacksLikeASmartptr))), |
| 51 | Callback); |
| 52 | } |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 53 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 54 | void registerMatchersForGetEquals(MatchFinder *Finder, |
| 55 | MatchFinder::MatchCallback *Callback) { |
| 56 | // This one is harder to do with duck typing. |
| 57 | // The operator==/!= that we are looking for might be member or non-member, |
| 58 | // might be on global namespace or found by ADL, might be a template, etc. |
| 59 | // For now, lets keep a list of known standard types. |
| 60 | |
| 61 | const auto IsAKnownSmartptr = recordDecl( |
| 62 | anyOf(hasName("::std::unique_ptr"), hasName("::std::shared_ptr"))); |
| 63 | |
| 64 | // Matches against nullptr. |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 65 | Finder->addMatcher( |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 66 | binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")), |
| 67 | hasEitherOperand(ignoringImpCasts(nullPtrLiteralExpr())), |
| 68 | hasEitherOperand(callToGet(IsAKnownSmartptr))), |
| 69 | Callback); |
| 70 | // TODO: Catch ptr.get() == other_ptr.get() |
| 71 | } |
| 72 | |
| 73 | |
| 74 | } // namespace |
| 75 | |
Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 76 | void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame^] | 77 | // Only register the matchers for C++; the functionality currently does not |
| 78 | // provide any benefit to other languages, despite being benign. |
| 79 | if (!getLangOpts().CPlusPlus) |
| 80 | return; |
| 81 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 82 | registerMatchersForGetArrowStart(Finder, this); |
| 83 | registerMatchersForGetEquals(Finder, this); |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | namespace { |
| 87 | bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) { |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 88 | if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr) |
| 89 | return true; |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 90 | // Verify that the types match. |
| 91 | // We can't do this on the matcher because the type nodes can be different, |
| 92 | // even though they represent the same type. This difference comes from how |
| 93 | // the type is referenced (eg. through a typedef, a type trait, etc). |
| 94 | const Type *OpArrowType = |
| 95 | Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType(); |
| 96 | const Type *OpStarType = |
| 97 | Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType(); |
| 98 | const Type *GetType = |
| 99 | Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType(); |
| 100 | return OpArrowType == OpStarType && OpArrowType == GetType; |
| 101 | } |
| 102 | } // namespace |
| 103 | |
Alexander Kornienko | 1b677db | 2015-03-09 12:18:39 +0000 | [diff] [blame] | 104 | void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) { |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 105 | if (!allReturnTypesMatch(Result)) return; |
| 106 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 107 | bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr; |
| 108 | bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr; |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 109 | const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get"); |
| 110 | const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer"); |
| 111 | |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 112 | if (IsPtrToPtr && IsMemberExpr) { |
| 113 | // Ignore this case (eg. Foo->get()->DoSomething()); |
| 114 | return; |
| 115 | } |
| 116 | |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 117 | StringRef SmartptrText = Lexer::getSourceText( |
| 118 | CharSourceRange::getTokenRange(Smartptr->getSourceRange()), |
Alexander Kornienko | 96e7b8b | 2015-01-22 12:40:47 +0000 | [diff] [blame] | 119 | *Result.SourceManager, Result.Context->getLangOpts()); |
Samuel Benzaquen | 110f3cc | 2014-04-09 14:17:23 +0000 | [diff] [blame] | 120 | // Replace foo->get() with *foo, and foo.get() with foo. |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 121 | std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str(); |
| 122 | diag(GetCall->getLocStart(), "Redundant get() call on smart pointer.") |
| 123 | << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement); |
| 124 | } |
| 125 | |
Alexander Kornienko | 35ddae4 | 2014-10-15 10:51:57 +0000 | [diff] [blame] | 126 | } // namespace readability |
Samuel Benzaquen | 3a57101 | 2014-03-27 17:42:26 +0000 | [diff] [blame] | 127 | } // namespace tidy |
| 128 | } // namespace clang |