10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/Lex/Lexer.h"
18internal::Matcher<Expr> callToGet(
const internal::Matcher<Decl> &OnClass) {
20 anyOf(cxxMemberCallExpr(
21 on(expr(anyOf(hasType(OnClass),
22 hasType(qualType(pointsTo(
23 decl(OnClass).bind(
"ptr_to_ptr"))))))
24 .bind(
"smart_pointer")),
26 memberExpr(hasObjectExpression(cxxThisExpr())))),
27 callee(cxxMethodDecl(hasName(
"get"),
28 returns(qualType(pointsTo(
29 type().bind(
"getType"))))))),
30 cxxDependentScopeMemberExpr(
33 expr(hasType(qualType(hasCanonicalType(
34 templateSpecializationType(hasDeclaration(
35 classTemplateDecl(has(cxxRecordDecl(
37 hasMethod(cxxMethodDecl(
41 "getType")))))))))))))))
42 .bind(
"smart_pointer")))))
43 .bind(
"redundant_get");
46internal::Matcher<Decl> knownSmartptr() {
47 return recordDecl(hasAnyName(
"::std::unique_ptr",
"::std::shared_ptr"));
50void registerMatchersForGetArrowStart(MatchFinder *Finder,
51 MatchFinder::MatchCallback *Callback) {
52 const auto MatchesOpArrow =
53 allOf(hasName(
"operator->"),
54 returns(qualType(pointsTo(type().bind(
"op->Type")))));
55 const auto MatchesOpStar =
56 allOf(hasName(
"operator*"),
57 returns(qualType(references(type().bind(
"op*Type")))));
58 const auto HasRelevantOps =
59 allOf(anyOf(hasMethod(MatchesOpArrow),
60 has(functionTemplateDecl(has(functionDecl(MatchesOpArrow))))),
61 anyOf(hasMethod(MatchesOpStar),
62 has(functionTemplateDecl(has(functionDecl(MatchesOpStar))))));
64 const auto QuacksLikeASmartptr =
65 cxxRecordDecl(cxxRecordDecl().bind(
"duck_typing"), HasRelevantOps);
68 const auto SmartptrAny = anyOf(knownSmartptr(), QuacksLikeASmartptr);
69 const auto SmartptrWithDeref = anyOf(
70 cxxRecordDecl(knownSmartptr(), HasRelevantOps), QuacksLikeASmartptr);
74 memberExpr(expr().bind(
"memberExpr"), isArrow(),
75 hasObjectExpression(callToGet(SmartptrWithDeref))),
80 unaryOperator(hasOperatorName(
"*"),
81 hasUnaryOperand(callToGet(SmartptrWithDeref))),
85 const auto CallToGetAsBool = callToGet(
86 recordDecl(SmartptrAny, has(cxxConversionDecl(returns(booleanType())))));
88 unaryOperator(hasOperatorName(
"!"), hasUnaryOperand(CallToGetAsBool)),
92 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
95 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
98 Finder->addMatcher(cxxDependentScopeMemberExpr(hasObjectExpression(
99 callExpr(has(callToGet(SmartptrAny))))),
103void registerMatchersForGetEquals(MatchFinder *Finder,
104 MatchFinder::MatchCallback *Callback) {
112 binaryOperator(hasAnyOperatorName(
"==",
"!="),
113 hasOperands(anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
114 integerLiteral(equals(0))),
115 callToGet(knownSmartptr()))),
125 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
129 registerMatchersForGetArrowStart(Finder,
this);
130 registerMatchersForGetEquals(Finder,
this);
134bool allReturnTypesMatch(
const MatchFinder::MatchResult &Result) {
135 if (Result.Nodes.getNodeAs<Decl>(
"duck_typing") ==
nullptr)
141 const Type *OpArrowType =
142 Result.Nodes.getNodeAs<Type>(
"op->Type")->getUnqualifiedDesugaredType();
143 const Type *OpStarType =
144 Result.Nodes.getNodeAs<Type>(
"op*Type")->getUnqualifiedDesugaredType();
145 const Type *GetType =
146 Result.Nodes.getNodeAs<Type>(
"getType")->getUnqualifiedDesugaredType();
147 return OpArrowType == OpStarType && OpArrowType == GetType;
152 if (!allReturnTypesMatch(Result))
155 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>(
"ptr_to_ptr") !=
nullptr;
156 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>(
"memberExpr") !=
nullptr;
157 const auto *GetCall = Result.Nodes.getNodeAs<Expr>(
"redundant_get");
158 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
161 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>(
"smart_pointer");
163 if (IsPtrToPtr && IsMemberExpr) {
168 auto SR = GetCall->getSourceRange();
171 if (isa<CXXDependentScopeMemberExpr>(GetCall))
172 SR.setEnd(Lexer::getLocForEndOfToken(SR.getEnd(), 0, *Result.SourceManager,
174 .getLocWithOffset(1));
176 StringRef SmartptrText = Lexer::getSourceText(
177 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
178 *Result.SourceManager, getLangOpts());
180 if (SmartptrText.ends_with(
"->")) {
181 SmartptrText = SmartptrText.drop_back(2);
184 std::string Replacement = Twine(IsPtrToPtr ?
"*" :
"", SmartptrText).str();
185 diag(GetCall->getBeginLoc(),
"redundant get() call on smart pointer")
186 << FixItHint::CreateReplacement(SR, Replacement);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
llvm::StringMap< ClangTidyValue > OptionMap