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