brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · f8de2c1 Raw
75 lines · c
1// RUN: %clang_cc1 -verify -Wgnu-folding-constant %s2 3/* WG14 N1330: Yes4 * Static assertions5 */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 C2x9// extension that allows you to elide the second argument.10int 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 requirements15_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.21struct 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    One30  } e;31};32 33void func(                                     // expected-note {{to match this '('}}34  _Static_assert(1, "this should not compile") // expected-error {{expected parameter declarator}} \35                                                  expected-error {{expected ')'}}36);37void 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 42void 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 per47  // 6.8.5p3 (requiring the declaration to only declare identifiers for objects48  // having auto or register storage class; a static assertion does not declare49  // 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 the55  // 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__ < 202311L60// The use of a _Static_assert in a K&R C function definition is prohibited per61// 6.9.1p6 requiring each declaration to have a declarator (which a static62// assertion does not have) and only declare identifiers from the identifier63// list.64// The error about expecting a ';' is due to the static assertion confusing the65// compiler. It'd be nice if we improved the diagnostics here, but because this66// involves a K&R C declaration, it's low priority.67void 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__75