blob: cb10723781b6a76d823bfeaf96d188cc882f33e1 [file] [log] [blame]
Alexander Kornienko1b677db2015-03-09 12:18:39 +00001//===--- RedundantSmartptrGetCheck.cpp - clang-tidy -----------------------===//
Samuel Benzaquen3a571012014-03-27 17:42:26 +00002//
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 Kornienko1b677db2015-03-09 12:18:39 +000010#include "RedundantSmartptrGetCheck.h"
Samuel Benzaquen3a571012014-03-27 17:42:26 +000011#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
Alexander Kornienko35ddae42014-10-15 10:51:57 +000018namespace readability {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000019
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000020namespace {
21internal::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 Benzaquen20e93f32014-04-29 13:41:23 +000026 unless(callee(memberExpr(hasObjectExpression(thisExpr())))),
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000027 callee(methodDecl(hasName("get")))).bind("redundant_get");
28}
29
30void registerMatchersForGetArrowStart(MatchFinder *Finder,
31 MatchFinder::MatchCallback *Callback) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000032 const auto QuacksLikeASmartptr = recordDecl(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000033 recordDecl().bind("duck_typing"),
Samuel Benzaquen3a571012014-03-27 17:42:26 +000034 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 Benzaquen3a571012014-03-27 17:42:26 +000041 // Catch 'ptr.get()->Foo()'
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000042 Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
43 hasObjectExpression(ignoringImpCasts(
44 callToGet(QuacksLikeASmartptr)))),
45 Callback);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000046
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000047 // Catch '*ptr.get()' or '*ptr->get()'
Samuel Benzaquen3a571012014-03-27 17:42:26 +000048 Finder->addMatcher(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000049 unaryOperator(hasOperatorName("*"),
50 hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
51 Callback);
52}
Samuel Benzaquen3a571012014-03-27 17:42:26 +000053
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000054void 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 Benzaquen3a571012014-03-27 17:42:26 +000065 Finder->addMatcher(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000066 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 Kornienko1b677db2015-03-09 12:18:39 +000076void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000077 registerMatchersForGetArrowStart(Finder, this);
78 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000079}
80
81namespace {
82bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000083 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
84 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +000085 // Verify that the types match.
86 // We can't do this on the matcher because the type nodes can be different,
87 // even though they represent the same type. This difference comes from how
88 // the type is referenced (eg. through a typedef, a type trait, etc).
89 const Type *OpArrowType =
90 Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType();
91 const Type *OpStarType =
92 Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType();
93 const Type *GetType =
94 Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
95 return OpArrowType == OpStarType && OpArrowType == GetType;
96}
97} // namespace
98
Alexander Kornienko1b677db2015-03-09 12:18:39 +000099void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000100 if (!allReturnTypesMatch(Result)) return;
101
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000102 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
103 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000104 const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
105 const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
106
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000107 if (IsPtrToPtr && IsMemberExpr) {
108 // Ignore this case (eg. Foo->get()->DoSomething());
109 return;
110 }
111
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000112 StringRef SmartptrText = Lexer::getSourceText(
113 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
Alexander Kornienko96e7b8b2015-01-22 12:40:47 +0000114 *Result.SourceManager, Result.Context->getLangOpts());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000115 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000116 std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
117 diag(GetCall->getLocStart(), "Redundant get() call on smart pointer.")
118 << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
119}
120
Alexander Kornienko35ddae42014-10-15 10:51:57 +0000121} // namespace readability
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000122} // namespace tidy
123} // namespace clang