| 1 |
/* Test C23 noreturn attribute: valid uses. */ |
| 2 |
/* { dg-do compile } */ |
| 3 |
/* { dg-options "-std=c23 -pedantic-errors" } */ |
| 4 |
|
| 5 |
[[noreturn]] void exit (int); |
| 6 |
|
| 7 |
[[__noreturn__]] int f1 (void); |
| 8 |
|
| 9 |
[[_Noreturn]] void f2 (void); |
| 10 |
|
| 11 |
[[___Noreturn__]] static void f3 (void) { exit (0); } |
| 12 |
|
| 13 |
/* Returning from a noreturn function is undefined at runtime, not a |
| 14 |
constraint violation, but recommended practice is to diagnose if |
| 15 |
such a return appears possible. */ |
| 16 |
|
| 17 |
[[noreturn]] int |
| 18 |
f4 (void) |
| 19 |
{ |
| 20 |
return 1; /* { dg-warning "has a 'return' statement" } */ |
| 21 |
/* { dg-warning "does return" "second warning" { target *-*-* } .-1 } */ |
| 22 |
} |
| 23 |
|
| 24 |
[[__noreturn__]] void |
| 25 |
f5 (void) |
| 26 |
{ |
| 27 |
return; /* { dg-warning "has a 'return' statement" } */ |
| 28 |
/* { dg-warning "does return" "second warning" { target *-*-* } .-1 } */ |
| 29 |
} |
| 30 |
|
| 31 |
[[_Noreturn]] void |
| 32 |
f6 (void) |
| 33 |
{ |
| 34 |
} /* { dg-warning "does return" } */ |
| 35 |
|
| 36 |
[[___Noreturn__]] void |
| 37 |
f7 (int a) |
| 38 |
{ |
| 39 |
if (a) |
| 40 |
exit (0); |
| 41 |
} /* { dg-warning "does return" } */ |
| 42 |
|
| 43 |
/* Declarations need not all have the attribute (buf if the first does not, |
| 44 |
there is undefined behavior). */ |
| 45 |
|
| 46 |
void f2 (void); |
| 47 |
|
| 48 |
/* Duplicate attribute, and use with _Noreturn, is OK. */ |
| 49 |
[[noreturn]] [[noreturn]] [[noreturn, __noreturn__]] void _Noreturn f9 (void); |
| 50 |
|
| 51 |
/* The attribute does not affect type compatibility. */ |
| 52 |
|
| 53 |
void (*fp) (void) = f5; |
| 54 |
|
| 55 |
/* Unlike the function specifier, the attribute may be used on main. */ |
| 56 |
[[noreturn]] int main (); |