diff options
Diffstat (limited to 'src/qml/qmldirparser/qqmldirparser.cpp')
-rw-r--r-- | src/qml/qmldirparser/qqmldirparser.cpp | 149 |
1 files changed, 143 insertions, 6 deletions
diff --git a/src/qml/qmldirparser/qqmldirparser.cpp b/src/qml/qmldirparser/qqmldirparser.cpp index 112c7d12d0..e6a5691d3d 100644 --- a/src/qml/qmldirparser/qqmldirparser.cpp +++ b/src/qml/qmldirparser/qqmldirparser.cpp @@ -218,7 +218,7 @@ bool QQmlDirParser::parse(const QString &source) continue; } else { reportError(lineNumber, 0, - QStringLiteral("only optional imports can have a a defaultl, " + QStringLiteral("only optional imports can have a default, " "not %1.") .arg(sections[1])); } @@ -233,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, @@ -362,6 +376,129 @@ bool QQmlDirParser::parse(const QString &source) return hasError(); } +/* 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) +{ + 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; +} + +static bool canDisambiguate( + const QString &fileName1, const QString &fileName2, QString *correctedFileName) +{ + // If the entries are exactly the same we can delete one without losing anything. + if (fileName1 == fileName2) + return true; + + // If we detect conflicting paths, we check if they agree when we remove anything + // looking like a file selector. + + // ugly heuristic to deal with file selectors + const qsizetype file2PotentialFileSelectorPos = fileName2.indexOf(u'+'); + const bool file2MightHaveFileSelector = file2PotentialFileSelectorPos != -1; + + 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; + } + + // new entry contains file selector (and we know that fileName1 did not) + if (file2MightHaveFileSelector + && pathWithoutFileSelectors(fileName2, file2PotentialFileSelectorPos) == fileName1) { + *correctedFileName = fileName1; + return true; + } + + return false; +} + +static void disambiguateFileSelectedComponents(QQmlDirComponents *components) +{ + using ConstIterator = QQmlDirComponents::const_iterator; + + // end iterator may get invalidated by the erasing below. + // Therefore, refetch it on each iteration. + for (ConstIterator cit = components->constBegin(); cit != components->constEnd();) { + + // We can erase and re-assign cit if we immediately forget cit2. + // But we cannot erase cit2 without potentially invalidating cit. + + 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; + } +} + +static void disambiguateFileSelectedScripts(QQmlDirScripts *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()); +} + +void QQmlDirParser::disambiguateFileSelectors() +{ + disambiguateFileSelectedComponents(&_components); + disambiguateFileSelectedScripts(&_scripts); +} + void QQmlDirParser::reportError(quint16 line, quint16 column, const QString &description) { QQmlJS::DiagnosticMessage error; |