2019-06-14 12:21:25 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the tools applications of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
|
|
|
** 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 General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** 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-3.0.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
#include "findwarnings.h"
|
2019-11-11 16:35:09 +00:00
|
|
|
#include "importedmembersvisitor.h"
|
2019-06-14 12:21:25 +00:00
|
|
|
#include "scopetree.h"
|
2019-11-11 17:18:04 +00:00
|
|
|
#include "typedescriptionreader.h"
|
2020-03-30 15:42:48 +00:00
|
|
|
#include "checkidentifiers.h"
|
2020-08-12 10:36:15 +00:00
|
|
|
#include "qmljstypereader.h"
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtQml/private/qqmljsast_p.h>
|
|
|
|
#include <QtQml/private/qqmljslexer_p.h>
|
|
|
|
#include <QtQml/private/qqmljsparser_p.h>
|
|
|
|
#include <QtQml/private/qv4codegen_p.h>
|
2020-06-10 14:25:16 +00:00
|
|
|
#include <QtQml/private/qqmlimportresolver_p.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qfile.h>
|
|
|
|
#include <QtCore/qdiriterator.h>
|
|
|
|
#include <QtCore/qscopedvaluerollback.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
static const QString prefixedName(const QString &prefix, const QString &name)
|
|
|
|
{
|
2020-03-16 11:15:10 +00:00
|
|
|
Q_ASSERT(!prefix.endsWith('.'));
|
2019-11-08 17:43:31 +00:00
|
|
|
return prefix.isEmpty() ? name : (prefix + QLatin1Char('.') + name);
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:34:08 +00:00
|
|
|
static QQmlDirParser createQmldirParserForFile(const QString &filename)
|
|
|
|
{
|
|
|
|
QFile f(filename);
|
|
|
|
f.open(QFile::ReadOnly);
|
|
|
|
QQmlDirParser parser;
|
|
|
|
parser.parse(f.readAll());
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::enterEnvironment(ScopeType type, const QString &name)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-09-25 08:13:26 +00:00
|
|
|
m_currentScope = ScopeTree::create(type, m_currentScope);
|
2020-09-25 10:39:17 +00:00
|
|
|
m_currentScope->setBaseTypeName(name);
|
|
|
|
m_currentScope->setIsComposite(true);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::leaveEnvironment()
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
m_currentScope = m_currentScope->parentScope();
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
static const QLatin1String SlashQmldir = QLatin1String("/qmldir");
|
|
|
|
static const QLatin1String SlashPluginsDotQmltypes = QLatin1String("/plugins.qmltypes");
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2020-09-24 11:23:32 +00:00
|
|
|
void FindWarningVisitor::Importer::readQmltypes(
|
|
|
|
const QString &filename, QHash<QString, ScopeTree::Ptr> *objects)
|
2019-11-14 16:08:15 +00:00
|
|
|
{
|
2020-04-29 08:47:19 +00:00
|
|
|
const QFileInfo fileInfo(filename);
|
|
|
|
if (!fileInfo.exists()) {
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnings.append(QLatin1String("QML types file does not exist: ") + filename);
|
2020-04-29 08:47:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileInfo.isDir()) {
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnings.append(QLatin1String("QML types file cannot be a directory: ") + filename);
|
2020-04-29 08:47:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
TypeDescriptionReader reader { filename, file.readAll() };
|
2020-06-22 16:38:43 +00:00
|
|
|
QStringList dependencies;
|
|
|
|
auto succ = reader(objects, &dependencies);
|
2019-11-14 16:08:15 +00:00
|
|
|
if (!succ)
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnings.append(reader.errorMessage());
|
2019-11-14 16:08:15 +00:00
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2020-09-24 08:45:58 +00:00
|
|
|
FindWarningVisitor::Importer::Import FindWarningVisitor::Importer::readQmldir(const QString &path)
|
2019-11-14 16:08:15 +00:00
|
|
|
{
|
|
|
|
Import result;
|
|
|
|
auto reader = createQmldirParserForFile(path + SlashQmldir);
|
2020-06-22 16:38:43 +00:00
|
|
|
result.imports.append(reader.imports());
|
2020-09-25 11:29:50 +00:00
|
|
|
result.dependencies.append(reader.dependencies());
|
2019-11-14 16:08:15 +00:00
|
|
|
|
2020-03-31 13:46:20 +00:00
|
|
|
QHash<QString, ScopeTree::Ptr> qmlComponents;
|
2019-11-14 16:08:15 +00:00
|
|
|
const auto components = reader.components();
|
|
|
|
for (auto it = components.begin(), end = components.end(); it != end; ++it) {
|
|
|
|
const QString filePath = path + QLatin1Char('/') + it->fileName;
|
|
|
|
if (!QFile::exists(filePath)) {
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnings.append(it->fileName + QLatin1String(" is listed as component in ")
|
|
|
|
+ path + SlashQmldir
|
|
|
|
+ QLatin1String(" but does not exist.\n"));
|
2019-11-14 16:08:15 +00:00
|
|
|
continue;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
|
|
|
|
auto mo = qmlComponents.find(it.key());
|
|
|
|
if (mo == qmlComponents.end())
|
2019-11-08 17:43:31 +00:00
|
|
|
mo = qmlComponents.insert(it.key(), localFile2ScopeTree(filePath));
|
2019-11-14 16:08:15 +00:00
|
|
|
|
|
|
|
(*mo)->addExport(
|
|
|
|
it.key(), reader.typeNamespace(),
|
2020-01-22 12:12:56 +00:00
|
|
|
ComponentVersion(it->version));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
for (auto it = qmlComponents.begin(), end = qmlComponents.end(); it != end; ++it)
|
2020-09-24 11:23:32 +00:00
|
|
|
result.objects.insert(it.key(), it.value());
|
2019-11-14 16:08:15 +00:00
|
|
|
|
|
|
|
if (!reader.plugins().isEmpty() && QFile::exists(path + SlashPluginsDotQmltypes))
|
2020-06-22 16:38:43 +00:00
|
|
|
readQmltypes(path + SlashPluginsDotQmltypes, &result.objects);
|
2019-11-14 16:08:15 +00:00
|
|
|
|
2020-08-21 12:50:52 +00:00
|
|
|
const auto scripts = reader.scripts();
|
|
|
|
for (const auto &script : scripts) {
|
|
|
|
const QString filePath = path + QLatin1Char('/') + script.fileName;
|
2020-09-25 13:00:13 +00:00
|
|
|
result.scripts.insert(script.nameSpace, localFile2ScopeTree(filePath));
|
2020-08-21 12:50:52 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
void FindWarningVisitor::Importer::importDependencies(
|
|
|
|
const FindWarningVisitor::Importer::Import &import,
|
|
|
|
FindWarningVisitor::ImportedTypes *types, const QString &prefix, QTypeRevision version)
|
2019-11-14 16:08:15 +00:00
|
|
|
{
|
2020-06-22 16:38:43 +00:00
|
|
|
// Import the dependencies with an invalid prefix. The prefix will never be matched by actual
|
|
|
|
// QML code but the C++ types will be visible.
|
|
|
|
const QString invalidPrefix = QString::fromLatin1("$dependency$");
|
|
|
|
for (auto const &dependency : qAsConst(import.dependencies))
|
2020-09-25 14:14:53 +00:00
|
|
|
importHelper(dependency.module, types, invalidPrefix, dependency.version);
|
2019-12-11 14:31:55 +00:00
|
|
|
|
2020-06-22 16:38:43 +00:00
|
|
|
for (auto const &import : qAsConst(import.imports)) {
|
2020-09-25 14:14:53 +00:00
|
|
|
importHelper(import.module, types, prefix,
|
2020-06-22 16:38:43 +00:00
|
|
|
import.isAutoImport ? version : import.version);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2020-09-25 14:14:53 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
void FindWarningVisitor::Importer::processImport(
|
|
|
|
const FindWarningVisitor::Importer::Import &import,
|
|
|
|
FindWarningVisitor::ImportedTypes *types,
|
|
|
|
const QString &prefix)
|
|
|
|
{
|
2020-09-25 13:00:13 +00:00
|
|
|
for (auto it = import.scripts.begin(); it != import.scripts.end(); ++it) {
|
2020-09-25 14:14:53 +00:00
|
|
|
types->importedQmlNames.insert(prefixedName(prefix, it.key()), it.value());
|
|
|
|
types->exportedQmlNames.insert(it.key(), it.value());
|
2020-08-21 12:50:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// add objects
|
2019-11-14 16:08:15 +00:00
|
|
|
for (auto it = import.objects.begin(); it != import.objects.end(); ++it) {
|
|
|
|
const auto &val = it.value();
|
2020-09-25 14:14:53 +00:00
|
|
|
types->cppNames.insert(val->internalName(), val);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const auto exports = val->exports();
|
2020-09-24 13:09:38 +00:00
|
|
|
for (const auto &valExport : exports) {
|
2020-09-25 14:14:53 +00:00
|
|
|
types->importedQmlNames.insert(prefixedName(prefix, valExport.type()), val);
|
|
|
|
types->exportedQmlNames.insert(valExport.type(), val);
|
2020-09-24 13:09:38 +00:00
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2020-09-25 14:14:53 +00:00
|
|
|
|
|
|
|
for (auto it = import.objects.begin(); it != import.objects.end(); ++it) {
|
|
|
|
const auto &val = it.value();
|
|
|
|
if (!val->isComposite()) // Otherwise we have already done it in localFile2ScopeTree()
|
|
|
|
val->resolveTypes(types->cppNames);
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 13:09:38 +00:00
|
|
|
FindWarningVisitor::ImportedTypes FindWarningVisitor::Importer::importBareQmlTypes(
|
2020-09-24 08:45:58 +00:00
|
|
|
const QStringList &qmltypesFiles)
|
2020-06-22 16:38:43 +00:00
|
|
|
{
|
2020-09-25 14:14:53 +00:00
|
|
|
ImportedTypes types;
|
2020-09-24 08:45:58 +00:00
|
|
|
|
|
|
|
for (auto const &dir : m_importPaths) {
|
2020-06-22 16:38:43 +00:00
|
|
|
Import result;
|
|
|
|
QDirIterator it { dir, QStringList() << QLatin1String("builtins.qmltypes"), QDir::NoFilter,
|
|
|
|
QDirIterator::Subdirectories };
|
|
|
|
while (it.hasNext())
|
|
|
|
readQmltypes(it.next(), &result.objects);
|
2020-09-25 14:14:53 +00:00
|
|
|
importDependencies(result, &types);
|
|
|
|
processImport(result, &types);
|
2020-06-22 16:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:45:58 +00:00
|
|
|
if (qmltypesFiles.isEmpty()) {
|
|
|
|
for (auto const &qmltypesPath : m_importPaths) {
|
2020-06-22 16:38:43 +00:00
|
|
|
if (QFile::exists(qmltypesPath + SlashQmldir))
|
|
|
|
continue;
|
|
|
|
Import result;
|
|
|
|
QDirIterator it {
|
|
|
|
qmltypesPath, QStringList { QLatin1String("*.qmltypes") }, QDir::Files };
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
const QString name = it.next();
|
|
|
|
if (!name.endsWith(QLatin1String("/builtins.qmltypes")))
|
|
|
|
readQmltypes(name, &result.objects);
|
|
|
|
}
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
importDependencies(result, &types);
|
|
|
|
processImport(result, &types);
|
2020-06-22 16:38:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Import result;
|
|
|
|
|
2020-09-24 08:45:58 +00:00
|
|
|
for (const auto &qmltypeFile : qmltypesFiles)
|
2020-06-22 16:38:43 +00:00
|
|
|
readQmltypes(qmltypeFile, &result.objects);
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
importDependencies(result, &types);
|
|
|
|
processImport(result, &types);
|
2020-06-22 16:38:43 +00:00
|
|
|
}
|
2020-09-24 08:45:58 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
return types;
|
2020-06-22 16:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 13:09:38 +00:00
|
|
|
FindWarningVisitor::ImportedTypes FindWarningVisitor::Importer::importModule(
|
2020-09-24 08:45:58 +00:00
|
|
|
const QString &module, const QString &prefix, QTypeRevision version)
|
|
|
|
{
|
2020-09-24 13:09:38 +00:00
|
|
|
ImportedTypes result;
|
2020-09-25 14:14:53 +00:00
|
|
|
importHelper(module, &result, prefix, version);
|
2020-09-24 08:45:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
void FindWarningVisitor::Importer::importHelper(const QString &module, ImportedTypes *types,
|
|
|
|
const QString &prefix, QTypeRevision version)
|
2019-11-14 16:08:15 +00:00
|
|
|
{
|
2020-09-25 14:14:53 +00:00
|
|
|
|
|
|
|
const QPair<QString, QTypeRevision> importId { module, version };
|
|
|
|
const auto it = m_seenImports.find(importId);
|
|
|
|
if (it != m_seenImports.end()) {
|
|
|
|
importDependencies(*it, types, prefix, version);
|
|
|
|
processImport(*it, types, prefix);
|
2019-11-14 16:08:15 +00:00
|
|
|
return;
|
2020-09-25 14:14:53 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
|
2020-09-24 08:45:58 +00:00
|
|
|
const auto qmltypesPaths = qQmlResolveImportPaths(module, m_importPaths, version);
|
2020-06-10 14:25:16 +00:00
|
|
|
for (auto const &qmltypesPath : qmltypesPaths) {
|
2020-09-25 14:14:53 +00:00
|
|
|
const QFileInfo file(qmltypesPath + SlashQmldir);
|
|
|
|
if (file.exists()) {
|
|
|
|
const auto import = readQmldir(file.canonicalPath());
|
|
|
|
m_seenImports.insert(importId, import);
|
|
|
|
importDependencies(import, types, prefix, version);
|
|
|
|
processImport(import, types, prefix);
|
|
|
|
return;
|
2020-06-10 14:25:16 +00:00
|
|
|
}
|
2020-03-26 09:57:12 +00:00
|
|
|
}
|
2020-09-25 14:14:53 +00:00
|
|
|
|
|
|
|
m_seenImports.insert(importId, {});
|
2019-11-14 16:08:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:45:58 +00:00
|
|
|
ScopeTree::Ptr FindWarningVisitor::Importer::localFile2ScopeTree(const QString &filePath)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-09-25 14:14:53 +00:00
|
|
|
const auto seen = m_importedFiles.find(filePath);
|
|
|
|
if (seen != m_importedFiles.end())
|
|
|
|
return *seen;
|
|
|
|
|
2020-08-12 10:36:15 +00:00
|
|
|
QmlJSTypeReader typeReader(filePath);
|
|
|
|
ScopeTree::Ptr result = typeReader();
|
2020-09-25 14:14:53 +00:00
|
|
|
m_importedFiles.insert(filePath, result);
|
2019-11-11 16:35:09 +00:00
|
|
|
|
2020-08-12 10:36:15 +00:00
|
|
|
const QStringList errors = typeReader.errors();
|
|
|
|
for (const QString &error : errors)
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnings.append(error);
|
2019-11-11 16:35:09 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
ImportedTypes types;
|
|
|
|
|
|
|
|
QDirIterator it {
|
|
|
|
QFileInfo(filePath).canonicalPath(),
|
|
|
|
QStringList() << QLatin1String("*.qml"),
|
|
|
|
QDir::NoFilter
|
|
|
|
};
|
|
|
|
while (it.hasNext()) {
|
|
|
|
ScopeTree::Ptr scope(localFile2ScopeTree(it.next()));
|
|
|
|
if (!scope->internalName().isEmpty())
|
|
|
|
types.importedQmlNames.insert(scope->internalName(), scope);
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:36:15 +00:00
|
|
|
const auto imports = typeReader.imports();
|
|
|
|
for (const auto &import : imports)
|
2020-09-25 14:14:53 +00:00
|
|
|
importHelper(import.module, &types, import.prefix, import.version);
|
2019-11-08 17:43:31 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
result->resolveTypes(types.importedQmlNames);
|
2020-08-12 10:36:15 +00:00
|
|
|
return result;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 13:09:38 +00:00
|
|
|
FindWarningVisitor::ImportedTypes FindWarningVisitor::Importer::importFileOrDirectory(
|
2020-09-24 08:45:58 +00:00
|
|
|
const QString &fileOrDirectory, const QString &prefix)
|
2019-08-16 15:03:30 +00:00
|
|
|
{
|
2020-09-24 13:09:38 +00:00
|
|
|
ImportedTypes result;
|
2020-09-24 08:45:58 +00:00
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
QString name = fileOrDirectory;
|
|
|
|
|
|
|
|
if (QFileInfo(name).isRelative())
|
2020-09-24 08:45:58 +00:00
|
|
|
name = QDir(m_currentDir).filePath(name);
|
2019-11-08 17:43:31 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
QFileInfo fileInfo(name);
|
|
|
|
if (fileInfo.isFile()) {
|
|
|
|
ScopeTree::Ptr scope(localFile2ScopeTree(fileInfo.canonicalFilePath()));
|
|
|
|
result.importedQmlNames.insert(prefix.isEmpty() ? scope->internalName() : prefix, scope);
|
|
|
|
result.exportedQmlNames.insert(scope->internalName(), scope);
|
2020-09-24 08:45:58 +00:00
|
|
|
return result;
|
2019-11-08 17:43:31 +00:00
|
|
|
}
|
2019-08-16 15:03:30 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
QDirIterator it {
|
|
|
|
fileInfo.canonicalFilePath(),
|
|
|
|
QStringList() << QLatin1String("*.qml"),
|
|
|
|
QDir::NoFilter
|
|
|
|
};
|
2019-08-16 15:03:30 +00:00
|
|
|
while (it.hasNext()) {
|
2020-09-24 11:23:32 +00:00
|
|
|
ScopeTree::Ptr scope(localFile2ScopeTree(it.next()));
|
2020-09-24 13:09:38 +00:00
|
|
|
if (!scope->internalName().isEmpty()) {
|
2020-09-25 14:14:53 +00:00
|
|
|
result.importedQmlNames.insert(prefixedName(prefix, scope->internalName()), scope);
|
|
|
|
result.exportedQmlNames.insert(scope->internalName(), scope);
|
2020-09-24 13:09:38 +00:00
|
|
|
}
|
2019-08-16 15:03:30 +00:00
|
|
|
}
|
2020-09-24 08:45:58 +00:00
|
|
|
|
|
|
|
return result;
|
2019-08-16 15:03:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
void FindWarningVisitor::importExportedNames(ScopeTree::ConstPtr scope)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-03-16 13:50:24 +00:00
|
|
|
QList<ScopeTree::ConstPtr> scopes;
|
2020-09-25 14:14:53 +00:00
|
|
|
while (!scope.isNull()) {
|
|
|
|
if (scopes.contains(scope)) {
|
|
|
|
QString inheritenceCycle;
|
|
|
|
for (const auto &seen: qAsConst(scopes)) {
|
|
|
|
if (!inheritenceCycle.isEmpty())
|
2020-03-16 13:50:24 +00:00
|
|
|
inheritenceCycle.append(QLatin1String(" -> "));
|
2020-09-25 14:14:53 +00:00
|
|
|
inheritenceCycle.append(seen->baseTypeName());
|
|
|
|
}
|
2020-03-16 13:50:24 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
if (m_warnInheritanceCycle) {
|
|
|
|
m_colorOut.write(QLatin1String("Warning: "), Warning);
|
|
|
|
m_colorOut.write(QString::fromLatin1("%1 is part of an inheritance cycle: %2\n")
|
|
|
|
.arg(scope->internalName())
|
|
|
|
.arg(inheritenceCycle));
|
|
|
|
}
|
2020-04-24 13:10:53 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
m_unknownImports.insert(scope->internalName());
|
|
|
|
m_visitFailed = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-04-24 13:10:53 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
scopes.append(scope);
|
|
|
|
|
|
|
|
const auto properties = scope->properties();
|
|
|
|
for (auto property : properties)
|
|
|
|
m_currentScope->insertPropertyIdentifier(property);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
m_currentScope->addMethods(scope->methods());
|
2020-09-24 13:09:38 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
if (scope->baseTypeName().isEmpty()) {
|
|
|
|
break;
|
|
|
|
} else if (auto newScope = scope->baseType()) {
|
|
|
|
scope = newScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
} else {
|
2019-07-23 14:10:24 +00:00
|
|
|
m_colorOut.write(QLatin1String("warning: "), Warning);
|
2020-09-25 14:14:53 +00:00
|
|
|
m_colorOut.write(scope->baseTypeName()
|
|
|
|
+ QLatin1String(" was not found."
|
|
|
|
" Did you add all import paths?\n"));
|
|
|
|
m_unknownImports.insert(scope->baseTypeName());
|
2019-11-14 15:31:48 +00:00
|
|
|
m_visitFailed = true;
|
2019-06-14 12:21:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::throwRecursionDepthError()
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-08-16 08:14:49 +00:00
|
|
|
m_colorOut.write(QStringLiteral("Error"), Error);
|
|
|
|
m_colorOut.write(QStringLiteral("Maximum statement or expression depth exceeded"), Error);
|
|
|
|
m_visitFailed = true;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiProgram *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::QMLScope, "program");
|
2020-09-24 08:45:58 +00:00
|
|
|
m_rootScopeImports = m_importer.importBareQmlTypes(m_qmltypesFiles);
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// add "self" (as we only ever check the first part of a qualified identifier, we get away with
|
2019-11-11 17:18:04 +00:00
|
|
|
// using an empty ScopeTree
|
2020-09-24 13:09:38 +00:00
|
|
|
m_rootScopeImports.importedQmlNames.insert(QFileInfo { m_filePath }.baseName(), {});
|
|
|
|
|
|
|
|
const auto imported = m_importer.importFileOrDirectory(".");
|
|
|
|
m_rootScopeImports.importedQmlNames.insert(imported.importedQmlNames);
|
|
|
|
m_rootScopeImports.cppNames.insert(imported.cppNames);
|
2020-09-24 08:45:58 +00:00
|
|
|
|
|
|
|
const QStringList warnings = m_importer.takeWarnings();
|
|
|
|
for (const QString &warning : warnings) {
|
|
|
|
m_colorOut.write(QStringLiteral("warning: "), Warning);
|
|
|
|
m_colorOut.writeUncolored(warning);
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::UiProgram *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::ClassExpression *ast)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::ClassExpression *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::ClassDeclaration *ast)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::ClassDeclaration *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::ForStatement *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "forloop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::ForStatement *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::ForEachStatement *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "foreachloop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::ForEachStatement *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::Block *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "block");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::Block *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::CaseBlock *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "case");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::CaseBlock *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::Catch *catchStatement)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "catch");
|
2019-11-11 17:18:04 +00:00
|
|
|
m_currentScope->insertJSIdentifier(catchStatement->patternElement->bindingIdentifier.toString(),
|
2020-03-30 15:42:48 +00:00
|
|
|
ScopeType::JSLexicalScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::Catch *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::WithStatement *withStatement)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-04-24 13:10:53 +00:00
|
|
|
if (m_warnWithStatement) {
|
|
|
|
m_colorOut.write(QString::fromLatin1("Warning: "), Warning);
|
|
|
|
m_colorOut.write(QString::fromLatin1(
|
|
|
|
"%1:%2: with statements are strongly discouraged in QML "
|
|
|
|
"and might cause false positives when analysing unqalified identifiers\n")
|
|
|
|
.arg(withStatement->firstSourceLocation().startLine)
|
|
|
|
.arg(withStatement->firstSourceLocation().startColumn),
|
|
|
|
Normal);
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "with");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::WithStatement *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2019-12-17 08:29:31 +00:00
|
|
|
static QString signalName(QStringView handlerName)
|
2019-08-16 09:31:51 +00:00
|
|
|
{
|
2019-12-17 08:29:31 +00:00
|
|
|
if (handlerName.startsWith(u"on") && handlerName.size() > 2) {
|
2019-08-16 09:31:51 +00:00
|
|
|
QString signal = handlerName.mid(2).toString();
|
|
|
|
for (int i = 0; i < signal.length(); ++i) {
|
2019-12-17 08:29:31 +00:00
|
|
|
QChar &ch = signal[i];
|
2019-08-16 09:31:51 +00:00
|
|
|
if (ch.isLower())
|
|
|
|
return QString();
|
|
|
|
if (ch.isUpper()) {
|
|
|
|
ch = ch.toLower();
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiScriptBinding *uisb)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
auto name = uisb->qualifiedId->name;
|
|
|
|
if (name == QLatin1String("id")) {
|
|
|
|
// found id
|
2019-11-11 17:18:04 +00:00
|
|
|
auto expstat = cast<ExpressionStatement *>(uisb->statement);
|
|
|
|
auto identexp = cast<IdentifierExpression *>(expstat->expression);
|
2019-11-08 13:48:32 +00:00
|
|
|
m_qmlid2scope.insert(identexp->name.toString(), m_currentScope);
|
2019-11-11 17:18:04 +00:00
|
|
|
if (m_currentScope->isVisualRootScope())
|
2019-06-14 12:21:25 +00:00
|
|
|
m_rootId = identexp->name.toString();
|
2019-08-16 09:31:51 +00:00
|
|
|
} else {
|
|
|
|
const QString signal = signalName(name);
|
|
|
|
if (signal.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!m_currentScope->methods().contains(signal)) {
|
|
|
|
m_currentScope->addUnmatchedSignalHandler(name.toString(), uisb->firstSourceLocation());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto statement = uisb->statement;
|
2019-06-14 12:21:25 +00:00
|
|
|
if (statement->kind == Node::Kind::Kind_ExpressionStatement) {
|
2019-11-11 17:18:04 +00:00
|
|
|
if (cast<ExpressionStatement *>(statement)->expression->asFunctionDefinition()) {
|
2019-06-14 12:21:25 +00:00
|
|
|
// functions are already handled
|
|
|
|
// they do not get names inserted according to the signal, but access their formal
|
|
|
|
// parameters
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 09:31:51 +00:00
|
|
|
|
2020-04-09 16:43:43 +00:00
|
|
|
const auto methods = m_currentScope->methods();
|
|
|
|
const auto methodsRange = methods.equal_range(signal);
|
|
|
|
for (auto method = methodsRange.first; method != methodsRange.second; ++method) {
|
|
|
|
if (method->methodType() != MetaMethod::Signal)
|
|
|
|
continue;
|
|
|
|
for (auto const ¶m : method->parameterNames()) {
|
|
|
|
const auto firstSourceLocation = statement->firstSourceLocation();
|
|
|
|
bool hasMultilineStatementBody
|
|
|
|
= statement->lastSourceLocation().startLine > firstSourceLocation.startLine;
|
|
|
|
m_currentScope->insertSignalIdentifier(param, *method, firstSourceLocation,
|
|
|
|
hasMultilineStatementBody);
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiPublicMember *uipm)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-08-18 19:22:10 +00:00
|
|
|
if (uipm->type == QQmlJS::AST::UiPublicMember::Signal) {
|
|
|
|
MetaMethod method;
|
|
|
|
method.setMethodType(MetaMethod::Signal);
|
|
|
|
method.setMethodName(uipm->name.toString());
|
|
|
|
QQmlJS::AST::UiParameterList *param = uipm->parameters;
|
|
|
|
while (param) {
|
|
|
|
method.addParameter(param->name.toString(), param->type->name.toString());
|
|
|
|
param = param->next;
|
|
|
|
}
|
|
|
|
m_currentScope->addMethod(method);
|
|
|
|
} else {
|
|
|
|
// property bool inactive: !active
|
|
|
|
// extract name inactive
|
|
|
|
MetaProperty property(
|
2019-11-11 17:18:04 +00:00
|
|
|
uipm->name.toString(),
|
2020-08-18 19:22:10 +00:00
|
|
|
// TODO: complex types etc.
|
2019-11-11 17:18:04 +00:00
|
|
|
uipm->memberType ? uipm->memberType->name.toString() : QString(),
|
2020-08-18 19:22:10 +00:00
|
|
|
uipm->typeModifier == QLatin1String("list"), !uipm->isReadonlyMember, false,
|
|
|
|
uipm->memberType ? (uipm->memberType->name == QLatin1String("alias")) : false, 0);
|
2020-09-24 13:09:38 +00:00
|
|
|
property.setType(m_rootScopeImports.importedQmlNames.value(property.typeName()));
|
2020-08-18 19:22:10 +00:00
|
|
|
m_currentScope->insertPropertyIdentifier(property);
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::IdentifierExpression *idexp)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
auto name = idexp->name;
|
2019-11-08 13:48:32 +00:00
|
|
|
m_currentScope->addIdToAccessed(name.toString(), idexp->firstSourceLocation());
|
|
|
|
m_fieldMemberBase = idexp;
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:38:43 +00:00
|
|
|
FindWarningVisitor::FindWarningVisitor(
|
|
|
|
QStringList qmltypeDirs, QStringList qmltypesFiles, QString code, QString fileName,
|
|
|
|
bool silent, bool warnUnqualified, bool warnWithStatement, bool warnInheritanceCycle)
|
2020-09-25 08:13:26 +00:00
|
|
|
: m_rootScope(ScopeTree::create(ScopeType::JSFunctionScope)),
|
2020-06-22 16:38:43 +00:00
|
|
|
m_qmltypesFiles(std::move(qmltypesFiles)),
|
2019-11-11 17:18:04 +00:00
|
|
|
m_code(std::move(code)),
|
2019-06-14 12:21:25 +00:00
|
|
|
m_rootId(QLatin1String("<id>")),
|
2019-11-11 17:18:04 +00:00
|
|
|
m_filePath(std::move(fileName)),
|
2020-04-24 13:10:53 +00:00
|
|
|
m_colorOut(silent),
|
|
|
|
m_warnUnqualified(warnUnqualified),
|
|
|
|
m_warnWithStatement(warnWithStatement),
|
2020-09-24 08:45:58 +00:00
|
|
|
m_warnInheritanceCycle(warnInheritanceCycle),
|
|
|
|
m_importer(QFileInfo(m_filePath).path(), qmltypeDirs)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-09-25 08:13:26 +00:00
|
|
|
m_rootScope->setInternalName("global");
|
2020-03-31 13:46:20 +00:00
|
|
|
m_currentScope = m_rootScope;
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// setup color output
|
2019-11-11 17:18:04 +00:00
|
|
|
m_colorOut.insertMapping(Error, ColorOutput::RedForeground);
|
|
|
|
m_colorOut.insertMapping(Warning, ColorOutput::PurpleForeground);
|
|
|
|
m_colorOut.insertMapping(Info, ColorOutput::BlueForeground);
|
|
|
|
m_colorOut.insertMapping(Normal, ColorOutput::DefaultColor);
|
|
|
|
m_colorOut.insertMapping(Hint, ColorOutput::GreenForeground);
|
2019-06-14 12:21:25 +00:00
|
|
|
QLatin1String jsGlobVars[] = {
|
2019-07-24 06:42:17 +00:00
|
|
|
/* Not listed on the MDN page; browser and QML extensions: */
|
|
|
|
// console/debug api
|
|
|
|
QLatin1String("console"), QLatin1String("print"),
|
|
|
|
// garbage collector
|
|
|
|
QLatin1String("gc"),
|
|
|
|
// i18n
|
2019-11-11 17:18:04 +00:00
|
|
|
QLatin1String("qsTr"), QLatin1String("qsTrId"), QLatin1String("QT_TR_NOOP"),
|
|
|
|
QLatin1String("QT_TRANSLATE_NOOP"), QLatin1String("QT_TRID_NOOP"),
|
2019-07-24 06:42:17 +00:00
|
|
|
// XMLHttpRequest
|
|
|
|
QLatin1String("XMLHttpRequest")
|
2019-06-14 12:21:25 +00:00
|
|
|
};
|
2019-11-11 17:18:04 +00:00
|
|
|
for (const char **globalName = QV4::Compiler::Codegen::s_globalNames;
|
|
|
|
*globalName != nullptr;
|
|
|
|
++globalName) {
|
|
|
|
m_currentScope->insertJSIdentifier(QString::fromLatin1(*globalName),
|
2020-03-30 15:42:48 +00:00
|
|
|
ScopeType::JSLexicalScope);
|
2019-07-24 06:42:17 +00:00
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
for (const auto& jsGlobVar: jsGlobVars)
|
2020-03-30 15:42:48 +00:00
|
|
|
m_currentScope->insertJSIdentifier(jsGlobVar, ScopeType::JSLexicalScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::check()
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-08-16 08:14:49 +00:00
|
|
|
if (m_visitFailed)
|
|
|
|
return false;
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// now that all ids are known, revisit any Connections whose target were perviously unknown
|
2019-11-11 17:18:04 +00:00
|
|
|
for (auto const &outstandingConnection: m_outstandingConnections) {
|
|
|
|
auto targetScope = m_qmlid2scope[outstandingConnection.targetName];
|
2020-05-06 11:21:21 +00:00
|
|
|
if (outstandingConnection.scope && targetScope != nullptr)
|
2019-11-11 17:18:04 +00:00
|
|
|
outstandingConnection.scope->addMethods(targetScope->methods());
|
2020-03-31 13:46:20 +00:00
|
|
|
QScopedValueRollback<ScopeTree::Ptr> rollback(m_currentScope, outstandingConnection.scope);
|
2019-06-14 12:21:25 +00:00
|
|
|
outstandingConnection.uiod->initializer->accept(this);
|
|
|
|
}
|
2020-03-30 15:42:48 +00:00
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
if (!m_warnUnqualified)
|
|
|
|
return true;
|
|
|
|
|
2020-09-25 10:39:17 +00:00
|
|
|
CheckIdentifiers check(&m_colorOut, m_code, m_rootScopeImports, m_filePath);
|
2020-03-31 13:46:20 +00:00
|
|
|
return check(m_qmlid2scope, m_rootScope, m_rootId);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::VariableDeclarationList *vdl)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
while (vdl) {
|
2020-03-30 15:42:48 +00:00
|
|
|
m_currentScope->insertJSIdentifier(
|
|
|
|
vdl->declaration->bindingIdentifier.toString(),
|
|
|
|
(vdl->declaration->scope == QQmlJS::AST::VariableScope::Var)
|
|
|
|
? ScopeType::JSFunctionScope
|
|
|
|
: ScopeType::JSLexicalScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
vdl = vdl->next;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::visitFunctionExpressionHelper(QQmlJS::AST::FunctionExpression *fexpr)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
2019-11-11 17:18:04 +00:00
|
|
|
auto name = fexpr->name.toString();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
if (m_currentScope->scopeType() == ScopeType::QMLScope)
|
|
|
|
m_currentScope->addMethod(MetaMethod(name, QLatin1String("void")));
|
|
|
|
else
|
2020-03-30 15:42:48 +00:00
|
|
|
m_currentScope->insertJSIdentifier(name, ScopeType::JSLexicalScope);
|
2019-11-11 17:18:04 +00:00
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, name);
|
|
|
|
} else {
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, QLatin1String("<anon>"));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::FunctionExpression *fexpr)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
visitFunctionExpressionHelper(fexpr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::FunctionExpression *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::FunctionDeclaration *fdecl)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
visitFunctionExpressionHelper(fdecl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::FunctionDeclaration *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::FormalParameterList *fpl)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2020-03-30 15:42:48 +00:00
|
|
|
for (auto const &boundName : fpl->boundNames())
|
|
|
|
m_currentScope->insertJSIdentifier(boundName.id, ScopeType::JSLexicalScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiImport *import)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
// construct path
|
|
|
|
QString prefix = QLatin1String("");
|
|
|
|
if (import->asToken.isValid()) {
|
2019-11-08 17:43:31 +00:00
|
|
|
prefix += import->importId;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
auto dirname = import->fileName.toString();
|
2020-09-24 13:09:38 +00:00
|
|
|
if (!dirname.isEmpty()) {
|
|
|
|
const auto imported = m_importer.importFileOrDirectory(dirname, prefix);
|
|
|
|
m_rootScopeImports.importedQmlNames.insert(imported.importedQmlNames);
|
|
|
|
m_rootScopeImports.cppNames.insert(imported.cppNames);
|
|
|
|
}
|
2019-08-16 15:03:30 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
QString path {};
|
|
|
|
if (!import->importId.isEmpty()) {
|
2019-11-11 17:18:04 +00:00
|
|
|
// TODO: do not put imported ids into the same space as qml IDs
|
2019-11-08 13:48:32 +00:00
|
|
|
const QString importId = import->importId.toString();
|
2020-09-24 13:09:38 +00:00
|
|
|
m_qmlid2scope.insert(importId, m_rootScopeImports.importedQmlNames.value(importId));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2020-06-22 16:38:43 +00:00
|
|
|
auto uri = import->importUri;
|
|
|
|
while (uri) {
|
|
|
|
path.append(uri->name);
|
|
|
|
path.append("/");
|
|
|
|
uri = uri->next;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2020-06-22 16:38:43 +00:00
|
|
|
path.chop(1);
|
|
|
|
|
2020-09-24 13:09:38 +00:00
|
|
|
const auto imported = m_importer.importModule(
|
|
|
|
path, prefix, import->version ? import->version->version : QTypeRevision());
|
|
|
|
|
|
|
|
m_rootScopeImports.importedQmlNames.insert(imported.importedQmlNames);
|
|
|
|
m_rootScopeImports.cppNames.insert(imported.cppNames);
|
2020-09-24 08:45:58 +00:00
|
|
|
|
|
|
|
const QStringList warnings = m_importer.takeWarnings();
|
|
|
|
for (const QString &warning : warnings) {
|
|
|
|
m_colorOut.write(QStringLiteral("warning: "), Warning);
|
|
|
|
m_colorOut.writeUncolored(warning);
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiEnumDeclaration *uied)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
MetaEnum qmlEnum(uied->name.toString());
|
|
|
|
for (const auto *member = uied->members; member; member = member->next)
|
|
|
|
qmlEnum.addKey(member->member.toString());
|
|
|
|
m_currentScope->addEnum(qmlEnum);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiObjectBinding *uiob)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
// property QtObject __styleData: QtObject {...}
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
QString name;
|
|
|
|
for (auto id = uiob->qualifiedTypeNameId; id; id = id->next)
|
2019-06-14 12:21:25 +00:00
|
|
|
name += id->name.toString() + QLatin1Char('.');
|
2020-09-25 14:14:53 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
name.chop(1);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-11-11 16:35:09 +00:00
|
|
|
MetaProperty prop(uiob->qualifiedId->name.toString(), name, false, true, true,
|
|
|
|
name == QLatin1String("alias"), 0);
|
2020-09-24 13:09:38 +00:00
|
|
|
prop.setType(m_rootScopeImports.importedQmlNames.value(uiob->qualifiedTypeNameId->name.toString()));
|
2019-11-11 17:18:04 +00:00
|
|
|
m_currentScope->addProperty(prop);
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
enterEnvironment(ScopeType::QMLScope, name);
|
2020-09-25 14:14:53 +00:00
|
|
|
m_currentScope->resolveTypes(m_rootScopeImports.importedQmlNames);
|
|
|
|
importExportedNames(m_currentScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::UiObjectBinding *uiob)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-11-08 13:48:32 +00:00
|
|
|
const auto childScope = m_currentScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
leaveEnvironment();
|
2019-11-08 13:48:32 +00:00
|
|
|
MetaProperty property(uiob->qualifiedId->name.toString(),
|
|
|
|
uiob->qualifiedTypeNameId->name.toString(),
|
2019-11-11 16:35:09 +00:00
|
|
|
false, true, true,
|
|
|
|
uiob->qualifiedTypeNameId->name == QLatin1String("alias"),
|
|
|
|
0);
|
2019-11-08 13:48:32 +00:00
|
|
|
property.setType(childScope);
|
|
|
|
m_currentScope->addProperty(property);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::UiObjectDefinition *uiod)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
|
2020-09-25 14:14:53 +00:00
|
|
|
QString name;
|
|
|
|
for (auto id = uiod->qualifiedTypeNameId; id; id = id->next)
|
2019-06-14 12:21:25 +00:00
|
|
|
name += id->name.toString() + QLatin1Char('.');
|
2020-09-25 14:14:53 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
name.chop(1);
|
2020-09-25 14:14:53 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
enterEnvironment(ScopeType::QMLScope, name);
|
2020-09-25 14:14:53 +00:00
|
|
|
m_currentScope->resolveTypes(m_rootScopeImports.importedQmlNames);
|
|
|
|
importExportedNames(m_currentScope);
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
if (name.isLower())
|
|
|
|
return false; // Ignore grouped properties for now
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
if (name.endsWith("Connections")) {
|
|
|
|
QString target;
|
|
|
|
auto member = uiod->initializer->members;
|
|
|
|
while (member) {
|
|
|
|
if (member->member->kind == QQmlJS::AST::Node::Kind_UiScriptBinding) {
|
|
|
|
auto asBinding = static_cast<QQmlJS::AST::UiScriptBinding*>(member->member);
|
|
|
|
if (asBinding->qualifiedId->name == QLatin1String("target")) {
|
|
|
|
if (asBinding->statement->kind == QQmlJS::AST::Node::Kind_ExpressionStatement) {
|
|
|
|
auto expr = static_cast<QQmlJS::AST::ExpressionStatement*>(asBinding->statement)->expression;
|
|
|
|
if (auto idexpr = QQmlJS::AST::cast<QQmlJS::AST::IdentifierExpression*>(expr)) {
|
|
|
|
target = idexpr->name.toString();
|
|
|
|
} else {
|
|
|
|
// more complex expressions are not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
member = member->next;
|
|
|
|
}
|
2020-03-31 13:46:20 +00:00
|
|
|
ScopeTree::ConstPtr targetScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
if (target.isEmpty()) {
|
|
|
|
// no target set, connection comes from parentF
|
2020-03-31 13:46:20 +00:00
|
|
|
ScopeTree::Ptr scope = m_currentScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
do {
|
|
|
|
scope = scope->parentScope(); // TODO: rename method
|
|
|
|
} while (scope->scopeType() != ScopeType::QMLScope);
|
2020-09-25 10:39:17 +00:00
|
|
|
targetScope = m_rootScopeImports.importedQmlNames.value(scope->baseTypeName());
|
2019-06-14 12:21:25 +00:00
|
|
|
} else {
|
|
|
|
// there was a target, check if we already can find it
|
2019-11-11 17:18:04 +00:00
|
|
|
auto scopeIt = m_qmlid2scope.find(target);
|
|
|
|
if (scopeIt != m_qmlid2scope.end()) {
|
|
|
|
targetScope = *scopeIt;
|
2019-06-14 12:21:25 +00:00
|
|
|
} else {
|
|
|
|
m_outstandingConnections.push_back({target, m_currentScope, uiod});
|
|
|
|
return false; // visit children later once target is known
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
if (targetScope)
|
|
|
|
m_currentScope->addMethods(targetScope->methods());
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::PatternElement *element)
|
2019-09-17 15:13:18 +00:00
|
|
|
{
|
|
|
|
if (element->isVariableDeclaration()) {
|
|
|
|
QQmlJS::AST::BoundNames names;
|
|
|
|
element->boundNames(&names);
|
2020-03-30 15:42:48 +00:00
|
|
|
for (const auto &name : names) {
|
|
|
|
m_currentScope->insertJSIdentifier(
|
|
|
|
name.id, (element->scope == QQmlJS::AST::VariableScope::Var)
|
|
|
|
? ScopeType::JSFunctionScope
|
|
|
|
: ScopeType::JSLexicalScope);
|
|
|
|
}
|
2019-09-17 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::UiObjectDefinition *)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
2019-11-14 14:59:56 +00:00
|
|
|
auto childScope = m_currentScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
leaveEnvironment();
|
2019-11-14 14:59:56 +00:00
|
|
|
childScope->updateParentProperty(m_currentScope);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-08 13:48:32 +00:00
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::FieldMemberExpression *)
|
2019-11-08 13:48:32 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::FieldMemberExpression *fieldMember)
|
2019-11-08 13:48:32 +00:00
|
|
|
{
|
2019-11-15 15:59:46 +00:00
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
ExpressionNode *base = fieldMember->base;
|
|
|
|
while (auto *nested = cast<NestedExpression *>(base))
|
|
|
|
base = nested->expression;
|
|
|
|
|
|
|
|
if (m_fieldMemberBase == base) {
|
|
|
|
QString type;
|
|
|
|
if (auto *binary = cast<BinaryExpression *>(base)) {
|
|
|
|
if (binary->op == QSOperator::As) {
|
2020-05-18 14:47:34 +00:00
|
|
|
if (auto *right = cast<TypeExpression *>(binary->right))
|
|
|
|
type = right->m_type->toString();
|
2019-11-15 15:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 13:48:32 +00:00
|
|
|
m_currentScope->accessMember(fieldMember->name.toString(),
|
2019-11-15 15:59:46 +00:00
|
|
|
type,
|
2019-11-08 13:48:32 +00:00
|
|
|
fieldMember->identifierToken);
|
|
|
|
m_fieldMemberBase = fieldMember;
|
|
|
|
} else {
|
|
|
|
m_fieldMemberBase = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 15:59:46 +00:00
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
bool FindWarningVisitor::visit(QQmlJS::AST::BinaryExpression *)
|
2019-11-15 15:59:46 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
void FindWarningVisitor::endVisit(QQmlJS::AST::BinaryExpression *binExp)
|
2019-11-15 15:59:46 +00:00
|
|
|
{
|
|
|
|
if (binExp->op == QSOperator::As && m_fieldMemberBase == binExp->left)
|
|
|
|
m_fieldMemberBase = binExp;
|
|
|
|
else
|
|
|
|
m_fieldMemberBase = nullptr;
|
|
|
|
}
|