blob: 9949ab2c4fcef7117d69306d97a83802749b7cd6 [file] [log] [blame]
Alexander Kornienko1b677db2015-03-09 12:18:39 +00001//===--- RedundantSmartptrGetCheck.cpp - clang-tidy -----------------------===//
Samuel Benzaquen3a571012014-03-27 17:42:26 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Samuel Benzaquen3a571012014-03-27 17:42:26 +00006//
7//===----------------------------------------------------------------------===//
8
Alexander Kornienko1b677db2015-03-09 12:18:39 +00009#include "RedundantSmartptrGetCheck.h"
Samuel Benzaquen3a571012014-03-27 17:42:26 +000010#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/Lex/Lexer.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
Alexander Kornienko35ddae42014-10-15 10:51:57 +000017namespace readability {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000018
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000019namespace {
Benjamin Kramer51a9cc92016-06-15 15:46:10 +000020internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) {
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000021 return cxxMemberCallExpr(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000022 on(expr(anyOf(hasType(OnClass),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000023 hasType(qualType(
24 pointsTo(decl(OnClass).bind("ptr_to_ptr"))))))
25 .bind("smart_pointer")),
26 unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))),
Samuel Benzaquen1fb8bc72016-02-17 16:13:14 +000027 callee(cxxMethodDecl(
28 hasName("get"),
29 returns(qualType(pointsTo(type().bind("getType")))))))
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000030 .bind("redundant_get");
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000031}
32
Florian Grosse25a0e92019-05-02 16:41:28 +000033internal::Matcher<Decl> knownSmartptr() {
34 return recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
35}
36
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000037void registerMatchersForGetArrowStart(MatchFinder *Finder,
38 MatchFinder::MatchCallback *Callback) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000039 const auto QuacksLikeASmartptr = recordDecl(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000040 recordDecl().bind("duck_typing"),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000041 has(cxxMethodDecl(hasName("operator->"),
42 returns(qualType(pointsTo(type().bind("op->Type")))))),
Samuel Benzaquen1fb8bc72016-02-17 16:13:14 +000043 has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
44 type().bind("op*Type")))))));
Samuel Benzaquen3a571012014-03-27 17:42:26 +000045
Florian Grosse25a0e92019-05-02 16:41:28 +000046 // Make sure we are not missing the known standard types.
47 const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr);
48
Samuel Benzaquen3a571012014-03-27 17:42:26 +000049 // Catch 'ptr.get()->Foo()'
Florian Grosse25a0e92019-05-02 16:41:28 +000050 Finder->addMatcher(
51 memberExpr(expr().bind("memberExpr"), isArrow(),
52 hasObjectExpression(ignoringImpCasts(callToGet(Smartptr)))),
53 Callback);
Samuel Benzaquen3a571012014-03-27 17:42:26 +000054
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000055 // Catch '*ptr.get()' or '*ptr->get()'
Samuel Benzaquen3a571012014-03-27 17:42:26 +000056 Finder->addMatcher(
Florian Grosse25a0e92019-05-02 16:41:28 +000057 unaryOperator(hasOperatorName("*"), hasUnaryOperand(callToGet(Smartptr))),
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000058 Callback);
Samuel Benzaquenc8148722018-01-15 18:03:20 +000059
60 // Catch '!ptr.get()'
Florian Grosse25a0e92019-05-02 16:41:28 +000061 const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(
62 recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType()))))));
Samuel Benzaquenc8148722018-01-15 18:03:20 +000063 Finder->addMatcher(
64 unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)),
65 Callback);
66
67 // Catch 'if(ptr.get())'
68 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
69
70 // Catch 'ptr.get() ? X : Y'
71 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
72 Callback);
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000073}
Samuel Benzaquen3a571012014-03-27 17:42:26 +000074
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000075void registerMatchersForGetEquals(MatchFinder *Finder,
76 MatchFinder::MatchCallback *Callback) {
77 // This one is harder to do with duck typing.
78 // The operator==/!= that we are looking for might be member or non-member,
79 // might be on global namespace or found by ADL, might be a template, etc.
Florian Grosse25a0e92019-05-02 16:41:28 +000080 // For now, lets keep it to the known standard types.
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000081
82 // Matches against nullptr.
Samuel Benzaquen3a571012014-03-27 17:42:26 +000083 Finder->addMatcher(
Nathan James97572fa2020-03-10 00:42:21 +000084 binaryOperator(hasAnyOperatorName("==", "!="),
Kirill Bobyrev9559f042016-09-26 07:22:37 +000085 hasEitherOperand(ignoringImpCasts(
86 anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
87 integerLiteral(equals(0))))),
Florian Grosse25a0e92019-05-02 16:41:28 +000088 hasEitherOperand(callToGet(knownSmartptr()))),
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000089 Callback);
Kirill Bobyrev9559f042016-09-26 07:22:37 +000090
Kirill Bobyrev9559f042016-09-26 07:22:37 +000091 // FIXME: Match and fix if (l.get() == r.get()).
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000092}
93
Kirill Bobyrev9559f042016-09-26 07:22:37 +000094} // namespace
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000095
Miklos Vajnae967a122018-10-21 19:16:25 +000096void RedundantSmartptrGetCheck::storeOptions(
97 ClangTidyOptions::OptionMap &Opts) {
98 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
99}
100
Alexander Kornienko1b677db2015-03-09 12:18:39 +0000101void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000102 registerMatchersForGetArrowStart(Finder, this);
103 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000104}
105
106namespace {
107bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000108 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
109 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000110 // Verify that the types match.
111 // We can't do this on the matcher because the type nodes can be different,
112 // even though they represent the same type. This difference comes from how
113 // the type is referenced (eg. through a typedef, a type trait, etc).
114 const Type *OpArrowType =
115 Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType();
116 const Type *OpStarType =
117 Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType();
118 const Type *GetType =
119 Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
120 return OpArrowType == OpStarType && OpArrowType == GetType;
121}
Kirill Bobyrev9559f042016-09-26 07:22:37 +0000122} // namespace
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000123
Alexander Kornienko1b677db2015-03-09 12:18:39 +0000124void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
Kirill Bobyrev9559f042016-09-26 07:22:37 +0000125 if (!allReturnTypesMatch(Result))
126 return;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000127
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000128 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
129 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Piotr Padlewski08124b12016-12-14 15:29:23 +0000130 const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
Miklos Vajnae967a122018-10-21 19:16:25 +0000131 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
132 return;
133
Piotr Padlewski08124b12016-12-14 15:29:23 +0000134 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000135
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000136 if (IsPtrToPtr && IsMemberExpr) {
137 // Ignore this case (eg. Foo->get()->DoSomething());
138 return;
139 }
140
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000141 StringRef SmartptrText = Lexer::getSourceText(
142 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
Gabor Horvathafad84c2016-09-24 02:13:45 +0000143 *Result.SourceManager, getLangOpts());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000144 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000145 std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
Stephen Kelly43465bf2018-08-09 22:42:26 +0000146 diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000147 << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
148}
149
Alexander Kornienko35ddae42014-10-15 10:51:57 +0000150} // namespace readability
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000151} // namespace tidy
152} // namespace clang