Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / C / C11 / n1330.c
blob153f1994192dde936d38ba878c36ac5e4378954b
1 // RUN: %clang_cc1 -verify -Wgnu-folding-constant %s
3 /* WG14 N1330: Yes
4 * Static assertions
5 */
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}}
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}}
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;
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 ')'}}
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 ) {}
42 void test(void) {
43 _Static_assert(1, "this works");
44 _Static_assert(0, "this fails"); // expected-error {{static assertion failed: this fails}}
46 // 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.
50 // FIXME: this diagnostic is pretty terrible.
51 int i = 0;
52 for (_Static_assert(1, "this should not compile"); i < 10; ++i) // expected-error {{expected identifier or '('}} \
53 expected-error {{expected ';' in 'for' statement specifier}}
56 // Ensure that only an integer constant expression can be used as the
57 // controlling expression.
58 _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}}
61 #if __STDC_VERSION__ < 202311L
62 // The use of a _Static_assert in a K&R C function definition is prohibited per
63 // 6.9.1p6 requiring each declaration to have a declarator (which a static
64 // assertion does not have) and only declare identifiers from the identifier
65 // list.
66 // The error about expecting a ';' is due to the static assertion confusing the
67 // compiler. It'd be nice if we improved the diagnostics here, but because this
68 // involves a K&R C declaration, it's low priority.
69 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}}
70 int a, b; // expected-error {{expected ';' at end of declaration}}
71 _Static_assert(1, "this should not compile"); // expected-error {{expected identifier or '('}} \
72 expected-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
73 float c;
76 #endif // __STDC_VERSION__