Skip to content

Commit 26d0055

Browse files
committed
prevent crashes
1 parent 0ceaa75 commit 26d0055

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ static bool hasPrivateConstructor(const CXXRecordDecl *RD) {
2424
static bool isDerivedParameterBefriended(const CXXRecordDecl *CRTP,
2525
const NamedDecl *Param) {
2626
return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
27-
const auto *TTPT =
28-
dyn_cast<TemplateTypeParmType>(Friend->getFriendType()->getType());
27+
const TypeSourceInfo *const FriendType = Friend->getFriendType();
28+
if (!FriendType) {
29+
return false;
30+
}
31+
32+
const auto *const TTPT =
33+
dyn_cast<TemplateTypeParmType>(FriendType->getType());
2934

3035
return TTPT && TTPT->getDecl() == Param;
3136
});
@@ -34,7 +39,12 @@ static bool isDerivedParameterBefriended(const CXXRecordDecl *CRTP,
3439
static bool isDerivedClassBefriended(const CXXRecordDecl *CRTP,
3540
const CXXRecordDecl *Derived) {
3641
return llvm::any_of(CRTP->friends(), [&](const FriendDecl *Friend) {
37-
return Friend->getFriendType()->getType()->getAsCXXRecordDecl() == Derived;
42+
const TypeSourceInfo *const FriendType = Friend->getFriendType();
43+
if (!FriendType) {
44+
return false;
45+
}
46+
47+
return FriendType->getType()->getAsCXXRecordDecl() == Derived;
3848
});
3949
}
4050

@@ -95,6 +105,10 @@ void CrtpConstructorAccessibilityCheck::check(
95105
const CXXRecordDecl *CRTPDeclaration =
96106
CRTPInstantiation->getSpecializedTemplate()->getTemplatedDecl();
97107

108+
if (!CRTPDeclaration->hasDefinition()) {
109+
return;
110+
}
111+
98112
const auto *DerivedTemplateParameter =
99113
getDerivedParameter(CRTPInstantiation, DerivedRecord);
100114

0 commit comments

Comments
 (0)