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