39 const CXXRecordDecl *Derived) {
40 return llvm::any_of(CRTP->friends(), [&](
const FriendDecl *Friend) {
41 const TypeSourceInfo *const FriendType = Friend->getFriendType();
46 return declaresSameEntity(FriendType->getType()->getAsCXXRecordDecl(),
53 const CXXRecordDecl *Derived) {
55 const bool AnyOf = llvm::any_of(
56 CRTP->getTemplateArgs().asArray(), [&](
const TemplateArgument &Arg) {
58 return Arg.getKind() == TemplateArgument::Type &&
59 declaresSameEntity(Arg.getAsType()->getAsCXXRecordDecl(),
63 return AnyOf ? CRTP->getSpecializedTemplate()
64 ->getTemplateParameters()
71 const std::string &OriginalAccess) {
72 std::vector<FixItHint> Hints;
74 Hints.emplace_back(FixItHint::CreateInsertion(
75 Ctor->getBeginLoc().getLocWithOffset(-1),
"private:\n"));
77 const ASTContext &ASTCtx = Ctor->getASTContext();
78 const SourceLocation CtorEndLoc =
79 Ctor->isExplicitlyDefaulted()
81 ASTCtx.getSourceManager(),
84 Hints.emplace_back(FixItHint::CreateInsertion(
85 CtorEndLoc.getLocWithOffset(1),
'\n' + OriginalAccess +
':' +
'\n'));
102 const MatchFinder::MatchResult &Result) {
103 const auto *CRTPInstantiation =
104 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"crtp");
105 const auto *DerivedRecord = Result.Nodes.getNodeAs<CXXRecordDecl>(
"derived");
106 const CXXRecordDecl *CRTPDeclaration =
107 CRTPInstantiation->getSpecializedTemplate()->getTemplatedDecl();
109 if (!CRTPDeclaration->hasDefinition()) {
113 const auto *DerivedTemplateParameter =
116 assert(DerivedTemplateParameter &&
117 "No template parameter corresponds to the derived class of the CRTP.");
119 const bool NeedsFriend =
121 DerivedTemplateParameter) &&
124 const FixItHint HintFriend = FixItHint::CreateInsertion(
125 CRTPDeclaration->getBraceRange().getEnd(),
126 "friend " + DerivedTemplateParameter->getNameAsString() +
';' +
'\n');
129 diag(CRTPDeclaration->getLocation(),
130 "the CRTP cannot be constructed from the derived class; consider "
131 "declaring the derived class as friend")
135 auto WithFriendHintIfNeeded = [&](
const DiagnosticBuilder &Diag,
141 if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
142 const bool IsStruct = CRTPDeclaration->isStruct();
144 WithFriendHintIfNeeded(
145 diag(CRTPDeclaration->getLocation(),
146 "the implicit default constructor of the CRTP is publicly "
147 "accessible; consider making it private%select{| and declaring "
148 "the derived class as friend}0")
150 << FixItHint::CreateInsertion(
151 CRTPDeclaration->getBraceRange().getBegin().getLocWithOffset(
153 (IsStruct ?
"\nprivate:\n" :
"\n") +
154 CRTPDeclaration->getNameAsString() +
"() = default;\n" +
155 (IsStruct ?
"public:\n" :
"")),
159 for (
auto &&Ctor : CRTPDeclaration->ctors()) {
160 if (Ctor->getAccess() == AS_private || Ctor->isDeleted())
163 const bool IsPublic = Ctor->getAccess() == AS_public;
164 const std::string Access = IsPublic ?
"public" :
"protected";
166 WithFriendHintIfNeeded(
167 diag(Ctor->getLocation(),
168 "%0 constructor allows the CRTP to be %select{inherited "
169 "from|constructed}1 as a regular template class; consider making "
170 "it private%select{| and declaring the derived class as friend}2")
171 << Access << IsPublic << NeedsFriend