Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp

raw
1 //===--- ForwardingReferenceOverloadCheck.cpp - clang-tidy-----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "ForwardingReferenceOverloadCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include <algorithm>
13
14 using namespace clang::ast_matchers;
15
16 namespace clang::tidy::bugprone {
17
18 namespace {
19 // Check if the given type is related to std::enable_if.
20 AST_MATCHER(QualType, isEnableIf) {
21 auto CheckTemplate = [](const TemplateSpecializationType *Spec) {
22 if (!Spec || !Spec->getTemplateName().getAsTemplateDecl()) {
23 return false;
24 }
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"));
30 };
31 const Type *BaseType = Node.getTypePtr();
32 // Case: pointer or reference to enable_if.
33 while (BaseType->isPointerType() || BaseType->isReferenceType()) {
34 BaseType = BaseType->getPointeeType().getTypePtr();
35 }
36 // Case: type parameter dependent (enable_if<is_integral<T>>).
37 if (const auto *Dependent = BaseType->getAs<DependentNameType>()) {
38 BaseType = Dependent->getQualifier()->getAsType();
39 }
40 if (!BaseType)
41 return false;
42 if (CheckTemplate(BaseType->getAs<TemplateSpecializationType>()))
43 return true; // Case: enable_if_t< >.
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>())) {
48 return true; // Case: enable_if< >::type.
49 }
50 }
51 }
52 return false;
53 }
54 AST_MATCHER_P(TemplateTypeParmDecl, hasDefaultArgument,
55 clang::ast_matchers::internal::Matcher<QualType>, TypeMatcher) {
56 return Node.hasDefaultArgument() &&
57 TypeMatcher.matches(Node.getDefaultArgument(), Finder, Builder);
58 }
59 } // namespace
60
61 void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
62 auto ForwardingRefParm =
63 parmVarDecl(
64 hasType(qualType(rValueReferenceType(),
65 references(templateTypeParmType(hasDeclaration(
66 templateTypeParmDecl().bind("type-parm-decl")))),
67 unless(references(isConstQualified())))))
68 .bind("parm-var");
69
70 DeclarationMatcher FindOverload =
71 cxxConstructorDecl(
72 hasParameter(0, ForwardingRefParm),
73 unless(hasAnyParameter(
74 // No warning: enable_if as constructor parameter.
75 parmVarDecl(hasType(isEnableIf())))),
76 unless(hasParent(functionTemplateDecl(anyOf(
77 // No warning: enable_if as type parameter.
78 has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
79 // No warning: enable_if as non-type template parameter.
80 has(nonTypeTemplateParmDecl(
81 hasType(isEnableIf()),
82 anyOf(hasDescendant(cxxBoolLiteral()),
83 hasDescendant(cxxNullPtrLiteralExpr()),
84 hasDescendant(integerLiteral())))))))))
85 .bind("ctor");
86 Finder->addMatcher(FindOverload, this);
87 }
88
89 void ForwardingReferenceOverloadCheck::check(
90 const MatchFinder::MatchResult &Result) {
91 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>("parm-var");
92 const auto *TypeParmDecl =
93 Result.Nodes.getNodeAs<TemplateTypeParmDecl>("type-parm-decl");
94
95 // Get the FunctionDecl and FunctionTemplateDecl containing the function
96 // parameter.
97 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
98 if (!FuncForParam)
99 return;
100 const FunctionTemplateDecl *FuncTemplate =
101 FuncForParam->getDescribedFunctionTemplate();
102 if (!FuncTemplate)
103 return;
104
105 // Check that the template type parameter belongs to the same function
106 // template as the function parameter of that type. (This implies that type
107 // deduction will happen on the type.)
108 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
109 if (!llvm::is_contained(*Params, TypeParmDecl))
110 return;
111
112 // Every parameter after the first must have a default value.
113 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
114 for (const auto *Param : llvm::drop_begin(Ctor->parameters())) {
115 if (!Param->hasDefaultArg())
116 return;
117 }
118 bool EnabledCopy = false, DisabledCopy = false, EnabledMove = false,
119 DisabledMove = false;
120 for (const auto *OtherCtor : Ctor->getParent()->ctors()) {
121 if (OtherCtor->isCopyOrMoveConstructor()) {
122 if (OtherCtor->isDeleted() || OtherCtor->getAccess() == AS_private)
123 (OtherCtor->isCopyConstructor() ? DisabledCopy : DisabledMove) = true;
124 else
125 (OtherCtor->isCopyConstructor() ? EnabledCopy : EnabledMove) = true;
126 }
127 }
128 bool Copy = (!EnabledMove && !DisabledMove && !DisabledCopy) || EnabledCopy;
129 bool Move = !DisabledMove || EnabledMove;
130 if (!Copy && !Move)
131 return;
132 diag(Ctor->getLocation(),
133 "constructor accepting a forwarding reference can "
134 "hide the %select{copy|move|copy and move}0 constructor%s1")
135 << (Copy && Move ? 2 : (Copy ? 0 : 1)) << Copy + Move;
136 for (const auto *OtherCtor : Ctor->getParent()->ctors()) {
137 if (OtherCtor->isCopyOrMoveConstructor() && !OtherCtor->isDeleted() &&
138 OtherCtor->getAccess() != AS_private) {
139 diag(OtherCtor->getLocation(),
140 "%select{copy|move}0 constructor declared here", DiagnosticIDs::Note)
141 << OtherCtor->isMoveConstructor();
142 }
143 }
144 }
145
146 } // namespace clang::tidy::bugprone