From 2053a5710a45dae5680e9ce5fad8fb8097013bcd Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 10 Aug 2017 20:53:15 +0200 Subject: [PATCH] qmake: make yet more use of ProString::toQStringRef() in most cases, the main advantage is not using toQString(m_tmp), which reduces the possibility of raw data leaks. in cases where we used toQString() without temporary, this is a slight optimization. Change-Id: Ib343acffd383aa2c4fefab75fb52762fb534dfc6 Reviewed-by: Joerg Bornemann (cherry picked from qtbase/eb0ba90b0af9fa7d5b70c74140f64295f2d05c18) Reviewed-by: Tobias Hunger --- src/shared/proparser/proitems.cpp | 7 ++++++- src/shared/proparser/proitems.h | 6 ++++-- src/shared/proparser/qmakebuiltins.cpp | 8 ++++---- src/shared/proparser/qmakeevaluator.cpp | 10 +++++----- src/shared/proparser/qmakeevaluator_p.h | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/shared/proparser/proitems.cpp b/src/shared/proparser/proitems.cpp index 30ddef65c40..db4f386cae8 100644 --- a/src/shared/proparser/proitems.cpp +++ b/src/shared/proparser/proitems.cpp @@ -71,6 +71,11 @@ ProString::ProString(const QString &str) : { } +ProString::ProString(const QStringRef &str) : + m_string(*str.string()), m_offset(str.position()), m_length(str.size()), m_file(0), m_hash(0x80000000) +{ +} + ProString::ProString(const char *str, DoPreHashing) : m_string(QString::fromLatin1(str)), m_offset(0), m_length(qstrlen(str)), m_file(0) { @@ -333,7 +338,7 @@ ProString ProString::trimmed() const QTextStream &operator<<(QTextStream &t, const ProString &str) { - t << str.toQString(); // XXX optimize ... somehow + t << str.toQStringRef(); return t; } diff --git a/src/shared/proparser/proitems.h b/src/shared/proparser/proitems.h index 6199efc8c72..58349ddb716 100644 --- a/src/shared/proparser/proitems.h +++ b/src/shared/proparser/proitems.h @@ -64,6 +64,7 @@ public: ProString(); ProString(const ProString &other); PROITEM_EXPLICIT ProString(const QString &str); + PROITEM_EXPLICIT ProString(const QStringRef &str); PROITEM_EXPLICIT ProString(const char *str); ProString(const QString &str, int offset, int length); void setValue(const QString &str); @@ -90,6 +91,7 @@ public: bool operator==(const ProString &other) const { return toQStringRef() == other.toQStringRef(); } bool operator==(const QString &other) const { return toQStringRef() == other; } + bool operator==(const QStringRef &other) const { return toQStringRef() == other; } bool operator==(QLatin1String other) const { return toQStringRef() == other; } bool operator==(const char *other) const { return toQStringRef() == QLatin1String(other); } bool operator!=(const ProString &other) const { return !(*this == other); } @@ -199,9 +201,9 @@ Q_DECLARE_TYPEINFO(ProKey, Q_MOVABLE_TYPE); uint qHash(const ProString &str); QString operator+(const ProString &one, const ProString &two); inline QString operator+(const ProString &one, const QString &two) - { return one + ProString(two); } + { return one.toQStringRef() + two; } inline QString operator+(const QString &one, const ProString &two) - { return ProString(one) + two; } + { return one + two.toQStringRef(); } inline QString operator+(const ProString &one, const char *two) { return one + ProString(two); } // XXX optimize diff --git a/src/shared/proparser/qmakebuiltins.cpp b/src/shared/proparser/qmakebuiltins.cpp index 4ca018c5de2..8331e86bfeb 100644 --- a/src/shared/proparser/qmakebuiltins.cpp +++ b/src/shared/proparser/qmakebuiltins.cpp @@ -730,9 +730,9 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand( const QString &sep = (args.count() == 2) ? args.at(1).toQString(m_tmp1) : statics.field_sep; const auto vars = values(map(args.at(0))); for (const ProString &var : vars) { - const auto splits = var.toQString(m_tmp2).split(sep); - for (const QString &splt : splits) - ret << (splt.isSharedWith(m_tmp2) ? var : ProString(splt).setSource(var)); + const auto splits = var.toQStringRef().split(sep); + for (const auto &splt : splits) + ret << ProString(splt).setSource(var); } } break; @@ -1538,7 +1538,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional( return ReturnFalse; } return returnBool(values(map(args.at(0))).join(statics.field_sep) - == args.at(1).toQString(m_tmp1)); + == args.at(1).toQStringRef()); case T_VERSION_AT_LEAST: case T_VERSION_AT_MOST: { if (args.count() != 2) { diff --git a/src/shared/proparser/qmakeevaluator.cpp b/src/shared/proparser/qmakeevaluator.cpp index b555102bf84..cfb78fc1d55 100644 --- a/src/shared/proparser/qmakeevaluator.cpp +++ b/src/shared/proparser/qmakeevaluator.cpp @@ -772,7 +772,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::visitProLoop( } infinite = true; } else { - const QString &itl = it_list.toQString(m_tmp1); + const QStringRef &itl = it_list.toQStringRef(); int dotdot = itl.indexOf(statics.strDotDot); if (dotdot != -1) { bool ok; @@ -869,13 +869,13 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::visitProVariable( ProStringList varVal; if (expandVariableReferences(tokPtr, sizeHint, &varVal, true) == ReturnError) return ReturnError; - const QString &val = varVal.at(0).toQString(m_tmp1); + const QStringRef &val = varVal.at(0).toQStringRef(); if (val.length() < 4 || val.at(0) != QLatin1Char('s')) { evalError(fL1S("The ~= operator can handle only the s/// function.")); return ReturnTrue; } QChar sep = val.at(1); - QStringList func = val.split(sep); + auto func = val.split(sep); if (func.count() < 3 || func.count() > 4) { evalError(fL1S("The s/// function expects 3 or 4 arguments.")); return ReturnTrue; @@ -887,8 +887,8 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::visitProVariable( case_sense = func[3].indexOf(QLatin1Char('i')) == -1; quote = func[3].indexOf(QLatin1Char('q')) != -1; } - QString pattern = func[1]; - QString replace = func[2]; + QString pattern = func[1].toString(); + QString replace = func[2].toString(); if (quote) pattern = QRegExp::escape(pattern); diff --git a/src/shared/proparser/qmakeevaluator_p.h b/src/shared/proparser/qmakeevaluator_p.h index 9f7a58e2fe6..0caa64c2321 100644 --- a/src/shared/proparser/qmakeevaluator_p.h +++ b/src/shared/proparser/qmakeevaluator_p.h @@ -39,7 +39,7 @@ r == ReturnNext ? "next" : \ r == ReturnReturn ? "return" : \ "") -# define dbgKey(s) qPrintable(s.toString().toQString()) +# define dbgKey(s) s.toString().toQStringRef().toLocal8Bit().constData() # define dbgStr(s) qPrintable(formatValue(s, true)) # define dbgStrList(s) qPrintable(formatValueList(s)) # define dbgSepStrList(s) qPrintable(formatValueList(s, true))