Skip to content

Commit 24338d5

Browse files
committed
comments
1 parent 7e487de commit 24338d5

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,59 @@ using namespace clang::ast_matchers;
1616
namespace clang::tidy::bugprone {
1717

1818
static bool hasPrivateConstructor(const CXXRecordDecl *RD) {
19-
for (auto &&Ctor : RD->ctors()) {
20-
if (Ctor->getAccess() == AS_private)
21-
return true;
22-
}
23-
24-
return false;
19+
return llvm::any_of(RD->ctors(), [](const CXXConstructorDecl *Ctor) {
20+
return Ctor->getAccess() == AS_private;
21+
});
2522
}
2623

2724
static bool isDerivedParameterBefriended(const CXXRecordDecl *CRTP,
2825
const NamedDecl *Param) {
29-
for (auto &&Friend : CRTP->friends()) {
26+
return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
3027
const auto *TTPT =
3128
dyn_cast<TemplateTypeParmType>(Friend->getFriendType()->getType());
3229

33-
if (TTPT && TTPT->getDecl() == Param)
34-
return true;
35-
}
36-
37-
return false;
30+
return TTPT && TTPT->getDecl() == Param;
31+
});
3832
}
3933

4034
static bool isDerivedClassBefriended(const CXXRecordDecl *CRTP,
4135
const CXXRecordDecl *Derived) {
42-
for (auto &&Friend : CRTP->friends()) {
43-
if (Friend->getFriendType()->getType()->getAsCXXRecordDecl() == Derived)
44-
return true;
45-
}
46-
47-
return false;
36+
return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
37+
return Friend->getFriendType()->getType()->getAsCXXRecordDecl() == Derived;
38+
});
4839
}
4940

50-
static std::optional<const NamedDecl *>
41+
static const NamedDecl *
5142
getDerivedParameter(const ClassTemplateSpecializationDecl *CRTP,
5243
const CXXRecordDecl *Derived) {
5344
size_t Idx = 0;
54-
bool Found = false;
55-
for (auto &&TemplateArg : CRTP->getTemplateArgs().asArray()) {
56-
if (TemplateArg.getKind() == TemplateArgument::Type &&
57-
TemplateArg.getAsType()->getAsCXXRecordDecl() == Derived) {
58-
Found = true;
59-
break;
60-
}
61-
++Idx;
62-
}
63-
64-
if (!Found)
65-
return std::nullopt;
66-
67-
return CRTP->getSpecializedTemplate()->getTemplateParameters()->getParam(Idx);
45+
bool AnyOf = llvm::any_of(
46+
CRTP->getTemplateArgs().asArray(), [&](const TemplateArgument &Arg) {
47+
++Idx;
48+
return Arg.getKind() == TemplateArgument::Type &&
49+
Arg.getAsType()->getAsCXXRecordDecl() == Derived;
50+
});
51+
52+
return AnyOf ? CRTP->getSpecializedTemplate()
53+
->getTemplateParameters()
54+
->getParam(Idx - 1)
55+
: nullptr;
6856
}
6957

7058
static std::vector<FixItHint>
7159
hintMakeCtorPrivate(const CXXConstructorDecl *Ctor,
72-
const std::string &OriginalAccess, const SourceManager &SM,
73-
const LangOptions &LangOpts) {
60+
const std::string &OriginalAccess) {
7461
std::vector<FixItHint> Hints;
7562

7663
Hints.emplace_back(FixItHint::CreateInsertion(
7764
Ctor->getBeginLoc().getLocWithOffset(-1), "private:\n"));
7865

66+
const ASTContext &ASTCtx = Ctor->getASTContext();
7967
const SourceLocation CtorEndLoc =
8068
Ctor->isExplicitlyDefaulted()
81-
? utils::lexer::findNextTerminator(Ctor->getEndLoc(), SM, LangOpts)
69+
? utils::lexer::findNextTerminator(Ctor->getEndLoc(),
70+
ASTCtx.getSourceManager(),
71+
ASTCtx.getLangOpts())
8272
: Ctor->getEndLoc();
8373
Hints.emplace_back(FixItHint::CreateInsertion(
8474
CtorEndLoc.getLocWithOffset(1), '\n' + OriginalAccess + ':' + '\n'));
@@ -121,7 +111,10 @@ void CrtpConstructorAccessibilityCheck::check(
121111
}
122112

123113
const auto *DerivedTemplateParameter =
124-
*getDerivedParameter(CRTPInstantiation, DerivedRecord);
114+
getDerivedParameter(CRTPInstantiation, DerivedRecord);
115+
116+
assert(DerivedTemplateParameter &&
117+
"No template parameter corresponds to the derived class of the CRTP.");
125118

126119
if (hasPrivateConstructor(CRTPDeclaration) &&
127120
!isDerivedParameterBefriended(CRTPDeclaration,
@@ -148,9 +141,7 @@ void CrtpConstructorAccessibilityCheck::check(
148141
diag(Ctor->getLocation(),
149142
"%0 contructor allows the CRTP to be %select{inherited "
150143
"from|constructed}1 as a regular template class")
151-
<< Access << IsPublic << Ctor
152-
<< hintMakeCtorPrivate(Ctor, Access, *Result.SourceManager,
153-
getLangOpts());
144+
<< Access << IsPublic << Ctor << hintMakeCtorPrivate(Ctor, Access);
154145
diag(Ctor->getLocation(), "consider making it private",
155146
DiagnosticIDs::Note);
156147
}

clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CRTP {};
1010
class A : CRTP<A> {};
1111
} // namespace class_implicit_ctor
1212

13-
namespace class_uncostructible {
13+
namespace class_unconstructible {
1414
template <typename T>
1515
class CRTP {
1616
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
@@ -20,7 +20,7 @@ class CRTP {
2020
};
2121

2222
class A : CRTP<A> {};
23-
} // namespace class_uncostructible
23+
} // namespace class_unconstructible
2424

2525
namespace class_public_default_ctor {
2626
template <typename T>

0 commit comments

Comments
 (0)