xref: /llvm-project/clang/test/Sema/static-assert.c (revision be427dfb9ea6689947253d737708dc3645e179dc)
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
5 
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
10 
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}}
14 
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
18 
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
26 }
27 
28 _Static_assert(1, invalid); // ext-warning {{'_Static_assert' is a C11 extension}}
29 #ifndef __cplusplus
30 // expected-error@-2 {{expected string literal for diagnostic message in static_assert}}
31 #endif
32 // cxx-error@-4 {{use of undeclared identifier 'invalid'}}
33 // cxx-warning@-5 {{'static_assert' with a user-generated message is a C++26 extension}}
34 
35 struct A {
36   int a;
37   _Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
38   _Static_assert(0, "0 is nonzero"); // expected-error {{static assertion failed: 0 is nonzero}} \
39                                      // ext-warning {{'_Static_assert' is a C11 extension}}
40 #ifdef MS
41   static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without}}
42 #endif
43 };
44 
45 #ifdef __cplusplus
46 #define ASSERT_IS_TYPE(T) __is_same(T, T)
47 #else
48 #define ASSERT_IS_TYPE(T) __builtin_types_compatible_p(T, T)
49 #endif
50 
51 #define UNION(T1, T2) union { \
52     __typeof__(T1) one; \
53     __typeof__(T2) two; \
54     _Static_assert(ASSERT_IS_TYPE(T1), "T1 is not a type"); \
55     _Static_assert(ASSERT_IS_TYPE(T2), "T2 is not a type"); \
56     _Static_assert(sizeof(T1) == sizeof(T2), "type size mismatch"); \
57   }
58 
59 typedef UNION(unsigned, struct A) U1; // ext-warning 3 {{'_Static_assert' is a C11 extension}}
60 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}}
61 typedef UNION(char, short) U3; // expected-error {{static assertion failed due to requirement 'sizeof(char) == sizeof(short)': type size mismatch}} \
62                                // expected-note{{evaluates to '1 == 2'}} \
63                                // ext-warning 3 {{'_Static_assert' is a C11 extension}}
64 typedef UNION(float, 0.5f) U4; // expected-error {{expected a type}} \
65                                // ext-warning 3 {{'_Static_assert' is a C11 extension}}
66 
67 // After defining the assert macro in MS-compatibility mode, we should
68 // no longer warn about including <assert.h> under the assumption the
69 // user already did that.
70 #ifdef MS
71 #define assert(expr)
72 static_assert(1, "1 is nonzero"); // ok
73 
74 // But we should still warn if the user did something bonkers.
75 #undef static_assert
76 static_assert(1, "1 is nonzero"); // ms-warning {{use of 'static_assert' without}}
77 #endif
78 
79 _Static_assert(1 , "") // expected-error {{expected ';' after '_Static_assert'}} \
80                       // ext-warning {{'_Static_assert' is a C11 extension}}
81 
82 static int static_var;
83 _Static_assert(&static_var != 0, "");  // ext-warning {{'_Static_assert' is a C11 extension}} \
84                                        // expected-warning {{comparison of address of 'static_var' not equal to a null pointer is always true}}
85 _Static_assert("" != 0, "");           // ext-warning {{'_Static_assert' is a C11 extension}}
86 _Static_assert(("" != 0), "");         // ext-warning {{'_Static_assert' is a C11 extension}}
87 _Static_assert(*"1", "");              // ext-warning {{'_Static_assert' is a C11 extension}}
88 _Static_assert("1"[0], "");            // ext-warning {{'_Static_assert' is a C11 extension}}
89 _Static_assert(1.0 != 0, "");          // ext-warning {{'_Static_assert' is a C11 extension}}
90 _Static_assert(__builtin_strlen("1"), "");  // ext-warning {{'_Static_assert' is a C11 extension}}
91 #ifndef __cplusplus
92 // expected-warning@-9 {{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 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
95 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
96 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
97 // expected-warning@-8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
98 // __builtin_strlen(literal) is considered an integer constant expression
99 // and doesn't cause a pedantic warning
100 #endif
101 
102 
103 _Static_assert(0, L"\xFFFFFFFF"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
104                                   // expected-error {{invalid escape sequence '\xFFFFFFFF' in an unevaluated string literal}} \
105                                   // expected-error {{hex escape sequence out of range}} \
106                                   // ext-warning {{'_Static_assert' is a C11 extension}}
107 _Static_assert(0, L"\u1234"); // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
108                               // expected-error {{static assertion failed: ሴ}} \
109                               // ext-warning {{'_Static_assert' is a C11 extension}}
110 
111 _Static_assert(0, L"\x1ff"      // expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect}} \
112                                 // expected-error {{hex escape sequence out of range}} \
113                                 // expected-error {{invalid escape sequence '\x1ff' in an unevaluated string literal}} \
114                                 // ext-warning {{'_Static_assert' is a C11 extension}}
115                    "0\x123"     // expected-error {{invalid escape sequence '\x123' in an unevaluated string literal}}
116                    "fx\xfffff"  // expected-error {{invalid escape sequence '\xfffff' in an unevaluated string literal}}
117                    "goop");
118