From acad4b6b2e1f82b67b7656d527257749b5af968a Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Tue, 4 Jul 2023 08:31:37 +0200 Subject: [PATCH] AutoTest: Improve QRegularExpression usages Use static const if possible to avoid re-initializations. Change-Id: Ibee9e320dccb90cd928737b809db3b56369961ec Reviewed-by: David Schulz --- .../autotest/boost/boosttestoutputreader.cpp | 44 +++++++++---------- .../autotest/catch/catchcodeparser.cpp | 2 +- .../autotest/catch/catchtestparser.cpp | 12 ++--- src/plugins/autotest/gtest/gtestparser.cpp | 2 +- src/plugins/autotest/gtest/gtestresult.cpp | 2 +- src/plugins/autotest/gtest/gtestvisitors.cpp | 2 +- src/plugins/autotest/qtest/qttestparser.cpp | 4 +- .../autotest/quick/quicktestparser.cpp | 2 +- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/plugins/autotest/boost/boosttestoutputreader.cpp b/src/plugins/autotest/boost/boosttestoutputreader.cpp index c206b3b498a..b903f6c10b1 100644 --- a/src/plugins/autotest/boost/boosttestoutputreader.cpp +++ b/src/plugins/autotest/boost/boosttestoutputreader.cpp @@ -62,7 +62,7 @@ static QString caseFromContent(const QString &content) } QString result = content.mid(index + 5); - static QRegularExpression functionName("\"(.+)\":.*"); + static const QRegularExpression functionName("\"(.+)\":.*"); const QRegularExpressionMatch matcher = functionName.match(result); if (!matcher.hasMatch()) { qCDebug(orLog) << "got no match"; @@ -173,29 +173,29 @@ void BoostTestOutputReader::handleMessageMatch(const QRegularExpressionMatch &ma void BoostTestOutputReader::processOutputLine(const QByteArray &outputLine) { - static QRegularExpression newTestStart("^Running (\\d+) test cases?\\.\\.\\.$"); - static QRegularExpression dependency("^Including test case (.+) as a dependency of " - "test case (.+)$"); - static QRegularExpression messages("^(.+)\\((\\d+)\\): (info: (.+)|error: (.+)|" - "fatal error: (.+)|last checkpoint: (.+)" - "|Entering test (case|suite) \"(.+)\"" - "|Leaving test (case|suite) \"(.+)\"; testing time: (\\d+.+)" - "|Test case \"(.+)\" is skipped because .+$)$"); - static QRegularExpression moduleMssg("^(Entering test module \"(.+)\"|" - "Leaving test module \"(.+)\"; testing time: (\\d+.+))$"); - static QRegularExpression noAssertion("^Test case (.*) did not check any assertions$"); + static const QRegularExpression newTestStart("^Running (\\d+) test cases?\\.\\.\\.$"); + static const QRegularExpression dependency("^Including test case (.+) as a dependency of " + "test case (.+)$"); + static const QRegularExpression messages("^(.+)\\((\\d+)\\): (info: (.+)|error: (.+)|" + "fatal error: (.+)|last checkpoint: (.+)" + "|Entering test (case|suite) \"(.+)\"" + "|Leaving test (case|suite) \"(.+)\"; testing time: (\\d+.+)" + "|Test case \"(.+)\" is skipped because .+$)$"); + static const QRegularExpression moduleMssg("^(Entering test module \"(.+)\"|" + "Leaving test module \"(.+)\"; testing time: (\\d+.+))$"); + static const QRegularExpression noAssertion("^Test case (.*) did not check any assertions$"); - static QRegularExpression summaryPreamble("^\\s*Test (module|suite|case) \"(.*)\" has " - "(failed|passed)( with:)?$"); - static QRegularExpression summarySkip("^\\s+Test case \"(.*)\" was skipped$"); - static QRegularExpression summaryDetail("^\\s+(\\d+) test cases? out of (\\d+) " - "(failed|passed|skipped)$"); - static QRegularExpression summaryAssertion("^\\s+(\\d+) assertions? out of (\\d+) " - "(failed|passed)$"); + static const QRegularExpression summaryPreamble("^\\s*Test (module|suite|case) \"(.*)\" has " + "(failed|passed)( with:)?$"); + static const QRegularExpression summarySkip("^\\s+Test case \"(.*)\" was skipped$"); + static const QRegularExpression summaryDetail("^\\s+(\\d+) test cases? out of (\\d+) " + "(failed|passed|skipped)$"); + static const QRegularExpression summaryAssertion("^\\s+(\\d+) assertions? out of (\\d+) " + "(failed|passed)$"); - static QRegularExpression finish("^\\*{3} (\\d+) failure(s are| is) detected in the " - "test module \"(.*)\"$"); - QString noErrors("*** No errors detected"); + static const QRegularExpression finish("^\\*{3} (\\d+) failure(s are| is) detected in the " + "test module \"(.*)\"$"); + const QString noErrors("*** No errors detected"); const QString line = removeCommandlineColors(QString::fromUtf8(outputLine)); if (line.trimmed().isEmpty()) diff --git a/src/plugins/autotest/catch/catchcodeparser.cpp b/src/plugins/autotest/catch/catchcodeparser.cpp index e2bab245b90..186dde5ea1b 100644 --- a/src/plugins/autotest/catch/catchcodeparser.cpp +++ b/src/plugins/autotest/catch/catchcodeparser.cpp @@ -41,7 +41,7 @@ static QStringList parseTags(const QString &tagsString) { QStringList tagsList; - const QRegularExpression tagRegEx("\\[(.*?)\\]",QRegularExpression::CaseInsensitiveOption); + static const QRegularExpression tagRegEx("\\[(.*?)\\]", QRegularExpression::CaseInsensitiveOption); int pos = 0; QRegularExpressionMatch it = tagRegEx.match(tagsString, pos); while (it.hasMatch()) { diff --git a/src/plugins/autotest/catch/catchtestparser.cpp b/src/plugins/autotest/catch/catchtestparser.cpp index 3cc7a4540be..a37f8b4ca73 100644 --- a/src/plugins/autotest/catch/catchtestparser.cpp +++ b/src/plugins/autotest/catch/catchtestparser.cpp @@ -104,12 +104,12 @@ bool CatchTestParser::processDocument(QPromise &promise, const QByteArray &fileContent = getFileContent(fileName); if (!hasCatchNames(doc)) { - const QRegularExpression regex("\\b(CATCH_)?" - "(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|" - "TEMPLATE_TEST_CASE(_METHOD)?_SIG|" - "TEMPLATE_PRODUCT_TEST_CASE(_METHOD)?_SIG|" - "TEMPLATE_LIST_TEST_CASE_METHOD|METHOD_AS_TEST_CASE|" - "REGISTER_TEST_CASE)"); + static const QRegularExpression regex("\\b(CATCH_)?" + "(SCENARIO|(TEMPLATE_(PRODUCT_)?)?TEST_CASE(_METHOD)?|" + "TEMPLATE_TEST_CASE(_METHOD)?_SIG|" + "TEMPLATE_PRODUCT_TEST_CASE(_METHOD)?_SIG|" + "TEMPLATE_LIST_TEST_CASE_METHOD|METHOD_AS_TEST_CASE|" + "REGISTER_TEST_CASE)"); if (!regex.match(QString::fromUtf8(fileContent)).hasMatch()) return false; } diff --git a/src/plugins/autotest/gtest/gtestparser.cpp b/src/plugins/autotest/gtest/gtestparser.cpp index c8cc92005e8..bb2285bd43a 100644 --- a/src/plugins/autotest/gtest/gtestparser.cpp +++ b/src/plugins/autotest/gtest/gtestparser.cpp @@ -80,7 +80,7 @@ bool GTestParser::processDocument(QPromise &promise, const QByteArray &fileContent = getFileContent(fileName); if (!hasGTestNames(doc)) { - const QRegularExpression regex("\\b(TEST(_[FP])?|TYPED_TEST(_P)?|(GTEST_TEST))"); + static const QRegularExpression regex("\\b(TEST(_[FP])?|TYPED_TEST(_P)?|(GTEST_TEST))"); if (!regex.match(QString::fromUtf8(fileContent)).hasMatch()) return false; } diff --git a/src/plugins/autotest/gtest/gtestresult.cpp b/src/plugins/autotest/gtest/gtestresult.cpp index b89c06bd8e3..f34b8f069d7 100644 --- a/src/plugins/autotest/gtest/gtestresult.cpp +++ b/src/plugins/autotest/gtest/gtestresult.cpp @@ -39,7 +39,7 @@ static ResultHooks::OutputStringHook outputStringHook(const QString &testCaseNam static QString normalizeName(const QString &name) { - static QRegularExpression parameterIndex("/\\d+"); + static const QRegularExpression parameterIndex("/\\d+"); QString nameWithoutParameterIndices = name; nameWithoutParameterIndices.remove(parameterIndex); diff --git a/src/plugins/autotest/gtest/gtestvisitors.cpp b/src/plugins/autotest/gtest/gtestvisitors.cpp index 629b4e1f91a..ea7f89863b3 100644 --- a/src/plugins/autotest/gtest/gtestvisitors.cpp +++ b/src/plugins/autotest/gtest/gtestvisitors.cpp @@ -62,7 +62,7 @@ bool GTestVisitor::visit(CPlusPlus::FunctionDefinitionAST *ast) testSuiteName = QString::fromUtf8(parameter1.spell()); testCaseName = QString::fromUtf8(parameter2.spell()); // test (suite) name needs to be a alpha numerical literal ( _ and $ allowed) - const QRegularExpression alnum("^[[:alnum:]_$]+$"); + static const QRegularExpression alnum("^[[:alnum:]_$]+$"); // test suite must not start with a number, test case may if (!alnum.match(testSuiteName).hasMatch() || (!testSuiteName.isEmpty() && testSuiteName.at(0).isNumber())) { diff --git a/src/plugins/autotest/qtest/qttestparser.cpp b/src/plugins/autotest/qtest/qttestparser.cpp index f68a87d7bf3..57c85b08a9e 100644 --- a/src/plugins/autotest/qtest/qttestparser.cpp +++ b/src/plugins/autotest/qtest/qttestparser.cpp @@ -112,8 +112,8 @@ TestCases QtTestParser::testCases(const CppEditor::CppModelManager *modelManager return astVisitor.testCases(); TestCases result; - const QRegularExpression regex("\\b(QTEST_(APPLESS_|GUILESS_)?MAIN)" - "\\s*\\(\\s*([[:alnum:]]+)\\s*\\)"); + static const QRegularExpression regex("\\b(QTEST_(APPLESS_|GUILESS_)?MAIN)" + "\\s*\\(\\s*([[:alnum:]]+)\\s*\\)"); QRegularExpressionMatchIterator it = regex.globalMatch(QString::fromUtf8(document->utf8Source())); while (it.hasNext()) { const QRegularExpressionMatch match = it.next(); diff --git a/src/plugins/autotest/quick/quicktestparser.cpp b/src/plugins/autotest/quick/quicktestparser.cpp index 65011d93986..a5acf697feb 100644 --- a/src/plugins/autotest/quick/quicktestparser.cpp +++ b/src/plugins/autotest/quick/quicktestparser.cpp @@ -146,7 +146,7 @@ QString QuickTestParser::quickTestName(const CPlusPlus::Document::Ptr &doc) cons } if (pchIncludes) { - const QRegularExpression regex("\\bQUICK_TEST_(MAIN|OPENGL_MAIN|MAIN_WITH_SETUP)"); + static const QRegularExpression regex("\\bQUICK_TEST_(MAIN|OPENGL_MAIN|MAIN_WITH_SETUP)"); const QRegularExpressionMatch match = regex.match(QString::fromUtf8(fileContent)); if (match.hasMatch()) return match.captured(); // we do not care for the name, just return something non-empty