aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCXX/attr-noreturn.cpp
blob: 6df008dffa813209bc8fdace4bf57b6dafccfb35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// RUN: %clang_cc1 -fsyntax-only -verify %s

// Reachability tests have to come first because they get suppressed
// if any errors have occurred.
namespace test5 {
  struct A {
    __attribute__((noreturn)) void fail();
    void nofail();
  } a;

  int &test1() {
    a.nofail();
  } // expected-warning {{control reaches end of non-void function}}

  int &test2() {
    a.fail();
  }
}

namespace destructor_tests {
  __attribute__((noreturn)) void fail();

  struct A {
    ~A() __attribute__((noreturn)) { fail(); }
  };
  struct B {
    B() {}
    ~B() __attribute__((noreturn)) { fail(); }
  };
  struct C : A {};
  struct D : B {};
  struct E : virtual A {};
  struct F : A, virtual B {};
  struct G : E {};
  struct H : virtual D {};
  struct I : A {};
  struct J : I {};
  struct K : virtual A {};
  struct L : K {};
  struct M : virtual C {};
  struct N : M {};
  struct O { N n; };

  __attribute__((noreturn)) void test_1() { A a; }
  __attribute__((noreturn)) void test_2() { B b; }
  __attribute__((noreturn)) void test_3() { C c; }
  __attribute__((noreturn)) void test_4() { D d; }
  __attribute__((noreturn)) void test_5() { E e; }
  __attribute__((noreturn)) void test_6() { F f; }
  __attribute__((noreturn)) void test_7() { G g; }
  __attribute__((noreturn)) void test_8() { H h; }
  __attribute__((noreturn)) void test_9() { I i; }
  __attribute__((noreturn)) void test_10() { J j; }
  __attribute__((noreturn)) void test_11() { K k; }
  __attribute__((noreturn)) void test_12() { L l; }
  __attribute__((noreturn)) void test_13() { M m; }
  __attribute__((noreturn)) void test_14() { N n; }
  __attribute__((noreturn)) void test_15() { O o; }

  __attribute__((noreturn)) void test_16() { const A& a = A(); }
  __attribute__((noreturn)) void test_17() { const B& b = B(); }
  __attribute__((noreturn)) void test_18() { const C& c = C(); }
  __attribute__((noreturn)) void test_19() { const D& d = D(); }
  __attribute__((noreturn)) void test_20() { const E& e = E(); }
  __attribute__((noreturn)) void test_21() { const F& f = F(); }
  __attribute__((noreturn)) void test_22() { const G& g = G(); }
  __attribute__((noreturn)) void test_23() { const H& h = H(); }
  __attribute__((noreturn)) void test_24() { const I& i = I(); }
  __attribute__((noreturn)) void test_25() { const J& j = J(); }
  __attribute__((noreturn)) void test_26() { const K& k = K(); }
  __attribute__((noreturn)) void test_27() { const L& l = L(); }
  __attribute__((noreturn)) void test_28() { const M& m = M(); }
  __attribute__((noreturn)) void test_29() { const N& n = N(); }
  __attribute__((noreturn)) void test_30() { const O& o = O(); }

  struct AA {};
  struct BB { BB() {} ~BB() {} };
  struct CC : AA {};
  struct DD : BB {};
  struct EE : virtual AA {};
  struct FF : AA, virtual BB {};
  struct GG : EE {};
  struct HH : virtual DD {};
  struct II : AA {};
  struct JJ : II {};
  struct KK : virtual AA {};
  struct LL : KK {};
  struct MM : virtual CC {};
  struct NN : MM {};
  struct OO { NN n; };

  __attribute__((noreturn)) void test_31() {
    AA a;
    BB b;
    CC c;
    DD d;
    EE e;
    FF f;
    GG g;
    HH h;
    II i;
    JJ j;
    KK k;
    LL l;
    MM m;
    NN n;
    OO o;

    const AA& aa = AA();
    const BB& bb = BB();
    const CC& cc = CC();
    const DD& dd = DD();
    const EE& ee = EE();
    const FF& ff = FF();
    const GG& gg = GG();
    const HH& hh = HH();
    const II& ii = II();
    const JJ& jj = JJ();
    const KK& kk = KK();
    const LL& ll = LL();
    const MM& mm = MM();
    const NN& nn = NN();
    const OO& oo = OO();
  }  // expected-warning {{function declared 'noreturn' should not return}}

  struct P {
    ~P() __attribute__((noreturn)) { fail(); }
    void foo() {}
  };
  struct Q : P { };
  __attribute__((noreturn)) void test31() {
    P().foo();
  }
  __attribute__((noreturn)) void test32() {
    Q().foo();
  }

  struct R {
    A a[5];
  };
  __attribute__((noreturn)) void test33() {
    R r;
  }

  // FIXME: Code flow analysis does not preserve information about non-null
  // pointers, so it can't determine that this function is noreturn.
  __attribute__((noreturn)) void test34() {
    A *a = new A;
    delete a;
  }  // expected-warning {{function declared 'noreturn' should not return}}

  struct S {
    virtual ~S();
  };
  struct T : S {
    __attribute__((noreturn)) ~T();
  };

  // FIXME: Code flow analysis does not preserve information about non-null
  // pointers or derived class pointers,  so it can't determine that this
  // function is noreturn.
  __attribute__((noreturn)) void test35() {
    S *s = new T;
    delete s;
  }  // expected-warning {{function declared 'noreturn' should not return}}
}

