clang-tools 22.0.0git
CrtpConstructorAccessibilityCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "../utils/LexerUtils.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17static bool hasPrivateConstructor(const CXXRecordDecl *RD) {
18 return llvm::any_of(RD->ctors(), [](const CXXConstructorDecl *Ctor) {
19 return Ctor->getAccess() == AS_private;
20 });
21}
22
23static bool isDerivedParameterBefriended(const CXXRecordDecl *CRTP,
24 const NamedDecl *Param) {
25 return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
26 const TypeSourceInfo *const FriendType = Friend->getFriendType();
27 if (!FriendType)
28 return false;
29
30 const auto *const TTPT =
31 dyn_cast<TemplateTypeParmType>(FriendType->getType());
32
33 return TTPT && TTPT->getDecl() == Param;
34 });
35}
36
37static bool isDerivedClassBefriended(const CXXRecordDecl *CRTP,
38 const CXXRecordDecl *Derived) {
39 return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
40 const TypeSourceInfo *const FriendType = Friend->getFriendType();
41 if (!FriendType)
42 return false;
43
44 return declaresSameEntity(FriendType->getType()->getAsCXXRecordDecl(),
45 Derived);
46 });
47}
48
49static const NamedDecl *
50getDerivedParameter(const ClassTemplateSpecializationDecl *CRTP,
51 const CXXRecordDecl *Derived) {
52 size_t Idx = 0;
53 const bool AnyOf = llvm::any_of(
54 CRTP->getTemplateArgs().asArray(), [&](const TemplateArgument &Arg) {
55 ++Idx;
56 return Arg.getKind() == TemplateArgument::Type &&
57 declaresSameEntity(Arg.getAsType()->getAsCXXRecordDecl(),
58 Derived);
59 });
60
61 return AnyOf ? CRTP->getSpecializedTemplate()
62 ->getTemplateParameters()
63 ->getParam(Idx - 1)
64 : nullptr;
65}
66
67static std::vector<FixItHint>
68hintMakeCtorPrivate(const CXXConstructorDecl *Ctor,
69 const std::string &OriginalAccess) {
70 std::vector<FixItHint> Hints;
71
72 Hints.emplace_back(FixItHint::CreateInsertion(
73 Ctor->getBeginLoc().getLocWithOffset(-1), "private:\n"));
74
75 const ASTContext &ASTCtx = Ctor->getASTContext();
76 const SourceLocation CtorEndLoc =
77 Ctor->isExplicitlyDefaulted()
78 ? utils::lexer::findNextTerminator(Ctor->getEndLoc(),
79 ASTCtx.getSourceManager(),
80 ASTCtx.getLangOpts())
81 : Ctor->getEndLoc();
82 Hints.emplace_back(FixItHint::CreateInsertion(
83 CtorEndLoc.getLocWithOffset(1), '\n' + OriginalAccess + ':' + '\n'));
84
85 return Hints;
86}
87
89 Finder->addMatcher(
90 classTemplateSpecializationDecl(
91 decl().bind("crtp"),
92 hasAnyTemplateArgument(refersToType(recordType(hasDeclaration(
93 cxxRecordDecl(
94 isDerivedFrom(cxxRecordDecl(equalsBoundNode("crtp"))))
95 .bind("derived")))))),
96 this);
97}
98
100 const MatchFinder::MatchResult &Result) {
101 const auto *CRTPInstantiation =
102 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("crtp");
103 const auto *DerivedRecord = Result.Nodes.getNodeAs<CXXRecordDecl>("derived");
104 const CXXRecordDecl *CRTPDeclaration =
105 CRTPInstantiation->getSpecializedTemplate()->getTemplatedDecl();
106
107 if (!CRTPDeclaration->hasDefinition())
108 return;
109
110 const auto *DerivedTemplateParameter =
111 getDerivedParameter(CRTPInstantiation, DerivedRecord);
112
113 assert(DerivedTemplateParameter &&
114 "No template parameter corresponds to the derived class of the CRTP.");
115
116 const bool NeedsFriend =
117 !isDerivedParameterBefriended(CRTPDeclaration,
118 DerivedTemplateParameter) &&
119 !isDerivedClassBefriended(CRTPDeclaration, DerivedRecord);
120
121 const FixItHint HintFriend = FixItHint::CreateInsertion(
122 CRTPDeclaration->getBraceRange().getEnd(),
123 "friend " + DerivedTemplateParameter->getNameAsString() + ';' + '\n');
124
125 if (hasPrivateConstructor(CRTPDeclaration) && NeedsFriend) {
126 diag(CRTPDeclaration->getLocation(),
127 "the CRTP cannot be constructed from the derived class; consider "
128 "declaring the derived class as friend")
129 << HintFriend;
130 }
131
132 auto WithFriendHintIfNeeded = [&](const DiagnosticBuilder &Diag,
133 bool NeedsFriend) {
134 if (NeedsFriend)
135 Diag << HintFriend;
136 };
137
138 if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
139 const bool IsStruct = CRTPDeclaration->isStruct();
140
141 WithFriendHintIfNeeded(
142 diag(CRTPDeclaration->getLocation(),
143 "the implicit default constructor of the CRTP is publicly "
144 "accessible; consider making it private%select{| and declaring "
145 "the derived class as friend}0")
146 << NeedsFriend
147 << FixItHint::CreateInsertion(
148 CRTPDeclaration->getBraceRange().getBegin().getLocWithOffset(
149 1),
150 (IsStruct ? "\nprivate:\n" : "\n") +
151 CRTPDeclaration->getNameAsString() + "() = default;\n" +
152 (IsStruct ? "public:\n" : "")),
153 NeedsFriend);
154 }
155
156 for (auto &&Ctor : CRTPDeclaration->ctors()) {
157 if (Ctor->getAccess() == AS_private || Ctor->isDeleted())
158 continue;
159
160 const bool IsPublic = Ctor->getAccess() == AS_public;
161 const std::string Access = IsPublic ? "public" : "protected";
162
163 WithFriendHintIfNeeded(
164 diag(Ctor->getLocation(),
165 "%0 constructor allows the CRTP to be %select{inherited "
166 "from|constructed}1 as a regular template class; consider making "
167 "it private%select{| and declaring the derived class as friend}2")
168 << Access << IsPublic << NeedsFriend
169 << hintMakeCtorPrivate(Ctor, Access),
170 NeedsFriend);
171 }
172}
173
175 const LangOptions &LangOpts) const {
176 return LangOpts.CPlusPlus11;
177}
178} // namespace clang::tidy::bugprone
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static bool hasPrivateConstructor(const CXXRecordDecl *RD)
static std::vector< FixItHint > hintMakeCtorPrivate(const CXXConstructorDecl *Ctor, const std::string &OriginalAccess)
static bool isDerivedParameterBefriended(const CXXRecordDecl *CRTP, const NamedDecl *Param)
static bool isDerivedClassBefriended(const CXXRecordDecl *CRTP, const CXXRecordDecl *Derived)
static const NamedDecl * getDerivedParameter(const ClassTemplateSpecializationDecl *CRTP, const CXXRecordDecl *Derived)
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)