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 QuacksLikeASmartptr = recordDecl(
53 recordDecl().bind(
"duck_typing"),
54 has(cxxMethodDecl(hasName(
"operator->"),
55 returns(qualType(pointsTo(type().bind(
"op->Type")))))),
56 has(cxxMethodDecl(hasName(
"operator*"), returns(qualType(references(
57 type().bind(
"op*Type")))))));
60 const auto Smartptr = anyOf(knownSmartptr(), QuacksLikeASmartptr);
63 Finder->addMatcher(memberExpr(expr().bind(
"memberExpr"), isArrow(),
64 hasObjectExpression(callToGet(Smartptr))),
69 unaryOperator(hasOperatorName(
"*"), hasUnaryOperand(callToGet(Smartptr))),
73 const auto CallToGetAsBool = callToGet(
74 recordDecl(Smartptr, has(cxxConversionDecl(returns(booleanType())))));
76 unaryOperator(hasOperatorName(
"!"), hasUnaryOperand(CallToGetAsBool)),
80 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
83 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
86 Finder->addMatcher(cxxDependentScopeMemberExpr(hasObjectExpression(
87 callExpr(has(callToGet(Smartptr))).bind(
"obj"))),
91void registerMatchersForGetEquals(MatchFinder *Finder,
92 MatchFinder::MatchCallback *Callback) {
100 binaryOperator(hasAnyOperatorName(
"==",
"!="),
101 hasOperands(anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
102 integerLiteral(equals(0))),
103 callToGet(knownSmartptr()))),
117 registerMatchersForGetArrowStart(Finder,
this);
118 registerMatchersForGetEquals(Finder,
this);
122bool allReturnTypesMatch(
const MatchFinder::MatchResult &Result) {
123 if (Result.Nodes.getNodeAs<
Decl>(
"duck_typing") ==
nullptr)
129 const Type *OpArrowType =
130 Result.Nodes.getNodeAs<
Type>(
"op->Type")->getUnqualifiedDesugaredType();
131 const Type *OpStarType =
132 Result.Nodes.getNodeAs<
Type>(
"op*Type")->getUnqualifiedDesugaredType();
133 const Type *GetType =
134 Result.Nodes.getNodeAs<
Type>(
"getType")->getUnqualifiedDesugaredType();
135 return OpArrowType == OpStarType && OpArrowType == GetType;
140 if (!allReturnTypesMatch(Result))
143 bool IsPtrToPtr = Result.Nodes.getNodeAs<
Decl>(
"ptr_to_ptr") !=
nullptr;
144 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>(
"memberExpr") !=
nullptr;
145 const auto *GetCall = Result.Nodes.getNodeAs<Expr>(
"redundant_get");
146 if (GetCall->getBeginLoc().isMacroID() && IgnoreMacros)
149 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>(
"smart_pointer");
151 if (IsPtrToPtr && IsMemberExpr) {
156 auto SR = GetCall->getSourceRange();
159 if (isa<CXXDependentScopeMemberExpr>(GetCall))
160 SR.setEnd(Lexer::getLocForEndOfToken(SR.getEnd(), 0, *Result.SourceManager,
162 .getLocWithOffset(1));
164 StringRef SmartptrText = Lexer::getSourceText(
165 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
168 if (SmartptrText.ends_with(
"->")) {
169 SmartptrText = SmartptrText.drop_back(2);
172 std::string Replacement = Twine(IsPtrToPtr ?
"*" :
"", SmartptrText).str();
173 diag(GetCall->getBeginLoc(),
"redundant get() call on smart pointer")
174 << FixItHint::CreateReplacement(SR, Replacement);
const FunctionDecl * Decl
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
llvm::StringMap< ClangTidyValue > OptionMap