Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / src / support / win32 / locale_win32.cpp
blobc9d1d63b2508afb5dc23bd6324abd0ea98b86294
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include <locale>
10 #include <cstdarg> // va_start, va_end
11 #include <memory>
12 #include <type_traits>
14 #include <__locale_dir/locale_base_api/locale_guard.h>
16 int __libcpp_vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
18 using std::__libcpp_locale_guard;
20 // FIXME: base and mask currently unused. Needs manual work to construct the new locale
21 locale_t newlocale(int /*mask*/, const char * locale, locale_t /*base*/)
23 return {_create_locale( LC_ALL, locale ), locale};
26 decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l )
28 #if defined(_LIBCPP_MSVCRT)
29 return ___mb_cur_max_l_func(__l);
30 #else
31 __libcpp_locale_guard __current(__l);
32 return MB_CUR_MAX;
33 #endif
36 lconv *localeconv_l( locale_t &loc )
38 __libcpp_locale_guard __current(loc);
39 lconv *lc = localeconv();
40 if (!lc)
41 return lc;
42 return loc.__store_lconv(lc);
44 size_t mbrlen_l( const char *__restrict s, size_t n,
45 mbstate_t *__restrict ps, locale_t loc )
47 __libcpp_locale_guard __current(loc);
48 return mbrlen( s, n, ps );
50 size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
51 size_t len, mbstate_t *__restrict ps, locale_t loc )
53 __libcpp_locale_guard __current(loc);
54 return mbsrtowcs( dst, src, len, ps );
56 size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
57 locale_t loc )
59 __libcpp_locale_guard __current(loc);
60 return wcrtomb( s, wc, ps );
62 size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
63 size_t n, mbstate_t *__restrict ps, locale_t loc )
65 __libcpp_locale_guard __current(loc);
66 return mbrtowc( pwc, s, n, ps );
68 size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
69 size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc )
71 __libcpp_locale_guard __current(loc);
72 return mbsnrtowcs( dst, src, nms, len, ps );
74 size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
75 size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc )
77 __libcpp_locale_guard __current(loc);
78 return wcsnrtombs( dst, src, nwc, len, ps );
80 wint_t btowc_l( int c, locale_t loc )
82 __libcpp_locale_guard __current(loc);
83 return btowc( c );
85 int wctob_l( wint_t c, locale_t loc )
87 __libcpp_locale_guard __current(loc);
88 return wctob( c );
91 int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...)
93 va_list ap;
94 va_start( ap, format );
95 #if defined(_LIBCPP_MSVCRT)
96 // FIXME: Remove usage of internal CRT function and globals.
97 int result = __stdio_common_vsprintf(
98 _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR,
99 ret, n, format, loc, ap);
100 #else
101 __libcpp_locale_guard __current(loc);
102 _LIBCPP_DIAGNOSTIC_PUSH
103 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
104 int result = vsnprintf( ret, n, format, ap );
105 _LIBCPP_DIAGNOSTIC_POP
106 #endif
107 va_end(ap);
108 return result;
111 int asprintf_l( char **ret, locale_t loc, const char *format, ... )
113 va_list ap;
114 va_start( ap, format );
115 int result = vasprintf_l( ret, loc, format, ap );
116 va_end(ap);
117 return result;
119 int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap )
121 __libcpp_locale_guard __current(loc);
122 return __libcpp_vasprintf( ret, format, ap );
125 #if !defined(_LIBCPP_MSVCRT)
126 float strtof_l(const char* nptr, char** endptr, locale_t loc) {
127 __libcpp_locale_guard __current(loc);
128 return strtof(nptr, endptr);
131 long double strtold_l(const char* nptr, char** endptr, locale_t loc) {
132 __libcpp_locale_guard __current(loc);
133 return strtold(nptr, endptr);
135 #endif
137 #if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
138 size_t strftime_l(char *ret, size_t n, const char *format, const struct tm *tm,
139 locale_t loc) {
140 __libcpp_locale_guard __current(loc);
141 return strftime(ret, n, format, tm);
143 #endif