xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/static-assert.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -xc++ -std=c++11 -fsyntax-only -verify %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc _Static_assert("foo", "string is nonzero");
5*f4a2713aSLionel Sambuc #ifndef __cplusplus
6*f4a2713aSLionel Sambuc // expected-error@-2 {{static_assert expression is not an integral constant expression}}
7*f4a2713aSLionel Sambuc #endif
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc _Static_assert(1, "1 is nonzero");
10*f4a2713aSLionel Sambuc _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}}
11*f4a2713aSLionel Sambuc 
foo(void)12*f4a2713aSLionel Sambuc void foo(void) {
13*f4a2713aSLionel Sambuc   _Static_assert(1, "1 is nonzero");
14*f4a2713aSLionel Sambuc   _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}}
15*f4a2713aSLionel Sambuc }
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc _Static_assert(1, invalid); // expected-error {{expected string literal for diagnostic message in static_assert}}
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc struct A {
20*f4a2713aSLionel Sambuc   int a;
21*f4a2713aSLionel Sambuc   _Static_assert(1, "1 is nonzero");
22*f4a2713aSLionel Sambuc   _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}}
23*f4a2713aSLionel Sambuc };
24*f4a2713aSLionel Sambuc 
25*f4a2713aSLionel Sambuc #ifdef __cplusplus
26*f4a2713aSLionel Sambuc #define ASSERT_IS_TYPE(T) __is_same(T, T)
27*f4a2713aSLionel Sambuc #else
28*f4a2713aSLionel Sambuc #define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T)
29*f4a2713aSLionel Sambuc #endif
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc #define UNION(T1, T2) union { \
32*f4a2713aSLionel Sambuc     __typeof__(T1) one; \
33*f4a2713aSLionel Sambuc     __typeof__(T2) two; \
34*f4a2713aSLionel Sambuc     _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \
35*f4a2713aSLionel Sambuc     _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \
36*f4a2713aSLionel Sambuc     _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \
37*f4a2713aSLionel Sambuc   }
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc typedef UNION(unsigned, struct A) U1;
40*f4a2713aSLionel Sambuc UNION(char[2], short) u2 = { .one = { 'a', 'b' } };
41*f4a2713aSLionel Sambuc typedef UNION(char, short) U3; // expected-error {{static_assert failed "type size mismatch"}}
42*f4a2713aSLionel Sambuc typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}}
43