-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc++][windows] Use _wsetlocale() in __locale_guard #160479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lb90
wants to merge
1
commit into
llvm:main
Choose a base branch
from
lb90:fix-issue-160478
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
libcxx/test/libcxx/localization/locales/windows_non_ascii_locale_names.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <locale> | ||
|
||
// REQUIRES: windows | ||
|
||
// The C RunTime library on Windows supports locale strings with | ||
// characters outside the ASCII range. This poses challenges for | ||
// code that temporarily set a custom thread locale. | ||
// | ||
// https://github.com/llvm/llvm-project/issues/160478 | ||
|
||
#include <locale> | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <algorithm> | ||
|
||
#include <cstdlib> | ||
#include <cassert> | ||
#include <clocale> | ||
|
||
#include "test_macros.h" | ||
|
||
void locale_name_replace_codepage(std::string& locale_name, const std::string& codepage) { | ||
auto dot_position = locale_name.rfind('.'); | ||
LIBCPP_ASSERT(dot_position != std::string::npos); | ||
|
||
locale_name = locale_name.substr(0, dot_position) + codepage; | ||
} | ||
|
||
int main(int, char**) { | ||
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE); | ||
|
||
std::string locale_name = std::setlocale(LC_ALL, "norwegian-bokmal"); | ||
|
||
const auto& not_ascii = [](char c) { return (c & 0x80) != 0; }; | ||
LIBCPP_ASSERT(std::any_of(locale_name.begin(), locale_name.end(), not_ascii)); | ||
|
||
locale_name_replace_codepage(locale_name, ".437"); | ||
LIBCPP_ASSERT(std::setlocale(LC_ALL, locale_name.c_str())); | ||
|
||
std::cerr.imbue(std::locale::classic()); | ||
std::cerr << std::setprecision(2) << 0.1 << std::endl; | ||
|
||
return EXIT_SUCCESS; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit of a pity that this requires calling
__wsetlocale()
a second time after the first__setlocale()
above; I'm wondering if this is a risk for performance degradation.See e.g. #56202 for a case where this has been measured to be a bottleneck - CC @alvinhochun.
Here, I guess the alternative is to unconditionally use
__wsetlocale()
above for fetching the name of the current locale, and that requires us to do more of the potentially messy charset conversions. Likewise - the form that this patch suggests feels a bit asymmetrical, when both narrow and wide APIs are being used for the same thing. But I see that it would require more of a mess and more local charset conversions (and require us to decide which charset to use for conversions) if we'd switch over entirely.So all in all, this is probably fine in this form; I think I agree that this is a reasonable compromise form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to follow up on my performance concern here... (We can't block a correctness fix on performance grounds, but it's at least good to be aware of the impact); I did repeat the benchmarks from #56202 with this patch, and I can't discern any significant difference in the benchmarks.
So just as we have the existing
__setlocale(nullptr)
as a cheap guard against doing needless locale changes, the extra__wsetlocale(nullptr)
also is cheap compared with the actual locale changes, which are the heavy operations we'd like to avoid repeating.