Utils: Fix matching wildcards to full paths

Fixes: QTCREATORBUG-24792
Change-Id: I82b4edea6260b07e1bdff065b157a4cd044ec629
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Christian Stenger
2020-10-16 13:38:57 +02:00
parent 9f6eb972b7
commit 6c5eaed92c
4 changed files with 75 additions and 2 deletions

View File

@@ -29,6 +29,7 @@
#include "fileutils.h"
#include "mapreduce.h"
#include "qtcassert.h"
#include "stringutils.h"
#include <QCoreApplication>
#include <QMutex>
@@ -474,7 +475,7 @@ QString matchCaseReplacement(const QString &originalText, const QString &replace
static QList<QRegularExpression> filtersToRegExps(const QStringList &filters)
{
return Utils::transform(filters, [](const QString &filter) {
return QRegularExpression(QRegularExpression::wildcardToRegularExpression(filter),
return QRegularExpression(Utils::wildcardToRegularExpression(filter),
QRegularExpression::CaseInsensitiveOption);
});
}

View File

@@ -393,4 +393,68 @@ QString formatElapsedTime(qint64 elapsed)
return QCoreApplication::translate("StringUtils", "Elapsed time: %1.").arg(time);
}
/*
* Basically QRegularExpression::wildcardToRegularExpression(), but let wildcards match
* path separators as well
*/
QString wildcardToRegularExpression(const QString &original)
{
const qsizetype wclen = original.size();
QString rx;
rx.reserve(wclen + wclen / 16);
qsizetype i = 0;
const QChar *wc = original.data();
const QLatin1String starEscape(".*");
const QLatin1String questionMarkEscape(".");
while (i < wclen) {
const QChar c = wc[i++];
switch (c.unicode()) {
case '*':
rx += starEscape;
break;
case '?':
rx += questionMarkEscape;
break;
case '\\':
case '$':
case '(':
case ')':
case '+':
case '.':
case '^':
case '{':
case '|':
case '}':
rx += QLatin1Char('\\');
rx += c;
break;
case '[':
rx += c;
// Support for the [!abc] or [!a-c] syntax
if (i < wclen) {
if (wc[i] == QLatin1Char('!')) {
rx += QLatin1Char('^');
++i;
}
if (i < wclen && wc[i] == QLatin1Char(']'))
rx += wc[i++];
while (i < wclen && wc[i] != QLatin1Char(']')) {
if (wc[i] == QLatin1Char('\\'))
rx += QLatin1Char('\\');
rx += wc[i++];
}
}
break;
default:
rx += c;
break;
}
}
return QRegularExpression::anchoredPattern(rx);
}
} // namespace Utils

View File

@@ -104,4 +104,11 @@ T makeUniquelyNumbered(const T &preferred, const Container &reserved)
QTCREATOR_UTILS_EXPORT QString formatElapsedTime(qint64 elapsed);
/* This function is only necessary if you need to match the wildcard expression against a
* string that might contain path separators - otherwise
* QRegularExpression::wildcardToRegularExpression() can be used.
* Working around QRegularExpression::wildcardToRegularExpression() taking native separators
* into account and handling them to disallow matching a wildcard characters.
*/
QTCREATOR_UTILS_EXPORT QString wildcardToRegularExpression(const QString &original);
} // namespace Utils

View File

@@ -38,6 +38,7 @@
#include <utils/algorithm.h>
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <QThread>
@@ -67,7 +68,7 @@ void CppcheckTool::updateOptions(const CppcheckOptions &options)
if (trimmedPattern.isEmpty())
continue;
const QRegularExpression re(QRegularExpression::wildcardToRegularExpression(trimmedPattern));
const QRegularExpression re(Utils::wildcardToRegularExpression(trimmedPattern));
if (re.isValid())
m_filters.push_back(re);
}