diff options
Diffstat (limited to 'src/qml/qmldirparser/qqmldirparser.cpp')
-rw-r--r-- | src/qml/qmldirparser/qqmldirparser.cpp | 290 |
1 files changed, 187 insertions, 103 deletions
diff --git a/src/qml/qmldirparser/qqmldirparser.cpp b/src/qml/qmldirparser/qqmldirparser.cpp index 1dbf35f1d3..e6a5691d3d 100644 --- a/src/qml/qmldirparser/qqmldirparser.cpp +++ b/src/qml/qmldirparser/qqmldirparser.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qqmldirparser_p.h" @@ -47,13 +11,13 @@ static int parseInt(QStringView str, bool *ok) { int pos = 0; int number = 0; - while (pos < str.length() && str.at(pos).isDigit()) { + while (pos < str.size() && str.at(pos).isDigit()) { if (pos != 0) number *= 10; number += str.at(pos).unicode() - '0'; ++pos; } - if (pos != str.length()) + if (pos != str.size()) *ok = false; else *ok = true; @@ -67,7 +31,7 @@ static QTypeRevision parseVersion(const QString &str) bool ok = false; const int major = parseInt(QStringView(str).left(dotIndex), &ok); if (!ok) return QTypeRevision(); - const int minor = parseInt(QStringView(str).mid(dotIndex + 1, str.length() - dotIndex - 1), &ok); + const int minor = parseInt(QStringView(str).mid(dotIndex + 1, str.size() - dotIndex - 1), &ok); return ok ? QTypeRevision::fromVersion(major, minor) : QTypeRevision(); } return QTypeRevision(); @@ -85,6 +49,7 @@ void QQmlDirParser::clear() _designerSupported = false; _typeInfos.clear(); _classNames.clear(); + _linkTarget.clear(); } inline static void scanSpace(const QChar *&ch) { @@ -240,6 +205,23 @@ bool QQmlDirParser::parse(const QString &source) "not %1.").arg(sections[1])); continue; } + } else if (sections[0] == QLatin1String("default")) { + if (sectionCount < 2) { + reportError(lineNumber, 0, + QStringLiteral("default directive requires further " + "arguments, but none were provided.")); + continue; + } + if (sections[1] == QLatin1String("import")) { + if (!readImport(sections + 1, sectionCount - 1, + Import::Flags({ Import::Optional, Import::OptionalDefault }))) + continue; + } else { + reportError(lineNumber, 0, + QStringLiteral("only optional imports can have a default, " + "not %1.") + .arg(sections[1])); + } } else if (sections[0] == QLatin1String("classname")) { if (sectionCount < 2) { reportError(lineNumber, 0, @@ -251,14 +233,28 @@ bool QQmlDirParser::parse(const QString &source) _classNames.append(sections[1]); } else if (sections[0] == QLatin1String("internal")) { - if (sectionCount != 3) { + if (sectionCount == 3) { + Component entry(sections[1], sections[2], QTypeRevision()); + entry.internal = true; + _components.insert(entry.typeName, entry); + } else if (sectionCount == 4) { + const QTypeRevision version = parseVersion(sections[2]); + if (version.isValid()) { + Component entry(sections[1], sections[3], version); + entry.internal = true; + _components.insert(entry.typeName, entry); + } else { + reportError(lineNumber, 0, + QStringLiteral("invalid version %1, expected <major>.<minor>") + .arg(sections[2])); + continue; + } + } else { reportError(lineNumber, 0, - QStringLiteral("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1)); + QStringLiteral("internal types require 2 or 3 arguments, " + "but %1 were provided").arg(sectionCount - 1)); continue; } - Component entry(sections[1], sections[2], QTypeRevision()); - entry.internal = true; - _components.insert(entry.typeName, entry); } else if (sections[0] == QLatin1String("singleton")) { if (sectionCount < 3 || sectionCount > 4) { reportError(lineNumber, 0, @@ -295,6 +291,16 @@ bool QQmlDirParser::parse(const QString &source) reportError(lineNumber, 0, QStringLiteral("designersupported does not expect any argument")); else _designerSupported = true; + } else if (sections[0] == QLatin1String("static")) { + if (sectionCount != 1) + reportError(lineNumber, 0, QStringLiteral("static does not expect any argument")); + else + _isStaticModule = true; + } else if (sections[0] == QLatin1String("system")) { + if (sectionCount != 1) + reportError(lineNumber, 0, QStringLiteral("system does not expect any argument")); + else + _isSystemModule = true; } else if (sections[0] == QLatin1String("import") || sections[0] == QLatin1String("depends")) { if (!readImport(sections, sectionCount, Import::Default)) @@ -321,6 +327,24 @@ bool QQmlDirParser::parse(const QString &source) } _preferredPath = sections[1]; + } else if (sections[0] == QLatin1String("linktarget")) { + if (sectionCount < 2) { + reportError(lineNumber, 0, + QStringLiteral("linktarget directive requires an argument, " + "but %1 were provided") + .arg(sectionCount - 1)); + continue; + } + + if (!_linkTarget.isEmpty()) { + reportError( + lineNumber, 0, + QStringLiteral( + "only one linktarget directive may be defined in a qmldir file")); + continue; + } + + _linkTarget = sections[1]; } else if (sectionCount == 2) { // No version specified (should only be used for relative qmldir files) const Component entry(sections[0], sections[1], QTypeRevision()); @@ -352,95 +376,155 @@ bool QQmlDirParser::parse(const QString &source) return hasError(); } -void QQmlDirParser::reportError(quint16 line, quint16 column, const QString &description) +/* removes all file selector occurrences in path + firstPlus is the position of the initial '+' in the path + which we always have as we check for '+' to decide whether + we need to do some work at all +*/ +static QString pathWithoutFileSelectors(QString path, // we want a copy of path + qsizetype firstPlus) { - QQmlJS::DiagnosticMessage error; - error.loc.startLine = line; - error.loc.startColumn = column; - error.message = description; - _errors.append(error); + do { + Q_ASSERT(path.at(firstPlus) == u'+'); + const auto eos = path.size(); + qsizetype terminatingSlashPos = firstPlus + 1; + while (terminatingSlashPos != eos && path.at(terminatingSlashPos) != u'/') + ++terminatingSlashPos; + path.remove(firstPlus, terminatingSlashPos - firstPlus + 1); + firstPlus = path.indexOf(u'+', firstPlus); + } while (firstPlus != -1); + return path; } -bool QQmlDirParser::hasError() const +static bool canDisambiguate( + const QString &fileName1, const QString &fileName2, QString *correctedFileName) { - if (! _errors.isEmpty()) + // If the entries are exactly the same we can delete one without losing anything. + if (fileName1 == fileName2) return true; - return false; -} + // If we detect conflicting paths, we check if they agree when we remove anything + // looking like a file selector. -void QQmlDirParser::setError(const QQmlJS::DiagnosticMessage &e) -{ - _errors.clear(); - reportError(e.loc.startLine, e.loc.startColumn, e.message); -} + // ugly heuristic to deal with file selectors + const qsizetype file2PotentialFileSelectorPos = fileName2.indexOf(u'+'); + const bool file2MightHaveFileSelector = file2PotentialFileSelectorPos != -1; -QList<QQmlJS::DiagnosticMessage> QQmlDirParser::errors(const QString &uri) const -{ - QList<QQmlJS::DiagnosticMessage> errors; - const int numErrors = _errors.size(); - errors.reserve(numErrors); - for (int i = 0; i < numErrors; ++i) { - QQmlJS::DiagnosticMessage e = _errors.at(i); - e.message.replace(QLatin1String("$$URI$$"), uri); - errors << e; + if (const qsizetype fileSelectorPos1 = fileName1.indexOf(u'+'); fileSelectorPos1 != -1) { + // existing entry was file selector entry, fix it up + // it could also be the case that _both_ are using file selectors + const QString baseName = file2MightHaveFileSelector + ? pathWithoutFileSelectors(fileName2, file2PotentialFileSelectorPos) + : fileName2; + + if (pathWithoutFileSelectors(fileName1, fileSelectorPos1) != baseName) + return false; + + *correctedFileName = baseName; + return true; } - return errors; -} -QString QQmlDirParser::typeNamespace() const -{ - return _typeNamespace; -} + // new entry contains file selector (and we know that fileName1 did not) + if (file2MightHaveFileSelector + && pathWithoutFileSelectors(fileName2, file2PotentialFileSelectorPos) == fileName1) { + *correctedFileName = fileName1; + return true; + } -void QQmlDirParser::setTypeNamespace(const QString &s) -{ - _typeNamespace = s; + return false; } -QList<QQmlDirParser::Plugin> QQmlDirParser::plugins() const +static void disambiguateFileSelectedComponents(QQmlDirComponents *components) { - return _plugins; -} + using ConstIterator = QQmlDirComponents::const_iterator; -QMultiHash<QString, QQmlDirParser::Component> QQmlDirParser::components() const -{ - return _components; -} + // end iterator may get invalidated by the erasing below. + // Therefore, refetch it on each iteration. + for (ConstIterator cit = components->constBegin(); cit != components->constEnd();) { -QList<QQmlDirParser::Import> QQmlDirParser::dependencies() const -{ - return _dependencies; -} + // We can erase and re-assign cit if we immediately forget cit2. + // But we cannot erase cit2 without potentially invalidating cit. -QList<QQmlDirParser::Import> QQmlDirParser::imports() const -{ - return _imports; + bool doErase = false; + const ConstIterator cend = components->constEnd(); + for (ConstIterator cit2 = ++ConstIterator(cit); cit2 != cend; ++cit2) { + if (cit2.key() != cit.key()) + break; + + Q_ASSERT(cit2->typeName == cit->typeName); + + if (cit2->version != cit->version + || cit2->internal != cit->internal + || cit2->singleton != cit->singleton) { + continue; + } + + // The two components may differ only by fileName now. + + if (canDisambiguate(cit->fileName, cit2->fileName, &(cit2->fileName))) { + doErase = true; + break; + } + } + + if (doErase) + cit = components->erase(cit); + else + ++cit; + } } -QList<QQmlDirParser::Script> QQmlDirParser::scripts() const +static void disambiguateFileSelectedScripts(QQmlDirScripts *scripts) { - return _scripts; + using Iterator = QQmlDirScripts::iterator; + + Iterator send = scripts->end(); + + for (Iterator sit = scripts->begin(); sit != send; ++sit) { + send = std::remove_if(++Iterator(sit), send, [sit](const QQmlDirParser::Script &script2) { + if (sit->nameSpace != script2.nameSpace || sit->version != script2.version) + return false; + + // The two scripts may differ only by fileName now. + return canDisambiguate(sit->fileName, script2.fileName, &(sit->fileName)); + }); + } + + scripts->erase(send, scripts->end()); } -QStringList QQmlDirParser::typeInfos() const +void QQmlDirParser::disambiguateFileSelectors() { - return _typeInfos; + disambiguateFileSelectedComponents(&_components); + disambiguateFileSelectedScripts(&_scripts); } -bool QQmlDirParser::designerSupported() const +void QQmlDirParser::reportError(quint16 line, quint16 column, const QString &description) { - return _designerSupported; + QQmlJS::DiagnosticMessage error; + error.loc.startLine = line; + error.loc.startColumn = column; + error.message = description; + _errors.append(error); } -QStringList QQmlDirParser::classNames() const +void QQmlDirParser::setError(const QQmlJS::DiagnosticMessage &e) { - return _classNames; + _errors.clear(); + reportError(e.loc.startLine, e.loc.startColumn, e.message); } -QString QQmlDirParser::preferredPath() const +QList<QQmlJS::DiagnosticMessage> QQmlDirParser::errors(const QString &uri) const { - return _preferredPath; + QList<QQmlJS::DiagnosticMessage> errors; + const int numErrors = _errors.size(); + errors.reserve(numErrors); + for (int i = 0; i < numErrors; ++i) { + QQmlJS::DiagnosticMessage e = _errors.at(i); + e.message.replace(QLatin1String("$$URI$$"), uri); + errors << e; + } + return errors; } QDebug &operator<< (QDebug &debug, const QQmlDirParser::Component &component) |