Debian Codegraph @

clang/test/Sema/static-assert.c

raw
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 -fms-compatibility -Wgnu-folding-constant -DMS -fsyntax-only -verify=expected,ms %s
4 // RUN: %clang_cc1 -std=c99 -pedantic -Wgnu-folding-constant -fsyntax-only -verify=expected,ext %s
5 // RUN: %clang_cc1 -xc++ -std=c++11 -pedantic -fsyntax-only -verify=expected,ext,cxx %s
6
7 _Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
8 #ifndef __cplusplus
9 // expected-warning@-2 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
10 #endif
11
12 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
13 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
14 // ext-warning {{'_Static_assert' is a C11 extension}}
15
16 #ifdef MS
17 static_assert(1, "1 is nonzero");
18 #endif
19
20 void foo(void) {
21 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
22 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
23 // ext-warning {{'_Static_assert' is a C11 extension}}
24 #ifdef MS
25 static_assert(1, "1 is nonzero");
26 #endif
27 }
28
29 _Static_assert(1, invalid); // ext-warning {{'_Static_assert' is a C11 extension}}
30 #ifndef __cplusplus
31 // expected-error@-2 {{expected string literal for diagnostic message in static_assert}}
32 #endif
33 // cxx-error@-4 {{use of undeclared identifier 'invalid'}}
34 // cxx-warning@-5 {{'static_assert' with a user-generated message is a C++26 extension}}
35
36 struct A {
37 int a;
38 _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
39 _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
40 // ext-warning {{'_Static_assert' is a C11 extension}}
41 #ifdef MS
42 static_assert(1, "1 is nonzero");
43 #endif
44 };
45
46 #ifdef __cplusplus
47 #define ASSERT_IS_TYPE(T) __is_same(T, T)
48 #else
49 #define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T)
50 #endif
51
52 #define UNION(T1, T2) union { \
53 __typeof__(T1) one; \
54 __typeof__(T2) two; \
55 _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \
56 _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \
57 _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \
58 }
59
60 typedef UNION(unsigned, struct A) U1; // ext-warning 3 {{'_Static_assert' is a C11 extension}}
61 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}}
62 typedef UNION(char, short) U3; // expected-error {{static assertion failed due to requirement 'sizeof(char) == sizeof(short)': type size mismatch}} \
63 // expected-note{{evaluates to '1 == 2'}} \
64 // ext-warning 3 {{'_Static_assert' is a C11 extension}}
65 typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \
66 // ext-warning 3 {{'_Static_assert' is a C11 extension}}
67
68 // MSVC accepts static_assert in all language modes without including <assert.h>
69 // and so do we in ms-compatibility mode. Unfortunately, there is no good way
70 // to diagnose that with a pedantic warning. We'd have to track inclusion of
71 // <assert.h> which is difficult when modules and PCH are involved. Adding
72 // implicit definition of the macro causes unexpected results in c99 mode.
73 #ifdef MS
74
75 #if __STDC_VERSION__ < 201112L
76 #if defined(static_assert)
77 static_assert(0, "0 is nonzero"); // ok because we should not define static_assert
78 // macro in c99.
79 #endif
80
81 static_assert(0, "0 is nonzero"); // ms-error {{static assertion failed: 0 is nonzero}}
82 #endif
83
84 #define assert(expr)
85 static_assert(1, "1 is nonzero"); // ok
86
87 #undef static_assert
88 static_assert(1, "1 is nonzero"); // yes, still ok.
89 #endif
90
91 _Static_assert(1 , "") // expected-error {{expected ';' after '_Static_assert'}} \
92 // ext-warning {{'_Static_assert' is a C11 extension}}
93
94 static int static_var;
95 _Static_assert(&static_var != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}} \
96 // expected-warning {{comparison of address of 'static_var' not equal to a null pointer is always true}}
97 _Static_assert("" != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}
98 _Static_assert(("" != 0), ""); // ext-warning {{'_Static_assert' is a C11 extension}}
99 _Static_assert(*"1", ""); // ext-warning {{'_Static_assert' is a C11 extension}}
100 _Static_assert("1"[0], ""); // ext-warning {{'_Static_assert' is a C11 extension}}
101 _Static_assert(1.0 != 0, ""); // ext-warning {{'_Static_assert' is a C11 extension}}
102 _Static_assert(__builtin_strlen("1"), ""); // ext-warning {{'_Static_assert' is a C11 extension}}
103 #ifndef __cplusplus
104 // expected-warning@-9 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
105 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
106 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
107 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
108 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
109 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
110 // __builtin_strlen(literal) is considered an integer constant expression
111 // and doesn't cause a pedantic warning
112 #endif
113
114
115 _Static_assert(0, L"\xFFFFFFFF"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
116 // expected-error {{invalid escape sequence '\xFFFFFFFF' in an unevaluated string literal}} \
117 // expected-error {{hex escape sequence out of range}} \
118 // ext-warning {{'_Static_assert' is a C11 extension}}
119 _Static_assert(0, L"\u1234"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
120 // expected-error {{static assertion failed: ሴ}} \
121 // ext-warning {{'_Static_assert' is a C11 extension}}
122
123 _Static_assert(0, L"\x1ff" // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
124 // expected-error {{hex escape sequence out of range}} \
125 // expected-error {{invalid escape sequence '\x1ff' in an unevaluated string literal}} \
126 // ext-warning {{'_Static_assert' is a C11 extension}}
127 "0\x123" // expected-error {{invalid escape sequence '\x123' in an unevaluated string literal}}
128 "fx\xfffff" // expected-error {{invalid escape sequence '\xfffff' in an unevaluated string literal}}
129 "goop");