ProParser: Adapt to QRegExp removal

That's part of a1947aeffe1 in qtbase, with manual adaptation.
It looks like we've been out-of-sync with upstream already before.

Change-Id: Ie6ef9e4fce44e01944194dc27bd3f54274ad7c6f
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
hjk
2020-06-23 12:09:57 +02:00
parent 562cf4a867
commit f922a0bbf1
3 changed files with 57 additions and 30 deletions

View File

@@ -27,7 +27,7 @@
#include <qdir.h> #include <qdir.h>
#include <qfile.h> #include <qfile.h>
#include <qregexp.h> #include <qregularexpression.h>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
# include <windows.h> # include <windows.h>
@@ -175,9 +175,9 @@ QString IoUtils::shellQuoteWin(const QString &arg)
// The process-level standard quoting allows escaping quotes with backslashes (note // The process-level standard quoting allows escaping quotes with backslashes (note
// that backslashes don't escape themselves, unless they are followed by a quote). // that backslashes don't escape themselves, unless they are followed by a quote).
// Consequently, quotes are escaped and their preceding backslashes are doubled. // Consequently, quotes are escaped and their preceding backslashes are doubled.
ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\\"")); ret.replace(QRegularExpression("(\\\\*)\""), QLatin1String("\\1\\1\\\""));
// Trailing backslashes must be doubled as well, as they are followed by a quote. // Trailing backslashes must be doubled as well, as they are followed by a quote.
ret.replace(QRegExp(QLatin1String("(\\\\+)$")), QLatin1String("\\1\\1")); ret.replace(QRegularExpression("(\\\\+)$"), QLatin1String("\\1\\1"));
// However, the shell also interprets the command, and no backslash-escaping exists // However, the shell also interprets the command, and no backslash-escaping exists
// there - a quote always toggles the quoting state, but is nonetheless passed down // there - a quote always toggles the quoting state, but is nonetheless passed down
// to the called process verbatim. In the unquoted state, the circumflex escapes // to the called process verbatim. In the unquoted state, the circumflex escapes

View File

