| 1 | //===--- MissingStdForwardCheck.cpp - clang-tidy --------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MissingStdForwardCheck.h" |
| 10 | #include "../utils/Matchers.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Basic/IdentifierTable.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang::tidy::cppcoreguidelines { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | using matchers::hasUnevaluatedContext; |
| 22 | |
| 23 | AST_MATCHER_P(QualType, possiblyPackExpansionOf, |
| 24 | ast_matchers::internal::Matcher<QualType>, InnerMatcher) { |
| 25 | return InnerMatcher.matches(Node: Node.getNonPackExpansionType(), Finder, Builder); |
| 26 | } |
| 27 | |
| 28 | AST_MATCHER(ParmVarDecl, isTemplateTypeParameter) { |
| 29 | ast_matchers::internal::Matcher<QualType> Inner = possiblyPackExpansionOf( |
| 30 | InnerMatcher: qualType(rValueReferenceType(), |
| 31 | references(InnerMatcher: templateTypeParmType( |
| 32 | hasDeclaration(InnerMatcher: templateTypeParmDecl()))), |
| 33 | unless(references(InnerMatcher: qualType(isConstQualified()))))); |
| 34 | if (!Inner.matches(Node: Node.getType(), Finder, Builder)) |
| 35 | return false; |
| 36 | |
| 37 | const auto *Function = dyn_cast<FunctionDecl>(Node.getDeclContext()); |
| 38 | if (!Function) |
| 39 | return false; |
| 40 | |
| 41 | const FunctionTemplateDecl *FuncTemplate = |
| 42 | Function->getDescribedFunctionTemplate(); |
| 43 | if (!FuncTemplate) |
| 44 | return false; |
| 45 | |
| 46 | QualType ParamType = |
| 47 | Node.getType().getNonPackExpansionType()->getPointeeType(); |
| 48 | const auto *TemplateType = ParamType->getAs<TemplateTypeParmType>(); |
| 49 | if (!TemplateType) |
| 50 | return false; |
| 51 | |
| 52 | return TemplateType->getDepth() == |
| 53 | FuncTemplate->getTemplateParameters()->getDepth(); |
| 54 | } |
| 55 | |
| 56 | AST_MATCHER_P(NamedDecl, hasSameNameAsBoundNode, std::string, BindingID) { |
| 57 | IdentifierInfo *II = Node.getIdentifier(); |
| 58 | if (nullptr == II) |
| 59 | return false; |
| 60 | StringRef Name = II->getName(); |
| 61 | |
| 62 | return Builder->removeBindings( |
| 63 | Predicate: [this, Name](const ast_matchers::internal::BoundNodesMap &Nodes) { |
| 64 | const DynTypedNode &BN = Nodes.getNode(ID: this->BindingID); |
| 65 | if (const auto *ND = BN.get<NamedDecl>()) { |
| 66 | if (!isa<FieldDecl, CXXMethodDecl, VarDecl>(Val: ND)) |
| 67 | return true; |
| 68 | return ND->getName() != Name; |
| 69 | } |
| 70 | return true; |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) { |
| 75 | return Node.getCaptureKind() == Kind; |
| 76 | } |
| 77 | |
| 78 | AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) { |
| 79 | return Node.getCaptureDefault() == Kind; |
| 80 | } |
| 81 | |
| 82 | AST_MATCHER(VarDecl, hasIdentifier) { |
| 83 | const IdentifierInfo *ID = Node.getIdentifier(); |
| 84 | return ID != NULL && !ID->isPlaceholder(); |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | void MissingStdForwardCheck::registerMatchers(MatchFinder *Finder) { |
| 90 | auto RefToParmImplicit = allOf( |
| 91 | equalsBoundNode(ID: "var" ), hasInitializer(InnerMatcher: ignoringParenImpCasts( |
| 92 | InnerMatcher: declRefExpr(to(InnerMatcher: equalsBoundNode(ID: "param" )))))); |
| 93 | auto RefToParm = capturesVar( |
| 94 | InnerMatcher: varDecl(anyOf(hasSameNameAsBoundNode(BindingID: "param" ), RefToParmImplicit))); |
| 95 | |
| 96 | auto CaptureInRef = |
| 97 | allOf(hasCaptureDefaultKind(Kind: LambdaCaptureDefault::LCD_ByRef), |
| 98 | unless(hasAnyCapture( |
| 99 | InnerMatcher: capturesVar(InnerMatcher: varDecl(hasSameNameAsBoundNode(BindingID: "param" )))))); |
| 100 | auto CaptureByRefExplicit = hasAnyCapture( |
| 101 | InnerMatcher: allOf(hasCaptureKind(Kind: LambdaCaptureKind::LCK_ByRef), RefToParm)); |
| 102 | |
| 103 | auto CapturedInBody = lambdaExpr(anyOf(CaptureInRef, CaptureByRefExplicit)); |
| 104 | auto CapturedInCaptureList = hasAnyCapture(InnerMatcher: capturesVar( |
| 105 | InnerMatcher: varDecl(hasInitializer(InnerMatcher: ignoringParenImpCasts(InnerMatcher: equalsBoundNode(ID: "call" )))))); |
| 106 | |
| 107 | auto CapturedInLambda = hasDeclContext(InnerMatcher: cxxRecordDecl( |
| 108 | isLambda(), |
| 109 | hasParent(lambdaExpr(forCallable(InnerMatcher: equalsBoundNode(ID: "func" )), |
| 110 | anyOf(CapturedInCaptureList, CapturedInBody))))); |
| 111 | |
| 112 | auto ToParam = hasAnyParameter(InnerMatcher: parmVarDecl(equalsBoundNode(ID: "param" ))); |
| 113 | |
| 114 | auto ForwardCallMatcher = callExpr( |
| 115 | callExpr().bind(ID: "call" ), argumentCountIs(N: 1), |
| 116 | hasArgument(N: 0, InnerMatcher: declRefExpr(to(InnerMatcher: varDecl().bind(ID: "var" )))), |
| 117 | forCallable( |
| 118 | InnerMatcher: anyOf(allOf(equalsBoundNode(ID: "func" ), |
| 119 | functionDecl(hasAnyParameter(InnerMatcher: parmVarDecl(allOf( |
| 120 | equalsBoundNode(ID: "param" ), equalsBoundNode(ID: "var" )))))), |
| 121 | CapturedInLambda)), |
| 122 | callee(InnerMatcher: unresolvedLookupExpr(hasAnyDeclaration( |
| 123 | InnerMatcher: namedDecl(hasUnderlyingDecl(InnerMatcher: hasName(Name: "::std::forward" )))))), |
| 124 | |
| 125 | unless(anyOf(hasAncestor(typeLoc()), |
| 126 | hasAncestor(expr(hasUnevaluatedContext()))))); |
| 127 | |
| 128 | Finder->addMatcher( |
| 129 | parmVarDecl( |
| 130 | parmVarDecl().bind("param" ), hasIdentifier(), |
| 131 | unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(), |
| 132 | hasAncestor(functionDecl().bind("func" )), |
| 133 | hasAncestor(functionDecl( |
| 134 | isDefinition(), equalsBoundNode("func" ), ToParam, |
| 135 | unless(anyOf(isDeleted(), |
| 136 | hasDescendant(std::move(ForwardCallMatcher))))))), |
| 137 | this); |
| 138 | } |
| 139 | |
| 140 | void MissingStdForwardCheck::check(const MatchFinder::MatchResult &Result) { |
| 141 | const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(ID: "param" ); |
| 142 | |
| 143 | if (!Param) |
| 144 | return; |
| 145 | |
| 146 | diag(Param->getLocation(), |
| 147 | "forwarding reference parameter %0 is never forwarded " |
| 148 | "inside the function body" ) |
| 149 | << Param; |
| 150 | } |
| 151 | |
| 152 | } // namespace clang::tidy::cppcoreguidelines |
| 153 | |