blob: 2b86c2fea3899a5b018e627f6848e9e8afafff2c [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) {
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000022 return cxxMemberCallExpr(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000023 on(expr(anyOf(hasType(OnClass),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000024 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 Benzaquen110f3cc2014-04-09 14:17:23 +000030}
31
32void registerMatchersForGetArrowStart(MatchFinder *Finder,
33 MatchFinder::MatchCallback *Callback) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000034 const auto QuacksLikeASmartptr = recordDecl(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000035 recordDecl().bind("duck_typing"),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000036 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 Benzaquen3a571012014-03-27 17:42:26 +000042
Samuel Benzaquen3a571012014-03-27 17:42:26 +000043 // Catch 'ptr.get()->Foo()'
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000044 Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
45 hasObjectExpression(ignoringImpCasts(
46 callToGet(QuacksLikeASmartptr)))),
47 Callback);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000048
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000049 // Catch '*ptr.get()' or '*ptr->get()'
Samuel Benzaquen3a571012014-03-27 17:42:26 +000050 Finder->addMatcher(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000051 unaryOperator(hasOperatorName("*"),
52 hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
53 Callback);
54}
Samuel Benzaquen3a571012014-03-27 17:42:26 +000055
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000056void 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 Benzaquen3a571012014-03-27 17:42:26 +000067 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000068 binaryOperator(
69 anyOf(hasOperatorName("=="), hasOperatorName("!=")),
70 hasEitherOperand(ignoringImpCasts(cxxNullPtrLiteralExpr())),
71 hasEitherOperand(callToGet(IsAKnownSmartptr))),
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000072 Callback);
73 // TODO: Catch ptr.get() == other_ptr.get()
74}
75
76
77} // namespace
78
Alexander Kornienko1b677db2015-03-09 12:18:39 +000079void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000080 // 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 Benzaquen110f3cc2014-04-09 14:17:23 +000085 registerMatchersForGetArrowStart(Finder, this);
86 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000087}
88
89namespace {
90bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000091 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
92 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +000093 // 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 Kornienko1b677db2015-03-09 12:18:39 +0000107void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000108 if (!allReturnTypesMatch(Result)) return;
109
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000110 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
111 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000112 const Expr *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
113 const Expr *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
114
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000115 if (IsPtrToPtr && IsMemberExpr) {
116 // Ignore this case (eg. Foo->get()->DoSomething());
117 return;
118 }
119
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000120 StringRef SmartptrText = Lexer::getSourceText(
121 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
Alexander Kornienko96e7b8b2015-01-22 12:40:47 +0000122 *Result.SourceManager, Result.Context->getLangOpts());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000123 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000124 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 Kornienko35ddae42014-10-15 10:51:57 +0000129} // namespace readability
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000130} // namespace tidy
131} // namespace clang