diff options
-rw-r--r-- | src/qml/.prev_CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/qml/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/qml/qml/qqmlimport.cpp | 58 | ||||
-rw-r--r-- | src/qml/qmldirparser/qmldirparser.pri | 6 | ||||
-rw-r--r-- | src/qml/qmldirparser/qqmlimportresolver.cpp | 122 | ||||
-rw-r--r-- | src/qml/qmldirparser/qqmlimportresolver_p.h | 65 | ||||
-rw-r--r-- | src/qmldevtools/.prev_CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/qmldevtools/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tools/qmllint/findwarnings.cpp | 99 |
9 files changed, 216 insertions, 138 deletions
diff --git a/src/qml/.prev_CMakeLists.txt b/src/qml/.prev_CMakeLists.txt index a76dc2b0fb..49d707034d 100644 --- a/src/qml/.prev_CMakeLists.txt +++ b/src/qml/.prev_CMakeLists.txt @@ -272,6 +272,7 @@ qt_add_module(Qml qml/v8/qv4domerrors.cpp qml/v8/qv4domerrors_p.h qml/v8/qv4sqlerrors.cpp qml/v8/qv4sqlerrors_p.h qmldirparser/qqmldirparser.cpp qmldirparser/qqmldirparser_p.h + qmldirparser/qqmlimportresolver.cpp qmldirparser/qqmlimportresolver_p.h qtqmlcompilerglobal.h qtqmlcompilerglobal_p.h qtqmlglobal.h qtqmlglobal_p.h types/qqmlbind.cpp types/qqmlbind_p.h diff --git a/src/qml/CMakeLists.txt b/src/qml/CMakeLists.txt index 9729702290..0195d9f571 100644 --- a/src/qml/CMakeLists.txt +++ b/src/qml/CMakeLists.txt @@ -272,6 +272,7 @@ qt_add_module(Qml qml/v8/qv4domerrors.cpp qml/v8/qv4domerrors_p.h qml/v8/qv4sqlerrors.cpp qml/v8/qv4sqlerrors_p.h qmldirparser/qqmldirparser.cpp qmldirparser/qqmldirparser_p.h + qmldirparser/qqmlimportresolver.cpp qmldirparser/qqmlimportresolver_p.h qtqmlcompilerglobal.h qtqmlcompilerglobal_p.h qtqmlglobal.h qtqmlglobal_p.h types/qqmlbind.cpp types/qqmlbind_p.h diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index fec42827b6..a6a230090e 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -59,6 +59,7 @@ #include <QtCore/qjsonobject.h> #include <QtCore/qjsonarray.h> #include <QtQml/private/qqmltype_p_p.h> +#include <QtQml/private/qqmlimportresolver_p.h> #include <algorithm> #include <functional> @@ -536,17 +537,6 @@ QList<QQmlImports::ScriptReference> QQmlImports::resolvedScripts() const return scripts; } -static QString joinStringRefs(const QVector<QStringRef> &refs, const QChar &sep) -{ - QString str; - for (auto it = refs.cbegin(); it != refs.cend(); ++it) { - if (it != refs.cbegin()) - str += sep; - str += *it; - } - return str; -} - /*! Forms complete paths to a qmldir file, from a base URL, a module URI and version specification. @@ -560,48 +550,10 @@ static QString joinStringRefs(const QVector<QStringRef> &refs, const QChar &sep) QStringList QQmlImports::completeQmldirPaths(const QString &uri, const QStringList &basePaths, QTypeRevision version) { - const QVector<QStringRef> parts = uri.splitRef(Dot, Qt::SkipEmptyParts); - - QStringList qmlDirPathsPaths; - // fully & partially versioned parts + 1 unversioned for each base path - qmlDirPathsPaths.reserve(basePaths.count() * (2 * parts.count() + 1)); - - for (int versionMode = FullyVersioned; versionMode <= Unversioned; ++versionMode) { - switch (versionMode) { - case FullyVersioned: - if (!version.hasMinorVersion()) - continue; - break; - case PartiallyVersioned: - if (!version.hasMajorVersion()) - continue; - break; - default: - break; - } - - const QString ver = versionString(version, QQmlImports::ImportVersion(versionMode)); - - for (const QString &path : basePaths) { - QString dir = path; - if (!dir.endsWith(Slash) && !dir.endsWith(Backslash)) - dir += Slash; - - // append to the end - qmlDirPathsPaths += dir + joinStringRefs(parts, Slash) + ver + Slash_qmldir; - - if (versionMode != Unversioned) { - // insert in the middle - for (int index = parts.count() - 2; index >= 0; --index) { - qmlDirPathsPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash) - + ver + Slash - + joinStringRefs(parts.mid(index + 1), Slash) + Slash_qmldir; - } - } - } - } - - return qmlDirPathsPaths; + QStringList paths = qQmlResolveImportPaths(uri, basePaths, version); + for (QString &path : paths) + path += Slash_qmldir; + return paths; } QString QQmlImports::versionString(QTypeRevision version, ImportVersion versionMode) diff --git a/src/qml/qmldirparser/qmldirparser.pri b/src/qml/qmldirparser/qmldirparser.pri index fefe2e75be..3923208e4a 100644 --- a/src/qml/qmldirparser/qmldirparser.pri +++ b/src/qml/qmldirparser/qmldirparser.pri @@ -2,7 +2,9 @@ INCLUDEPATH += $$PWD INCLUDEPATH += $$OUT_PWD HEADERS += \ - $$PWD/qqmldirparser_p.h + $$PWD/qqmldirparser_p.h \ + $$PWD/qqmlimportresolver_p.h SOURCES += \ - $$PWD/qqmldirparser.cpp + $$PWD/qqmldirparser.cpp \ + $$PWD/qqmlimportresolver.cpp diff --git a/src/qml/qmldirparser/qqmlimportresolver.cpp b/src/qml/qmldirparser/qqmlimportresolver.cpp new file mode 100644 index 0000000000..d427706140 --- /dev/null +++ b/src/qml/qmldirparser/qqmlimportresolver.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2020 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$ +** +****************************************************************************/ + +#include "qqmlimportresolver_p.h" + +QT_BEGIN_NAMESPACE + +enum ImportVersion { FullyVersioned, PartiallyVersioned, Unversioned }; + +/*! + Forms complete paths to a module, from a list of base paths, + a module URI and version specification. + + For example, QtQml.Models 2.0: + - base/QtQml/Models.2.0 + - base/QtQml.2.0/Models + - base/QtQml/Models.2 + - base/QtQml.2/Models + - base/QtQml/Models +*/ +QStringList qQmlResolveImportPaths(const QString &uri, const QStringList &basePaths, + QTypeRevision version) +{ + static const QLatin1Char Slash('/'); + static const QLatin1Char Backslash('\\'); + + const QVector<QStringRef> parts = uri.splitRef(QLatin1Char('.'), Qt::SkipEmptyParts); + + QStringList importPaths; + // fully & partially versioned parts + 1 unversioned for each base path + importPaths.reserve(2 * parts.count() + 1); + + auto versionString = [](QTypeRevision version, ImportVersion mode) + { + if (mode == FullyVersioned) { + // extension with fully encoded version number (eg. MyModule.3.2) + return QString::fromLatin1(".%1.%2").arg(version.majorVersion()) + .arg(version.minorVersion()); + } + if (mode == PartiallyVersioned) { + // extension with encoded version major (eg. MyModule.3) + return QString::fromLatin1(".%1").arg(version.majorVersion()); + } + // else extension without version number (eg. MyModule) + return QString(); + }; + + auto joinStringRefs = [](const QVector<QStringRef> &refs, const QChar &sep) { + QString str; + for (auto it = refs.cbegin(); it != refs.cend(); ++it) { + if (it != refs.cbegin()) + str += sep; + str += *it; + } + return str; + }; + + const ImportVersion initial = (version.hasMinorVersion()) + ? FullyVersioned + : (version.hasMajorVersion() ? PartiallyVersioned : Unversioned); + for (int mode = initial; mode <= Unversioned; ++mode) { + const QString ver = versionString(version, ImportVersion(mode)); + + for (const QString &path : basePaths) { + QString dir = path; + if (!dir.endsWith(Slash) && !dir.endsWith(Backslash)) + dir += Slash; + + // append to the end + importPaths += dir + joinStringRefs(parts, Slash) + ver; + + if (mode != Unversioned) { + // insert in the middle + for (int index = parts.count() - 2; index >= 0; --index) { + importPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash) + + ver + Slash + + joinStringRefs(parts.mid(index + 1), Slash); + } + } + } + } + + return importPaths; +} + +QT_END_NAMESPACE diff --git a/src/qml/qmldirparser/qqmlimportresolver_p.h b/src/qml/qmldirparser/qqmlimportresolver_p.h new file mode 100644 index 0000000000..bfc0592bf8 --- /dev/null +++ b/src/qml/qmldirparser/qqmlimportresolver_p.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef QQMLIMPORTRESOLVER_P_H +#define QQMLIMPORTRESOLVER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qglobal.h> +#include <QtCore/qstring.h> +#include <QtCore/qversionnumber.h> + +QT_BEGIN_NAMESPACE + +QStringList qQmlResolveImportPaths(const QString &uri, const QStringList &basePaths, + QTypeRevision version); + +QT_END_NAMESPACE + +#endif // QQMLIMPORTRESOLVER_P_H diff --git a/src/qmldevtools/.prev_CMakeLists.txt b/src/qmldevtools/.prev_CMakeLists.txt index a1f121d6d0..5a26da799d 100644 --- a/src/qmldevtools/.prev_CMakeLists.txt +++ b/src/qmldevtools/.prev_CMakeLists.txt @@ -38,6 +38,7 @@ qt_add_module(QmlDevTools ../qml/parser/qqmljskeywords_p.h ../qml/parser/qqmljslexer.cpp ../qml/parser/qqmljslexer_p.h ../qml/qmldirparser/qqmldirparser.cpp ../qml/qmldirparser/qqmldirparser_p.h + ../qml/qmldirparser/qqmlimportresolver.cpp ../qml/qmldirparser/qqmlimportresolver_p.h INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}/../qml ${CMAKE_CURRENT_BINARY_DIR}/../qml/compiler diff --git a/src/qmldevtools/CMakeLists.txt b/src/qmldevtools/CMakeLists.txt index 4648c6854c..9bd5563a5f 100644 --- a/src/qmldevtools/CMakeLists.txt +++ b/src/qmldevtools/CMakeLists.txt @@ -38,6 +38,7 @@ qt_add_module(QmlDevTools ../qml/parser/qqmljskeywords_p.h ../qml/parser/qqmljslexer.cpp ../qml/parser/qqmljslexer_p.h ../qml/qmldirparser/qqmldirparser.cpp ../qml/qmldirparser/qqmldirparser_p.h + ../qml/qmldirparser/qqmlimportresolver.cpp ../qml/qmldirparser/qqmlimportresolver_p.h INCLUDE_DIRECTORIES ${CMAKE_CURRENT_BINARY_DIR}/../qml ${CMAKE_CURRENT_BINARY_DIR}/../qml/compiler diff --git a/tools/qmllint/findwarnings.cpp b/tools/qmllint/findwarnings.cpp index 797b312d92..59b2fc90dc 100644 --- a/tools/qmllint/findwarnings.cpp +++ b/tools/qmllint/findwarnings.cpp @@ -37,6 +37,7 @@ #include <QtQml/private/qqmljsparser_p.h> #include <QtQml/private/qv4codegen_p.h> #include <QtQml/private/qqmldirparser_p.h> +#include <QtQml/private/qqmlimportresolver_p.h> #include <QtCore/qfile.h> #include <QtCore/qdiriterator.h> @@ -108,72 +109,6 @@ ScopeTree::Ptr FindWarningVisitor::parseProgram(QQmlJS::AST::Program *program, return result; } -enum ImportVersion { FullyVersioned, PartiallyVersioned, Unversioned, BasePath }; - -QStringList completeImportPaths(const QString &uri, const QString &basePath, QTypeRevision version) -{ - static const QLatin1Char Slash('/'); - static const QLatin1Char Backslash('\\'); - - const QVector<QStringRef> parts = uri.splitRef(QLatin1Char('.'), Qt::SkipEmptyParts); - - QStringList qmlDirPathsPaths; - // fully & partially versioned parts + 1 unversioned for each base path - qmlDirPathsPaths.reserve(2 * parts.count() + 1); - - auto versionString = [](QTypeRevision version, ImportVersion mode) - { - if (mode == FullyVersioned) { - // extension with fully encoded version number (eg. MyModule.3.2) - return QString::fromLatin1(".%1.%2").arg(version.majorVersion()) - .arg(version.minorVersion()); - } - if (mode == PartiallyVersioned) { - // extension with encoded version major (eg. MyModule.3) - return QString::fromLatin1(".%1").arg(version.majorVersion()); - } - // else extension without version number (eg. MyModule) - return QString(); - }; - auto joinStringRefs = [](const QVector<QStringRef> &refs, const QChar &sep) { - QString str; - for (auto it = refs.cbegin(); it != refs.cend(); ++it) { - if (it != refs.cbegin()) - str += sep; - str += *it; - } - return str; - }; - - const ImportVersion initial = (version.hasMinorVersion()) - ? FullyVersioned - : (version.hasMajorVersion() ? PartiallyVersioned : Unversioned); - for (int mode = initial; mode <= BasePath; ++mode) { - const QString ver = versionString(version, ImportVersion(mode)); - - QString dir = basePath; - if (!dir.endsWith(Slash) && !dir.endsWith(Backslash)) - dir += Slash; - - if (mode == BasePath) { - qmlDirPathsPaths += dir; - } else { - // append to the end - qmlDirPathsPaths += dir + joinStringRefs(parts, Slash) + ver; - } - - if (mode < Unversioned) { - // insert in the middle - for (int index = parts.count() - 2; index >= 0; --index) { - qmlDirPathsPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash) - + ver + Slash - + joinStringRefs(parts.mid(index + 1), Slash); - } - } - } - return qmlDirPathsPaths; -} - static const QLatin1String SlashQmldir = QLatin1String("/qmldir"); static const QLatin1String SlashPluginsDotQmltypes = QLatin1String("/plugins.qmltypes"); @@ -280,30 +215,28 @@ void FindWarningVisitor::importHelper(const QString &module, const QString &pref return; m_alreadySeenImports.insert(importId); - for (const QString &qmltypeDir : m_qmltypeDirs) { - auto qmltypesPaths = completeImportPaths(id, qmltypeDir, version); + auto qmltypesPaths = qQmlResolveImportPaths(id, m_qmltypeDirs, version) + m_qmltypeDirs; - for (auto const &qmltypesPath : qmltypesPaths) { - if (QFile::exists(qmltypesPath + SlashQmldir)) { - processImport(prefix, readQmldir(qmltypesPath)); + for (auto const &qmltypesPath : qmltypesPaths) { + if (QFile::exists(qmltypesPath + SlashQmldir)) { + processImport(prefix, readQmldir(qmltypesPath)); - // break so that we don't import unversioned qml components - // in addition to versioned ones - break; - } + // break so that we don't import unversioned qml components + // in addition to versioned ones + break; + } - if (!m_qmltypeFiles.isEmpty()) - continue; + if (!m_qmltypeFiles.isEmpty()) + continue; - Import result; + Import result; - QDirIterator it { qmltypesPath, QStringList() << QLatin1String("*.qmltypes"), QDir::Files }; + QDirIterator it { qmltypesPath, QStringList() << QLatin1String("*.qmltypes"), QDir::Files }; - while (it.hasNext()) - readQmltypes(it.next(), &result.objects, &result.dependencies); + while (it.hasNext()) + readQmltypes(it.next(), &result.objects, &result.dependencies); - processImport(prefix, result); - } + processImport(prefix, result); } if (!m_qmltypeFiles.isEmpty()) |