From 63eec03997cb3a22b1596829a864a80efd192dcb Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Thu, 14 Dec 2023 09:12:45 +0100 Subject: Port away from Q_FOREACH Fixes: QTBUG-115806 Change-Id: I32818c549fe110bfb61458864ad4d75d9fab28c4 Reviewed-by: Marc Mutz Reviewed-by: Serg Kryvonos --- tests/postbuild/bic/CMakeLists.txt | 3 - tests/postbuild/bic/qbic.cpp | 21 +++-- tests/postbuild/bic/tst_bic.cpp | 12 ++- tests/postbuild/guiapplauncher/CMakeLists.txt | 2 - .../guiapplauncher/tst_guiapplauncher.cpp | 7 +- tests/postbuild/headers/tst_headers.cpp | 14 ++-- tests/postbuild/symbols/tst_symbols.cpp | 93 ++++++++++++---------- 7 files changed, 73 insertions(+), 79 deletions(-) (limited to 'tests') diff --git a/tests/postbuild/bic/CMakeLists.txt b/tests/postbuild/bic/CMakeLists.txt index 05f2da8a..2734ef19 100644 --- a/tests/postbuild/bic/CMakeLists.txt +++ b/tests/postbuild/bic/CMakeLists.txt @@ -34,9 +34,6 @@ qt_internal_add_test(tst_bic ../global.h qbic.cpp qbic.h tst_bic.cpp - NO_PCH_SOURCES - qbic.cpp # undef QT_NO_FOREACH - tst_qbic.cpp # undef QT_NO_FOREACH DEFINES SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}/" INCLUDE_DIRECTORIES diff --git a/tests/postbuild/bic/qbic.cpp b/tests/postbuild/bic/qbic.cpp index be66e2bb..e903bf87 100644 --- a/tests/postbuild/bic/qbic.cpp +++ b/tests/postbuild/bic/qbic.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses - #include "qbic.h" #include "QtCore/qfile.h" @@ -55,16 +53,16 @@ static QStringList nonVirtualThunkToDestructorSymbols(const QString &className) { const QString symbolTemplate = QString::fromLatin1("%1::_ZThn%2_N%3%4"); - QStringList candidates; - candidates << symbolTemplate.arg(className).arg(16).arg(className.size()).arg(className) - << symbolTemplate.arg(className).arg(32).arg(className.size()).arg(className) - << symbolTemplate.arg(className).arg(40).arg(className.size()).arg(className) - ; + const QString candidates[] = { + symbolTemplate.arg(className).arg(16).arg(className.size()).arg(className), + symbolTemplate.arg(className).arg(32).arg(className.size()).arg(className), + symbolTemplate.arg(className).arg(40).arg(className.size()).arg(className) + }; QStringList result; for (int i = 0; i <= 1; ++i) { const QString suffix = QString::fromLatin1("D%1Ev").arg(i); - foreach (const QString &candidate, candidates) + for (const QString &candidate : candidates) result << candidate + suffix; } @@ -97,11 +95,12 @@ static bool matchDestructor(const QString &mangledClassName, const QString &symb parseClassName(mangledClassName, &className, &qualifiedClassName); const QString destructor = qualifiedClassName + QLatin1String("::~") + className; - QStringList nonVirtualThunkToDestructorSymbolCandidates = nonVirtualThunkToDestructorSymbols(className); + const QStringList nonVirtualThunkToDestructorSymbolCandidates + = nonVirtualThunkToDestructorSymbols(className); if (qualifiedTailMatch(destructor, symbol)) return true; - foreach (const QString &candidate, nonVirtualThunkToDestructorSymbolCandidates) { + for (const QString &candidate : nonVirtualThunkToDestructorSymbolCandidates) { if (qualifiedTailMatch(candidate, symbol)) return true; } @@ -174,7 +173,7 @@ QBic::Info QBic::parseOutput(const QByteArray &ba) const Info info; const QStringList source = QString::fromLatin1(ba).split("\n\n"); - foreach(QString str, source) { + for (const QString &str : source) { QStringList entry = str.split('\n'); if (entry.size() < 2) continue; diff --git a/tests/postbuild/bic/tst_bic.cpp b/tests/postbuild/bic/tst_bic.cpp index 69999dc7..40c60dd0 100644 --- a/tests/postbuild/bic/tst_bic.cpp +++ b/tests/postbuild/bic/tst_bic.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses - #include #include #include "qbic.h" @@ -606,7 +604,7 @@ void tst_Bic::sizesAndVTables() const QBic::Info currentLibInfo = getCurrentInfo(libName); QVERIFY(!currentLibInfo.classVTables.isEmpty()); - QBic::VTableDiff diff = bic.diffVTables(oldLibInfo, currentLibInfo); + const QBic::VTableDiff diff = bic.diffVTables(oldLibInfo, currentLibInfo); if (!diff.removedVTables.isEmpty()) { qWarning() << "VTables for the following classes were removed" << diff.removedVTables; @@ -617,7 +615,7 @@ void tst_Bic::sizesAndVTables() if (diff.modifiedVTables.size() != 1 || strcmp(QTest::currentDataTag(), "4.4") != 0 || diff.modifiedVTables.at(0).first != "QGraphicsProxyWidget") { - foreach(QStringPair entry, diff.modifiedVTables) + for (const QStringPair &entry : diff.modifiedVTables) qWarning() << "modified VTable:\n Old: " << entry.first << "\n New: " << entry.second; isFailed = true; @@ -631,15 +629,15 @@ void tst_Bic::sizesAndVTables() } if (isPatchRelease && !diff.reimpMethods.isEmpty()) { - foreach(QStringPair entry, diff.reimpMethods) + for (const QStringPair &entry : diff.reimpMethods) qWarning() << "reimplemented virtual in patch release:\n Old: " << entry.first << "\n New: " << entry.second; isFailed = true; } - QBic::SizeDiff sizeDiff = bic.diffSizes(oldLibInfo, currentLibInfo); + const QBic::SizeDiff sizeDiff = bic.diffSizes(oldLibInfo, currentLibInfo); if (!sizeDiff.mismatch.isEmpty()) { - foreach (QString className, sizeDiff.mismatch) + for (QString className : sizeDiff.mismatch) qWarning() << "size mismatch for" << className << "old" << oldLibInfo.classSizes.value(className) << "new" << currentLibInfo.classSizes.value(className); diff --git a/tests/postbuild/guiapplauncher/CMakeLists.txt b/tests/postbuild/guiapplauncher/CMakeLists.txt index 69ae8a0a..4eeb50b6 100644 --- a/tests/postbuild/guiapplauncher/CMakeLists.txt +++ b/tests/postbuild/guiapplauncher/CMakeLists.txt @@ -11,8 +11,6 @@ qt_internal_add_test(tst_guiapplauncher SOURCES tst_guiapplauncher.cpp windowmanager.cpp windowmanager.h - NO_PCH_SOURCES - tst_guiapplauncher.cpp # undef QT_NO_FOREACH DEFINES SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}/" LIBRARIES diff --git a/tests/postbuild/guiapplauncher/tst_guiapplauncher.cpp b/tests/postbuild/guiapplauncher/tst_guiapplauncher.cpp index 070946bd..b3f9f438 100644 --- a/tests/postbuild/guiapplauncher/tst_guiapplauncher.cpp +++ b/tests/postbuild/guiapplauncher/tst_guiapplauncher.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses - #include "windowmanager.h" #include @@ -188,7 +186,8 @@ static inline QString guiBinary(QString in) void tst_GuiAppLauncher::run_data() { QTest::addColumn("data"); - foreach(const TestDataEntry &data, testData()) { + const TestDataEntries testDataEntries = testData(); + for (const TestDataEntry &data : testDataEntries) { qDebug() << data.first << data.second.binary; QTest::newRow(data.first) << data.second; } @@ -356,7 +355,7 @@ bool tst_GuiAppLauncher::runApp(const AppLaunchData &data, QString *errorMessage const int exitCode = process.exitCode(); // check stderr const QStringList stderrOutput = QString::fromLocal8Bit(process.readAllStandardOutput()).split(QLatin1Char('\n')); - foreach(const QString &stderrLine, stderrOutput) { + for (const QString &stderrLine : stderrOutput) { // Skip expected QPainter warnings from oxygen. if (stderrWhiteList().contains(stderrLine)) { qWarning("%s: stderr: %s\n", qPrintable(data.binary), qPrintable(stderrLine)); diff --git a/tests/postbuild/headers/tst_headers.cpp b/tests/postbuild/headers/tst_headers.cpp index 3aff59fe..19f4e7a7 100644 --- a/tests/postbuild/headers/tst_headers.cpp +++ b/tests/postbuild/headers/tst_headers.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses - #include #include @@ -53,7 +51,7 @@ static QStringList getHeaders(const QString &path) // Recreate the whole file path so we can open the file from disk QStringList result; - foreach (QString entry, entries) + for (const QString &entry : std::as_const(entries)) result += path + "/" + entry; return result; @@ -76,9 +74,9 @@ static QStringList getModuleHeaders(const QString &moduleRoot) "print join(\"\\n\", @searchPaths);"); QString string(captureOutput("perl", QStringList() << "-e" << perlReadSyncProfileExpr << moduleRoot)); - QStringList entries = string.split("\n"); + const QStringList entries = string.split("\n"); QStringList headers; - foreach (const QString &headersPath, entries) + for (const QString &headersPath : entries) headers += getHeaders(headersPath); return headers; } @@ -133,7 +131,7 @@ void tst_Headers::allHeadersData() if (headers.isEmpty()) QSKIP("can't find any headers in your $QT_MODULE_TO_TEST/src."); - foreach (QString hdr, headers) { + for (const QString &hdr : std::as_const(headers)) { if (hdr.contains("/3rdparty/") || hdr.endsWith("/src/tools/uic/qclass_lib_map.h")) continue; @@ -167,8 +165,8 @@ void tst_Headers::privateSlots() QFile f(header); QVERIFY2(f.open(QIODevice::ReadOnly), qPrintable(f.errorString())); - QStringList content = QString::fromLocal8Bit(f.readAll()).split("\n"); - foreach (QString line, content) { + const QStringList content = QString::fromLocal8Bit(f.readAll()).split("\n"); + for (const QString &line : content) { if (line.contains("Q_PRIVATE_SLOT(") && !line.contains("define Q_PRIVATE_SLOT")) QVERIFY2(line.contains("_q_"), qPrintable(explainPrivateSlot(line))); } diff --git a/tests/postbuild/symbols/tst_symbols.cpp b/tests/postbuild/symbols/tst_symbols.cpp index 7749e162..523515ed 100644 --- a/tests/postbuild/symbols/tst_symbols.cpp +++ b/tests/postbuild/symbols/tst_symbols.cpp @@ -1,8 +1,6 @@ // Copyright (C) 2017 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 -#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses - #include #include @@ -116,20 +114,21 @@ static QString symbolToLine(const QString &symbol, const QString &lib) void tst_Symbols::globalObjects() { // these are regexps for global objects that are allowed in Qt - QStringList whitelist = QStringList() + const QRegularExpression whitelist[] = { // ignore qInitResources - they are safe to use - << "^_Z[0-9]*qInitResources_" - << "qrc_.*\\.cpp" + QRegularExpression("^_Z[0-9]*qInitResources_"), + QRegularExpression("qrc_.*\\.cpp"), // ignore qRegisterGuiVariant - it's a safe fallback to register GUI Variants - << "qRegisterGuiVariant"; + QRegularExpression("qRegisterGuiVariant"), + }; bool isFailed = false; QDir dir(qtLibDir, "*.so"); - QStringList files = dir.entryList(); + const QStringList files = dir.entryList(); QVERIFY(!files.isEmpty()); - foreach (QString lib, files) { + for (const QString &lib : files) { if (!keys.contains(lib)) continue; @@ -149,9 +148,9 @@ void tst_Symbols::globalObjects() QCOMPARE(proc.exitCode(), 0); QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()), QString()); - QStringList symbols = QString::fromLocal8Bit(proc.readAll()).split("\n"); + const QStringList symbols = QString::fromLocal8Bit(proc.readAll()).split("\n"); QVERIFY(!symbols.isEmpty()); - foreach (QString symbol, symbols) { + for (const QString &symbol : symbols) { if (symbol.isEmpty()) continue; @@ -165,8 +164,8 @@ void tst_Symbols::globalObjects() QString cap = match.captured(1); bool whitelisted = false; - foreach (QString white, whitelist) { - if (cap.indexOf(QRegularExpression(white)) != -1) { + for (const QRegularExpression &white : whitelist) { + if (cap.indexOf(white) != -1) { whitelisted = true; break; } @@ -194,11 +193,13 @@ void tst_Symbols::globalObjects() #ifndef QT_CROSS_COMPILED void tst_Symbols::prefix() { - QStringList qtTypes; - qtTypes << "QString" << "QChar" << "QWidget" << "QObject" << "QVariant" << "QList" - << "QMap" << "QHash" << "QVector" << "QRect" << "QSize" << "QPoint" - << "QTextFormat" << "QTextLength" << "QPen" << "QFont" << "QIcon" - << "QPixmap" << "QImage" << "QRegion" << "QPolygon"; + const QString qtTypes[] = { + "QString", "QChar", "QWidget", "QObject", "QVariant", "QList", + "QMap", "QHash", "QVector", "QRect", "QSize", "QPoint", + "QTextFormat", "QTextLength", "QPen", "QFont", "QIcon", + "QPixmap", "QImage", "QRegion", "QPolygon", + }; + QStringList qAlgorithmFunctions; qAlgorithmFunctions << "qAbs" << "qMin" << "qMax" << "qBound" << "qSwap" << "qHash" << "qDeleteAll"; @@ -209,24 +210,24 @@ void tst_Symbols::prefix() << "XPoint::~XPoint()" << "glyph_metrics_t::"; // #### Qt 4.2 - QStringList stupidCSymbols; - stupidCSymbols << "Add_Glyph_Property" - << "Check_Property" - << "Coverage_Index" - << "Get_Class" - << "Get_Device" - << "rcsid3" - << "sfnt_module_class" - << "t1cid_driver_class" - << "t42_driver_class" - << "winfnt_driver_class" - << "pshinter_module_class" - << "psnames_module_class" + const QString stupidCSymbols[] = { + "Add_Glyph_Property", + "Check_Property", + "Coverage_Index", + "Get_Class", + "Get_Device", + "rcsid3", + "sfnt_module_class", + "t1cid_driver_class", + "t42_driver_class", + "winfnt_driver_class", + "pshinter_module_class", + "psnames_module_class", // C symbols from Qt - << "qt_addObject" - << "qt_removeObject" - << "qt_startup_hook" - ; + "qt_addObject", + "qt_removeObject", + "qt_startup_hook", + }; QHash excusedPrefixes; excusedPrefixes[QString()] = @@ -304,11 +305,11 @@ void tst_Symbols::prefix() QStringList() << ns + "Phonon"; QDir dir(qtLibDir, "*.so"); - QStringList files = dir.entryList(); + const QStringList files = dir.entryList(); QVERIFY(!files.isEmpty()); bool isFailed = false; - foreach (QString lib, files) { + for (const QString &lib : files) { if (!keys.contains(lib)) continue; @@ -328,12 +329,15 @@ void tst_Symbols::prefix() QCOMPARE(proc.exitCode(), 0); QCOMPARE(QString::fromLocal8Bit(proc.readAllStandardError()), QString()); - QStringList symbols = QString::fromLocal8Bit(proc.readAll()).split("\n"); - QVERIFY(!symbols.isEmpty()); - foreach (QString symbol, symbols) { - if (symbol.isEmpty()) + const auto content = proc.readAll(); + QVERIFY(!content.isEmpty()); + + // treat output as L1, because we only parse ASCII tokens: + for (QLatin1StringView line : qTokenize(QLatin1StringView{content}, u'\n')) { + if (line.isEmpty()) continue; + QString symbol = QString::fromLocal8Bit(QByteArrayView{line}); if (symbol.startsWith("unsigned ")) // strip modifiers symbol = symbol.mid(symbol.indexOf(' ') + 1); @@ -388,7 +392,7 @@ void tst_Symbols::prefix() for ( ; it != excusedPrefixes.constEnd(); ++it) { if (!lib.contains(it.key())) continue; - foreach (QString prefix, it.value()) + for (const QString &prefix : it.value()) if (symbol.startsWith(prefix)) { symbolOk = true; break; @@ -398,7 +402,7 @@ void tst_Symbols::prefix() if (symbolOk) continue; - foreach (QString cSymbolPattern, stupidCSymbols) + for (const QString &cSymbolPattern : stupidCSymbols) if (symbol.contains(cSymbolPattern)) { symbolOk = true; break; @@ -422,7 +426,8 @@ void tst_Symbols::prefix() ) continue; - foreach (QString acceptedPattern, qAlgorithmFunctions + exceptionalSymbols) + const QStringList acceptedPatterns = qAlgorithmFunctions + exceptionalSymbols; + for (const QString &acceptedPattern : acceptedPatterns) if (symbol.contains(acceptedPattern)) { symbolOk = true; break; @@ -437,7 +442,7 @@ void tst_Symbols::prefix() plainSymbol += QLatin1Char(' '); plainSymbol += fields.at(i); } - foreach (QString qtType, qtTypes) + for (const QString &qtType : qtTypes) if (plainSymbol.contains(qtType)) { symbolOk = true; break; -- cgit v1.2.3