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