| 1 |
// RUN: %clang_cc1 -verify -Wgnu-folding-constant %s |
| 2 |
|
| 3 |
/* WG14 N1330: Yes |
| 4 |
* Static assertions |
| 5 |
*/ |
| 6 |
|
| 7 |
// Test syntactic requirements: first argument must be a constant expression, |
| 8 |
// and the second argument must be a string literal. We support the C2x |
| 9 |
// extension that allows you to elide the second argument. |
| 10 |
int a; |
| 11 |
_Static_assert(a, ""); // expected-error {{static assertion expression is not an integral constant expression}} |
| 12 |
_Static_assert(1); // expected-warning {{'_Static_assert' with no message is a C23 extension}} |
| 13 |
|
| 14 |
// Test functional requirements |
| 15 |
_Static_assert(1, "this works"); |
| 16 |
_Static_assert(0, "this fails"); // expected-error {{static assertion failed: this fails}} |
| 17 |
_Static_assert(0); // expected-error {{static assertion failed}} \ |
| 18 |
expected-warning {{'_Static_assert' with no message is a C23 extension}} |
| 19 |
|
| 20 |
// Test declaration contexts. We've already demonstrated that file scope works. |
| 21 |
struct S { |
| 22 |
_Static_assert(1, "this works"); |
| 23 |
union U { |
| 24 |
long l; |
| 25 |
_Static_assert(1, "this works"); |
| 26 |
} u; |
| 27 |
enum E { |
| 28 |
_Static_assert(1, "this should not compile"); // expected-error {{expected identifier}} |
| 29 |
One |
| 30 |
} e; |
| 31 |
}; |
| 32 |
|
| 33 |
void func( // expected-note {{to match this '('}} |
| 34 |
_Static_assert(1, "this should not compile") // expected-error {{expected parameter declarator}} \ |
| 35 |
expected-error {{expected ')'}} |
| 36 |
); |
| 37 |
void func( // expected-note {{to match this '('}} |
| 38 |
_Static_assert(1, "this should not compile") // expected-error {{expected parameter declarator}} \ |
| 39 |
expected-error {{expected ')'}} |
| 40 |
) {} |
| 41 |
|
| 42 |
void test(void) { |
| 43 |
_Static_assert(1, "this works"); |
| 44 |
_Static_assert(0, "this fails"); // expected-error {{static assertion failed: this fails}} |
| 45 |
|
| 46 |
// While the use of a _Static_assert in a for loop declaration is prohibited per |
| 47 |
// 6.8.5p3 (requiring the declaration to only declare identifiers for objects |
| 48 |
// having auto or register storage class; a static assertion does not declare |
| 49 |
// an identifier nor an object), we permit it as an extension. |
| 50 |
int i = 0; |
| 51 |
for (_Static_assert(1, "this should compile"); i < 10; ++i) |
| 52 |
; |
| 53 |
|
| 54 |
// Ensure that only an integer constant expression can be used as the |
| 55 |
// controlling expression. |
| 56 |
_Static_assert(1.0f, "this should not compile"); // expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}} |
| 57 |
} |
| 58 |
|
| 59 |
#if __STDC_VERSION__ < 202311L |
| 60 |
// The use of a _Static_assert in a K&R C function definition is prohibited per |
| 61 |
// 6.9.1p6 requiring each declaration to have a declarator (which a static |
| 62 |
// assertion does not have) and only declare identifiers from the identifier |
| 63 |
// list. |
| 64 |
// The error about expecting a ';' is due to the static assertion confusing the |
| 65 |
// compiler. It'd be nice if we improved the diagnostics here, but because this |
| 66 |
// involves a K&R C declaration, it's low priority. |
| 67 |
void knr(a, b, c) // expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} |
| 68 |
int a, b; // expected-error {{expected ';' at end of declaration}} |
| 69 |
_Static_assert(1, "this should not compile"); // expected-error {{expected identifier or '('}} \ |
| 70 |
expected-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}} |
| 71 |
float c; |
| 72 |
{ |
| 73 |
} |
| 74 |
#endif // __STDC_VERSION__ |