summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-02-27 16:40:59 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-03-02 17:39:27 +0000
commitf03ccd29bd5d36a79c6b73eccc6485c1b49a109c (patch)
treef13941fb8f3c0bd74b2602d25de4708daf1ab56d
parent90a4d74abcb9d0c1c153c64838cbc3f87922bfe9 (diff)
downloadqtbase-f03ccd29bd5d36a79c6b73eccc6485c1b49a109c.tar.gz
Tidy up QCoreApplicationPrivate::initLocale()'s #if-ery
Explain each exception cleanly and as itself, thereby avoiding the need for long and tangled #if-ery conditions. Make sure to setlocale(LC_ALL, "") everywhere we think we have initialized the locale, including Integrity - it plainly has setlocale(), since we call it for LC_CTYPE - since we should at least give it the chance to set its implementation-defined default locale, instead of the standard-defined POSIX locale in use on entry to main(). Done-with: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Change-Id: Iab00984ba45dfc9a324b6a3c12e3d330b655a5a9 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit 91bea4470ea25c1e6fe7e46559a37bdd7b703b63) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 355d1e42cc..96efb94b0c 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -548,22 +548,30 @@ QString qAppName()
void QCoreApplicationPrivate::initLocale()
{
-#if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED)
+#if defined(QT_BOOTSTRAPPED)
+ // Don't try to control bootstrap library locale or encoding.
+#elif defined(Q_OS_UNIX)
Q_CONSTINIT static bool qt_locale_initialized = false;
if (qt_locale_initialized)
return;
qt_locale_initialized = true;
-# ifdef Q_OS_INTEGRITY
+ // By default the portable "C"/POSIX locale is selected and active.
+ // Apply the locale from the environment, via setlocale(), which will
+ // read LC_ALL, LC_<category>, and LANG, in order (for each category).
+ const char *locale = setlocale(LC_ALL, "");
+
+ // Next, let's ensure that LC_CTYPE is UTF-8, since QStringConverter's
+ // QLocal8Bit hard-codes this, and we need to be consistent.
+# if defined(Q_OS_INTEGRITY)
setlocale(LC_CTYPE, "UTF-8");
+# elif defined(Q_OS_QNX)
+ // QNX has no nl_langinfo, so we can't check.
+ // FIXME: Shouldn't we still setlocale("UTF-8")?
+# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
+ // Android 6 still lacks nl_langinfo(), so we can't check.
+ // FIXME: Shouldn't we still setlocale("UTF-8")?
# else
-# if defined(Q_OS_QNX) || (defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__)
- // Android 6 still lacks nl_langinfo(), as does QNX, so we just assume it's
- // always UTF-8 on these platforms.
- auto nl_langinfo = [](int) { return "UTF-8"; };
-# endif // QNX or Android NDK < 26, "O".
-
- const char *locale = setlocale(LC_ALL, "");
const char *codec = nl_langinfo(CODESET);
if (Q_UNLIKELY(qstricmp(codec, "UTF-8") != 0 && qstricmp(codec, "utf8") != 0)) {
QByteArray oldLocale = locale;
@@ -575,8 +583,8 @@ void QCoreApplicationPrivate::initLocale()
newLocale += ".UTF-8";
newLocale = setlocale(LC_CTYPE, newLocale);
- // if locale doesn't exist, try some fallbacks
-# ifdef Q_OS_DARWIN
+ // If that locale doesn't exist, try some fallbacks:
+# if defined(Q_OS_DARWIN)
if (newLocale.isEmpty())
newLocale = setlocale(LC_CTYPE, "UTF-8");
# endif
@@ -590,7 +598,7 @@ void QCoreApplicationPrivate::initLocale()
"reconfigure your locale. See the locale(1) manual for more information.",
codec, oldLocale.constData(), newLocale.constData());
}
-# endif // Integrity
+# endif // Platform choice
#endif // Unix
}