10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
21 auto CheckTemplate = [](
const TemplateSpecializationType *Spec) {
22 if (!Spec || !Spec->getTemplateName().getAsTemplateDecl()) {
25 const NamedDecl *TypeDecl =
26 Spec->getTemplateName().getAsTemplateDecl()->getTemplatedDecl();
27 return TypeDecl->isInStdNamespace() &&
28 (TypeDecl->getName() ==
"enable_if" ||
29 TypeDecl->getName() ==
"enable_if_t");
31 const Type *BaseType =
Node.getTypePtr();
33 while (BaseType->isPointerType() || BaseType->isReferenceType()) {
34 BaseType = BaseType->getPointeeType().getTypePtr();
37 if (
const auto *Dependent = BaseType->getAs<DependentNameType>()) {
38 BaseType = Dependent->getQualifier()->getAsType();
42 if (CheckTemplate(BaseType->getAs<TemplateSpecializationType>()))
44 if (
const auto *Elaborated = BaseType->getAs<ElaboratedType>()) {
45 if (
const auto *Q = Elaborated->getQualifier())
46 if (
const auto *Qualifier = Q->getAsType()) {
47 if (CheckTemplate(Qualifier->getAs<TemplateSpecializationType>())) {
55 clang::ast_matchers::internal::Matcher<QualType>, TypeMatcher) {
56 return Node.hasDefaultArgument() &&
58 Node.getDefaultArgument().getArgument().getAsType(), Finder,
61AST_MATCHER(TemplateDecl, hasAssociatedConstraints) {
62 return Node.hasAssociatedConstraints();
67 auto ForwardingRefParm =
69 hasType(qualType(rValueReferenceType(),
70 references(templateTypeParmType(hasDeclaration(
71 templateTypeParmDecl().bind(
"type-parm-decl")))),
72 unless(references(isConstQualified())))))
75 DeclarationMatcher FindOverload =
77 hasParameter(0, ForwardingRefParm), unless(isDeleted()),
78 unless(hasAnyParameter(
80 parmVarDecl(hasType(isEnableIf())))),
81 unless(hasParent(functionTemplateDecl(anyOf(
84 hasAssociatedConstraints(),
86 has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
88 has(nonTypeTemplateParmDecl(
89 hasType(isEnableIf()),
90 anyOf(hasDescendant(cxxBoolLiteral()),
91 hasDescendant(cxxNullPtrLiteralExpr()),
92 hasDescendant(integerLiteral())))))))))
94 Finder->addMatcher(FindOverload,
this);
98 const MatchFinder::MatchResult &Result) {
99 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>(
"parm-var");
100 const auto *TypeParmDecl =
101 Result.Nodes.getNodeAs<TemplateTypeParmDecl>(
"type-parm-decl");
105 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
108 const FunctionTemplateDecl *FuncTemplate =
109 FuncForParam->getDescribedFunctionTemplate();
116 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
117 if (!llvm::is_contained(*Params, TypeParmDecl))
121 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
122 for (
const auto *Param : llvm::drop_begin(Ctor->parameters())) {
123 if (!Param->hasDefaultArg())
126 bool EnabledCopy =
false, DisabledCopy =
false, EnabledMove =
false,
127 DisabledMove =
false;
128 for (
const auto *OtherCtor : Ctor->getParent()->ctors()) {
129 if (OtherCtor->isCopyOrMoveConstructor()) {
130 if (OtherCtor->isDeleted() || OtherCtor->getAccess() == AS_private)
131 (OtherCtor->isCopyConstructor() ? DisabledCopy : DisabledMove) =
true;
133 (OtherCtor->isCopyConstructor() ? EnabledCopy : EnabledMove) =
true;
136 bool Copy = (!EnabledMove && !DisabledMove && !DisabledCopy) || EnabledCopy;
137 bool Move = !DisabledMove || EnabledMove;
140 diag(Ctor->getLocation(),
141 "constructor accepting a forwarding reference can "
142 "hide the %select{copy|move|copy and move}0 constructor%s1")
144 for (
const auto *OtherCtor : Ctor->getParent()->ctors()) {
145 if (OtherCtor->isCopyOrMoveConstructor() && !OtherCtor->isDeleted() &&
146 OtherCtor->getAccess() != AS_private) {
147 diag(OtherCtor->getLocation(),
148 "%select{copy|move}0 constructor declared here", DiagnosticIDs::Note)
149 << OtherCtor->isMoveConstructor();
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(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)