// PR5620
void f0() __attribute__((__noreturn__));
void f1(void (*)()); 
void f2() { f1(f0); }

// Taking the address of a noreturn function
void test_f0a() {
  void (*fp)() = f0;
  void (*fp1)() __attribute__((noreturn)) = f0;
}

// Taking the address of an overloaded noreturn function 
void f0(int) __attribute__((__noreturn__));

void test_f0b() {
  void (*fp)() = f0;
  void (*fp1)() __attribute__((noreturn)) = f0;
}

// No-returned function pointers
typedef void (* noreturn_fp)() __attribute__((noreturn));

void f3(noreturn_fp); // expected-note{{candidate function}}

void test_f3() {
  f3(f0); // okay
  f3(f2); // expected-error{{no matching function for call}}
}


class xpto {
  int blah() __attribute__((noreturn));
};

int xpto::blah() {
  return 3; // expected-warning {{function 'blah' declared 'noreturn' should not return}}
}

// PR12948

namespace PR12948 {
  template<int>
  void foo() __attribute__((__noreturn__));

  template<int>
  void foo() {
    while (1) continue;
  }

  void bar() __attribute__((__noreturn__));

  void bar() {
    foo<0>();
  }


  void baz() __attribute__((__noreturn__));
  typedef void voidfn();
  voidfn baz;

  template<typename> void wibble()  __attribute__((__noreturn__));
  template<typename> voidfn wibble;
}

// PR15291
// Overload resolution per over.over should allow implicit noreturn adjustment.
namespace PR15291 {
  __attribute__((noreturn)) void foo(int) {}
  __attribute__((noreturn)) void foo(double) {}

  template <typename T>
  __attribute__((noreturn)) void bar(T) {}

  void baz(int) {}
  void baz(double) {}

  template <typename T>
  void qux(T) {}

  // expected-note@+5 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'baz' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}}
  // expected-note@+4 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'qux' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}}
  // expected-note@+3 {{candidate function [with T = void (*)(int) __attribute__((noreturn))] not viable: no overload of 'bar' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}}
  // expected-note@+2 {{candidate function [with T = void (*)(int)] not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}}
  // expected-note@+1 {{candidate function [with T = void (int)] not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}}
  template <typename T> void accept_T(T) {}

  // expected-note@+1 {{candidate function not viable: no overload of 'bar' matching 'void (*)(int)' for 1st argument}}
  void accept_fptr(void (*f)(int)) {
    f(42);
  }

  // expected-note@+2 {{candidate function not viable: no overload of 'baz' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}}
  // expected-note@+1 {{candidate function not viable: no overload of 'qux' matching 'void (*)(int) __attribute__((noreturn))' for 1st argument}}
  void accept_noreturn_fptr(void __attribute__((noreturn)) (*f)(int)) {
    f(42);
  }

  typedef void (*fptr_t)(int);
  typedef void __attribute__((noreturn)) (*fptr_noreturn_t)(int);

  // expected-note@+1 {{candidate function not viable: no overload of 'bar' matching 'fptr_t' (aka 'void (*)(int)') for 1st argument}}
  void accept_fptr_t(fptr_t f) {
    f(42);
  }

  // expected-note@+2 {{candidate function not viable: no overload of 'baz' matching 'fptr_noreturn_t' (aka 'void (*)(int) __attribute__((noreturn))') for 1st argument}}
  // expected-note@+1 {{candidate function not viable: no overload of 'qux' matching 'fptr_noreturn_t' (aka 'void (*)(int) __attribute__((noreturn))') for 1st argument}}
  void accept_fptr_noreturn_t(fptr_noreturn_t f) {
    f(42);
  }

  // Stripping noreturn should work if everything else is correct.
  void strip_noreturn() {
    accept_fptr(foo);
    accept_fptr(bar<int>);
    accept_fptr(bar<double>); // expected-error {{no matching function for call to 'accept_fptr'}}

    accept_fptr_t(foo);
    accept_fptr_t(bar<int>);
    accept_fptr_t(bar<double>); // expected-error {{no matching function for call to 'accept_fptr_t'}}

    accept_T<void __attribute__((noreturn)) (*)(int)>(foo);
    accept_T<void __attribute__((noreturn)) (*)(int)>(bar<int>);
    accept_T<void __attribute__((noreturn)) (*)(int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}}

    accept_T<void (*)(int)>(foo);
    accept_T<void (*)(int)>(bar<int>);
    accept_T<void (*)(int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}}

    accept_T<void (int)>(foo);
    accept_T<void (int)>(bar<int>);
    accept_T<void (int)>(bar<double>); // expected-error {{no matching function for call to 'accept_T'}}
  }

  // Introducing noreturn should not work.
  void introduce_noreturn() {
    accept_noreturn_fptr(baz); // expected-error {{no matching function for call to 'accept_noreturn_fptr'}}
    accept_noreturn_fptr(qux<int>); // expected-error {{no matching function for call to 'accept_noreturn_fptr'}}

    accept_fptr_noreturn_t(baz); // expected-error {{no matching function for call to 'accept_fptr_noreturn_t'}}
    accept_fptr_noreturn_t(qux<int>); // expected-error {{no matching function for call to 'accept_fptr_noreturn_t'}}

    accept_T<void __attribute__((noreturn)) (*)(int)>(baz); // expected-error {{no matching function for call to 'accept_T'}}
    accept_T<void __attribute__((noreturn)) (*)(int)>(qux<int>); // expected-error {{no matching function for call to 'accept_T'}}
  }
}