blob: 189130efcae5ab13ae5e878dc3ed976c5cd0d489 [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 {
Benjamin Kramer51a9cc92016-06-15 15:46:10 +000021internal::Matcher<Expr> callToGet(const 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())))),
Samuel Benzaquen1fb8bc72016-02-17 16:13:14 +000028 callee(cxxMethodDecl(
29 hasName("get"),
30 returns(qualType(pointsTo(type().bind("getType")))))))
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000031 .bind("redundant_get");
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000032}
33
34void registerMatchersForGetArrowStart(MatchFinder *Finder,
35 MatchFinder::MatchCallback *Callback) {
Samuel Benzaquen3a571012014-03-27 17:42:26 +000036 const auto QuacksLikeASmartptr = recordDecl(
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000037 recordDecl().bind("duck_typing"),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000038 has(cxxMethodDecl(hasName("operator->"),
39 returns(qualType(pointsTo(type().bind("op->Type")))))),
Samuel Benzaquen1fb8bc72016-02-17 16:13:14 +000040 has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
41 type().bind("op*Type")))))));
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);
Samuel Benzaquenc8148722018-01-15 18:03:20 +000054
55 // Catch '!ptr.get()'
56 const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(recordDecl(
57 QuacksLikeASmartptr, has(cxxConversionDecl(returns(booleanType()))))));
58 Finder->addMatcher(
59 unaryOperator(hasOperatorName("!"), hasUnaryOperand(CallToGetAsBool)),
60 Callback);
61
62 // Catch 'if(ptr.get())'
63 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
64
65 // Catch 'ptr.get() ? X : Y'
66 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
67 Callback);
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000068}
Samuel Benzaquen3a571012014-03-27 17:42:26 +000069
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000070void registerMatchersForGetEquals(MatchFinder *Finder,
71 MatchFinder::MatchCallback *Callback) {
72 // This one is harder to do with duck typing.
73 // The operator==/!= that we are looking for might be member or non-member,
74 // might be on global namespace or found by ADL, might be a template, etc.
75 // For now, lets keep a list of known standard types.
76
Kirill Bobyrev9559f042016-09-26 07:22:37 +000077 const auto IsAKnownSmartptr =
78 recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000079
80 // Matches against nullptr.
Samuel Benzaquen3a571012014-03-27 17:42:26 +000081 Finder->addMatcher(
Kirill Bobyrev9559f042016-09-26 07:22:37 +000082 binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
83 hasEitherOperand(ignoringImpCasts(
84 anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
85 integerLiteral(equals(0))))),
86 hasEitherOperand(callToGet(IsAKnownSmartptr))),
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000087 Callback);
Kirill Bobyrev9559f042016-09-26 07:22:37 +000088
Kirill Bobyrev9559f042016-09-26 07:22:37 +000089 // FIXME: Match and fix if (l.get() == r.get()).
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000090}
91
Kirill Bobyrev9559f042016-09-26 07:22:37 +000092} // namespace
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +000093
Miklos Vajnae967a122018-10-21 19:16:25 +000094void RedundantSmartptrGetCheck::storeOptions(
95 ClangTidyOptions::OptionMap &Opts) {
96 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
97}
98
Alexander Kornienko1b677db2015-03-09 12:18:39 +000099void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +0000100 // Only register the matchers for C++; the functionality currently does not
101 // provide any benefit to other languages, despite being benign.
102 if (!getLangOpts().CPlusPlus)
103 return;
104
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000105 registerMatchersForGetArrowStart(Finder, this);
106 registerMatchersForGetEquals(Finder, this);
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000107}
108
109namespace {
110bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000111 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
112 return true;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000113 // Verify that the types match.
114 // We can't do this on the matcher because the type nodes can be different,
115 // even though they represent the same type. This difference comes from how
116 // the type is referenced (eg. through a typedef, a type trait, etc).
117 const Type *OpArrowType =
118 Result.Nodes.getNodeAs<Type>("op->Type")->getUnqualifiedDesugaredType();
119 const Type *OpStarType =
120 Result.Nodes.getNodeAs<Type>("op*Type")->getUnqualifiedDesugaredType();
121 const Type *GetType =
122 Result.Nodes.getNodeAs<Type>("getType")->getUnqualifiedDesugaredType();
123 return OpArrowType == OpStarType && OpArrowType == GetType;
124}
Kirill Bobyrev9559f042016-09-26 07:22:37 +0000125} // namespace
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000126
Alexander Kornienko1b677db2015-03-09 12:18:39 +0000127void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
Kirill Bobyrev9559f042016-09-26 07:22:37 +0000128 if (!allReturnTypesMatch(Result))
129 return;
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000130
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000131 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
132 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
Piotr Padlewski08124b12016-12-14 15:29:23 +0000133 const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
Miklos Vajnae967a122018-10-21 19:16:25 +0000134 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
135 return;
136
Piotr Padlewski08124b12016-12-14 15:29:23 +0000137 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000138
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000139 if (IsPtrToPtr && IsMemberExpr) {
140 // Ignore this case (eg. Foo->get()->DoSomething());
141 return;
142 }
143
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000144 StringRef SmartptrText = Lexer::getSourceText(
145 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
Gabor Horvathafad84c2016-09-24 02:13:45 +0000146 *Result.SourceManager, getLangOpts());
Samuel Benzaquen110f3cc2014-04-09 14:17:23 +0000147 // Replace foo->get() with *foo, and foo.get() with foo.
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000148 std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
Stephen Kelly43465bf2018-08-09 22:42:26 +0000149 diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000150 << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
151}
152
Alexander Kornienko35ddae42014-10-15 10:51:57 +0000153} // namespace readability
Samuel Benzaquen3a571012014-03-27 17:42:26 +0000154} // namespace tidy
155} // namespace clang