1*89a1d03eSRichard // RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++17 -target x86_64-pc-linux-gnu \
2*89a1d03eSRichard // RUN: | FileCheck %s -check-prefix=CHECK-EXCEPTIONS \
3*89a1d03eSRichard // RUN: -implicit-check-not="{{warning|error}}:"
4*89a1d03eSRichard // RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -DNONEXCEPTIONS -fno-exceptions -std=c++17 -target x86_64-pc-linux-gnu \
5*89a1d03eSRichard // RUN: | FileCheck %s -allow-empty -check-prefix=CHECK-NONEXCEPTIONS \
6*89a1d03eSRichard // RUN: -implicit-check-not="{{warning|error}}:"
7*89a1d03eSRichard
8*89a1d03eSRichard struct S {
9*89a1d03eSRichard S() noexcept(false);
10*89a1d03eSRichard };
11*89a1d03eSRichard
12*89a1d03eSRichard struct T {
13*89a1d03eSRichard T() noexcept;
14*89a1d03eSRichard };
15*89a1d03eSRichard
16*89a1d03eSRichard struct U {
UU17*89a1d03eSRichard U() {}
18*89a1d03eSRichard };
19*89a1d03eSRichard
20*89a1d03eSRichard struct V {
VV21*89a1d03eSRichard explicit V(const char *) {} // Can throw
22*89a1d03eSRichard };
23*89a1d03eSRichard
24*89a1d03eSRichard struct Cleanup {
~CleanupCleanup25*89a1d03eSRichard ~Cleanup() {}
26*89a1d03eSRichard };
27*89a1d03eSRichard
28*89a1d03eSRichard struct W {
29*89a1d03eSRichard W(Cleanup c = {}) noexcept(false);
30*89a1d03eSRichard };
31*89a1d03eSRichard
32*89a1d03eSRichard struct X {
33*89a1d03eSRichard X(S = {}) noexcept;
34*89a1d03eSRichard };
35*89a1d03eSRichard
36*89a1d03eSRichard struct Y {
37*89a1d03eSRichard S s;
38*89a1d03eSRichard };
39*89a1d03eSRichard
40*89a1d03eSRichard struct Z {
41*89a1d03eSRichard T t;
42*89a1d03eSRichard };
43*89a1d03eSRichard
44*89a1d03eSRichard int f();
45*89a1d03eSRichard int g() noexcept(false);
46*89a1d03eSRichard int h() noexcept(true);
47*89a1d03eSRichard
48*89a1d03eSRichard struct UserConv_Bad {
49*89a1d03eSRichard operator int() noexcept(false);
50*89a1d03eSRichard };
51*89a1d03eSRichard
52*89a1d03eSRichard struct UserConv_Good {
53*89a1d03eSRichard operator int() noexcept;
54*89a1d03eSRichard };
55*89a1d03eSRichard
56*89a1d03eSRichard UserConv_Bad some_bad_func() noexcept;
57*89a1d03eSRichard UserConv_Good some_good_func() noexcept;
58*89a1d03eSRichard
59*89a1d03eSRichard S s;
60*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 's' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
61*89a1d03eSRichard // CHECK-EXCEPTIONS: 9:3: note: possibly throwing constructor declared here
62*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
63*89a1d03eSRichard T t; // ok
64*89a1d03eSRichard U u;
65*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'u' with static storage duration may throw an exception that cannot be caught
66*89a1d03eSRichard // CHECK-EXCEPTIONS: 17:3: note: possibly throwing constructor declared here
67*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
68*89a1d03eSRichard V v("v");
69*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'v' with static storage duration may throw an exception that cannot be caught
70*89a1d03eSRichard // CHECK-EXCEPTIONS: 21:12: note: possibly throwing constructor declared here
71*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
72*89a1d03eSRichard W w;
73*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'w' with static storage duration may throw an exception that cannot be caught
74*89a1d03eSRichard // CHECK-EXCEPTIONS: 29:3: note: possibly throwing constructor declared here
75*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
76*89a1d03eSRichard X x1(S{});
77*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'x1' with static storage duration may throw an exception that cannot be caught
78*89a1d03eSRichard // CHECK-EXCEPTIONS: 9:3: note: possibly throwing constructor declared here
79*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
80*89a1d03eSRichard X x2;
81*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'x2' with static storage duration may throw an exception that cannot be caught
82*89a1d03eSRichard // CHECK-EXCEPTIONS: 9:3: note: possibly throwing constructor declared here
83*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
84*89a1d03eSRichard Y y;
85*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'y' with static storage duration may throw an exception that cannot be caught
86*89a1d03eSRichard // CHECK-EXCEPTIONS: 36:8: note: possibly throwing constructor declared here
87*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
88*89a1d03eSRichard Z z;
89*89a1d03eSRichard
90*89a1d03eSRichard int i = f();
91*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:5: warning: initialization of 'i' with static storage duration may throw an exception that cannot be caught
92*89a1d03eSRichard // CHECK-EXCEPTIONS: 44:5: note: possibly throwing function declared here
93*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
94*89a1d03eSRichard int j = g();
95*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:5: warning: initialization of 'j' with static storage duration may throw an exception that cannot be caught
96*89a1d03eSRichard // CHECK-EXCEPTIONS: 45:5: note: possibly throwing function declared here
97*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
98*89a1d03eSRichard int k = h();
99*89a1d03eSRichard int l = some_bad_func();
100*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:5: warning: initialization of 'l' with static storage duration may throw an exception that cannot be caught
101*89a1d03eSRichard // CHECK-EXCEPTIONS: 49:3: note: possibly throwing function declared here
102*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
103*89a1d03eSRichard int m = some_good_func();
104*89a1d03eSRichard
105*89a1d03eSRichard typedef decltype(sizeof(int)) size_t;
operator new(size_t sz,void * here)106*89a1d03eSRichard inline void *operator new(size_t sz, void *here) noexcept { return here; }
107*89a1d03eSRichard char n[sizeof(int)];
108*89a1d03eSRichard int *o = new (n) int();
109*89a1d03eSRichard int *p = new int();
110*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'p' with static storage duration may throw an exception that cannot be caught
111*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
112*89a1d03eSRichard
113*89a1d03eSRichard thread_local S s3;
114*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 's3' with thread_local storage duration may throw an exception that cannot be caught
115*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
116*89a1d03eSRichard thread_local T t3; // ok
117*89a1d03eSRichard thread_local U u3;
118*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'u3' with thread_local storage duration may throw an exception that cannot be caught
119*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
120*89a1d03eSRichard thread_local V v3("v");
121*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'v3' with thread_local storage duration may throw an exception that cannot be caught
122*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
123*89a1d03eSRichard thread_local W w3;
124*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'w3' with thread_local storage duration may throw an exception that cannot be caught
125*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
126*89a1d03eSRichard
f(S s1,T t1,U u1,V v1,W w1)127*89a1d03eSRichard void f(S s1, T t1, U u1, V v1, W w1) { // ok, ok, ok, ok, ok
128*89a1d03eSRichard S s2; // ok
129*89a1d03eSRichard T t2; // ok
130*89a1d03eSRichard U u2; // ok
131*89a1d03eSRichard V v2("v"); // ok
132*89a1d03eSRichard W w2; // ok
133*89a1d03eSRichard
134*89a1d03eSRichard thread_local S s3; // ok
135*89a1d03eSRichard thread_local T t3; // ok
136*89a1d03eSRichard thread_local U u3; // ok
137*89a1d03eSRichard thread_local V v3("v"); // ok
138*89a1d03eSRichard thread_local W w3; // ok
139*89a1d03eSRichard
140*89a1d03eSRichard static S s4; // ok
141*89a1d03eSRichard static T t4; // ok
142*89a1d03eSRichard static U u4; // ok
143*89a1d03eSRichard static V v4("v"); // ok
144*89a1d03eSRichard static W w4; // ok
145*89a1d03eSRichard }
146*89a1d03eSRichard
147*89a1d03eSRichard namespace {
148*89a1d03eSRichard S s;
149*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 's' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
150*89a1d03eSRichard // CHECK-EXCEPTIONS: 9:3: note: possibly throwing constructor declared here
151*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
152*89a1d03eSRichard T t; // ok
153*89a1d03eSRichard U u;
154*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'u' with static storage duration may throw an exception that cannot be caught
155*89a1d03eSRichard // CHECK-EXCEPTIONS: 17:3: note: possibly throwing constructor declared here
156*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
157*89a1d03eSRichard V v("v");
158*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'v' with static storage duration may throw an exception that cannot be caught
159*89a1d03eSRichard // CHECK-EXCEPTIONS: 21:12: note: possibly throwing constructor declared here
160*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
161*89a1d03eSRichard W w;
162*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:3: warning: initialization of 'w' with static storage duration may throw an exception that cannot be caught
163*89a1d03eSRichard // CHECK-EXCEPTIONS: 29:3: note: possibly throwing constructor declared here
164*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
165*89a1d03eSRichard
166*89a1d03eSRichard thread_local S s3;
167*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 's3' with thread_local storage duration may throw an exception that cannot be caught
168*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
169*89a1d03eSRichard thread_local T t3; // ok
170*89a1d03eSRichard thread_local U u3;
171*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'u3' with thread_local storage duration may throw an exception that cannot be caught
172*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
173*89a1d03eSRichard thread_local V v3("v");
174*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'v3' with thread_local storage duration may throw an exception that cannot be caught
175*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
176*89a1d03eSRichard thread_local W w3;
177*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:16: warning: initialization of 'w3' with thread_local storage duration may throw an exception that cannot be caught
178*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
179*89a1d03eSRichard }; // namespace
180*89a1d03eSRichard
181*89a1d03eSRichard class Statics {
182*89a1d03eSRichard static S s; // warn when initialized
183*89a1d03eSRichard static T t; // ok
184*89a1d03eSRichard static U u; // warn when initialized
185*89a1d03eSRichard static V v; // warn when initialized
186*89a1d03eSRichard static W w; // warn when initialized
187*89a1d03eSRichard
f(S s,T t,U u,V v)188*89a1d03eSRichard void f(S s, T t, U u, V v) {
189*89a1d03eSRichard S s2; // ok
190*89a1d03eSRichard T t2; // ok
191*89a1d03eSRichard U u2; // ok
192*89a1d03eSRichard V v2("v"); // ok
193*89a1d03eSRichard W w2; // ok
194*89a1d03eSRichard
195*89a1d03eSRichard thread_local S s3; // ok
196*89a1d03eSRichard thread_local T t3; // ok
197*89a1d03eSRichard thread_local U u3; // ok
198*89a1d03eSRichard thread_local V v3("v"); // ok
199*89a1d03eSRichard thread_local W w3; // ok
200*89a1d03eSRichard
201*89a1d03eSRichard static S s4; // ok
202*89a1d03eSRichard static T t4; // ok
203*89a1d03eSRichard static U u4; // ok
204*89a1d03eSRichard static V v4("v"); // ok
205*89a1d03eSRichard static W w4; // ok
206*89a1d03eSRichard }
207*89a1d03eSRichard };
208*89a1d03eSRichard
209*89a1d03eSRichard S Statics::s;
210*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:12: warning: initialization of 's' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
211*89a1d03eSRichard // CHECK-EXCEPTIONS: 9:3: note: possibly throwing constructor declared here
212*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
213*89a1d03eSRichard T Statics::t;
214*89a1d03eSRichard U Statics::u;
215*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:12: warning: initialization of 'u' with static storage duration may throw an exception that cannot be caught
216*89a1d03eSRichard // CHECK-EXCEPTIONS: 17:3: note: possibly throwing constructor declared here
217*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
218*89a1d03eSRichard V Statics::v("v");
219*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:12: warning: initialization of 'v' with static storage duration may throw an exception that cannot be caught
220*89a1d03eSRichard // CHECK-EXCEPTIONS: 21:12: note: possibly throwing constructor declared here
221*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
222*89a1d03eSRichard W Statics::w;
223*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:12: warning: initialization of 'w' with static storage duration may throw an exception that cannot be caught
224*89a1d03eSRichard // CHECK-EXCEPTIONS: 29:3: note: possibly throwing constructor declared here
225*89a1d03eSRichard // CHECK-NONEXCEPTIONS-NOT: warning:
226*89a1d03eSRichard
227*89a1d03eSRichard #ifndef NONEXCEPTIONS
228*89a1d03eSRichard namespace pr35457 {
foo(int x)229*89a1d03eSRichard constexpr int foo(int x) { if (x <= 0) throw 12; return x; }
230*89a1d03eSRichard
231*89a1d03eSRichard constexpr int bar = foo(1); // OK
232*89a1d03eSRichard // CHECK-EXCEPTIONS-NOT: warning: initialization of 'bar' with static storage
233*89a1d03eSRichard int baz = foo(0); // Not OK; throws at runtime when exceptions are enabled.
234*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:5: warning: initialization of 'baz' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
235*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-6]]:15: note: possibly throwing function declared here
236*89a1d03eSRichard } // namespace pr35457
237*89a1d03eSRichard #endif // NONEXCEPTIONS
238*89a1d03eSRichard
239*89a1d03eSRichard namespace pr39777 {
240*89a1d03eSRichard struct S { S(); };
241*89a1d03eSRichard struct T { T() noexcept; };
242*89a1d03eSRichard
__anon245c8bdc0202null243*89a1d03eSRichard auto Okay1 = []{ S s; };
__anon245c8bdc0302null244*89a1d03eSRichard auto Okay2 = []{ (void)new int; };
__anon245c8bdc0402null245*89a1d03eSRichard auto NotOkay1 = []{ S s; return 12; }(); // Because the lambda call is not noexcept
246*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay1' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
247*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-7]]:12: note: possibly throwing constructor declared here
__anon245c8bdc0502() 248*89a1d03eSRichard auto NotOkay2 = []() noexcept { S s; return 12; }(); // Because S::S() is not noexcept
249*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay2' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
250*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-10]]:12: note: possibly throwing constructor declared here
__anon245c8bdc0602() 251*89a1d03eSRichard auto Okay3 = []() noexcept { T t; return t; }();
252*89a1d03eSRichard
253*89a1d03eSRichard struct U {
254*89a1d03eSRichard U() noexcept;
getBadLambdapr39777::U255*89a1d03eSRichard auto getBadLambda() const noexcept {
256*89a1d03eSRichard return []{ S s; return s; };
257*89a1d03eSRichard }
258*89a1d03eSRichard };
__anon245c8bdc0802null259*89a1d03eSRichard auto Okay4 = []{ U u; return u.getBadLambda(); }();
__anon245c8bdc0902() 260*89a1d03eSRichard auto NotOkay3 = []() noexcept { U u; return u.getBadLambda(); }()(); // Because the lambda returned and called is not noexcept
261*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay3' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp]
262*89a1d03eSRichard // CHECK-EXCEPTIONS: :[[@LINE-6]]:12: note: possibly throwing function declared here
263*89a1d03eSRichard
264*89a1d03eSRichard #ifndef NONEXCEPTIONS
265*89a1d03eSRichard struct Bad {
Badpr39777::Bad266*89a1d03eSRichard Bad() {
267*89a1d03eSRichard throw 12;
268*89a1d03eSRichard }
269*89a1d03eSRichard };
270*89a1d03eSRichard
__anon245c8bdc0a02()271*89a1d03eSRichard static auto NotOkay4 = [bad = Bad{}](){};
272*89a1d03eSRichard // FIXME: the above should be diagnosed because the capture init can trigger
273*89a1d03eSRichard // an exception when constructing the Bad object.
274*89a1d03eSRichard #endif // NONEXCEPTIONS
275*89a1d03eSRichard }
276