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