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