blob: 87fa0504b200a21d505b8db8b7a637852383cf9a [file] [log] [blame]
Andy Gibbs74b9fa12013-04-03 09:46:04 +00001// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -xc++ -std=c++11 -fsyntax-only -verify %s
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003
Andy Gibbs74b9fa12013-04-03 09:46:04 +00004_Static_assert("foo", "string is nonzero");
5#ifndef __cplusplus
6// expected-error@-2 {{static_assert expression is not an integral constant expression}}
7#endif
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00008
9_Static_assert(1, "1 is nonzero");
10_Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}}
11
12void 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}
Andy Gibbs97f84612012-11-17 19:16:52 +000016
17_Static_assert(1, invalid); // expected-error {{expected string literal for diagnostic message in static_assert}}
Andy Gibbs74b9fa12013-04-03 09:46:04 +000018
19struct 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
39typedef UNION(unsigned, struct A) U1;
40UNION(char[2], short) u2 = { .one = { 'a', 'b' } };
41typedef UNION(char, short) U3; // expected-error {{static_assert failed "type size mismatch"}}
42typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}}