11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Basic/IdentifierTable.h"
21using matchers::hasUnevaluatedContext;
24 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
25 return InnerMatcher.matches(Node.getNonPackExpansionType(), Finder, Builder);
29 const ast_matchers::internal::Matcher<QualType> Inner =
30 possiblyPackExpansionOf(
31 qualType(rValueReferenceType(),
32 references(templateTypeParmType(
33 hasDeclaration(templateTypeParmDecl()))),
34 unless(references(qualType(isConstQualified())))));
35 if (!Inner.matches(Node.getType(), Finder, Builder))
38 const auto *
Function = dyn_cast<FunctionDecl>(Node.getDeclContext());
42 const FunctionTemplateDecl *FuncTemplate =
43 Function->getDescribedFunctionTemplate();
47 const QualType ParamType =
48 Node.getType().getNonPackExpansionType()->getPointeeType();
54 if (Node.isExplicitObjectParameter())
55 if (
const auto *TTPT = ParamType->getAs<TemplateTypeParmType>())
56 if (
const auto *Decl = TTPT->getDecl(); Decl && Decl->hasTypeConstraint())
59 const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
63 return TemplateType->getDepth() ==
64 FuncTemplate->getTemplateParameters()->getDepth();
67AST_MATCHER_P(NamedDecl, hasSameNameAsBoundNode, std::string, BindingID) {
68 const IdentifierInfo *II = Node.getIdentifier();
71 const StringRef Name = II->getName();
73 return Builder->removeBindings(
74 [
this, Name](
const ast_matchers::internal::BoundNodesMap &Nodes) {
75 const DynTypedNode &BN = Nodes.getNode(this->BindingID);
76 if (
const auto *ND = BN.get<NamedDecl>()) {
77 if (!isa<FieldDecl, CXXMethodDecl, VarDecl>(ND))
79 return ND->getName() != Name;
85AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) {
86 return Node.getCaptureKind() == Kind;
89AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
90 return Node.getCaptureDefault() == Kind;
94 const IdentifierInfo *ID = Node.getIdentifier();
95 return ID !=
nullptr && !ID->isPlaceholder();
101 auto RefToParmImplicit = allOf(
102 equalsBoundNode(
"var"), hasInitializer(ignoringParenImpCasts(
103 declRefExpr(to(equalsBoundNode(
"param"))))));
104 auto RefToParm = capturesVar(
105 varDecl(anyOf(hasSameNameAsBoundNode(
"param"), RefToParmImplicit)));
108 allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
109 unless(hasAnyCapture(
110 capturesVar(varDecl(hasSameNameAsBoundNode(
"param"))))));
111 auto CaptureByRefExplicit = hasAnyCapture(
112 allOf(hasCaptureKind(LambdaCaptureKind::LCK_ByRef), RefToParm));
114 auto CapturedInBody = lambdaExpr(anyOf(CaptureInRef, CaptureByRefExplicit));
115 auto CapturedInCaptureList = hasAnyCapture(capturesVar(
116 varDecl(hasInitializer(ignoringParenImpCasts(equalsBoundNode(
"call"))))));
118 auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
120 hasParent(lambdaExpr(forCallable(equalsBoundNode(
"func")),
121 anyOf(CapturedInCaptureList, CapturedInBody)))));
123 auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode(
"param")));
125 auto ForwardCallMatcher = callExpr(
126 callExpr().bind(
"call"), argumentCountIs(1),
127 hasArgument(0, declRefExpr(to(varDecl().bind(
"var")))),
129 anyOf(allOf(equalsBoundNode(
"func"),
130 functionDecl(hasAnyParameter(parmVarDecl(allOf(
131 equalsBoundNode(
"param"), equalsBoundNode(
"var")))))),
133 callee(unresolvedLookupExpr(hasAnyDeclaration(
134 namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
136 unless(anyOf(hasAncestor(typeLoc()),
137 hasAncestor(expr(hasUnevaluatedContext())))));
141 parmVarDecl().bind(
"param"), hasIdentifier(),
142 unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
143 hasAncestor(functionDecl().bind(
"func")),
144 hasAncestor(functionDecl(
145 isDefinition(), equalsBoundNode(
"func"), ToParam,
146 unless(anyOf(isDeleted(), hasDescendant(ForwardCallMatcher)))))),
151 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
156 diag(Param->getLocation(),
157 "forwarding reference parameter %0 is never forwarded "
158 "inside the function body")
165 ForwardFunction(Options.get(
"ForwardFunction",
"::std::forward")) {}
168 Options.store(Opts,
"ForwardFunction", ForwardFunction);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
MissingStdForwardCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap