Debian Codegraph @

clang/test/C/drs/dr0xx.c

raw
1 /* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions %s
2 RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-declaration-after-statement -Wno-c11-extensions -fno-signed-char %s
3 RUN: %clang_cc1 -std=c99 -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s
4 RUN: %clang_cc1 -std=c11 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s
5 RUN: %clang_cc1 -std=c17 -fsyntax-only -verify=expected,c99untilc2x -pedantic %s
6 RUN: %clang_cc1 -std=c2x -fsyntax-only -verify=expected,c2xandup -pedantic %s
7 */
8
9 /* The following are DRs which do not require tests to demonstrate
10 * conformance or nonconformance.
11 *
12 * WG14 DR001: yes
13 * Do functions return values by copying?
14 *
15 * WG14 DR005: yes
16 * May a conforming implementation define and recognize a pragma which would
17 * change the semantics of the language?
18 *
19 * WG14 DR008: yes
20 * Can a conforming C compiler to perform dead-store elimination?
21 *
22 * WG14 DR020: yes
23 * Is a compiler which allows the Relaxed Ref/Def linkage model to be
24 * considered a conforming compiler?
25 *
26 * WG14 DR025: yes
27 * What is meant by 'representable floating-point value?'
28 *
29 * WG14 DR026: yes
30 * Can a strictly conforming program contain a string literal with '$' or '@'?
31 *
32 * WG14 DR033: yes
33 * Conformance questions around 'shall' violations outside of constraints
34 * sections
35 *
36 * WG14 DR036: yes
37 * May floating-point constants be represented with more precision than implied
38 * by its type?
39 *
40 * WG14 DR037: yes
41 * Questions about multibyte characters and Unicode
42 *
43 * WG14 DR051: yes
44 * Question on pointer arithmetic
45 *
46 * WG14 DR052: yes
47 * Editorial corrections
48 *
49 * WG14 DR056: yes
50 * Floating-point representation precision requirements
51 *
52 * WG14 DR057: yes
53 * Is there an integral type for every pointer?
54 *
55 * WG14 DR059: yes
56 * Do types have to be completed?
57 *
58 * WG14 DR063: dup 056
59 * Floating-point representation precision requirements
60 *
61 * WG14 DR067: yes
62 * Integer and integral type confusion
63 *
64 * WG14 DR069: yes
65 * Questions about the representation of integer types
66 *
67 * WG14 DR077: yes
68 * Stability of addresses
69 *
70 * WG14 DR080: yes
71 * Merging of string constants
72 *
73 * WG14 DR085: yes
74 * Returning from main
75 *
76 * WG14 DR087: yes
77 * Order of evaluation
78 * Note: this DR is covered by C/C11/n1282.c
79 *
80 * WG14 DR086: yes
81 * Object-like macros in system headers
82 *
83 * WG14 DR091: yes
84 * Multibyte encodings
85 *
86 * WG14 DR092: dup 060
87 * Partial initialization of strings
88 *
89 * WG14 DR093: yes
90 * Reservation of identifiers
91 */
92
93
94 /* WG14 DR004: yes
95 * Are multiple definitions of unused identifiers with external linkage
96 * permitted?
97 */
98 int dr004(void) {return 0;} /* expected-note {{previous definition is here}} */
99 int dr004(void) {return 1;} /* expected-error {{redefinition of 'dr004'}} */
100
101 /* WG14 DR007: yes
102 * Are declarations of the form struct-or-union identifier ; permitted after
103 * the identifier tag has already been declared?
104 */
105 struct dr007_a;
106 struct dr007_a;
107 struct dr007_a {int a;};
108 struct dr007_a;
109 struct dr007_b {int a;};
110 struct dr007_b;
111
112
113 /* WG14 DR009: no
114 * Use of typedef names in parameter declarations
115 *
116 * FIXME: This should be diagnosed as expecting a declaration specifier instead
117 * of treated as declaring a parameter of type 'int (*)(dr009_t);'
118 */
119 typedef int dr009_t;
120 void dr009_f((dr009_t)); /* c99untilc2x-error {{type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int}}
121 c2xandup-error {{a type specifier is required for all declarations}} */
122
123 /* WG14 DR010:
124 * Is a typedef to an incomplete type legal?
125 */
126 typedef int dr010_t[];
127 dr010_t dr010_a = {1};
128 dr010_t dr010_b = {1, 2};
129 int dr010_c = sizeof(dr010_t); /* expected-error {{invalid application of 'sizeof' to an incomplete type 'dr010_t' (aka 'int[]')}} */
130
131 /* WG14 DR011: yes
132 * Merging of declarations for linked identifier
133 *
134 * Note: more of this DR is tested in dr011.c
135 *
136 * WG14 DR034: yes
137 * External declarations in different scopes
138 *
139 * Note: DR034 has a question resolved by DR011 and another question where the
140 * result is UB.
141 */
142 static int dr011_a[]; /* expected-warning {{tentative array definition assumed to have one element}} */
143 void dr011(void) {
144 extern int i[];
145 {
146 /* a different declaration of the same object */
147 extern int i[10];
148 (void)sizeof(i);
149 _Static_assert(sizeof(i) == 10 * sizeof(int), "fail");
150 }
151 (void)sizeof(i); /* expected-error {{invalid application of 'sizeof' to an incomplete type 'int[]'}} */
152
153 extern int dr011_a[10];
154 (void)sizeof(dr011_a);
155 _Static_assert(sizeof(dr011_a) == 10 * sizeof(int), "fail");
156
157 extern int j[10];
158 {
159 extern int j[];
160 (void)sizeof(j);
161 _Static_assert(sizeof(j) == 10 * sizeof(int), "fail");
162 }
163 }
164
165 /* WG14 DR012: yes
166 * Is it valid to take the address of a dereferenced void pointer?
167 */
168 void dr012(void *p) {
169 /* The behavior changed between C89 and C99. */
170 (void)&*p; /* c89only-warning {{ISO C forbids taking the address of an expression of type 'void'}}
171 c89only-warning {{ISO C does not allow indirection on operand of type 'void *'}} */
172 }
173
174 /* WG14 DR013: yes
175 * Compatible and composite function types
176 */
177 int dr013(int a[4]);
178 int dr013(int a[5]);
179 int dr013(int *a);
180
181 struct dr013_t {
182 struct dr013_t *p;
183 } dr013_v[sizeof(struct dr013_t)];
184
185 /* WG14 DR015: yes
186 * What is the promoted type of a plain int bit-field?
187 */
188 void dr015(void) {
189 struct S {
190 int small_int_bitfield : 16;
191 unsigned int small_uint_bitfield : 16;
192 int int_bitfield : 32;
193 unsigned int uint_bitfield : 32;
194 } s;
195 _Static_assert(__builtin_types_compatible_p(__typeof__(+s.small_int_bitfield), int), "fail");
196 _Static_assert(__builtin_types_compatible_p(__typeof__(+s.small_uint_bitfield), int), "fail");
197 _Static_assert(__builtin_types_compatible_p(__typeof__(+s.int_bitfield), int), "fail");
198 _Static_assert(__builtin_types_compatible_p(__typeof__(+s.uint_bitfield), unsigned int), "fail");
199 }
200
201 /* WG14 DR027: yes
202 * Can there be characters in the character set that are not in the required
203 * source character set?
204 */
205 #define THIS$AND$THAT(a, b) ((a) + (b)) /* expected-warning 2 {{'$' in identifier}} */
206 _Static_assert(THIS$AND$THAT(1, 1) == 2, "fail"); /* expected-warning 2 {{'$' in identifier}} */
207
208
209 /* WG14 DR029: no
210 * Do two types have to have the same tag to be compatible?
211 * Note: the rule changed in C99 to be different than the resolution to DR029,
212 * so it's not clear there's value in implementing this DR.
213 */
214 _Static_assert(__builtin_types_compatible_p(struct S { int a; }, union U { int a; }), "fail"); /* expected-error {{static assertion failed due to requirement '__builtin_types_compatible_p(struct S, union U)': fail}} */
215
216 /* WG14 DR031: yes
217 * Can constant expressions overflow?
218 */
219 void dr031(int i) {
220 switch (i) {
221 case __INT_MAX__ + 1: break; /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}} */
222 #pragma clang diagnostic push
223 #pragma clang diagnostic ignored "-Wswitch"
224 /* Silence the targets which issue:
225 * warning: overflow converting case value to switch condition type (2147483649 to 18446744071562067969)
226 */
227 case __INT_MAX__ + 2ul: break;
228 #pragma clang diagnostic pop
229 case (__INT_MAX__ * 4) / 4: break; /* expected-warning {{overflow in expression; result is -4 with type 'int'}} */
230 }
231 }
232
233 /* WG14 DR032: no
234 * Must implementations diagnose extensions to the constant evaluation rules?
235 *
236 * This should issue a diagnostic because a constant-expression is a
237 * conditional-expression, which excludes the comma operator.
238 */
239 int dr032 = (1, 2); /* expected-warning {{left operand of comma operator has no effect}} */
240
241 #if __STDC_VERSION__ < 202311L
242 /* WG14 DR035: partial
243 * Questions about definition of functions without a prototype
244 */
245 void dr035_1(a, b) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */
246 int a(enum b {x, y}); /* expected-warning {{declaration of 'enum b' will not be visible outside of this function}} */
247 int b; {
248 int test = x; /* expected-error {{use of undeclared identifier 'x'}} */
249 }
250
251 void dr035_2(c) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */
252 enum m{q, r} c; { /* expected-warning {{declaration of 'enum m' will not be visible outside of this function}} */
253 /* FIXME: This should be accepted because the scope of m, q, and r ends at
254 * the closing brace of the function per C89 6.1.2.1.
255 */
256 int test = q; /* expected-error {{use of undeclared identifier 'q'}} */
257 }
258 #endif /* __STDC_VERSION__ < 202311L */
259
260 /* WG14 DR038: yes
261 * Questions about argument substitution during macro expansion
262 */
263 #define DR038_X 0x000E
264 #define DR038_Y 0x0100
265 #define DR038(a) a
266 _Static_assert(DR038(DR038_X + DR038_Y) == DR038_X + DR038_Y, "fail");
267
268 /* WG14 DR039: yes
269 * Questions about the "C" locale
270 */
271 _Static_assert(sizeof('a') == sizeof(int), "fail");
272
273 /* WG14 DR040: partial
274 * 9 unrelated questions about C89
275 *
276 * Question 6
277 */
278 struct dr040 { /* expected-note {{definition of 'struct dr040' is not complete until the closing '}'}} */
279 char c;
280 short s;
281 int i[__builtin_offsetof(struct dr040, s)]; /* expected-error {{offsetof of incomplete type 'struct dr040'}} */
282 };
283
284 /* WG14 DR043: yes
285 * On the definition of the NULL macro
286 */
287 void dr043(void) {
288 #include <stddef.h>
289 /* NULL has to be an integer constant expression with the value 0, or such an
290 * expression cast to void *. If it's an integer constant expression other
291 * than the literal 0 (such as #define NULL 4-4), this would fail to compile
292 * unless the macro replacement list is properly parenthesized as it would
293 * expand to: (void)(void *)4-4;
294 */
295 (void)(void *)NULL;
296
297 /* If the NULL macro is an integer constant expression with the value 0 and
298 * it has been cast to void *, ensure that it's also fully parenthesized. If
299 * it isn't (such as #define NULL (void *)0), this would fail to compile as
300 * would expand to (void *)0->a; which gives a diagnostic about int not being
301 * a pointer, instead of((void *)0)->a; which gives a diagnostic about the
302 * base reference being void and not a structure.
303 */
304 NULL->a; /* expected-error {{member reference base type 'void' is not a structure or union}} */
305 }
306
307 /* WG14 DR044: yes
308 * On the result of the offsetof macro
309 */
310 void dr044(void) {
311 #include <stddef.h>
312 struct S { int a, b; };
313 /* Ensure that the result of offsetof is usable in a constant expression. */
314 _Static_assert(offsetof(struct S, b) == sizeof(int), "fail");
315 }
316
317 /* WG14 DR046: yes
318 * Use of typedef names in parameter declarations
319 */
320 typedef int dr046_t;
321 int dr046(int dr046_t) { return dr046_t; }
322
323 /* WG14 DR047: yes
324 * Questions about declaration conformance
325 */
326 struct dr047_t; /* expected-note 2 {{forward declaration of 'struct dr047_t'}} */
327 struct dr047_t *dr047_1(struct dr047_t *p) {return p; }
328 struct dr047_t *dr047_2(struct dr047_t a[]) {return a; } /* expected-error {{array has incomplete element type 'struct dr047_t'}} */
329 int *dr047_3(int a2[][]) {return *a2; } /* expected-error {{array has incomplete element type 'int[]'}} */
330 extern struct dr047_t es1;
331 extern struct dr047_t es2[1]; /* expected-error {{array has incomplete element type 'struct dr047_t'}} */
332
333 /* WG14 DR050: yes
334 * Do wide string literals implicitly include <stddef.h>?
335 */
336 void dr050(void) {
337 /* The NULL macro is previously defined because we include <stddef.h> for
338 * other tests. Undefine the macro to demonstrate that use of a wide string
339 * literal doesn't magically include the header file.
340 */
341 #undef NULL
342 (void)L"huttah!";
343 (void)NULL; /* expected-error {{use of undeclared identifier 'NULL'}} */
344 }
345
346 #if __STDC_VERSION__ < 202311L
347 /* WG14 DR053: yes
348 * Accessing a pointer to a function with a prototype through a pointer to
349 * pointer to function without a prototype
350 */
351 void dr053(void) {
352 int f(int);
353 int (*fp1)(int);
354 int (*fp2)(); /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */
355 int (**fpp)(); /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */
356
357 fp1 = f;
358 fp2 = fp1;
359 (*fp2)(3); /* expected-warning {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}} */
360 fpp = &fp1;
361 (**fpp)(3); /* expected-warning {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}} */
362 }
363 #endif /* __STDC_VERSION__ < 202311L */
364
365 /* WG14 DR064: yes
366 * Null pointer constants
367 */
368 char *dr064_1(int i, int *pi) {
369 *pi = i;
370 return 0;
371 }
372
373 char *dr064_2(int i, int *pi) {
374 return (*pi = i, 0); /* expected-error {{incompatible integer to pointer conversion returning 'int' from a function with result type 'char *'}} */
375 }
376
377 /* WG14 DR068: yes
378 * 'char' and signed vs unsigned integer types
379 */
380 void dr068(void) {
381 #include <limits.h>
382
383 #if CHAR_MAX == SCHAR_MAX
384 /* char is signed */
385 _Static_assert('\xFF' == -1, "fail");
386 #else
387 /* char is unsigned */
388 _Static_assert('\xFF' == 0xFF, "fail");
389 #endif
390 }
391
392 #if __STDC_VERSION__ < 202311L
393 /* WG14: DR070: yes
394 * Interchangeability of function arguments
395 *
396 * Note: we could issue a pedantic warning in this case. We are claiming
397 * conformance not because we diagnose the UB when we could but because we're
398 * not obligated to do anything about it and we make it "just work" via the
399 * usual conversion rules.
400 *
401 * This behavior is specific to functions without prototypes. A function with
402 * a prototype causes implicit conversions rather than relying on default
403 * argument promotion and warm thoughts.
404 */
405 void dr070_1(c) /* expected-warning {{a function definition without a prototype is deprecated in all versions of C and is not supported in C23}} */
406 int c; {
407 }
408
409 void dr070_2(void) {
410 dr070_1(6);
411 dr070_1(6U); /* Pedantically UB */
412 }
413 #endif /* __STDC_VERSION__ < 202311L */
414
415 /* WG14 DR071: yes
416 * Enumerated types
417 */
418 enum dr071_t { foo_A = 0, foo_B = 1, foo_C = 8 };
419 void dr071(void) {
420 /* Test that in-range values not present in the enumeration still round-trip
421 * to the original value.
422 */
423 _Static_assert(100 == (int)(enum dr071_t)100, "fail");
424 }
425
426 /* WG14 DR081: yes
427 * Left shift operator
428 */
429 void dr081(void) {
430 /* Demonstrate that we don't crash when left shifting a signed value; that's
431 * implementation defined behavior.
432 */
433 _Static_assert(-1 << 1 == -2, "fail"); /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
434 expected-note {{left shift of negative value -1}} */
435 _Static_assert(1 << 3 == 1u << 3u, "fail"); /* Shift of a positive signed value does sensible things. */
436 }
437
438 /* WG14 DR084: yes
439 * Incomplete type in function declaration
440 *
441 * Note: because the situation is UB, we're free to do what we want. We elect
442 * to accept and require the incomplete type to be completed before the
443 * function definition.
444 */
445 struct dr084_t; /* expected-note {{forward declaration of 'struct dr084_t'}} */
446 extern void (*dr084_1)(struct dr084_t);
447 void dr084_2(struct dr084_t);
448 void dr084_2(struct dr084_t val) {} /* expected-error {{variable has incomplete type 'struct dr084_t'}} */
449
450 /* WG14 DR088: yes
451 * Compatibility of incomplete types
452 */
453 struct dr088_t_1;
454
455 void dr088_f(struct dr088_t_1 *); /* expected-note {{passing argument to parameter here}} */
456 void dr088_1(void) {
457 /* Distinct type from the file scope forward declaration. */
458 struct dr088_t_1;
459 /* FIXME: this diagnostic could be improved to not be utterly baffling. */
460 dr088_f((struct dr088_t_1 *)0); /* expected-warning {{incompatible pointer types passing 'struct dr088_t_1 *' to parameter of type 'struct dr088_t_1 *'}} */
461 }
462
463 void dr088_2(struct dr088_t_1 *p) { /* Pointer to incomplete type. */ }
464 struct dr088_t_1 { int i; }; /* Type is completed. */
465 void dr088_3(struct dr088_t_1 s) {
466 /* When passing a pointer to the completed type, is it the same type as the
467 * incomplete type used in the call declaration?
468 */
469 dr088_2(&s);
470 }
471
472 /* WG14 DR089: yes
473 * Multiple definitions of macros
474 */
475 #define DR089 object_like /* expected-note {{previous definition is here}} */
476 #define DR089(argument) function_like /* expected-warning {{'DR089' macro redefined}} */
477
478 /* WG14 DR095: yes
479 * Is initialization as constrained as assignment?
480 */
481 void dr095(void) {
482 /* Ensure that type compatibility constraints on assignment are also honored
483 * for initializations.
484 */
485 struct One {
486 int a;
487 } one;
488 struct Two {
489 float f;
490 } two = one; /* expected-error {{initializing 'struct Two' with an expression of incompatible type 'struct One'}} */
491
492 two = one; /* expected-error {{assigning to 'struct Two' from incompatible type 'struct One'}} */
493 }
494
495 /* WG14 DR096: yes
496 * Arrays of incomplete types
497 */
498 void dr096(void) {
499 typedef void func_type(void);
500 func_type array_funcs[10]; /* expected-error {{'array_funcs' declared as array of functions of type 'func_type' (aka 'void (void)')}} */
501
502 void array_void[10]; /* expected-error {{array has incomplete element type 'void'}} */
503
504 struct S; /* expected-note {{forward declaration of 'struct S'}} */
505 struct S s[10]; /* expected-error {{array has incomplete element type 'struct S'}} */
506
507 union U; /* expected-note {{forward declaration of 'union U'}} */
508 union U u[10]; /* expected-error {{array has incomplete element type 'union U'}} */
509 union U { int i; };
510
511 int never_completed_incomplete_array[][]; /* expected-error {{array has incomplete element type 'int[]'}} */
512
513 extern int completed_later[][]; /* expected-error {{array has incomplete element type 'int[]'}} */
514 extern int completed_later[10][10];
515 }
516
517 /* WG14 DR098: yes
518 * Pre/post increment/decrement of function or incomplete types
519 */
520 void dr098(void) {
521 typedef void func_type(void);
522 func_type fp;
523 struct incomplete *incomplete_ptr;
524
525 ++fp; /* expected-error {{cannot increment value of type 'func_type' (aka 'void (void)')}} */
526 fp++; /* expected-error {{cannot increment value of type 'func_type' (aka 'void (void)')}} */
527 --fp; /* expected-error {{cannot decrement value of type 'func_type' (aka 'void (void)')}} */
528 fp--; /* expected-error {{cannot decrement value of type 'func_type' (aka 'void (void)')}} */
529
530 (*incomplete_ptr)++; /* expected-error {{cannot increment value of type 'struct incomplete'}} */
531 ++(*incomplete_ptr); /* expected-error {{cannot increment value of type 'struct incomplete'}} */
532 (*incomplete_ptr)--; /* expected-error {{cannot decrement value of type 'struct incomplete'}} */
533 --(*incomplete_ptr); /* expected-error {{cannot decrement value of type 'struct incomplete'}} */
534 }