From ac6f080d16361fa43bc9b31968f252fadd22b81b Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 19 Sep 2024 17:00:29 +0200 Subject: [PATCH] Remove a duplicate inclusion of qlocale_data_p.h In tst_qlocale.cpp there was a #include that caused the static data tables to be duplicated, violating ODR. All it actually needed was the ability to iterate all rows of the locale_data[] table, so export a method to do that from QLocaleData and have the test run that instead of pulling in a second copy of the tables. Conflict resolution at 6.8 needed the declaration of locale_data_size moved up qlocale.cpp, which happened after 6.8 as part of other work that isn't included in 6.8. Pick-to: 6.5 6.2 Task-number: QTBUG-128930 Change-Id: Ie5ebdf508a622eeca93f8785bc09b086502aa0e2 Reviewed-by: Thiago Macieira (cherry picked from commit 96c07655c46afdd3e2da7ef7d339b518d36c83c5) Reviewed-by: Marc Mutz --- src/corelib/text/qlocale.cpp | 12 ++++++++++-- src/corelib/text/qlocale_p.h | 2 ++ .../auto/corelib/text/qlocale/tst_qlocale.cpp | 18 +++++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index de71916ea81..12af8dc3082 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -484,6 +484,16 @@ static qsizetype findLocaleIndexById(QLocaleId localeId) noexcept return -1; } +static constexpr qsizetype locale_data_size = q20::ssize(locale_data) - 1; // trailing guard +bool QLocaleData::allLocaleDataRows(bool (*check)(qsizetype, const QLocaleData &)) +{ + for (qsizetype index = 0; index < locale_data_size; ++index) { + if (!(*check)(index, locale_data[index])) + return false; + } + return true; +} + qsizetype QLocaleData::findLocaleIndex(QLocaleId lid) noexcept { QLocaleId localeId = lid; @@ -875,8 +885,6 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l) } #endif // QT_NO_DATASTREAM -static constexpr qsizetype locale_data_size = q20::ssize(locale_data) - 1; // trailing guard - Q_GLOBAL_STATIC(QSharedDataPointer, defaultLocalePrivate, new QLocalePrivate(defaultData(), defaultIndex())) diff --git a/src/corelib/text/qlocale_p.h b/src/corelib/text/qlocale_p.h index ee65076780b..2141c6f277b 100644 --- a/src/corelib/text/qlocale_p.h +++ b/src/corelib/text/qlocale_p.h @@ -246,6 +246,8 @@ public: // data, e.g. calendar locales, as well as the main CLDR-derived data. [[nodiscard]] static qsizetype findLocaleIndex(QLocaleId localeId) noexcept; [[nodiscard]] static const QLocaleData *c() noexcept; + [[nodiscard]] Q_AUTOTEST_EXPORT + static bool allLocaleDataRows(bool (*check)(qsizetype, const QLocaleData &)); enum DoubleForm { DFExponent = 0, diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 0047ed1ad08..cf46889b39f 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -3064,10 +3064,9 @@ void tst_QLocale::negativeNumbers() QT_TEST_EQUALITY_OPS(egypt, farsi, false); } +#ifdef QT_BUILD_INTERNAL #include -#include - -static const int locale_data_count = sizeof(locale_data)/sizeof(locale_data[0]); +#endif void tst_QLocale::testNames_data() { @@ -3076,16 +3075,21 @@ void tst_QLocale::testNames_data() QLocale::setDefault(QLocale(QLocale::C)); // Ensures predictable fall-backs - for (int i = 0; i < locale_data_count; ++i) { - const QLocaleData &item = locale_data[i]; +#ifdef QT_BUILD_INTERNAL + bool ok = QLocaleData::allLocaleDataRows([](qsizetype index, const QLocaleData &item) { const QByteArray lang = QLocale::languageToString(QLocale::Language(item.m_language_id)).toUtf8(); const QByteArray land = QLocale::territoryToString(QLocale::Territory(item.m_territory_id)).toUtf8(); - QTest::addRow("data_%d (%s/%s)", i, lang.constData(), land.constData()) + QTest::addRow("data_%d (%s/%s)", int(index), lang.constData(), land.constData()) << QLocale::Language(item.m_language_id) << QLocale::Territory(item.m_territory_id); - } + return true; + }); + QVERIFY(ok); +#else + QSKIP("Only internal builds can access the data to set up this test"); +#endif // QT_BUILD_INTERNAL } void tst_QLocale::testNames()