clang-tools 8.0.0

RedundantSmartptrGetCheck.cpp
Go to the documentation of this file.
1 //===--- RedundantSmartptrGetCheck.cpp - clang-tidy -----------------------===//
2 //
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
10 #include "RedundantSmartptrGetCheck.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19
20 namespace {
21 internal::Matcher<Expr> callToGet(const internal::Matcher<Decl> &OnClass) {
22 return cxxMemberCallExpr(
23 on(expr(anyOf(hasType(OnClass),
24 hasType(qualType(
25 pointsTo(decl(OnClass).bind("ptr_to_ptr"))))))
26 .bind("smart_pointer")),
27 unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))),
28 callee(cxxMethodDecl(
29 hasName("get"),
30 returns(qualType(pointsTo(type().bind("getType")))))))
31 .bind("redundant_get");
32 }
33
34 void registerMatchersForGetArrowStart(MatchFinder *Finder,
35 MatchFinder::MatchCallback *Callback) {
36 const auto QuacksLikeASmartptr = recordDecl(
37 recordDecl().bind("duck_typing"),
38 has(cxxMethodDecl(hasName("operator->"),
39 returns(qualType(pointsTo(type().bind("op->Type")))))),
40 has(cxxMethodDecl(hasName("operator*"), returns(qualType(references(
41 type().bind("op*Type")))))));
42
43 // Catch 'ptr.get()->Foo()'
44 Finder->addMatcher(memberExpr(expr().bind("memberExpr"), isArrow(),
45 hasObjectExpression(ignoringImpCasts(
46 callToGet(QuacksLikeASmartptr)))),
47 Callback);
48
49 // Catch '*ptr.get()' or '*ptr->get()'
50 Finder->addMatcher(
51 unaryOperator(hasOperatorName("*"),
52 hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
53 Callback);
54
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);
68 }
69
70 void 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
77 const auto IsAKnownSmartptr =
78 recordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
79
80 // Matches against nullptr.
81 Finder->addMatcher(
82 binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
83 hasEitherOperand(ignoringImpCasts(
84 anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
85 integerLiteral(equals(0))))),
86 hasEitherOperand(callToGet(IsAKnownSmartptr))),
87 Callback);
88
89 // FIXME: Match and fix if (l.get() == r.get()).
90 }
91
92 } // namespace
93
94 void RedundantSmartptrGetCheck::storeOptions(
95 ClangTidyOptions::OptionMap &Opts) {
96 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
97 }
98
99 void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
100 // 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
105 registerMatchersForGetArrowStart(Finder, this);
106 registerMatchersForGetEquals(Finder, this);
107 }
108
109 namespace {
110 bool allReturnTypesMatch(const MatchFinder::MatchResult &Result) {
111 if (Result.Nodes.getNodeAs<Decl>("duck_typing") == nullptr)
112 return true;
113 // 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 }
125 } // namespace
126
127 void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
128 if (!allReturnTypesMatch(Result))
129 return;
130
131 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>("ptr_to_ptr") != nullptr;
132 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>("memberExpr") != nullptr;
133 const auto *GetCall = Result.Nodes.getNodeAs<Expr>("redundant_get");
134 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
135 return;
136
137 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>("smart_pointer");
138
139 if (IsPtrToPtr && IsMemberExpr) {
140 // Ignore this case (eg. Foo->get()->DoSomething());
141 return;
142 }
143
144 StringRef SmartptrText = Lexer::getSourceText(
145 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
146 *Result.SourceManager, getLangOpts());
147 // Replace foo->get() with *foo, and foo.get() with foo.
148 std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
149 diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
150 << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
151 }
152
153 } // namespace readability
154 } // namespace tidy
155 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::clangd::Callback
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:29
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
RedundantSmartptrGetCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18