11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Lex/Lexer.h" 18 namespace readability {
21 internal::Matcher<Expr> callToGet(
const internal::Matcher<Decl> &OnClass) {
22 return cxxMemberCallExpr(
23 on(expr(anyOf(hasType(OnClass),
25 pointsTo(decl(OnClass).bind(
"ptr_to_ptr"))))))
26 .bind(
"smart_pointer")),
27 unless(callee(memberExpr(hasObjectExpression(cxxThisExpr())))),
30 returns(qualType(pointsTo(type().bind(
"getType")))))))
31 .bind(
"redundant_get");
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")))))));
44 Finder->addMatcher(memberExpr(expr().bind(
"memberExpr"), isArrow(),
45 hasObjectExpression(ignoringImpCasts(
46 callToGet(QuacksLikeASmartptr)))),
51 unaryOperator(hasOperatorName(
"*"),
52 hasUnaryOperand(callToGet(QuacksLikeASmartptr))),
56 const auto CallToGetAsBool = ignoringParenImpCasts(callToGet(recordDecl(
57 QuacksLikeASmartptr, has(cxxConversionDecl(returns(booleanType()))))));
59 unaryOperator(hasOperatorName(
"!"), hasUnaryOperand(CallToGetAsBool)),
63 Finder->addMatcher(ifStmt(hasCondition(CallToGetAsBool)), Callback);
66 Finder->addMatcher(conditionalOperator(hasCondition(CallToGetAsBool)),
70 void registerMatchersForGetEquals(MatchFinder *Finder,
71 MatchFinder::MatchCallback *Callback) {
77 const auto IsAKnownSmartptr =
78 recordDecl(hasAnyName(
"::std::unique_ptr",
"::std::shared_ptr"));
82 binaryOperator(anyOf(hasOperatorName(
"=="), hasOperatorName(
"!=")),
83 hasEitherOperand(ignoringImpCasts(
84 anyOf(cxxNullPtrLiteralExpr(), gnuNullExpr(),
85 integerLiteral(equals(0))))),
86 hasEitherOperand(callToGet(IsAKnownSmartptr))),
94 void RedundantSmartptrGetCheck::registerMatchers(MatchFinder *Finder) {
97 if (!getLangOpts().CPlusPlus)
100 registerMatchersForGetArrowStart(Finder,
this);
101 registerMatchersForGetEquals(Finder,
this);
105 bool allReturnTypesMatch(
const MatchFinder::MatchResult &Result) {
106 if (Result.Nodes.getNodeAs<Decl>(
"duck_typing") ==
nullptr)
112 const Type *OpArrowType =
113 Result.Nodes.getNodeAs<Type>(
"op->Type")->getUnqualifiedDesugaredType();
114 const Type *OpStarType =
115 Result.Nodes.getNodeAs<Type>(
"op*Type")->getUnqualifiedDesugaredType();
116 const Type *GetType =
117 Result.Nodes.getNodeAs<Type>(
"getType")->getUnqualifiedDesugaredType();
118 return OpArrowType == OpStarType && OpArrowType == GetType;
122 void RedundantSmartptrGetCheck::check(
const MatchFinder::MatchResult &Result) {
123 if (!allReturnTypesMatch(Result))
126 bool IsPtrToPtr = Result.Nodes.getNodeAs<Decl>(
"ptr_to_ptr") !=
nullptr;
127 bool IsMemberExpr = Result.Nodes.getNodeAs<Expr>(
"memberExpr") !=
nullptr;
128 const auto *GetCall = Result.Nodes.getNodeAs<Expr>(
"redundant_get");
129 const auto *Smartptr = Result.Nodes.getNodeAs<Expr>(
"smart_pointer");
131 if (IsPtrToPtr && IsMemberExpr) {
136 StringRef SmartptrText = Lexer::getSourceText(
137 CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
138 *Result.SourceManager, getLangOpts());
140 std::string Replacement = Twine(IsPtrToPtr ?
"*" :
"", SmartptrText).str();
141 diag(GetCall->getLocStart(),
"redundant get() call on smart pointer")
142 << FixItHint::CreateReplacement(GetCall->getSourceRange(), Replacement);
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//