blob: 97398af297754cf1146513d1e2369601edc21b0d [file] [log] [blame]
Samuel Benzaquen3a571012014-03-27 17:42:26 +00001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000019namespace {
20internal::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
28void registerMatchersForGetArrowStart(MatchFinder *Finder,
29 MatchFinder::MatchCallback *Callback) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000030 const auto QuacksLikeASmartptr = recordDecl(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000031 recordDecl().bind("duck_typing"),
Samuel Benzaquen3a571012014-03-27 17:42:26 +000032 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 Benzaquen3a571012014-03-27 17:42:26 +000039 // Catch 'ptr.get()->Foo()'
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000040 Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
41 hasObjectExpression(ignoringImpCasts(
42 callToGet(QuacksLikeASmartptr)))),
43 Callback);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000044
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000045 // Catch '*ptr.get()' or '*ptr->get()'
Samuel Benzaquen3a571012014-03-27 17:42:26 +000046 Finder->addMatcher(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000047 unaryOperator(hasOperatorName("*"),
48 hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
49 Callback);
50}
Samuel Benzaquen3a571012014-03-27 17:42:26 +000051
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000052void 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 Benzaquen3a571012014-03-27 17:42:26 +000063 Finder->addMatcher(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000064 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
74void RedundantSmartptrGet::registerMatchers(MatchFinder *Finder) {
75 registerMatchersForGetArrowStart(Finder, this);
76 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000077}
78
79namespace {
80bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000081 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
82 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +000083 // 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
97void RedundantSmartptrGet::check(const MatchFinder::MatchResult &Result) {
98 if (!allReturnTypesMatch(Result)) return;
99
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000100 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
101 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000102 const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
103 const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
104
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000105 if (IsPtrToPtr && IsMemberExpr) {
106 // Ignore this case (eg. Foo->get()->DoSomething());
107 return;
108 }
109
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000110 StringRef SmartptrText = Lexer::getSourceText(
111 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
112 *Result.SourceManager, LangOptions());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000113 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000114 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