clang-tools 10.0.0

ForwardingReferenceOverloadCheck.cpp
Go to the documentation of this file.
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 {
17 namespace tidy {
18 namespace bugprone {
19
20 namespace {
21 // Check if the given type is related to std::enable_if.
22 AST_MATCHER(QualType, isEnableIf) {
23 auto CheckTemplate = [](const TemplateSpecializationType *Spec) {
24 if (!Spec || !Spec->getTemplateName().getAsTemplateDecl()) {
25 return false;
26 }
27 const NamedDecl *TypeDecl =
28 Spec->getTemplateName().getAsTemplateDecl()->getTemplatedDecl();
29 return TypeDecl->isInStdNamespace() &&
30 (TypeDecl->getName().equals("enable_if") ||
31 TypeDecl->getName().equals("enable_if_t"));
32 };
33 const Type *BaseType = Node.getTypePtr();
34 // Case: pointer or reference to enable_if.
35 while (BaseType->isPointerType() || BaseType->isReferenceType()) {
36 BaseType = BaseType->getPointeeType().getTypePtr();
37 }
38 // Case: type parameter dependent (enable_if<is_integral<T>>).
39 if (const auto *Dependent = BaseType->getAs<DependentNameType>()) {
40 BaseType = Dependent->getQualifier()->getAsType();
41 }
42 if (!BaseType)
43 return false;
44 if (CheckTemplate(BaseType->getAs<TemplateSpecializationType>())) {
45 return true; // Case: enable_if_t< >.
46 } else if (const auto *Elaborated = BaseType->getAs<ElaboratedType>()) {
47 if (const auto *Qualifier = Elaborated->getQualifier()->getAsType()) {
48 if (CheckTemplate(Qualifier->getAs<TemplateSpecializationType>())) {
49 return true; // Case: enable_if< >::type.
50 }
51 }
52 }
53 return false;
54 }
55 AST_MATCHER_P(TemplateTypeParmDecl, hasDefaultArgument,
56 clang::ast_matchers::internal::Matcher<QualType>, TypeMatcher) {
57 return Node.hasDefaultArgument() &&
58 TypeMatcher.matches(Node.getDefaultArgument(), Finder, Builder);
59 }
60 } // namespace
61
62 void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
63 // Forwarding references require C++11 or later.
64 if (!getLangOpts().CPlusPlus11)
65 return;
66
67 auto ForwardingRefParm =
68 parmVarDecl(
69 hasType(qualType(rValueReferenceType(),
70 references(templateTypeParmType(hasDeclaration(
71 templateTypeParmDecl().bind("type-parm-decl")))),
72 unless(references(isConstQualified())))))
73 .bind("parm-var");
74
75 DeclarationMatcher findOverload =
76 cxxConstructorDecl(
77 hasParameter(0, ForwardingRefParm),
78 unless(hasAnyParameter(
79 // No warning: enable_if as constructor parameter.
80 parmVarDecl(hasType(isEnableIf())))),
81 unless(hasParent(functionTemplateDecl(has(templateTypeParmDecl(
82 // No warning: enable_if as type parameter.
83 hasDefaultArgument(isEnableIf())))))))
84 .bind("ctor");
85 Finder->addMatcher(findOverload, this);
86 }
87
88 void ForwardingReferenceOverloadCheck::check(
89 const MatchFinder::MatchResult &Result) {
90 const auto *ParmVar = Result.Nodes.getNodeAs<ParmVarDecl>("parm-var");
91 const auto *TypeParmDecl =
92 Result.Nodes.getNodeAs<TemplateTypeParmDecl>("type-parm-decl");
93
94 // Get the FunctionDecl and FunctionTemplateDecl containing the function
95 // parameter.
96 const auto *FuncForParam = dyn_cast<FunctionDecl>(ParmVar->getDeclContext());
97 if (!FuncForParam)
98 return;
99 const FunctionTemplateDecl *FuncTemplate =
100 FuncForParam->getDescribedFunctionTemplate();
101 if (!FuncTemplate)
102 return;
103
104 // Check that the template type parameter belongs to the same function
105 // template as the function parameter of that type. (This implies that type
106 // deduction will happen on the type.)
107 const TemplateParameterList *Params = FuncTemplate->getTemplateParameters();
108 if (!llvm::is_contained(*Params, TypeParmDecl))
109 return;
110
111 // Every parameter after the first must have a default value.
112 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
113 for (auto Iter = Ctor->param_begin() + 1; Iter != Ctor->param_end(); ++Iter) {
114 if (!(*Iter)->hasDefaultArg())
115 return;
116 }
117 bool EnabledCopy = false, DisabledCopy = false, EnabledMove = false,
118 DisabledMove = false;
119 for (const auto *OtherCtor : Ctor->getParent()->ctors()) {
120 if (OtherCtor->isCopyOrMoveConstructor()) {
121 if (OtherCtor->isDeleted() || OtherCtor->getAccess() == AS_private)
122 (OtherCtor->isCopyConstructor() ? DisabledCopy : DisabledMove) = true;
123 else
124 (OtherCtor->isCopyConstructor() ? EnabledCopy : EnabledMove) = true;
125 }
126 }
127 bool Copy = (!EnabledMove && !DisabledMove && !DisabledCopy) || EnabledCopy;
128 bool Move = !DisabledMove || EnabledMove;
129 if (!Copy && !Move)
130 return;
131 diag(Ctor->getLocation(),
132 "constructor accepting a forwarding reference can "
133 "hide the %select{copy|move|copy and move}0 constructor%s1")
134 << (Copy && Move ? 2 : (Copy ? 0 : 1)) << Copy + Move;
135 for (const auto *OtherCtor : Ctor->getParent()->ctors()) {
136 if (OtherCtor->isCopyOrMoveConstructor() && !OtherCtor->isDeleted() &&
137 OtherCtor->getAccess() != AS_private) {
138 diag(OtherCtor->getLocation(),
139 "%select{copy|move}0 constructor declared here", DiagnosticIDs::Note)
140 << OtherCtor->isMoveConstructor();
141 }
142 }
143 }
144
145 } // namespace bugprone
146 } // namespace tidy
147 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
ForwardingReferenceOverloadCheck.h
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::tidy::readability::@556::Qualifier
Qualifier
Definition: QualifiedAutoCheck.cpp:29
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:52