xref: /llvm-project/clang/test/C/C23/n2975.c (revision 50c81128de8616117118564eff22cf508cba7848)
1*50c81128SAaron Ballman // RUN: %clang_cc1 -verify -ffreestanding -Wpre-c2x-compat -std=c2x %s
2*50c81128SAaron Ballman 
3*50c81128SAaron Ballman /* WG14 N2975: partial
4*50c81128SAaron Ballman  * Relax requirements for va_start
5*50c81128SAaron Ballman  */
6*50c81128SAaron Ballman 
7*50c81128SAaron Ballman #include <stdarg.h>
8*50c81128SAaron Ballman 
9*50c81128SAaron Ballman #define DERP this is an error
10*50c81128SAaron Ballman 
func(...)11*50c81128SAaron Ballman void func(...) { // expected-warning {{'...' as the only parameter of a function is incompatible with C standards before C23}}
12*50c81128SAaron Ballman   // Show that va_start doesn't require the second argument in C23 mode.
13*50c81128SAaron Ballman   va_list list;
14*50c81128SAaron Ballman   va_start(list); // expected-warning {{passing no argument for the '...' parameter of a variadic macro is incompatible with C standards before C23}} expected-note@* {{macro 'va_start' defined here}}
15*50c81128SAaron Ballman   va_end(list);
16*50c81128SAaron Ballman 
17*50c81128SAaron Ballman   // Show that va_start doesn't expand or evaluate the second argument.
18*50c81128SAaron Ballman   va_start(list, DERP);
19*50c81128SAaron Ballman   va_end(list);
20*50c81128SAaron Ballman 
21*50c81128SAaron Ballman   // FIXME: it would be kinder to diagnose this instead of silently accepting it.
22*50c81128SAaron Ballman   va_start(list, 1, 2);
23*50c81128SAaron Ballman   va_end(list);
24*50c81128SAaron Ballman 
25*50c81128SAaron Ballman   // We didn't change the behavior of __builtin_va_start (and neither did GCC).
26*50c81128SAaron Ballman   __builtin_va_start(list); // expected-error {{too few arguments to function call, expected 2, have 1}}
27*50c81128SAaron Ballman 
28*50c81128SAaron Ballman   // Verify that the return type of a call to va_start is 'void'.
29*50c81128SAaron Ballman   _Static_assert(__builtin_types_compatible_p(__typeof__(va_start(list)), void), ""); // expected-warning {{passing no argument for the '...' parameter of a variadic macro is incompatible with C standards before C23}} expected-note@* {{macro 'va_start' defined here}}
30*50c81128SAaron Ballman   _Static_assert(__builtin_types_compatible_p(__typeof__(__builtin_va_start(list, 0)), void), "");
31*50c81128SAaron Ballman }
32*50c81128SAaron Ballman 
33*50c81128SAaron Ballman // Show that function pointer types also don't need an argument before the
34*50c81128SAaron Ballman // ellipsis.
35*50c81128SAaron Ballman typedef void (*fp)(...); // expected-warning {{'...' as the only parameter of a function is incompatible with C standards before C23}}
36*50c81128SAaron Ballman 
37*50c81128SAaron Ballman // Passing something other than the argument before the ... is still not valid.
diag(int a,int b,...)38*50c81128SAaron Ballman void diag(int a, int b, ...) {
39*50c81128SAaron Ballman   va_list list;
40*50c81128SAaron Ballman   // FIXME: the call to va_start should also diagnose the same way as the call
41*50c81128SAaron Ballman   // to __builtin_va_start. However, because va_start is not allowed to expand
42*50c81128SAaron Ballman   // or evaluate the second argument, we can't pass it along to
43*50c81128SAaron Ballman   // __builtin_va_start to get that diagnostic. So in C17 and earlier, we will
44*50c81128SAaron Ballman   // diagnose this use through the macro, but in C23 and later we've lost the
45*50c81128SAaron Ballman   // diagnostic entirely. GCC has the same issue currently.
46*50c81128SAaron Ballman   va_start(list, a);
47*50c81128SAaron Ballman   // However, the builtin itself is under no such constraints regarding
48*50c81128SAaron Ballman   // expanding or evaluating the second argument, so it can still diagnose.
49*50c81128SAaron Ballman   __builtin_va_start(list, a); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
50*50c81128SAaron Ballman   va_end(list);
51*50c81128SAaron Ballman }
52*50c81128SAaron Ballman 
53*50c81128SAaron Ballman void foo(int a...); // expected-error {{C requires a comma prior to the ellipsis in a variadic function type}}
54*50c81128SAaron Ballman 
use(void)55*50c81128SAaron Ballman void use(void) {
56*50c81128SAaron Ballman   // Demonstrate that we can actually call the variadic function when it has no
57*50c81128SAaron Ballman   // formal parameters.
58*50c81128SAaron Ballman   func(1, '2', 3.0, "4");
59*50c81128SAaron Ballman   func();
60*50c81128SAaron Ballman 
61*50c81128SAaron Ballman   // And that assignment still works as expected.
62*50c81128SAaron Ballman   fp local = func;
63*50c81128SAaron Ballman 
64*50c81128SAaron Ballman   // ...including conversion errors.
65*50c81128SAaron Ballman   fp other_local = diag; // expected-error {{incompatible function pointer types initializing 'fp' (aka 'void (*)(...)') with an expression of type 'void (int, int, ...)'}}
66*50c81128SAaron Ballman }
67