1 // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s 2 // RUN: %clang_cc1 -std=c99 -pedantic -fsyntax-only -verify=expected,ext %s 3 // RUN: %clang_cc1 -xc++ -std=c++11 -pedantic -fsyntax-only -verify=expected,ext,cxx %s 4 5 _Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}} 6 #ifndef __cplusplus 7 // expected-error@-2 {{static_assert expression is not an integral constant expression}} 8 #endif 9 10 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}} 11 _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}} \ 12 // ext-warning {{'_Static_assert' is a C11 extension}} 13 14 void foo(void) { 15 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}} 16 _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}} \ 17 // ext-warning {{'_Static_assert' is a C11 extension}} 18 } 19 20 _Static_assert(1, invalid); // expected-error {{expected string literal for diagnostic message in static_assert}} \ 21 // ext-warning {{'_Static_assert' is a C11 extension}} 22 23 struct A { 24 int a; 25 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}} 26 _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}} \ 27 // ext-warning {{'_Static_assert' is a C11 extension}} 28 }; 29 30 #ifdef __cplusplus 31 #define ASSERT_IS_TYPE(T) __is_same(T, T) 32 #else 33 #define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T) 34 #endif 35 36 #define UNION(T1, T2) union { \ 37 __typeof__(T1) one; \ 38 __typeof__(T2) two; \ 39 _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \ 40 _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \ 41 _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \ 42 } 43 44 typedef UNION(unsigned, struct A) U1; // ext-warning 3 {{'_Static_assert' is a C11 extension}} 45 UNION(char[2], short) u2 = { .one = { 'a', 'b' } }; // ext-warning 3 {{'_Static_assert' is a C11 extension}} cxx-warning {{designated initializers are a C++20 extension}} 46 typedef UNION(char, short) U3; // expected-error {{static_assert failed due to requirement 'sizeof(char) == sizeof(short)' "type size mismatch"}} \ 47 // ext-warning 3 {{'_Static_assert' is a C11 extension}} 48 typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \ 49 // ext-warning 3 {{'_Static_assert' is a C11 extension}} 50