[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Sema / static-assert.c
blob6ede89dc0069073eaaed38e47ece09886f63704c
1 // RUN: %clang_cc1 -std=c11 -Wgnu-folding-constant -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms %s
3 // RUN: %clang_cc1 -std=c99 -pedantic -Wgnu-folding-constant -fsyntax-only -verify=expected,ext %s
4 // RUN: %clang_cc1 -xc++ -std=c++11 -pedantic -fsyntax-only -verify=expected,ext,cxx %s
6 _Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
7 #ifndef __cplusplus
8 // expected-warning@-2 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
9 #endif
11 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
12 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
13 // ext-warning {{'_Static_assert' is a C11 extension}}
15 #ifdef MS
16 static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without inclusion of <assert.h> is a Microsoft extension}}
17 #endif
19 void foo(void) {
20 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
21 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
22 // ext-warning {{'_Static_assert' is a C11 extension}}
23 #ifdef MS
24 static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without}}
25 #endif
28 _Static_assert(1, invalid); // expected-error {{expected string literal for diagnostic message in static_assert}} \
29 // ext-warning {{'_Static_assert' is a C11 extension}}
31 struct A {
32 int a;
33 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
34 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
35 // ext-warning {{'_Static_assert' is a C11 extension}}
36 #ifdef MS
37 static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without}}
38 #endif
41 #ifdef __cplusplus
42 #define ASSERT_IS_TYPE(T) __is_same(T, T)
43 #else
44 #define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T)
45 #endif
47 #define UNION(T1, T2) union { \
48 __typeof__(T1) one; \
49 __typeof__(T2) two; \
50 _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \
51 _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \
52 _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \
55 typedef UNION(unsigned, struct A) U1; // ext-warning 3 {{'_Static_assert' is a C11 extension}}
56 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}}
57 typedef UNION(char, short) U3; // expected-error {{static assertion failed due to requirement 'sizeof(char) == sizeof(short)': type size mismatch}} \
58 // expected-note{{evaluates to '1 == 2'}} \
59 // ext-warning 3 {{'_Static_assert' is a C11 extension}}
60 typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \
61 // ext-warning 3 {{'_Static_assert' is a C11 extension}}
63 // After defining the assert macro in MS-compatibility mode, we should
64 // no longer warn about including <assert.h> under the assumption the
65 // user already did that.
66 #ifdef MS
67 #define assert(expr)
68 static_assert(1, "1 is nonzero"); // ok
70 // But we should still warn if the user did something bonkers.
71 #undef static_assert
72 static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without}}
73 #endif
75 _Static_assert(1 , "") // expected-error {{expected ';' after '_Static_assert'}} \
76 // ext-warning {{'_Static_assert' is a C11 extension}}
78 static int static_var;
79 _Static_assert(&static_var != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}} \
80 // expected-warning {{comparison of address of 'static_var' not equal to a null pointer is always true}}
81 _Static_assert("" != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}
82 _Static_assert(("" != 0), ""); // ext-warning {{'_Static_assert' is a C11 extension}}
83 _Static_assert(*"1", ""); // ext-warning {{'_Static_assert' is a C11 extension}}
84 _Static_assert("1"[0], ""); // ext-warning {{'_Static_assert' is a C11 extension}}
85 _Static_assert(1.0 != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}
86 _Static_assert(__builtin_strlen("1"), ""); // ext-warning {{'_Static_assert' is a C11 extension}}
87 #ifndef __cplusplus
88 // expected-warning@-9 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
89 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
90 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
91 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
92 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
93 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
94 // __builtin_strlen(literal) is considered an integer constant expression
95 // and doesn't cause a pedantic warning
96 #endif