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
9 /* The following are DRs which do not require tests to demonstrate
10 * conformance or nonconformance.
13 * Integer types longer than long
16 * Accuracy of decimal string to/from "binary" (non-decimal) floating-point conversions
22 * Signs of non-numeric floating point values
28 * Lacuna in pointer arithmetic
31 * Partially initialized structures
37 * Missing paragraph numbers
40 * Are values a form of behaviour?
43 * Limits are required for optional types
46 * Non-prototyped function calls and argument mismatches
49 * Typos in 5.1.2.3, 7.24.4.4.5, 7.24.6.1, 7.24.6.1
52 * Meaning of __STDC_ISO_10646__
55 * Lacuna in character encodings
58 * Wide character code values for members of the basic character set
61 * Flexible array members & struct padding
64 * Use of the word variable
69 * size_t and ptrdiff_t as a long long type
72 __typeof__(sizeof(0)) s
;
73 __typeof__((int *)0 - (int *)0) p
;
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.
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).
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).
91 * Note, as above, unsigned/signed int is also acceptable due to having a
92 * lesser integer conversion rank.
94 (void)_Generic(s
+ sl
, unsigned long : 1, unsigned int : 1);
95 (void)_Generic(p
+ sl
, signed long : 1, signed int : 1);
97 #error "Something has gone off the rails"
101 /* WG14 DR207: partial
102 * Handling of imaginary types
104 * FIXME: Clang recognizes the _Imaginary keyword but does not support the data
108 _Imaginary
float f
; /* expected-error {{imaginary types are not supported}}
109 c89only-warning {{'_Imaginary' is a C99 extension}}
114 * Source character encodings
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');
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');
126 A('0'); A('1'); A('2'); A('3'); A('4');
127 A('5'); A('6'); A('7'); A('8'); A('9');
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('|');
137 * Enumerated type rank
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.
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
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);
159 * Semantics of text-line and non-directive
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.
167 /* FIXME: this should not fail. */
168 # nope /* expected-error {{invalid preprocessing directive}} */
170 /* FIXME: this should fail, but not because of the unknown directive; it should
171 * fail because of the invalid preprocessing-token.
174 /* expected-error@-1 {{invalid preprocessing directive}} \
175 expected-warning@-1 {{missing terminating ' character}}
179 * Declarations using [static]
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}}
186 dr237_f(array
); /* expected-warning {{array argument is too small; contains 4 elements, callee requires at least 10}} */
189 /* FIXME: the composite type for this declaration should retain the static
190 * array extent instead of losing it.
192 void dr237_f(int array
[]);
196 /* FIXME: this should diagnose the same as above. */
201 * Completion of declarators
204 int i
[i
]; /* expected-error {{use of undeclared identifier 'i'}} */
208 * Non-directives within macro arguments
211 #define dr250_nothing(x)
213 /* FIXME: See DR231 regarding the error about an invalid preprocessing
218 #nondirective /* expected-error {{invalid preprocessing directive}}
219 expected-warning {{embedding a directive within macro arguments has undefined behavior}}
227 * Are struct fred and union fred the same type?
229 union dr251_fred
{ int a
; }; /* expected-note {{previous use is here}} */
231 struct dr251_fred
*ptr
; /* expected-error {{use of 'dr251_fred' with tag type that does not match previous declaration}} */
234 #if __STDC_VERSION__ < 202311L
236 * Incomplete argument types when calling non-prototyped functions
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}} */
241 /* It's a constraint violation to pass an argument to a function with a
242 * prototype that specifies a void parameter.
244 dr252_proto(dr252_no_proto()); /* expected-error {{too many arguments to function call, expected 0, have 1}} */
246 /* It's technically UB to pass an incomplete type to a function without a
247 * prototype, but Clang treats it as an error.
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}}
253 #endif /* __STDC_VERSION__ < 202311L */
256 * Ordering of "defined" and macro replacement
259 /* We get the diagnostic twice because the argument is used twice in the
261 #define repeat(x) x && x
262 #if repeat(defined fred) /* expected-warning 2 {{macro expansion producing 'defined' has undefined behavior}} */
265 /* We get no diagnostic because the argument is unused. */
267 #if forget(defined fred)
275 * Constant expressions
278 /* This is still an integer constant expression despite the overflow. */
280 ex1
= __INT_MAX__
+ 1 /* expected-warning {{overflow in expression; result is -2147483648 with type 'int'}} */
283 /* This is not an integer constant expression, because of the comma operator,
284 * but we fold it as a constant expression anyway as a GNU extension.
287 ex2
= __INT_MAX__
+ (0, 1) /* expected-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
288 expected-note {{value 2147483648 is outside the range of representable values of type 'int'}}
289 expected-warning {{left operand of comma operator has no effect}}
293 /* It's a bit weird that we issue a "congratulations, you did the thing"
294 * diagnostic, but the diagnostic does help demonstrate that we correctly
295 * treat it as a null pointer constant value.
297 char *p1
= (1 - 1); /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */
299 /* This is an invalid initialization/assignment because the right-hand side
300 * does not have pointer to void or pointer to char type and is not the null
301 * pointer constant. */
302 char *p2
= (42, 1 - 1); /* expected-error {{incompatible integer to pointer conversion initializing 'char *' with an expression of type 'int'}}
303 expected-warning {{left operand of comma operator has no effect}}
305 p1
= (42, 1 - 1); /* expected-error {{incompatible integer to pointer conversion assigning to 'char *' from 'int'}}
306 expected-warning {{left operand of comma operator has no effect}}
309 /* These are both valid. The initialization doesn't require an integer
310 * constant expression, nor does the assignment.
312 short s1
= 42 + (0, 1); /* c89only-warning {{mixing declarations and code is a C99 extension}}
313 expected-warning {{left operand of comma operator has no effect}}
315 s1
= (42, 69); /* expected-warning {{left operand of comma operator has no effect}} */
317 /* These are both valid because they are constant expressions and the value
318 * is the null pointer constant.
321 p2
= 1 - 1; /* expected-warning {{expression which evaluates to zero treated as a null pointer constant of type 'char *'}} */
325 * Maximum size of bit fields
328 _Static_assert(sizeof(short) == 2, "short is not two chars?");
330 short field
: __CHAR_BIT__
* 2; /* ok */
331 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)}} */
336 * All-zero bits representations
338 * This tests that the integer value 0 is not comprised of any non-zero bits,
339 * which demonstrates that a value with all zero bits will be treated as the
340 * integer value zero.
342 _Static_assert(__builtin_popcount(0) < 1, "zero is not all zero bits");
346 * Preprocessor arithmetic
348 #if __UINT_MAX__ == 0xFFFFFFFF
349 /* Ensure that the literal is interpreted as intptr_t instead of uintptr_t,
350 * despite that being the phase 7 behavior being that the literal is unsigned.
353 #error "Interpreting the literal incorrectly in the preprocessor"
355 #endif /* __UINT_MAX__ == 0xFFFFFFFF */
362 /* Some targets support a maximum size which cannot be represented by an
363 * unsigned long, and so unsigned long long is used instead. However, C89
364 * doesn't have the long long type, so we issue a pedantic warning about it.
365 * Disable the warning momentarily so we don't have to add target triples to
366 * the RUN lines pinning the targets down concretely.
368 #pragma clang diagnostic push
369 #pragma clang diagnostic ignored "-Wlong-long"
370 (void)sizeof(int[__SIZE_MAX__
/ 2][__SIZE_MAX__
/ 2]); /* expected-error-re 2 {{array is too large ({{[0-9]+}} elements)}} */
371 #pragma clang diagnostic pop
378 /* The crux of this DR is to confirm that lvalue conversion of the rhs on an
379 * assignment expression strips top-level qualifiers, and not all qualifiers,
380 * from the resulting expression type.
382 const int * volatile ptr
;
383 (void)_Generic(ptr
= 0, const int * : 1); /* expected-warning {{expression with side effects has no effect in an unevaluated context}} */
387 * Declarations within iteration statements
390 /* FIXME: it's a bit silly to issue both of these warnings at the same time
391 * in pedantic mode given that they're both effectively the same root cause.
393 * C99 6.8.5p3: The declaration part of a for statement shall only declare
394 * identifiers for objects having storage class auto or register.
396 * FIXME: we don't issue a pedantic warning below for the declaration of E,
397 * and its enumerators, none of which declare an object with auto or register
400 for (enum E
{ one
, two
} i
= one
; i
< two
; ++i
) /* c89only-warning {{variable declaration in for loop is a C99-specific feature}}
401 c89only-warning {{GCC does not allow variable declarations in for loop initializers before C99}}
406 #if __STDC_VERSION__ >= 199901L
408 * Function prototype with [restrict]
410 * Ensure that we support [restrict] array syntax as an abstract declarator and
411 * not just as a direct declarator.
413 void dr289(int * restrict
const [restrict
]);
414 #endif /* __STDC_VERSION__ >= 199901L */
417 * Incomplete types for function parameters
419 struct NotCompleted
; /* expected-note {{forward declaration of 'struct NotCompleted'}} */
420 void dr295_1(struct NotCompleted
);
421 void dr295_1(struct NotCompleted Val
) { /* expected-error {{variable has incomplete type 'struct NotCompleted'}} */
424 /* There's no reason to reject this code, but it's technically undefined
425 * behavior, so diagnosing it is reasonable.
427 * FIXME: either downgrade this error into a warning or remove it entirely; it
428 * doesn't add a whole lot of value as an error.
430 void dr295_2(void param
); /* expected-error {{argument may not have 'void' type}} */
432 /* WG14 DR298: partial
433 * Validity of constant in unsigned long long range
435 * I'm giving this one a partial because we fail to pedantically diagnose the
436 * use of 'long long' through a constant value. We correctly warn about the
437 * type when spelled out and when using an explicit suffix, but we fail to warn
440 #if __LLONG_WIDTH__ >= 64 && __LONG_WIDTH__ < 64
441 /* This test requires that long long be at least 64-bits and long be smaller
442 * because the test is whether the integer literal which is too large to fit in
443 * a constant of type long long. This is undefined behavior in C, which means
444 * we're free to pick a different type so long as we diagnose the extension
448 /* FIXME: These uses of the constants need a pedantic warning in C89 mode;
449 * we've picked a type that does not exist in C89.
451 (void)_Generic(9223372036854775808, /* expected-warning {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
452 c89only-warning {{'long long' is an extension when C99 mode is not enabled}}
454 unsigned long long : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
455 (void)_Generic(9223372036854775807, /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
456 long long : 1); /* c89only-warning {{'long long' is an extension when C99 mode is not enabled}} */
458 #endif /* __LLONG_WIDTH__ == 64 && __LONG_WIDTH__ < 64 */