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().equals(
"enable_if") ||
29 TypeDecl->getName().equals(
"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() &&
57 TypeMatcher.matches(
Node.getDefaultArgument(), Finder,
Builder);
59AST_MATCHER(TemplateDecl, hasAssociatedConstraints) {
60 return Node.hasAssociatedConstraints();
65 auto ForwardingRefParm =
67 hasType(qualType(rValueReferenceType(),
68 references(templateTypeParmType(hasDeclaration(
69 templateTypeParmDecl().bind(
"type-parm-decl")))),
70 unless(references(isConstQualified())))))
73 DeclarationMatcher FindOverload =
75 hasParameter(0, ForwardingRefParm),
76 unless(hasAnyParameter(
78 parmVarDecl(hasType(isEnableIf())))),
79 unless(hasParent(functionTemplateDecl(anyOf(
82 hasAssociatedConstraints(),
84 has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
86 has(nonTypeTemplateParmDecl(
87 hasType(isEnableIf()),
88 anyOf(hasDescendant(cxxBoolLiteral()),
89 hasDescendant(cxxNullPtrLiteralExpr()),
90 hasDescendant(integerLiteral())))))))))
92 Finder->addMatcher(FindOverload,
this);
96 const MatchFinder::MatchResult &Result) {
97 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>(
"parm-var");
98 const auto *TypeParmDecl =
99 Result.Nodes.getNodeAs<TemplateTypeParmDecl>(
"type-parm-decl");
103 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
106 const FunctionTemplateDecl *FuncTemplate =
107 FuncForParam->getDescribedFunctionTemplate();
114 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
115 if (!llvm::is_contained(*Params, TypeParmDecl))
119 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>(
"ctor");
120 for (
const auto *Param : llvm::drop_begin(Ctor->parameters())) {
121 if (!Param->hasDefaultArg())
124 bool EnabledCopy =
false, DisabledCopy =
false, EnabledMove =
false,
125 DisabledMove =
false;
126 for (
const auto *OtherCtor : Ctor->getParent()->ctors()) {
127 if (OtherCtor->isCopyOrMoveConstructor()) {
128 if (OtherCtor->isDeleted() || OtherCtor->getAccess() == AS_private)
129 (OtherCtor->isCopyConstructor() ? DisabledCopy : DisabledMove) =
true;
131 (OtherCtor->isCopyConstructor() ? EnabledCopy : EnabledMove) =
true;
134 bool Copy = (!EnabledMove && !DisabledMove && !DisabledCopy) || EnabledCopy;
135 bool Move = !DisabledMove || EnabledMove;
138 diag(Ctor->getLocation(),
139 "constructor accepting a forwarding reference can "
140 "hide the %select{copy|move|copy and move}0 constructor%s1")
142 for (
const auto *OtherCtor : Ctor->getParent()->ctors()) {
143 if (OtherCtor->isCopyOrMoveConstructor() && !OtherCtor->isDeleted() &&
144 OtherCtor->getAccess() != AS_private) {
145 diag(OtherCtor->getLocation(),
146 "%select{copy|move}0 constructor declared here", DiagnosticIDs::Note)
147 << 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)