Files
gcc-reflection/gcc/ginclude/stdarg.h
Jakub Jelinek 4e44fe4280 c++: C++26 va_start - part of P3348R4 - C++26 should refer to C23 not C17
The C++26 https://wg21.link/P3348R4 C++26 should refer to C23 not C17
paper among other things changes va_start macro in the similar way
how C23 has changed it.  Now, unlike C17 and older, C++ has since forever
allowed int (...) but just one wasn't able to use va_start/va_arg/va_end
in such functions.
With the current C++26 draft wording, we'd have to
  #define va_start(V, ...) __builtin_va_start (V, 0)
like we've used for C23 before the PR107980 change.
But Jonathan has kindly filed
https://cplusplus.github.io/LWG/issue4388
which similarly to C23 will if accepted allow to define it as
  #define va_start(...) __builtin_c23_va_start(__VA_ARGS__)
and let the compiler diagnose undesirable cases (see stdarg6.C
testcase in the patch for what it can diagnose, basically anything
that isn't either va_start (ap) or va_start (ap, i) where i is the
last argument's identifier).  This patch implements what assumes
LWG4388 will pass.

It also defines
  #define __STDC_VERSION_STDARG_H__ 202311L
also for C++26.

The hardest part is actually something different.
C23 had to differentiate between C99 void foo (); i.e. unspecified
arguments (but not stdarg) and the new C23 void bar (...); which
is stdarg, but in both cases TYPE_ARG_TYPES (fntype) is NULL.
This has been implemented through the new TYPE_NO_NAMED_ARGS_STDARG_P
flag, fntypes with that flag set are considered stdarg_p and allow
va_start in those, while fntypes with NULL TYPE_ARG_TYPES but the
flag cleared are not stdarg_p, can accept any number of arguments
but can't use va_start.
So, I had to change various places in the C++ FE to pass true
as the third argument to build_function_type for calls which are
meant to be (...) so that one can actually use va_start in those.
Done only for C++26 in order not to disturb older versions too much.
And there is a problem with some of the builtins and #pragma weak
which are using (...) declarations more in the sense of C17
unspecified arguments rather than this call has variable arguments.

So, structural_comptypes now considers the non-C++26 (...) used
for selected builtins and #pragma weak incompatible with C++26 (...)
to avoid ICEs.

2025-10-09  Jakub Jelinek  <jakub@redhat.com>

gcc/
	* ginclude/stdarg.h (va_start): Use __builtin_c23_va_start
	also for C++26.
	(__STDC_VERSION_STDARG_H__): Also define for C++26.
gcc/c-family/
	* c-common.h (D_CXX26): Define.
	* c-common.cc (c_common_resword): Add D_CXX26 to
	__builtin_c23_va_start flags, mention D_CXX26 in comment.
gcc/cp/
	* cp-tree.h (cp_build_function_type): Declare.
	* lex.cc: Implement va_start changes from P3348R4 - C++26 should
	refer to C23 not C17 paper.
	(init_reswords): Set D_CXX26 in mask for C++23 and older.
	* parser.cc (cp_parser_primary_expression): Handle RID_C23_VA_START.
	(cp_parser_builtin_c23_va_start): New function.
	* cp-objcp-common.cc (names_builtin_p): Likewise.
	* decl.cc (grokfndecl, check_function_type): Pass
	TYPE_NO_NAMED_ARGS_STDARG_P as last arg to build_function_type.
	(grokdeclarator, static_fn_type): Use cp_build_function_type instead
	of build_function_type.
	* typeck.cc (merge_types): Likewise.
	(structural_comptypes): Return false for TYPE_NO_NAMED_ARGS_STDARG_P
	differences.
	* lambda.cc (maybe_add_lambda_conv_op): Use cp_build_function_type
	instead of build_function_type.
	* tree.cc (cp_build_function_type): New function.
	(strip_typedefs): Pass TYPE_NO_NAMED_ARGS_STDARG_P as last arg to
	build_function_type.
	* name-lookup.cc (push_local_extern_decl_alias): Likewise.
	* module.cc (trees_in::tree_node): Use cp_build_function_type instead
	of build_function_type.
	* pt.cc (copy_default_args_to_explicit_spec,
	rebuild_function_or_method_type, build_deduction_guide): Likewise.
	(alias_ctad_tweaks): Pass TYPE_NO_NAMED_ARGS_STDARG_P as last arg to
	build_function_type.
	* decl2.cc (change_return_type, cp_reconstruct_complex_type):
	Likewise.
gcc/testsuite/
	* c-c++-common/cpp/has-builtin-4.c: Expect
	__has_builtin (__builtin_c23_va_start) == 1 also for C++26.
	* c-c++-common/Wvarargs.c (foo3): Don't expect undefined behavior
	warning for C++26.
	* g++.dg/cpp26/stdarg1.C: New test.
	* g++.dg/cpp26/stdarg2.C: New test.
	* g++.dg/cpp26/stdarg3.C: New test.
	* g++.dg/cpp26/stdarg4.C: New test.
	* g++.dg/cpp26/stdarg5.C: New test.
	* g++.dg/cpp26/stdarg6.C: New test.
	* g++.dg/cpp26/stdarg7.C: New test.
	* g++.dg/cpp26/stdarg8.C: New test.
	* g++.dg/cpp26/stdarg9.C: New test.
	* g++.dg/opt/pr60849.C (foo): Add explicit cast.
2025-10-09 22:41:30 +02:00

138 lines
4.3 KiB
C++

/* Copyright (C) 1989-2025 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
/*
* ISO C Standard: 7.15 Variable arguments <stdarg.h>
*/
#ifndef _STDARG_H
#ifndef _ANSI_STDARG_H_
#ifndef __need___va_list
#define _STDARG_H
#define _ANSI_STDARG_H_
#endif /* not __need___va_list */
#undef __need___va_list
/* Define __gnuc_va_list. */
#ifndef __GNUC_VA_LIST
#define __GNUC_VA_LIST
typedef __builtin_va_list __gnuc_va_list;
#endif
/* Define the standard macros for the user,
if this invocation was from the user program. */
#ifdef _STDARG_H
#if (defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L) \
|| __cplusplus >= 202400L
#define va_start(...) __builtin_c23_va_start(__VA_ARGS__)
#else
#define va_start(v,l) __builtin_va_start(v,l)
#endif
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L \
|| __cplusplus + 0 >= 201103L
#define va_copy(d,s) __builtin_va_copy(d,s)
#endif
#define __va_copy(d,s) __builtin_va_copy(d,s)
/* Define va_list, if desired, from __gnuc_va_list. */
/* We deliberately do not define va_list when called from
stdio.h, because ANSI C says that stdio.h is not supposed to define
va_list. stdio.h needs to have access to that data type,
but must not use that name. It should use the name __gnuc_va_list,
which is safe because it is reserved for the implementation. */
#ifdef _BSD_VA_LIST
#undef _BSD_VA_LIST
#endif
#if defined(__svr4__) || (defined(_SCO_DS) && !defined(__VA_LIST))
/* SVR4.2 uses _VA_LIST for an internal alias for va_list,
so we must avoid testing it and setting it here.
SVR4 uses _VA_LIST as a flag in stdarg.h, but we should
have no conflict with that. */
#ifndef _VA_LIST_
#define _VA_LIST_
#ifdef __i860__
#ifndef _VA_LIST
#define _VA_LIST va_list
#endif
#endif /* __i860__ */
typedef __gnuc_va_list va_list;
#ifdef _SCO_DS
#define __VA_LIST
#endif
#endif /* _VA_LIST_ */
#else /* not __svr4__ || _SCO_DS */
/* The macro _VA_LIST_ is the same thing used by this file in Ultrix.
But on BSD NET2 we must not test or define or undef it.
(Note that the comments in NET 2's ansi.h
are incorrect for _VA_LIST_--see stdio.h!) */
#if !defined (_VA_LIST_) || defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__) || defined(WINNT)
/* The macro _VA_LIST_DEFINED is used in Windows NT 3.5 */
#ifndef _VA_LIST_DEFINED
/* The macro _VA_LIST is used in SCO Unix 3.2. */
#ifndef _VA_LIST
/* The macro _VA_LIST_T_H is used in the Bull dpx2 */
#ifndef _VA_LIST_T_H
/* The macro __va_list__ is used by BeOS. */
#ifndef __va_list__
typedef __gnuc_va_list va_list;
#endif /* not __va_list__ */
#endif /* not _VA_LIST_T_H */
#endif /* not _VA_LIST */
#endif /* not _VA_LIST_DEFINED */
#if !(defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__))
#define _VA_LIST_
#endif
#ifndef _VA_LIST
#define _VA_LIST
#endif
#ifndef _VA_LIST_DEFINED
#define _VA_LIST_DEFINED
#endif
#ifndef _VA_LIST_T_H
#define _VA_LIST_T_H
#endif
#ifndef __va_list__
#define __va_list__
#endif
#endif /* not _VA_LIST_, except on certain systems */
#endif /* not __svr4__ */
#if (defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L) \
|| __cplusplus >= 202400L
#define __STDC_VERSION_STDARG_H__ 202311L
#endif
#endif /* _STDARG_H */
#endif /* not _ANSI_STDARG_H_ */
#endif /* not _STDARG_H */