xref: /llvm-project/clang/test/C/drs/dr2xx.c (revision e4163c0927772f2ec73cf16d53e000614c419c45)
1 /* RUN: %clang_cc1 -std=c89 -fsyntax-only -verify=expected,c89only -pedantic -Wno-c11-extensions %s
2    RUN: %clang_cc1 -std=c99 -triple x86_64-unknown-linux -fsyntax-only -verify=expected,c99untilc2x -pedantic -Wno-c11-extensions %s
3    RUN: %clang_cc1 -std=c99 -triple x86_64-unknown-win32 -fms-compatibility -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 DR201: yes
13  * Integer types longer than long
14  *
15  * WG14 DR211: yes
16  * Accuracy of decimal string to/from "binary" (non-decimal) floating-point conversions
17  *
18  * WG14 DR215: yes
19  * Equality operators
20  *
21  * WG14 DR218: yes
22  * Signs of non-numeric floating point values
23  *
24  * WG14 DR219: yes
25  * Effective types
26  *
27  * WG14 DR221: yes
28  * Lacuna in pointer arithmetic
29  *
30  * WG14 DR222: yes
31  * Partially initialized structures
32  *
33  * WG14 DR234: yes
34  * Miscellaneous Typos
35  *
36  * WG14 DR245: yes
37  * Missing paragraph numbers
38  *
39  * WG14 DR247: yes
40  * Are values a form of behaviour?
41  *
42  * WG14 DR248: yes
43  * Limits are required for optional types
44  *
45  * WG14 DR255: yes
46  * Non-prototyped function calls and argument mismatches
47  *
48  * WG14 DR267: yes
49  * Typos in 5.1.2.3, 7.24.4.4.5, 7.24.6.1, 7.24.6.1
50  *
51  * WG14 DR273: yes
52  * Meaning of __STDC_ISO_10646__
53  *
54  * WG14 DR278: yes
55  * Lacuna in character encodings
56  *
57  * WG14 DR279: yes
58  * Wide character code values for members of the basic character set
59  *
60  * WG14 DR282: yes
61  * Flexible array members & struct padding
62  *
63  * WG14 DR292: yes
64  * Use of the word variable
65  */
66 
67 
68 /* WG14 DR204: yes
69  * size_t and ptrdiff_t as a long long type
70  */
71 void dr204(void) {
72   __typeof__(sizeof(0)) s;
73   __typeof__((int *)0 - (int *)0) p;
74   signed long sl;
75 #if __LLONG_WIDTH__ > __LONG_WIDTH__
76   /* If the implementation supports a standard integer type larger than signed
77    * long, it's okay for size_t and ptrdiff_t to have a greater integer
78    * conversion rank than signed long.
79    *
80    * Note, it's not required that the implementation use that larger conversion
81    * rank; it's acceptable to use an unsigned long or unsigned int for the size
82    * type (those ranks are not greater than that of signed long).
83    */
84    (void)_Generic(s + sl, unsigned long long : 1, unsigned long : 1, unsigned int : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
85    (void)_Generic(p + sl, signed long long : 1, signed long : 1, signed int : 1);       /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
86 #elif __LLONG_WIDTH__ == __LONG_WIDTH__
87   /* But if the implementation doesn't support a larger standard integer type
88    * than signed long, the conversion rank should prefer signed long if the type
89    * is signed (ptrdiff_t) or unsigned long if the type is unsigned (size_t).
90    *
91    * Note, as above, unsigned/signed int is also acceptable due to having a
92    * lesser integer conversion rank.
93    */
94    (void)_Generic(s + sl, unsigned long : 1, unsigned int : 1);
95    (void)_Generic(p + sl, signed long : 1, signed int : 1);
96 #else
97 #error "Something has gone off the rails"
98 #endif
99 }
100 
101 /* WG14 DR207: partial
102  * Handling of imaginary types
103  *
104  * FIXME: Clang recognizes the _Imaginary keyword but does not support the data
105  * type.
106  */
107 void dr207(void) {
108   _Imaginary float f; /* expected-error {{imaginary types are not supported}}
109                          c89only-warning {{'_Imaginary' is a C99 extension}}
110                        */
111 }
112 
113 /* WG14 DR216: yes
114  * Source character encodings
115  */
116 void dr216(void) {
117 #define A(x) _Static_assert((char)x >= 0, "no")
118   A('A'); A('B'); A('C'); A('D'); A('E'); A('F'); A('G'); A('H'); A('I');
119   A('J'); A('K'); A('L'); A('M'); A('N'); A('O'); A('P'); A('Q'); A('R');
120   A('S'); A('T'); A('U'); A('V'); A('W'); A('X'); A('Y'); A('Z');
121 
122   A('a'); A('b'); A('c'); A('d'); A('e'); A('f'); A('g'); A('h'); A('i');
123   A('j'); A('k'); A('l'); A('m'); A('n'); A('o'); A('p'); A('q'); A('r');
124   A('s'); A('t'); A('u'); A('v'); A('w'); A('x'); A('y'); A('z');
125 
126   A('0'); A('1'); A('2'); A('3'); A('4');
127   A('5'); A('6'); A('7'); A('8'); A('9');
128 
129   A('!'); A('"'); A('#'); A('%'); A('&'); A('\''); A('('); A(')'); A('*');
130   A('+'); A(','); A('-'); A('.'); A('/'); A(':'); A(';'); A('<'); A('=');
131   A('>'); A('?'); A('['); A('\\'); A(']'); A('^'); A('_'); A('{'); A('|');
132   A('}'); A('~');
133 #undef A
134 }
135 
136 /* WG14 DR230: yes
137  * Enumerated type rank
138  */
139 void dr230(void) {
140   enum E {
141     Value = __INT_MAX__
142   } e;
143   /* The enumeration type has a compatible type that is a signed or unsigned
144    * integer type, or char. But it has to be large enough to hold all of the
145    * values of the enumerators. So it needs to be at least int or unsigned int.
146    *
147    * The integer conversion rank for an enumeration is the same as its
148    * compatible type (C99 6.3.1.1p1), so it's eligible for integer promotions
149    * to either int or unsigned int, depending on the compatible type
150    * (C99 6.3.1.1p2).
151    */
152   (void)_Generic(e, int : 1, unsigned int : 1);
153   (void)_Generic((enum E)Value, int : 1, unsigned int : 1);
154   /* The enumerators themselves have type int (C99 6.7.2.2p3). */
155   (void)_Generic(Value, int : 1);
156 }
157 
158 /* WG14 DR231: no
159  * Semantics of text-line and non-directive
160  *
161  * One of the preprocessing groups to support is # non-directive (C99 6.10p1),
162  * which is defined as pp-tokens followed by a newline. However, we fail to
163  * translate the program if we don't recognize the directive, and we don't take
164  * note when what follows the # is not a valid preprocessing token.
165  */
166 
167 /* FIXME: this should not fail. */
168 # nope /* expected-error {{invalid preprocessing directive}} */
169 
170 /* FIXME: this should fail, but not because of the unknown directive; it should
171  * fail because of the invalid preprocessing-token.
172  */
173 # 'a
174 /* expected-error@-1 {{invalid preprocessing directive}} \
175    expected-warning@-1 {{missing terminating ' character}}
176 */
177 
178 /* WG14 DR237: no
179  * Declarations using [static]
180  */
181 void dr237_f(int array[static 10]); /* c89only-warning {{static array size is a C99 feature}}
182                                        expected-note {{callee declares array parameter as static here}}
183                                      */
184 void dr237_1(void) {
185   int array[4];
186   dr237_f(array); /* expected-warning {{array argument is too small; contains 4 elements, callee requires at least 10}} */
187 }
188 
189 /* FIXME: the composite type for this declaration should retain the static
190  * array extent instead of losing it.
191  */
192 void dr237_f(int array[]);
193 
194 void dr237_2(void) {
195   int array[4];
196   /* FIXME: this should diagnose the same as above. */
197   dr237_f(array);
198 }
199 
200 /* WG14 DR246: yes
201  * Completion of declarators
202  */
203 void dr246(void) {
204   int i[i]; /* expected-error {{use of undeclared identifier 'i'}} */
205 }
206 
207 /* WG14 DR250: yes
208  * Non-directives within macro arguments
209  */
210 void dr250(void) {
211 #define dr250_nothing(x)
212 
213   /* FIXME: See DR231 regarding the error about an invalid preprocessing
214    * directive.
215    */
216 
217   dr250_nothing(
218 #nondirective    /* expected-error {{invalid preprocessing directive}}
219                     expected-warning {{embedding a directive within macro arguments has undefined behavior}}
220                   */
221   )
222 
223 #undef dr250_nothing
224 }
225 
226 /* WG14 DR251: yes
227  * Are struct fred and union fred the same type?
228  */
229 union dr251_fred { int a; }; /* expected-note {{previous use is here}} */
230 void dr251(void) {
231   struct dr251_fred *ptr; /* expected-error {{use of 'dr251_fred' with tag type that does not match previous declaration}} */
232 }
233 
234 #if __STDC_VERSION__ < 202311L
235 /* WG14 DR252: yes
236  * Incomplete argument types when calling non-prototyped functions
237  */
238 void dr252_no_proto();  /* expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} */
239 void dr252_proto(void); /* expected-note {{'dr252_proto' declared here}} */
240 void dr252(void) {
241   /* It's a constraint violation to pass an argument to a function with a
242    * prototype that specifies a void parameter.
243    */
244   dr252_proto(dr252_no_proto()); /* expected-error {{too many arguments to function call, expected 0, have 1}} */
245 
246   /* It's technically UB to pass an incomplete type to a function without a
247    * prototype, but Clang treats it as an error.
248    */
249   dr252_no_proto(dr252_proto()); /* expected-error {{argument type 'void' is incomplete}}
250                                     expected-warning {{passing arguments to 'dr252_no_proto' without a prototype is deprecated in all versions of C and is not supported in C23}}
251                                   */
252 }
253 #endif /* __STDC_VERSION__ < 202311L */
254 
255 /* WG14 DR258: yes
256  * Ordering of "defined" and macro replacement
257  */
258 void dr258(void) {
259   /* We get the diagnostic twice because the argument is used twice in the
260    * expansion. */
261 #define repeat(x) x && x
262 #if repeat(defined fred) /* expected-warning 2 {{macro expansion producing 'defined' has undefined behavior}} */
263 #endif
264 
265   /* We get no diagnostic because the argument is unused. */
266 #define forget(x) 0
267 #if forget(defined fred)
268 #endif
269 
270 #undef repeat
271 #undef forget
272 }
273 
274 /* WG14 DR261: yes
275  * Constant expressions
276  */
277 void dr261(void) {
278   /* This is not an integer constant expression because of the overflow,
279    * but we fold it as a constant expression anyway as a GNU extension. */
280   enum e1 {
281     ex1 = __INT_MAX__ + 1  /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}}
282                               expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
283                               expected-note {{value 2147483648 is outside the range of representable values of type 'int'}} */
284   };
285 
286   /* This is not an integer constant expression, because of the comma operator,
287    * but we fold it as a constant expression anyway as a GNU extension.
288    */
289   enum e2 {
290     ex2 = __INT_MAX__ + (0, 1) /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
291                                   expected-note {{value 2147483648 is outside the range of representable values of type 'int'}}
292                                   expected-warning {{left operand of comma operator has no effect}}
293                                 */
294   };
295 
296   /* It's a bit weird that we issue a "congratulations, you did the thing"
297    * diagnostic, but the diagnostic does help demonstrate that we correctly
298    * treat it as a null pointer constant value.
299    */
300   char *p1 = (1 - 1); /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */
301 
302   /* This is an invalid initialization/assignment because the right-hand side
303    * does not have pointer to void or pointer to char type and is not the null
304    * pointer constant. */
305   char *p2 = (42, 1 - 1); /* expected-error {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'int'}}
306                              expected-warning {{left operand of comma operator has no effect}}
307                            */
308   p1 = (42, 1 - 1);       /* expected-error {{incompatible integer to pointer conversion assigning to 'char *' from 'int'}}
309                              expected-warning {{left operand of comma operator has no effect}}
310                            */
311 
312   /* These are both valid. The initialization doesn't require an integer
313    * constant expression, nor does the assignment.
314    */
315   short s1 = 42 + (0, 1); /* c89only-warning {{mixing declarations and code is a C99 extension}}
316                              expected-warning {{left operand of comma operator has no effect}}
317                            */
318   s1 = (42, 69); /* expected-warning {{left operand of comma operator has no effect}} */
319 
320   /* These are both valid because they are constant expressions and the value
321    * is the null pointer constant.
322    */
323   p2 = 0;
324   p2 = 1 - 1; /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */
325 }
326 
327 /* WG14 DR262: yes
328  * Maximum size of bit fields
329  */
330 void dr262(void) {
331   _Static_assert(sizeof(short) == 2, "short is not two chars?");
332   struct S {
333     short field : __CHAR_BIT__ * 2; /* ok */
334     short other_field : __CHAR_BIT__ * 2 + 1; /* expected-error-re {{width of bit-field 'other_field' ({{[0-9]+}} bits) exceeds the width of its type ({{[0-9]+}} bits)}} */
335   };
336 }
337 
338 /* WG14 DR263: yes
339  * All-zero bits representations
340  *
341  * This tests that the integer value 0 is not comprised of any non-zero bits,
342  * which demonstrates that a value with all zero bits will be treated as the
343  * integer value zero.
344  */
345 _Static_assert(__builtin_popcount(0) < 1, "zero is not all zero bits");
346 
347 
348 /* WG14 DR265: yes
349  * Preprocessor arithmetic
350  */
351 #if __UINT_MAX__ == 0xFFFFFFFF
352 /* Ensure that the literal is interpreted as intptr_t instead of uintptr_t,
353  * despite that being the phase 7 behavior being that the literal is unsigned.
354  */
355 #if -0xFFFFFFFF >= 0
356 #error "Interpreting the literal incorrectly in the preprocessor"
357 #endif
358 #endif /* __UINT_MAX__ == 0xFFFFFFFF */
359 
360 
361 /* WG14 DR266: yes
362  * Overflow of sizeof
363  */
364 void dr266(void) {
365   /* Some targets support a maximum size which cannot be represented by an
366    * unsigned long, and so unsigned long long is used instead. However, C89
367    * doesn't have the long long type, so we issue a pedantic warning about it.
368    * Disable the warning momentarily so we don't have to add target triples to
369    * the RUN lines pinning the targets down concretely.
370    */
371 #pragma clang diagnostic push
372 #pragma clang diagnostic ignored "-Wlong-long"
373   (void)sizeof(int[__SIZE_MAX__ / 2][__SIZE_MAX__ / 2]); /* expected-error-re 2 {{array is too large ({{[0-9]+}} elements)}} */
374 #pragma clang diagnostic pop
375 }
376 
377 /* WG14 DR272: yes
378  * Type category
379  */
380 void dr272(void) {
381   /* The crux of this DR is to confirm that lvalue conversion of the rhs on an
382    * assignment expression strips top-level qualifiers, and not all qualifiers,
383    * from the resulting expression type.
384    */
385   const int * volatile ptr;
386   (void)_Generic(ptr = 0, const int * : 1); /* expected-warning {{expression with side effects has no effect in an unevaluated context}} */
387 }
388 
389 /* WG14 DR277: no
390  * Declarations within iteration statements
391  */
392 void dr277(void) {
393   /* FIXME: it's a bit silly to issue both of these warnings at the same time
394    * in pedantic mode given that they're both effectively the same root cause.
395    *
396    * C99 6.8.5p3: The declaration part of a for statement shall only declare
397    * identifiers for objects having storage class auto or register.
398    *
399    * FIXME: we don't issue a pedantic warning below for the declaration of E,
400    * and its enumerators, none of which declare an object with auto or register
401    * storage classes.
402    */
403   for (enum E { one, two } i = one; i < two; ++i) /* c89only-warning {{variable declaration in for loop is a C99-specific feature}}
404                                                      c89only-warning {{GCC does not allow variable declarations in for loop initializers before C99}}
405                                                    */
406     ;
407 }
408 
409 #if __STDC_VERSION__ >= 199901L
410 /* WG14 DR289: yes
411  * Function prototype with [restrict]
412  *
413  * Ensure that we support [restrict] array syntax as an abstract declarator and
414  * not just as a direct declarator.
415  */
416 void dr289(int * restrict const [restrict]);
417 #endif /* __STDC_VERSION__ >= 199901L */
418 
419 /* WG14 DR295: yes
420  * Incomplete types for function parameters
421  */
422 struct NotCompleted;                    /* expected-note {{forward declaration of 'struct NotCompleted'}} */
423 void dr295_1(struct NotCompleted);
424 void dr295_1(struct NotCompleted Val) { /* expected-error {{variable has incomplete type 'struct NotCompleted'}} */
425 }
426 
427 /* There's no reason to reject this code, but it's technically undefined
428  * behavior, so diagnosing it is reasonable.
429  *
430  * FIXME: either downgrade this error into a warning or remove it entirely; it
431  * doesn't add a whole lot of value as an error.
432  */
433 void dr295_2(void param); /* expected-error {{argument may not have 'void' type}} */
434 
435 /* WG14 DR298: partial
436  * Validity of constant in unsigned long long range
437  *
438  * I'm giving this one a partial because we fail to pedantically diagnose the
439  * use of 'long long' through a constant value. We correctly warn about the
440  * type when spelled out and when using an explicit suffix, but we fail to warn
441  * otherwise.
442  */
443 #if __LLONG_WIDTH__ >= 64 && __LONG_WIDTH__ < 64
444 /* This test requires that long long be at least 64-bits and long be smaller
445  * because the test is whether the integer literal which is too large to fit in
446  * a constant of type long long. This is undefined behavior in C, which means
447  * we're free to pick a different type so long as we diagnose the extension
448  * appropriately.
449  */
450 void dr298(void) {
451   /* FIXME: These uses of the constants need a pedantic warning in C89 mode;
452    * we've picked a type that does not exist in C89.
453    */
454   (void)_Generic(9223372036854775808,     /* expected-warning {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
455                                              c89only-warning {{'long long' is an extension when C99 mode is not enabled}}
456                                            */
457                  unsigned long long : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
458   (void)_Generic(9223372036854775807,     /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
459                  long long : 1);          /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
460 }
461 #endif /* __LLONG_WIDTH__ == 64 && __LONG_WIDTH__ < 64 */
462