10#include "../utils/Matchers.h"
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())))));
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->getAs<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();
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 != NULL && !
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)));
95 auto HasRefToParm = hasAnyCapture(RefToParm);
98 allOf(hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByRef),
100 capturesVar(varDecl(hasSameNameAsBoundNode(
"param"))))));
101 auto CaptureInCopy = allOf(
102 hasCaptureDefaultKind(LambdaCaptureDefault::LCD_ByCopy), HasRefToParm);
103 auto CaptureByRefExplicit = hasAnyCapture(
104 allOf(hasCaptureKind(LambdaCaptureKind::LCK_ByRef), RefToParm));
106 auto CapturedInBody =
107 lambdaExpr(anyOf(CaptureInRef, CaptureInCopy, CaptureByRefExplicit));
108 auto CapturedInCaptureList = hasAnyCapture(capturesVar(
109 varDecl(hasInitializer(ignoringParenImpCasts(equalsBoundNode(
"call"))))));
111 auto CapturedInLambda = hasDeclContext(cxxRecordDecl(
113 hasParent(lambdaExpr(forCallable(equalsBoundNode(
"func")),
114 anyOf(CapturedInCaptureList, CapturedInBody)))));
116 auto ToParam = hasAnyParameter(parmVarDecl(equalsBoundNode(
"param")));
118 auto ForwardCallMatcher = callExpr(
119 callExpr().bind(
"call"), argumentCountIs(1),
120 hasArgument(0, declRefExpr(to(varDecl().bind(
"var")))),
122 anyOf(allOf(equalsBoundNode(
"func"),
123 functionDecl(hasAnyParameter(parmVarDecl(allOf(
124 equalsBoundNode(
"param"), equalsBoundNode(
"var")))))),
126 callee(unresolvedLookupExpr(hasAnyDeclaration(
127 namedDecl(hasUnderlyingDecl(hasName(
"::std::forward")))))),
129 unless(anyOf(hasAncestor(typeLoc()),
130 hasAncestor(expr(hasUnevaluatedContext())))));
134 parmVarDecl().bind(
"param"), hasIdentifier(),
135 unless(hasAttr(attr::Kind::Unused)), isTemplateTypeParameter(),
136 hasAncestor(functionDecl().bind(
"func")),
137 hasAncestor(functionDecl(
138 isDefinition(), equalsBoundNode(
"func"), ToParam,
139 unless(anyOf(isDeleted(),
140 hasDescendant(std::move(ForwardCallMatcher))))))),
145 const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
150 diag(Param->getLocation(),
151 "forwarding reference parameter %0 is never forwarded "
152 "inside the function body")
std::pair< Context, Canceler > Inner
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
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.
AST_MATCHER_P(UserDefinedLiteral, hasLiteral, clang::ast_matchers::internal::Matcher< Expr >, InnerMatcher)
AST_MATCHER(CXXRecordDecl, hasPublicVirtualOrProtectedNonVirtualDestructor)