Skip to content

Commit ab53e1c

Browse files
authored
[C2y] Claim conformance to WG14 N3363 (#130980)
This paper clarifies two things: * a call to va_start must be within the body of a variadic function, * the va_list arguments must be lvalues Clang has correctly diagnosed the first point since Clang 5.0. Clang generally diagnoses the second point, except for va_copy. However, the second point is not a constraint violation, so a diagnostic is not strictly required. That said, it would be good for us to properly diagnose va_copy.
1 parent 7f415e4 commit ab53e1c

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

clang/test/C/C2y/n3363.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clang_cc1 -triple x86_64-pc-linux -verify=expected,array-type -std=c2y -Wall -pedantic -ffreestanding %s
2+
// RUN: %clang_cc1 -triple aarch64-pc-win32 -verify=expected,char-ptr-type -std=c2y -Wall -pedantic -ffreestanding %s
3+
4+
/* WG14 N3363: Clang 5
5+
* stdarg.h wording... v3
6+
*
7+
* Clarifies that va_start can only be used within a function body, and that
8+
* the macros and functions require an lvalue of type va_list instead of an
9+
* rvalue.
10+
*
11+
* Clang has diagnosed this correctly in all cases except va_copy since
12+
* Clang 5. va_copy still needs to be updated to issue a diagnostic about use
13+
* of an rvalue. However, our lack of a diagnostic is still conforming because
14+
* a diagnostic is not required (it's not a constraint violation).
15+
*/
16+
17+
#include <stdarg.h>
18+
19+
// The va_list argument given to every macro defined in this subclause shall be
20+
// an lvalue of this type or the result of array-to-pointer decay of such an
21+
// lvalue.
22+
void f(int a, ...) {
23+
va_list rvalue(); // array-type-error {{function cannot return array type 'va_list' (aka '__builtin_va_list')}}
24+
va_start(rvalue()); // / char-ptr-type-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a temporary of type 'va_list' (aka 'char *')}}
25+
va_arg(rvalue(), int); // char-ptr-type-error {{expression is not assignable}}
26+
// FIXME: this should get two diagnostics about use of a non-lvalue.
27+
va_copy(rvalue(), rvalue()); // char-ptr-type-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a temporary of type 'va_list' (aka 'char *')}}
28+
va_end(rvalue()); // char-ptr-type-error {{non-const lvalue reference to type '__builtin_va_list' cannot bind to a temporary of type 'va_list' (aka 'char *')}}
29+
}
30+
31+
// The va_start macro may only be invoked in the compound-statement of the body
32+
// of a variadic function.
33+
void g(va_list ap, int [(va_start(ap), 1)], ...) { // expected-error {{'va_start' cannot be used outside a function}}
34+
va_end(ap);
35+
}
36+
37+
va_list ap;
38+
struct S {
39+
int array[(va_start(ap), 1)]; // expected-error {{'va_start' cannot be used outside a function}}
40+
};

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ <h2 id="c2y">C2y implementation status</h2>
263263
<tr>
264264
<td>stdarg.h wording... v3</td>
265265
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3363.pdf">N3363</a></td>
266-
<td class="unknown" align="center">Unknown</td>
266+
<td class="full" align="center">Clang 5</td>
267267
</tr>
268268
<tr>
269269
<td>Preprocessor integer expressions, v. 2</td>

0 commit comments

Comments
 (0)