Debian Codegraph @

clang/test/C/C2y/n3353.c

raw
1 // RUN: %clang_cc1 -verify=expected,c2y,c -pedantic -std=c2y -Wno-gnu-line-marker %s
2 // RUN: %clang_cc1 -verify=expected,c2y,compat -Wpre-c2y-compat -std=c2y -Wno-gnu-line-marker %s
3 // RUN: %clang_cc1 -verify=expected,ext,c -pedantic -std=c23 -Wno-gnu-line-marker %s
4 // RUN: %clang_cc1 -verify=expected,cpp -pedantic -x c++ -Wno-c11-extensions -Wno-gnu-line-marker %s
5
6
7 /* WG14 N3353: Clang 21
8 * Obsolete implicitly octal literals and add delimited escape sequences
9 */
10
11 constexpr int i = 0234; // c2y-warning {{octal literals without a '0o' prefix are deprecated}}
12 constexpr int j = 0o234; /* ext-warning {{octal integer literals are a C2y extension}}
13 cpp-warning {{octal integer literals are a Clang extension}}
14 compat-warning {{octal integer literals are incompatible with standards before C2y}}
15 */
16
17 static_assert(i == 156);
18 static_assert(j == 156);
19
20 // Show that 0O is the same as Oo (tested above)
21 static_assert(0O1234 == 0o1234); /* ext-warning 2 {{octal integer literals are a C2y extension}}
22 cpp-warning 2 {{octal integer literals are a Clang extension}}
23 compat-warning 2 {{octal integer literals are incompatible with standards before C2y}}
24 */
25
26 // Show that you can use them with the usual integer literal suffixes.
27 static_assert(0o234ull == 156); /* ext-warning {{octal integer literals are a C2y extension}}
28 cpp-warning {{octal integer literals are a Clang extension}}
29 compat-warning {{octal integer literals are incompatible with standards before C2y}}
30 */
31
32 // And it's still a valid null pointer constant.
33 static const void *ptr = 0o0; /* ext-warning {{octal integer literals are a C2y extension}}
34 cpp-warning {{octal integer literals are a Clang extension}}
35 compat-warning {{octal integer literals are incompatible with standards before C2y}}
36 */
37
38 // Demonstrate that it works fine in the preprocessor.
39 #if 0o123 != 0x53 /* ext-warning {{octal integer literals are a C2y extension}}
40 cpp-warning {{octal integer literals are a Clang extension}}
41 compat-warning {{octal integer literals are incompatible with standards before C2y}}
42 */
43 #error "oh no, math stopped working!"
44 #endif
45
46 // 0 by itself is not deprecated, of course.
47 int k1 = 0;
48 unsigned int k2 = 0u;
49 long k3 = 0l;
50 unsigned long k4 = 0ul;
51 long long k5 = 0ll;
52 unsigned long long k6 = 0ull;
53
54 // Test a preprocessor use of 0 by itself, which is also not deprecated.
55 #if 0
56 #endif
57
58 // Make sure there are no surprises with auto and type deduction. Promotion
59 // turns this into an 'int', and 'constexpr' implies 'const'.
60 constexpr auto l = 0o1234567; /* ext-warning {{octal integer literals are a C2y extension}}
61 cpp-warning {{octal integer literals are a Clang extension}}
62 compat-warning {{octal integer literals are incompatible with standards before C2y}}
63 */
64 static_assert(l == 0x53977);
65 static_assert(__extension__ _Generic(typeof(0o1), typeof(01) : 1, default : 0)); /* c2y-warning {{octal literals without a '0o' prefix are deprecated}}
66 compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}}
67 compat-warning {{octal integer literals are incompatible with standards before C2y}}
68 */
69 static_assert(__extension__ _Generic(typeof(l), const int : 1, default : 0)); // compat-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}}
70
71 // Note that 0o by itself is an invalid literal.
72 int m = 0o; /* expected-error {{invalid suffix 'o' on integer constant}}
73 */
74
75 // Ensure negation works as expected.
76 static_assert(-0o1234 == -668); /* ext-warning {{octal integer literals are a C2y extension}}
77 cpp-warning {{octal integer literals are a Clang extension}}
78 compat-warning {{octal integer literals are incompatible with standards before C2y}}
79 */
80
81 // FIXME: it would be better to not diagnose the compat and ext warnings when
82 // the octal literal is invalid.
83 // We expect diagnostics for non-octal digits.
84 int n = 0o18; /* expected-error {{invalid digit '8' in octal constant}}
85 compat-warning {{octal integer literals are incompatible with standards before C2y}}
86 ext-warning {{octal integer literals are a C2y extension}}
87 cpp-warning {{octal integer literals are a Clang extension}}
88 */
89 int o1 = 0o8; /* expected-error {{invalid suffix 'o8' on integer constant}}
90 */
91 // FIXME: however, it matches the behavior for hex literals in terms of the
92 // error reported. Unfortunately, we then go on to think 0 is an octal literal
93 // without a prefix, which is again a bit confusing.
94 int o2 = 0xG; /* expected-error {{invalid suffix 'xG' on integer constant}}
95 */
96
97 // Show that floating-point suffixes on octal literals are rejected.
98 auto f1 = 0o0.; /* expected-error {{invalid suffix '.' on integer constant}}
99 compat-warning {{octal integer literals are incompatible with standards before C2y}}
100 ext-warning {{octal integer literals are a C2y extension}}
101 cpp-warning {{octal integer literals are a Clang extension}}
102 */
103 auto f2 = 0o0.1; /* expected-error {{invalid suffix '.1' on integer constant}}
104 compat-warning {{octal integer literals are incompatible with standards before C2y}}
105 ext-warning {{octal integer literals are a C2y extension}}
106 cpp-warning {{octal integer literals are a Clang extension}}
107 */
108 auto f3 = 0o0e1; /* expected-error {{invalid suffix 'e1' on integer constant}}
109 compat-warning {{octal integer literals are incompatible with standards before C2y}}
110 ext-warning {{octal integer literals are a C2y extension}}
111 cpp-warning {{octal integer literals are a Clang extension}}
112 */
113 auto f4 = 0o0E1; /* expected-error {{invalid suffix 'E1' on integer constant}}
114 compat-warning {{octal integer literals are incompatible with standards before C2y}}
115 ext-warning {{octal integer literals are a C2y extension}}
116 cpp-warning {{octal integer literals are a Clang extension}}
117 */
118
119 // Show that valid floating-point literals with a leading 0 do not produce octal-related warnings.
120 auto f5 = 0.;
121 auto f7 = 00.;
122 auto f8 = 01.;
123 auto f9 = 0e1;
124 auto f10 = 0E1;
125 auto f11 = 00e1;
126 auto f12 = 00E1;
127
128 // Ensure digit separators work as expected.
129 constexpr int p = 0o0'1'2'3'4'5'6'7; /* compat-warning {{octal integer literals are incompatible with standards before C2y}}
130 ext-warning {{octal integer literals are a C2y extension}}
131 cpp-warning {{octal integer literals are a Clang extension}}
132 */
133 static_assert(p == 01234567); // c2y-warning {{octal literals without a '0o' prefix are deprecated}}
134 int q = 0o'0'1; /* expected-error {{invalid suffix 'o'0'1' on integer constant}}
135 */
136
137 #define M 0o123
138 int r = M; /* compat-warning {{octal integer literals are incompatible with standards before C2y}}
139 ext-warning {{octal integer literals are a C2y extension}}
140 cpp-warning {{octal integer literals are a Clang extension}}
141 */
142
143 // Also, test delimited escape sequences. Note, this paper added a delimited
144 // escape sequence for octal *and* hex.
145 auto a = "\x{12}\o{12}\N{SPARKLES}"; /* compat-warning 2 {{delimited escape sequences are incompatible with C standards before C2y}}
146 ext-warning 2 {{delimited escape sequences are a C2y extension}}
147 cpp-warning 2 {{delimited escape sequences are a C++23 extension}}
148 cpp-warning {{named escape sequences are a C++23 extension}}
149 c-warning {{named escape sequences are a Clang extension}}
150 */
151
152 #ifdef __cplusplus
153 template <unsigned N>
154 struct S {
155 static_assert(N == 0o567); /* ext-warning {{octal integer literals are a C2y extension}}
156 cpp-warning {{octal integer literals are a Clang extension}}
157 compat-warning {{octal integer literals are incompatible with standards before C2y}}
158 */
159 };
160
161 void foo() {
162 S<0o567> s; /* ext-warning {{octal integer literals are a C2y extension}}
163 cpp-warning {{octal integer literals are a Clang extension}}
164 compat-warning {{octal integer literals are incompatible with standards before C2y}}
165 */
166 }
167 #endif
168
169 int other_func(int i, ...);
170 #172 "system_header.h" 3
171 #define SYS_HEADER_MACRO_1 0o02
172 #define SYS_HEADER_MACRO_2 02
173 #define SYS_HEADER_MACRO_FN1(...) other_func(0o02, __VA_ARGS__)
174 #define SYS_HEADER_MACRO_FN2(...) other_func(2, __VA_ARGS__)
175 #174 "n3353.c" 1
176
177 #define USER_HEADER_MACRO_1 0o02
178 #define USER_HEADER_MACRO_2 02
179 #define USER_HEADER_MACRO_FN1(...) other_func(0o02, __VA_ARGS__)
180 #define USER_HEADER_MACRO_FN2(...) other_func(2, __VA_ARGS__)
181
182 // Test what happens when a user macro expands to a system macro.
183 #define USER_OBJECT_MACRO SYS_HEADER_MACRO_1
184 #define USER_FUNCTION_MACRO() SYS_HEADER_MACRO_1
185
186 void test_macro_behavior(void) {
187 // No diagnostic because these expanded from a macro defined in a system
188 // header.
189 int i = SYS_HEADER_MACRO_1;
190 int j = SYS_HEADER_MACRO_2;
191 int k = SYS_HEADER_MACRO_FN1(12);
192
193 // This one diagnoses because the literal is not in the system header.
194 int l = SYS_HEADER_MACRO_FN2(0o02); /* ext-warning {{octal integer literals are a C2y extension}}
195 cpp-warning {{octal integer literals are a Clang extension}}
196 compat-warning {{octal integer literals are incompatible with standards before C2y}}
197 */
198
199 // Neither of these diagnose because the actual octal literal tokens are
200 // defined in a system header even if there's an intervening user macro.
201 int m = USER_OBJECT_MACRO;
202 int n = USER_FUNCTION_MACRO();
203
204 // Diagnose other macro expansions though.
205 int a = USER_HEADER_MACRO_1; /* ext-warning {{octal integer literals are a C2y extension}}
206 cpp-warning {{octal integer literals are a Clang extension}}
207 compat-warning {{octal integer literals are incompatible with standards before C2y}}
208 */
209 int b = USER_HEADER_MACRO_2; // c2y-warning {{octal literals without a '0o' prefix are deprecated}}
210 int c = USER_HEADER_MACRO_FN1(12); /* ext-warning {{octal integer literals are a C2y extension}}
211 cpp-warning {{octal integer literals are a Clang extension}}
212 compat-warning {{octal integer literals are incompatible with standards before C2y}}
213 */
214 int d = USER_HEADER_MACRO_FN2(0o02); /* ext-warning {{octal integer literals are a C2y extension}}
215 cpp-warning {{octal integer literals are a Clang extension}}
216 compat-warning {{octal integer literals are incompatible with standards before C2y}}
217 */
218 }
219
220 #line 0123 // expected-warning {{#line directive interprets number as decimal, not octal}}
221 #line 0o123 // expected-error {{#line directive requires a simple digit sequence}}