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 ast_matchers::internal::Matcher<QualType> Inner = possiblyPackExpansionOf(
30 qualType(rValueReferenceType(),
31 references(templateTypeParmType(
32 hasDeclaration(templateTypeParmDecl()))),
33 unless(references(qualType(isConstQualified())))));
34 if (!Inner.matches(Node.getType(), Finder, Builder))
37 const auto *
Function = dyn_cast<FunctionDecl>(Node.getDeclContext());
41 const FunctionTemplateDecl *FuncTemplate =
42 Function->getDescribedFunctionTemplate();
47 Node.getType().getNonPackExpansionType()->getPointeeType();
48 const auto *TemplateType = ParamType->getAsCanonical<TemplateTypeParmType>();
52 return TemplateType->getDepth() ==
53 FuncTemplate->getTemplateParameters()->getDepth();
56AST_MATCHER_P(NamedDecl, hasSameNameAsBoundNode, std::string, BindingID) {
57 IdentifierInfo *II = Node.getIdentifier();
60 StringRef Name = II->getName();
62 return Builder->removeBindings(
63 [
this, Name](
const ast_matchers::internal::BoundNodesMap &Nodes) {
64 const DynTypedNode &BN = Nodes.getNode(this->BindingID);
65 if (
const auto *ND = BN.get<NamedDecl>()) {
66 if (!isa<FieldDecl, CXXMethodDecl, VarDecl>(ND))
68 return ND->getName() != Name;
74AST_MATCHER_P(LambdaCapture, hasCaptureKind, LambdaCaptureKind, Kind) {
75 return Node.getCaptureKind() == Kind;
78AST_MATCHER_P(LambdaExpr, hasCaptureDefaultKind, LambdaCaptureDefault, Kind) {
79 return Node.getCaptureDefault() == Kind;
83 const IdentifierInfo *ID = Node.getIdentifier();
84 return ID !=
nullptr && !ID->isPlaceholder();
90 auto RefToParmImplicit = allOf(
91 equalsBoundNode(
"var"), hasInitializer(ignoringParenImpCasts(
92 declRefExpr(to(equalsBoundNode(
"param"))))));
93 auto RefToParm = capturesVar(
94 varDecl(anyOf(hasSameNameAsBoundNode(
"param"), RefToParmImplicit)));
97 allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
99 capturesVar(varDecl(hasSameNameAsBoundNode(
"param"))))));
100 auto CaptureByRefExplicit = hasAnyCapture(
101 allOf(hasCaptureKind(LambdaCaptureKind::LCK_ByRef), RefToParm));
103 auto CapturedInBody = lambdaExpr(anyOf(CaptureInRef, CaptureByRefExplicit));
104 auto CapturedInCaptureList = hasAnyCapture(capturesVar(
105 varDecl(hasInitializer(ignoringParenImpCasts(equalsBoundNode(
"call"))))));
107 auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
109 hasParent(lambdaExpr(forCallable(equalsBoundNode(
"func")),
110 anyOf(CapturedInCaptureList, CapturedInBody)))));
112 auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode(
"param")));
114 auto ForwardCallMatcher = callExpr(
115 callExpr().bind(
"call"), argumentCountIs(1),
116 hasArgument(0, declRefExpr(to(varDecl().bind(
"var")))),
118 anyOf(allOf(equalsBoundNode(
"func"),
119 functionDecl(hasAnyParameter(parmVarDecl(allOf(
120 equalsBoundNode(
"param"), equalsBoundNode(
"var")))))),
122 callee(unresolvedLookupExpr(hasAnyDeclaration(
123 namedDecl(hasUnderlyingDecl(hasName(ForwardFunction)))))),
125 unless(anyOf(hasAncestor(typeLoc()),
126 hasAncestor(expr(hasUnevaluatedContext())))));
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(), hasDescendant(ForwardCallMatcher)))))),
140 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
145 diag(Param->getLocation(),
146 "forwarding reference parameter %0 is never forwarded "
147 "inside the function body")
154 ForwardFunction(Options.get(
"ForwardFunction",
"::std::forward")) {}
157 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