File: n2975.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (66 lines) | stat: -rw-r--r-- 3,125 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// RUN: %clang_cc1 -verify -ffreestanding -Wpre-c2x-compat -std=c2x %s

/* WG14 N2975: partial
 * Relax requirements for va_start
 */

#include <stdarg.h>

#define DERP this is an error

void func(...) { // expected-warning {{'...' as the only parameter of a function is incompatible with C standards before C23}}
  // Show that va_start doesn't require the second argument in C23 mode.
  va_list list;
  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}}
  va_end(list);

  // Show that va_start doesn't expand or evaluate the second argument.
  va_start(list, DERP);
  va_end(list);

  // FIXME: it would be kinder to diagnose this instead of silently accepting it.
  va_start(list, 1, 2);
  va_end(list);

  // We didn't change the behavior of __builtin_va_start (and neither did GCC).
  __builtin_va_start(list); // expected-error {{too few arguments to function call, expected 2, have 1}}

  // Verify that the return type of a call to va_start is 'void'.
  _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}}
  _Static_assert(__builtin_types_compatible_p(__typeof__(__builtin_va_start(list, 0)), void), "");
}

// Show that function pointer types also don't need an argument before the
// ellipsis.
typedef void (*fp)(...); // expected-warning {{'...' as the only parameter of a function is incompatible with C standards before C23}}

// Passing something other than the argument before the ... is still not valid.
void diag(int a, int b, ...) {
  va_list list;
  // FIXME: the call to va_start should also diagnose the same way as the call
  // to __builtin_va_start. However, because va_start is not allowed to expand
  // or evaluate the second argument, we can't pass it along to
  // __builtin_va_start to get that diagnostic. So in C17 and earlier, we will
  // diagnose this use through the macro, but in C23 and later we've lost the
  // diagnostic entirely. GCC has the same issue currently.
  va_start(list, a);
  // However, the builtin itself is under no such constraints regarding
  // expanding or evaluating the second argument, so it can still diagnose.
  __builtin_va_start(list, a); // expected-warning {{second argument to 'va_start' is not the last named parameter}}
  va_end(list);
}

void foo(int a...); // expected-error {{C requires a comma prior to the ellipsis in a variadic function type}}

void use(void) {
  // Demonstrate that we can actually call the variadic function when it has no
  // formal parameters.
  func(1, '2', 3.0, "4");
  func();

  // And that assignment still works as expected.
  fp local = func;

  // ...including conversion errors.
  fp other_local = diag; // expected-error {{incompatible function pointer types initializing 'fp' (aka 'void (*)(...)') with an expression of type 'void (int, int, ...)'}}
}