From 902f46bae58d6983aed7b635fa9f03d66b3ba109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Mon, 11 Apr 2022 19:15:42 +0200 Subject: [PATCH] some minor QRegularExpression usage optimizations and cleanups (#3999) --- gui/checkthread.cpp | 4 ++-- gui/libraryaddfunctiondialog.cpp | 2 +- gui/mainwindow.cpp | 11 +++++++---- gui/statsdialog.cpp | 2 +- tools/triage/mainwindow.cpp | 5 ++++- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 2a3513ee3..5aeaed85b 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -307,8 +307,8 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS { QList errorItems; ErrorItem errorItem; - const QRegularExpression r1("^(.+):([0-9]+):([0-9]+): (note|warning|error|fatal error): (.*)$"); - const QRegularExpression r2("^(.*)\\[([a-zA-Z0-9\\-_\\.]+)\\]$"); + static const QRegularExpression r1("^(.+):([0-9]+):([0-9]+): (note|warning|error|fatal error): (.*)$"); + static const QRegularExpression r2("^(.*)\\[([a-zA-Z0-9\\-_\\.]+)\\]$"); QTextStream in(&err, QIODevice::ReadOnly); while (!in.atEnd()) { QString line = in.readLine(); diff --git a/gui/libraryaddfunctiondialog.cpp b/gui/libraryaddfunctiondialog.cpp index 5fc979b1f..e45c34f28 100644 --- a/gui/libraryaddfunctiondialog.cpp +++ b/gui/libraryaddfunctiondialog.cpp @@ -28,7 +28,7 @@ LibraryAddFunctionDialog::LibraryAddFunctionDialog(QWidget *parent) : mUi(new Ui::LibraryAddFunctionDialog) { mUi->setupUi(this); - const QRegularExpression rx(NAMES); + static const QRegularExpression rx(NAMES); QValidator *validator = new QRegularExpressionValidator(rx, this); mUi->functionName->setValidator(validator); } diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 837577343..dfabb0f9d 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -262,9 +262,9 @@ void MainWindow::handleCLIParams(const QStringList ¶ms) } else { loadResults(logFile); } - } else if ((index = params.indexOf(QRegularExpression(".*\\.cppcheck$", QRegularExpression::CaseInsensitiveOption), 0)) >= 0 && index < params.length() && QFile(params[index]).exists()) { + } else if ((index = params.indexOf(QRegularExpression(".*\\.cppcheck$", QRegularExpression::CaseInsensitiveOption))) >= 0 && index < params.length() && QFile(params[index]).exists()) { loadProjectFile(params[index]); - } else if ((index = params.indexOf(QRegularExpression(".*\\.xml$", QRegularExpression::CaseInsensitiveOption), 0)) >= 0 && index < params.length() && QFile(params[index]).exists()) { + } else if ((index = params.indexOf(QRegularExpression(".*\\.xml$", QRegularExpression::CaseInsensitiveOption))) >= 0 && index < params.length() && QFile(params[index]).exists()) { loadResults(params[index],QDir::currentPath()); } else doAnalyzeFiles(params); @@ -1142,8 +1142,11 @@ void MainWindow::clearResults() if (mProjectFile && !mProjectFile->getBuildDir().isEmpty()) { QDir dir(QFileInfo(mProjectFile->getFilename()).absolutePath() + '/' + mProjectFile->getBuildDir()); for (const QString& f: dir.entryList(QDir::Files)) { - if (!f.endsWith("files.txt") && !QRegularExpression("^.*.s[0-9]+$").match(f).hasMatch()) - dir.remove(f); + if (!f.endsWith("files.txt")) { + static const QRegularExpression rx("^.*.s[0-9]+$"); + if (!rx.match(f).hasMatch()) + dir.remove(f); + } } } mUI->mResults->clear(true); diff --git a/gui/statsdialog.cpp b/gui/statsdialog.cpp index 85c51e121..ca1430666 100644 --- a/gui/statsdialog.cpp +++ b/gui/statsdialog.cpp @@ -406,7 +406,7 @@ QLineSeries *StatsDialog::numberOfReports(const QString &fileName, const QString QTextStream in(&f); while (!in.atEnd()) { QString line = in.readLine(); - const QRegularExpression rxdate("^\\[(\\d\\d)\\.(\\d\\d)\\.(\\d\\d\\d\\d)\\]$"); + static const QRegularExpression rxdate("^\\[(\\d\\d)\\.(\\d\\d)\\.(\\d\\d\\d\\d)\\]$"); const QRegularExpressionMatch matchRes = rxdate.match(line); if (matchRes.hasMatch()) { int y = matchRes.captured(3).toInt(); diff --git a/tools/triage/mainwindow.cpp b/tools/triage/mainwindow.cpp index d872b6c18..43a90dde3 100644 --- a/tools/triage/mainwindow.cpp +++ b/tools/triage/mainwindow.cpp @@ -111,7 +111,10 @@ void MainWindow::load(QTextStream &textStream) if (!errorMessage.isEmpty()) mAllErrors << errorMessage; errorMessage.clear(); - } else if (!url.isEmpty() && QRegularExpression("^.*: (error|warning|style|note):.*$").match(line).hasMatch()) { + } else if (!url.isEmpty()) { + static const QRegularExpression severityRe("^.*: (error|warning|style|note):.*$"); + if (severityRe.match(line).hasMatch()) + continue; const QRegularExpressionMatch matchRes = mVersionRe.match(line); if (matchRes.hasMatch()) { const QString version = matchRes.captured(1);