Skip to content

Commit 0ceaa75

Browse files
committed
comments
1 parent eaa77d3 commit 0ceaa75

File tree

2 files changed

+30
-60
lines changed

2 files changed

+30
-60
lines changed

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,11 @@ void CrtpConstructorAccessibilityCheck::check(
111111

112112
if (hasPrivateConstructor(CRTPDeclaration) && NeedsFriend) {
113113
diag(CRTPDeclaration->getLocation(),
114-
"the CRTP cannot be constructed from the derived class")
115-
<< CRTPDeclaration << HintFriend;
116-
diag(CRTPDeclaration->getLocation(),
117-
"consider declaring the derived class as friend", DiagnosticIDs::Note);
114+
"the CRTP cannot be constructed from the derived class; consider "
115+
"declaring the derived class as friend")
116+
<< HintFriend;
118117
}
119118

120-
auto DiagNoteFriendPrivate = [&](const SourceLocation &Loc, bool Friend) {
121-
return diag(Loc,
122-
"consider making it private%select{| and declaring the derived "
123-
"class as friend}0",
124-
DiagnosticIDs::Note)
125-
<< Friend;
126-
};
127-
128119
auto WithFriendHintIfNeeded =
129120
[&](const DiagnosticBuilder &Diag,
130121
bool NeedsFriend) -> const DiagnosticBuilder & {
@@ -140,17 +131,16 @@ void CrtpConstructorAccessibilityCheck::check(
140131
WithFriendHintIfNeeded(
141132
diag(CRTPDeclaration->getLocation(),
142133
"the implicit default constructor of the CRTP is publicly "
143-
"accessible")
144-
<< CRTPDeclaration
134+
"accessible; consider making it private%select{| and declaring "
135+
"the derived class as friend}0")
136+
<< NeedsFriend
145137
<< FixItHint::CreateInsertion(
146138
CRTPDeclaration->getBraceRange().getBegin().getLocWithOffset(
147139
1),
148140
(IsStruct ? "\nprivate:\n" : "\n") +
149141
CRTPDeclaration->getNameAsString() + "() = default;\n" +
150142
(IsStruct ? "public:\n" : "")),
151143
NeedsFriend);
152-
153-
DiagNoteFriendPrivate(CRTPDeclaration->getLocation(), NeedsFriend);
154144
}
155145

156146
for (auto &&Ctor : CRTPDeclaration->ctors()) {
@@ -163,11 +153,11 @@ void CrtpConstructorAccessibilityCheck::check(
163153
WithFriendHintIfNeeded(
164154
diag(Ctor->getLocation(),
165155
"%0 contructor allows the CRTP to be %select{inherited "
166-
"from|constructed}1 as a regular template class")
167-
<< Access << IsPublic << Ctor << hintMakeCtorPrivate(Ctor, Access),
156+
"from|constructed}1 as a regular template class; consider making "
157+
"it private%select{| and declaring the derived class as friend}2")
158+
<< Access << IsPublic << NeedsFriend
159+
<< hintMakeCtorPrivate(Ctor, Access),
168160
NeedsFriend);
169-
170-
DiagNoteFriendPrivate(Ctor->getLocation(), NeedsFriend);
171161
}
172162
}
173163

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

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace class_implicit_ctor {
44
template <typename T>
55
class CRTP {};
6-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
7-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider making it private and declaring the derived class as friend
6+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
87
// CHECK-FIXES: CRTP() = default;
98
// CHECK-FIXES: friend T;
109

@@ -14,8 +13,7 @@ class A : CRTP<A> {};
1413
namespace class_unconstructible {
1514
template <typename T>
1615
class CRTP {
17-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
18-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider declaring the derived class as friend
16+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class; consider declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
1917
// CHECK-FIXES: friend T;
2018
CRTP() = default;
2119
};
@@ -28,8 +26,7 @@ template <typename T>
2826
class CRTP {
2927
public:
3028
CRTP() = default;
31-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class [bugprone-crtp-constructor-accessibility]
32-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
29+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
3330
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
3431
// CHECK-FIXES: friend T;
3532
};
@@ -42,8 +39,7 @@ template <typename T>
4239
class CRTP {
4340
public:
4441
CRTP(int) {}
45-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class [bugprone-crtp-constructor-accessibility]
46-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
42+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
4743
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
4844
// CHECK-FIXES: friend T;
4945
};
@@ -56,12 +52,10 @@ template <typename T>
5652
class CRTP {
5753
public:
5854
CRTP(int) {}
59-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class [bugprone-crtp-constructor-accessibility]
60-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
55+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
6156
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
6257
CRTP(float) {}
63-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class [bugprone-crtp-constructor-accessibility]
64-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
58+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
6559
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) {}{{[[:space:]]*}}public:
6660

6761
// CHECK-FIXES: friend T;
@@ -76,16 +70,13 @@ template <typename T>
7670
class CRTP {
7771
protected:
7872
CRTP(int) {}
79-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class [bugprone-crtp-constructor-accessibility]
80-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
73+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
8174
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}protected:
8275
CRTP() = default;
83-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class [bugprone-crtp-constructor-accessibility]
84-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
76+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
8577
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}protected:
8678
CRTP(float) {}
87-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class [bugprone-crtp-constructor-accessibility]
88-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
79+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows the CRTP to be inherited from as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
8980
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) {}{{[[:space:]]*}}protected:
9081

