mingw-w64/mingw-w64-headers/crt/swprintf.inl
Pali Rohár 73cf399a16 crt: Add support for C95 (v)swprintf() functions when __USE_MINGW_ANSI_STDIO=0
msvcr80+ has following wide-buffer swprintf like function symbols, none of
them is compatible with C95 (v)swprintf function, differences are:

  _(v)swprintf   - does not take buffer size argument, like non-wide C89
                   sprintf
  _(v)snwprintf  - does not fill nul-term wide char if there is not space
                   for it, like non-wide msvcr _(v)snprintf
  _(v)swprintf_c - clears the first wide character in buffer on error
                   (error means that there is no space for nul-term or
                   conversion error occurred)

Version before msvcr80 has following function symbols:

  _(v)snwprintf - same as in msvcrt80+
  (v)swprintf - same as _(v)swprintf in msvcrt80+

There is no (v)swprintf symbol in msvcr80+ (neither in UCRT) and there are
no _(v)swprintf_c/_(v)swprintf symbols before msvcr80.

Additionally msvcr80+ header files defines (v)swprintf() function as inline
wrapper function around _vswprintf_c_l() with NULL locale, which is same as
_vswprintf_c(). Therefore that msvcr80+ inline function (v)swprintf() is
not C95 compatible.

UCRT header files provides all those msvcr80+ swprintf family functions
defined as inline. But UCRT changed behavior of _(v)swprintf_c function,
on error it does not clear the first wide character in buffer, but instead
puts the nul wide character at current position. So with this change the
(v)swprintf() function definition in UCRT header file is C95 compatible.

mingw-w64 header files declares and defines (v)swprintf() without the
buffer size argument, matching the pre-msvcr80 (v)swprintf symbol. So
mingw-w64 has currently broken C95+ support for swprintf(), vswprintf(),
__ms_swprintf() and __ms_vswprintf() symbols, and for swprintf() and
vswprintf() functions provided by mingw-w64 header files when macro
__USE_MINGW_ANSI_STDIO is set to 0 (which is default for UCRT).

Fix this issue.

Ensure that swprintf and vswprintf symbols are always C95 compatible in
every CRT import library. Function symbols which do not take buffer size
are always under name with underscore prefix.

For non-UCRT import libraries provide mingw-w64 swprintf and vswprintf
implementations which calls _vsnwprintf and fill nul-term wide char. Original
pre-msvcr80 DLL symbols swprintf and vswprintf are renamed to _swprintf and
_vswprintf, for compatibility with msvcr80+ and UCRT.

Fix declarations of __ms_swprintf and __ms_vswprintf functions in header
files to include buffer size argument, to make them C95 compatible too.
crt-aliases.def.in file is updated to reflect this change (which completely
drops incompatible __ms_swprintf and __ms_vswprintf aliases and then also
unused PRE_C95_SWPRINTF macro).

With all these changes, functions swprintf() and vswprintf() when
__USE_MINGW_ANSI_STDIO is set to 0 are now compatible with ISO C95+.

Signed-off-by: Martin Storsjö <martin@martin.st>
2024-11-15 23:54:11 +02:00

51 lines
1.3 KiB
C++

/**
* This file has no copyright assigned and is placed in the Public Domain.
* This file is part of the mingw-w64 runtime package.
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
#ifndef _INC_SWPRINTF_INL
#define _INC_SWPRINTF_INL
#include <vadefs.h>
#ifdef __cplusplus
extern "C++" {
__mingw_ovr
/* __attribute__((__format__ (gnu_wprintf, 2, 0))) */ __MINGW_ATTRIB_NONNULL(2)
int vswprintf (wchar_t *__stream, const wchar_t *__format, __builtin_va_list __local_argv)
{
return _vswprintf( __stream, __format, __local_argv );
}
__mingw_ovr
/* __attribute__((__format__ (gnu_wprintf, 2, 3))) */ __MINGW_ATTRIB_NONNULL(2)
int swprintf (wchar_t *__stream, const wchar_t *__format, ...)
{
#ifdef __clang__
/* clang does not support __builtin_va_arg_pack(), so forward swprintf() to vswprintf() */
int __retval;
__builtin_va_list __local_argv;
__builtin_va_start( __local_argv, __format );
__retval = vswprintf( __stream, __format, __local_argv );
__builtin_va_end( __local_argv );
return __retval;
#else
return _swprintf( __stream, __format, __builtin_va_arg_pack() );
#endif
}
}
#elif defined(_CRT_NON_CONFORMING_SWPRINTFS)
#define swprintf _swprintf
#define vswprintf _vswprintf
#endif /* __cplusplus */
#endif /* _INC_SWPRINTF_INL */