@@ -40,7 +40,7 @@
#include <qfile.h> #include <qfile.h>
#include <qfileinfo.h> #include <qfileinfo.h>
#include <qlist.h> #include <qlist.h>
#include <qregexp.h> #include <qregularexpression.h>
#include <qset.h> #include <qset.h>
#include <qstringlist.h> #include <qstringlist.h>
#include <qtextstream.h> #include <qtextstream.h>
@@ -586,7 +586,11 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (!var.isEmpty()) { if (!var.isEmpty()) {
const auto strings = values(map(var)); const auto strings = values(map(var));
if (regexp) { if (regexp) {
QRegExp sepRx(sep); QRegularExpression sepRx(sep, QRegularExpression::DotMatchesEverythingOption);
if (!sepRx.isValid()) {
evalError(fL1S("section(): Encountered invalid regular expression '%1'.").arg(sep));
goto allfail;
}
for (const ProString &str : strings) { for (const ProString &str : strings) {
const QString &rstr = str.toQString(m_tmp[m_toggle ^= 1]).section(sepRx, beg, end); const QString &rstr = str.toQString(m_tmp[m_toggle ^= 1]).section(sepRx, beg, end);
ret << (rstr.isSharedWith(m_tmp[m_toggle]) ? str : ProString(rstr).setSource(str)); ret << (rstr.isSharedWith(m_tmp[m_toggle]) ? str : ProString(rstr).setSource(str));
@@ -888,10 +892,14 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (args.count() != 2) { if (args.count() != 2) {
evalError(fL1S("find(var, str) requires two arguments.")); evalError(fL1S("find(var, str) requires two arguments."));
} else { } else {
QRegExp regx(args.at(1).toQString()); QRegularExpression regx(args.at(1).toQString(), QRegularExpression::DotMatchesEverythingOption);
if (!regx.isValid()) {
evalError(fL1S("find(): Encountered invalid regular expression '%1'.").arg(regx.pattern()));
goto allfail;
}
const auto vals = values(map(args.at(0))); const auto vals = values(map(args.at(0)));
for (const ProString &val : vals) { for (const ProString &val : vals) {
if (regx.indexIn(val.toQString(m_tmp[m_toggle ^= 1])) != -1) if (val.toQString(m_tmp[m_toggle ^= 1]).contains(regx))
ret += val; ret += val;
} }
} }
@@ -1001,7 +1009,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
break; break;
case E_RE_ESCAPE: case E_RE_ESCAPE:
for (int i = 0; i < args.size(); ++i) { for (int i = 0; i < args.size(); ++i) {
const QString &rstr = QRegExp::escape(args.at(i).toQString(m_tmp1)); const QString &rstr = QRegularExpression::escape(args.at(i).toQString(m_tmp1));
ret << (rstr.isSharedWith(m_tmp1) ? args.at(i) : ProString(rstr).setSource(args.at(i))); ret << (rstr.isSharedWith(m_tmp1) ? args.at(i) : ProString(rstr).setSource(args.at(i)));
} }
break; break;
@@ -1054,8 +1062,12 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
dirs.append(QString()); dirs.append(QString());
} }
r.detach(); // Keep m_tmp out of QRegExp's cache QString pattern = QRegularExpression::wildcardToRegularExpression(r);
QRegExp regex(r, Qt::CaseSensitive, QRegExp::Wildcard); QRegularExpression regex(pattern, QRegularExpression::DotMatchesEverythingOption);
if (!regex.isValid()) {
evalError(fL1S("section(): Encountered invalid wildcard expression '%1'.").arg(pattern));
goto allfail;
}
for (int d = 0; d < dirs.count(); d++) { for (int d = 0; d < dirs.count(); d++) {
QString dir = dirs[d]; QString dir = dirs[d];
QDir qdir(pfx + dir); QDir qdir(pfx + dir);
@@ -1067,7 +1079,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (recursive) if (recursive)
dirs.append(fname + QLatin1Char('/')); dirs.append(fname + QLatin1Char('/'));
} }
if (regex.exactMatch(qdir[i])) if (regex.match(qdir[i]).hasMatch())
ret += ProString(fname).setSource(currentFileId()); ret += ProString(fname).setSource(currentFileId());
} }
} }
@@ -1109,7 +1121,11 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (args.count() != 3 ) { if (args.count() != 3 ) {
evalError(fL1S("replace(var, before, after) requires three arguments.")); evalError(fL1S("replace(var, before, after) requires three arguments."));
} else { } else {
const QRegExp before(args.at(1).toQString()); const QRegularExpression before(args.at(1).toQString(), QRegularExpression::DotMatchesEverythingOption);
if (!before.isValid()) {
evalError(fL1S("replace(): Encountered invalid regular expression '%1'.").arg(before.pattern()));
goto allfail;
}
const QString &after(args.at(2).toQString(m_tmp2)); const QString &after(args.at(2).toQString(m_tmp2));
const auto vals = values(map(args.at(0))); const auto vals = values(map(args.at(0)));
for (const ProString &val : vals) { for (const ProString &val : vals) {
@@ -1309,6 +1325,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
break; break;
} }
allfail:
return ReturnTrue; return ReturnTrue;
} }
@@ -1425,16 +1442,20 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
return ok; return ok;
if (args.count() == 2) if (args.count() == 2)
return returnBool(vars.contains(map(args.at(1)))); return returnBool(vars.contains(map(args.at(1))));
QRegExp regx; QRegularExpression regx;
const QString &qry = args.at(2).toQString(m_tmp1); const QString &qry = args.at(2).toQString(m_tmp1);
if (qry != QRegExp::escape(qry)) { if (qry != QRegularExpression::escape(qry)) {
QString copy = qry; regx.setPattern(QRegularExpression::anchoredPattern(qry));
copy.detach(); if (!regx.isValid()) {
regx.setPattern(copy); evalError(fL1S("infile(): Encountered invalid regular expression '%1'.").arg(qry));
return ReturnFalse;
}
} }
const auto strings = vars.value(map(args.at(1))); const auto strings = vars.value(map(args.at(1)));
for (const ProString &s : strings) { for (const ProString &s : strings) {
if ((!regx.isEmpty() && regx.exactMatch(s.toQString(m_tmp[m_toggle ^= 1]))) || s == qry) if ((!regx.pattern().isEmpty()
&& regx.match(s.toQString(m_tmp[m_toggle ^= 1])).hasMatch())
|| s == qry)
return ReturnTrue; return ReturnTrue;
} }
} }
@@ -1492,17 +1513,22 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
} }
const QString &qry = args.at(1).toQString(m_tmp1); const QString &qry = args.at(1).toQString(m_tmp1);
QRegExp regx; QRegularExpression regx;
if (qry != QRegExp::escape(qry)) { regx.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
QString copy = qry; if (qry != QRegularExpression::escape(qry)) {
copy.detach(); regx.setPattern(QRegularExpression::anchoredPattern(qry));
regx.setPattern(copy); if (!regx.isValid()) {
evalError(fL1S("contains(): Encountered invalid regular expression '%1'.").arg(qry));
return ReturnFalse;
} }
}
const ProStringList &l = values(map(args.at(0))); const ProStringList &l = values(map(args.at(0)));
if (args.count() == 2) { if (args.count() == 2) {
for (int i = 0; i < l.size(); ++i) { for (int i = 0; i < l.size(); ++i) {
const ProString &val = l[i]; const ProString &val = l[i];
if ((!regx.isEmpty() && regx.exactMatch(val.toQString(m_tmp[m_toggle ^= 1]))) || val == qry) if ((!regx.pattern().isEmpty() && regx.match(val.toQString(m_tmp[m_toggle ^= 1])).hasMatch())
|| val == qry)
return ReturnTrue; return ReturnTrue;
} }
} else { } else {
@@ -1511,8 +1537,8 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
const ProString val = l[i]; const ProString val = l[i];
for (int mut = 0; mut < mutuals.count(); mut++) { for (int mut = 0; mut < mutuals.count(); mut++) {
if (val.toQStringRef() == mutuals[mut].trimmed()) { if (val.toQStringRef() == mutuals[mut].trimmed()) {
return returnBool((!regx.isEmpty() return returnBool((!regx.pattern().isEmpty()
&& regx.exactMatch(val.toQString(m_tmp[m_toggle ^= 1]))) && regx.match(val.toQString(m_tmp[m_toggle ^= 1])).hasMatch())
|| val == qry); || val == qry);
} }
} }

View File

@@ -38,7 +38,7 @@
#include <qfile.h> #include <qfile.h>
#include <qfileinfo.h> #include <qfileinfo.h>
#include <qlist.h> #include <qlist.h>
#include <qregexp.h> #include <qregularexpression.h>
#include <qset.h> #include <qset.h>
#include <qstack.h> #include <qstack.h>
#include <qstring.h> #include <qstring.h>
@@ -330,7 +330,7 @@ ProStringList QMakeEvaluator::split_value_list(const QStringRef &vals, int sourc
} }
static void replaceInList(ProStringList *varlist, static void replaceInList(ProStringList *varlist,
const QRegExp &regexp, const QString &replace, bool global, QString &tmp) const QRegularExpression &regexp, const QString &replace, bool global, QString &tmp)
{ {
for (ProStringList::Iterator varit = varlist->begin(); varit != varlist->end(); ) { for (ProStringList::Iterator varit = varlist->begin(); varit != varlist->end(); ) {
QString val = varit->toQString(tmp); QString val = varit->toQString(tmp);
@@ -893,9 +893,10 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::visitProVariable(
QString pattern = func[1].toString(); QString pattern = func[1].toString();
QString replace = func[2].toString(); QString replace = func[2].toString();
if (quote) if (quote)
pattern = QRegExp::escape(pattern); pattern = QRegularExpression::escape(pattern);
QRegExp regexp(pattern, case_sense ? Qt::CaseSensitive : Qt::CaseInsensitive); QRegularExpression regexp(pattern, case_sense ? QRegularExpression::NoPatternOption :
QRegularExpression::CaseInsensitiveOption);
// We could make a union of modified and unmodified values, // We could make a union of modified and unmodified values,
// but this will break just as much as it fixes, so leave it as is. // but this will break just as much as it fixes, so leave it as is.