9182
// CHECK-FIXES: friend T;
@@ -99,8 +90,7 @@ class A : CRTP<A> {};
9990
namespace struct_implicit_ctor {
10091
template <typename T>
10192
struct CRTP {};
102-
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
103-
// CHECK-MESSAGES: :[[@LINE-2]]:8: note: consider making it private and declaring the derived class as friend
93+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
10494
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
10595
// CHECK-FIXES: friend T;
10696

@@ -111,8 +101,7 @@ namespace struct_default_ctor {
111101
template <typename T>
112102
struct CRTP {
113103
CRTP() = default;
114-
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class [bugprone-crtp-constructor-accessibility]
115-
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: consider making it private and declaring the derived class as friend
104+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the CRTP to be constructed as a regular template class; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
116105
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
117106
// CHECK-FIXES: friend T;
118107
};
@@ -123,15 +112,13 @@ class A : CRTP<A> {};
123112
namespace same_class_multiple_crtps {
124113
template <typename T>
125114
struct CRTP {};
126-
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
127-
// CHECK-MESSAGES: :[[@LINE-2]]:8: note: consider making it private and declaring the derived class as friend
115+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
128116
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = default;{{[[:space:]]*}}public:
129117
// CHECK-FIXES: friend T;
130118

131119
template <typename T>
132120
struct CRTP2 {};
133-
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
134-
// CHECK-MESSAGES: :[[@LINE-2]]:8: note: consider making it private and declaring the derived class as friend
121+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
135122
// CHECK-FIXES: private:{{[[:space:]]*}}CRTP2() = default;{{[[:space:]]*}}public:
136123
// CHECK-FIXES: friend T;
137124

@@ -141,8 +128,7 @@ class A : CRTP<A>, CRTP2<A> {};
141128
namespace same_crtp_multiple_classes {
142129
template <typename T>
143130
class CRTP {
144-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
145-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider declaring the derived class as friend
131+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class; consider declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
146132
// CHECK-FIXES: friend T;
147133
CRTP() = default;
148134
};
@@ -154,8 +140,7 @@ class B : CRTP<B> {};
154140
namespace crtp_template {
155141
template <typename T, typename U>
156142
class CRTP {
157-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
158-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider declaring the derived class as friend
143+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class; consider declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
159144
// CHECK-FIXES: friend U;
160145
CRTP() = default;
161146
};
@@ -166,8 +151,7 @@ class A : CRTP<int, A> {};
166151
namespace crtp_template2 {
167152
template <typename T, typename U>
168153
class CRTP {
169-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
170-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider declaring the derived class as friend
154+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class; consider declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
171155
// CHECK-FIXES: friend T;
172156
CRTP() = default;
173157
};
@@ -178,8 +162,7 @@ class A : CRTP<A, A> {};
178162
namespace template_derived {
179163
template <typename T>
180164
class CRTP {};
181-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
182-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider making it private and declaring the derived class as friend
165+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
183166
// CHECK-FIXES: CRTP() = default;
184167
// CHECK-FIXES: friend T;
185168

@@ -196,8 +179,7 @@ void foo() {
196179
namespace template_derived_explicit_specialization {
197180
template <typename T>
198181
class CRTP {};
199-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
200-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider making it private and declaring the derived class as friend
182+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private and declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
201183
// CHECK-FIXES: CRTP() = default;
202184
// CHECK-FIXES: friend T;
203185

@@ -225,8 +207,7 @@ class A;
225207

226208
template <typename T>
227209
class CRTP {
228-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class [bugprone-crtp-constructor-accessibility]
229-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider declaring the derived class as friend
210+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the CRTP cannot be constructed from the derived class; consider declaring the derived class as friend [bugprone-crtp-constructor-accessibility]
230211
// CHECK-FIXES: friend T;
231212
CRTP() = default;
232213
friend A;
@@ -241,8 +222,7 @@ class A;
241222

242223
template <typename T>
243224
class CRTP {
244-
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible [bugprone-crtp-constructor-accessibility]
245-
// CHECK-MESSAGES: :[[@LINE-2]]:7: note: consider making it private
225+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: the implicit default constructor of the CRTP is publicly accessible; consider making it private [bugprone-crtp-constructor-accessibility]
246226
// CHECK-FIXES: CRTP() = default;
247227
friend A;
248228
};

0 commit comments

Comments
 (0)