blob: 5b40d92cd4c9a357e1d88242f3eaad56fd6ee05f [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) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000077 // 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 Benzaquen110f3cc2014-04-09 14:17:23 +000082 registerMatchersForGetArrowStart(Finder, this);
83 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000084}
85
86namespace {
87bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000088 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
89 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +000090 // 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 Kornienko1b677db2015-03-09 12:18:39 +0000104void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000105 if (!allReturnTypesMatch(Result)) return;
106
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000107 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
108 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000109 const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
110 const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
111
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000112 if (IsPtrToPtr && IsMemberExpr) {
113 // Ignore this case (eg. Foo->get()->DoSomething());
114 return;
115 }
116
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000117 StringRef SmartptrText = Lexer::getSourceText(
118 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
Alexander Kornienko96e7b8b2015-01-22 12:40:47 +0000119 *Result.SourceManager, Result.Context->getLangOpts());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000120 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000121 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 Kornienko35ddae42014-10-15 10:51:57 +0000126} // namespace readability
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000127} // namespace tidy
128} // namespace clang