aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-10-12 11:38:08 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-10-15 09:37:06 +0200
commited9ffa109eb2f242b59443ad430204a90c8028c4 (patch)
tree6bc947fc1b2083bda907be03fac18d97b32a670a
parent9344fcf1a6f4dc6c8176912ac7f2c37e05bf873f (diff)
qmllint: Unify the printing of diagnostics
We can use QQmlJS::DiagnosticMessage to carry message, type, and location. Change-Id: I3868bc8035b4da13efad0d1b7d2f8dfeff1ef234 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/qmlcompiler/qqmljsimporter.cpp48
-rw-r--r--src/qmlcompiler/qqmljsimporter_p.h6
-rw-r--r--src/qmlcompiler/qqmljsimportvisitor.cpp16
-rw-r--r--src/qmlcompiler/qqmljsimportvisitor_p.h5
-rw-r--r--src/qmlcompiler/qqmljstypereader_p.h5
-rw-r--r--tools/qmllint/findwarnings.cpp92
-rw-r--r--tools/qmllint/findwarnings.h1
7 files changed, 116 insertions, 57 deletions
diff --git a/src/qmlcompiler/qqmljsimporter.cpp b/src/qmlcompiler/qqmljsimporter.cpp
index e33538b653..717b559c44 100644
--- a/src/qmlcompiler/qqmljsimporter.cpp
+++ b/src/qmlcompiler/qqmljsimporter.cpp
@@ -61,12 +61,20 @@ void QQmlJSImporter::readQmltypes(
{
const QFileInfo fileInfo(filename);
if (!fileInfo.exists()) {
- m_warnings.append(QLatin1String("QML types file does not exist: ") + filename);
+ m_warnings.append({
+ QStringLiteral("QML types file does not exist: ") + filename,
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
return;
}
if (fileInfo.isDir()) {
- m_warnings.append(QLatin1String("QML types file cannot be a directory: ") + filename);
+ m_warnings.append({
+ QStringLiteral("QML types file cannot be a directory: ") + filename,
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
return;
}
@@ -76,15 +84,19 @@ void QQmlJSImporter::readQmltypes(
QStringList dependencyStrings;
auto succ = reader(objects, &dependencyStrings);
if (!succ)
- m_warnings.append(reader.errorMessage());
+ m_warnings.append({ reader.errorMessage(), QtCriticalMsg, QQmlJS::SourceLocation() });
if (dependencyStrings.isEmpty())
return;
- m_warnings.append(QStringLiteral("Found deprecated dependency specifications in %1."
- "Specify dependencies in qmldir and use qmltyperegistrar to "
- "generate qmltypes files without dependencies.")
- .arg(filename));
+ m_warnings.append({
+ QStringLiteral("Found deprecated dependency specifications in %1."
+ "Specify dependencies in qmldir and use qmltyperegistrar "
+ "to generate qmltypes files without dependencies.")
+ .arg(filename),
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
for (const QString &dependency : qAsConst(dependencyStrings)) {
const auto blank = dependency.indexOf(u' ');
@@ -125,9 +137,13 @@ QQmlJSImporter::Import QQmlJSImporter::readQmldir(const QString &path)
for (auto it = components.begin(), end = components.end(); it != end; ++it) {
const QString filePath = path + QLatin1Char('/') + it->fileName;
if (!QFile::exists(filePath)) {
- m_warnings.append(it->fileName + QLatin1String(" is listed as component in ")
- + path + SlashQmldir
- + QLatin1String(" but does not exist.\n"));
+ m_warnings.append({
+ it->fileName + QStringLiteral(" is listed as component in ")
+ + path + SlashQmldir
+ + QStringLiteral(" but does not exist.\n"),
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
continue;
}
@@ -150,8 +166,12 @@ QQmlJSImporter::Import QQmlJSImporter::readQmldir(const QString &path)
if (typeInfos.isEmpty() && !reader.plugins().isEmpty()) {
const QString defaultTypeInfoPath = path + SlashPluginsDotQmltypes;
if (QFile::exists(defaultTypeInfoPath)) {
- m_warnings.append(QStringLiteral("typeinfo not declared in qmldir file: ")
- + defaultTypeInfoPath);
+ m_warnings.append({
+ QStringLiteral("typeinfo not declared in qmldir file: ")
+ + defaultTypeInfoPath,
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
readQmltypes(defaultTypeInfoPath, &result.objects, &result.dependencies);
}
}
@@ -287,9 +307,7 @@ QQmlJSScope::Ptr QQmlJSImporter::localFile2ScopeTree(const QString &filePath)
QQmlJSScope::Ptr result = typeReader();
m_importedFiles.insert(filePath, result);
- const QStringList errors = typeReader.errors();
- for (const QString &error : errors)
- m_warnings.append(error);
+ m_warnings.append(typeReader.errors());
AvailableTypes types;
types.qmlNames.insert(importDirectory(QFileInfo(filePath).canonicalPath()));
diff --git a/src/qmlcompiler/qqmljsimporter_p.h b/src/qmlcompiler/qqmljsimporter_p.h
index 2f4d51a33c..1acd2a5ed1 100644
--- a/src/qmlcompiler/qqmljsimporter_p.h
+++ b/src/qmlcompiler/qqmljsimporter_p.h
@@ -61,9 +61,9 @@ public:
const QString &module, const QString &prefix = QString(),
QTypeRevision version = QTypeRevision());
- QStringList takeWarnings()
+ QList<QQmlJS::DiagnosticMessage> takeWarnings()
{
- QStringList result = std::move(m_warnings);
+ const auto result = std::move(m_warnings);
m_warnings.clear();
return result;
}
@@ -102,7 +102,7 @@ private:
QStringList m_importPaths;
QHash<QPair<QString, QTypeRevision>, Import> m_seenImports;
QHash<QString, QQmlJSScope::Ptr> m_importedFiles;
- QStringList m_warnings;
+ QList<QQmlJS::DiagnosticMessage> m_warnings;
};
QT_END_NAMESPACE
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp
index 87f9bc086e..0b79339b62 100644
--- a/src/qmlcompiler/qqmljsimportvisitor.cpp
+++ b/src/qmlcompiler/qqmljsimportvisitor.cpp
@@ -138,10 +138,12 @@ bool QQmlJSImportVisitor::visit(UiSourceElement *sourceElement)
// nothing to do
} else {
const auto loc = sourceElement->firstSourceLocation();
- m_errors.append(
- QStringLiteral("unsupportedd sourceElement at ")
- + QString::fromLatin1("%1:%2: ").arg(loc.startLine).arg(loc.startColumn)
- + QString::number(sourceElement->sourceElement->kind));
+ m_errors.append({
+ QStringLiteral("unsupportedd sourceElement %1")
+ .arg(sourceElement->sourceElement->kind),
+ QtWarningMsg,
+ loc
+ });
}
return true;
}
@@ -167,7 +169,11 @@ bool QQmlJSImportVisitor::visit(QQmlJS::AST::UiEnumDeclaration *uied)
void QQmlJSImportVisitor::throwRecursionDepthError()
{
- m_errors.append(QStringLiteral("Maximum statement or expression depth exceeded"));
+ m_errors.append({
+ QStringLiteral("Maximum statement or expression depth exceeded"),
+ QtCriticalMsg,
+ QQmlJS::SourceLocation()
+ });
}
QT_END_NAMESPACE
diff --git a/src/qmlcompiler/qqmljsimportvisitor_p.h b/src/qmlcompiler/qqmljsimportvisitor_p.h
index c8cbd66f14..8f0d937968 100644
--- a/src/qmlcompiler/qqmljsimportvisitor_p.h
+++ b/src/qmlcompiler/qqmljsimportvisitor_p.h
@@ -42,6 +42,7 @@
#include "qqmljsscope_p.h"
#include <private/qqmljsast_p.h>
+#include <private/qqmljsdiagnosticmessage_p.h>
QT_BEGIN_NAMESPACE
@@ -49,7 +50,7 @@ class QQmlJSImportVisitor : public QQmlJS::AST::Visitor
{
public:
QQmlJSScope::Ptr result(const QString &scopeName) const;
- QStringList errors() const { return m_errors; }
+ QList<QQmlJS::DiagnosticMessage> errors() const { return m_errors; }
private:
bool visit(QQmlJS::AST::UiObjectDefinition *) override;
@@ -66,7 +67,7 @@ private:
QQmlJSScope::ConstPtr m_rootObject;
QHash<QString, QQmlJSScope::Ptr> m_objects;
- QStringList m_errors;
+ QList<QQmlJS::DiagnosticMessage> m_errors;
};
QT_END_NAMESPACE
diff --git a/src/qmlcompiler/qqmljstypereader_p.h b/src/qmlcompiler/qqmljstypereader_p.h
index 0ac4571c95..4a6c2c2cd0 100644
--- a/src/qmlcompiler/qqmljstypereader_p.h
+++ b/src/qmlcompiler/qqmljstypereader_p.h
@@ -42,6 +42,7 @@
#include "qqmljsscope_p.h"
#include <QtQml/private/qqmljsastfwd_p.h>
+#include <QtQml/private/qqmljsdiagnosticmessage_p.h>
#include <QtCore/qpair.h>
#include <QtCore/qset.h>
@@ -61,12 +62,12 @@ public:
QQmlJSScope::Ptr operator()();
QList<Import> imports() const { return m_imports; }
- QStringList errors() const { return m_errors; }
+ QList<QQmlJS::DiagnosticMessage> errors() const { return m_errors; }
private:
QString m_file;
QList<Import> m_imports;
- QStringList m_errors;
+ QList<QQmlJS::DiagnosticMessage> m_errors;
};
QT_END_NAMESPACE
diff --git a/tools/qmllint/findwarnings.cpp b/tools/qmllint/findwarnings.cpp
index bcff23eba4..62dce68bb7 100644
--- a/tools/qmllint/findwarnings.cpp
+++ b/tools/qmllint/findwarnings.cpp
@@ -68,10 +68,13 @@ void FindWarningVisitor::importExportedNames(QQmlJSScope::ConstPtr scope)
}
if (m_warnInheritanceCycle) {
- m_colorOut.writePrefixedMessage(
- QString::fromLatin1("%1 is part of an inheritance cycle: %2\n")
- .arg(scope->internalName())
- .arg(inheritenceCycle), Warning);
+ m_errors.append({
+ QStringLiteral("%1 is part of an inheritance cycle: %2\n")
+ .arg(scope->internalName())
+ .arg(inheritenceCycle),
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
}
m_unknownImports.insert(scope->internalName());
@@ -92,10 +95,12 @@ void FindWarningVisitor::importExportedNames(QQmlJSScope::ConstPtr scope)
} else if (auto newScope = scope->baseType()) {
scope = newScope;
} else {
- m_colorOut.writePrefixedMessage(
- scope->baseTypeName()
- + QLatin1String(" was not found. Did you add all import paths?\n"),
- Warning);
+ m_errors.append({
+ scope->baseTypeName() + QStringLiteral(
+ " was not found. Did you add all import paths?\n"),
+ QtWarningMsg,
+ QQmlJS::SourceLocation()
+ });
m_unknownImports.insert(scope->baseTypeName());
m_visitFailed = true;
break;
@@ -118,8 +123,11 @@ void FindWarningVisitor::flushPendingSignalParameters()
void FindWarningVisitor::throwRecursionDepthError()
{
- m_colorOut.writePrefixedMessage(
- QStringLiteral("Maximum statement or expression depth exceeded"), Error);
+ m_errors.append({
+ QStringLiteral("Maximum statement or expression depth exceeded"),
+ QtCriticalMsg,
+ QQmlJS::SourceLocation()
+ });
m_visitFailed = true;
}
@@ -136,10 +144,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiProgram *)
const auto imported = m_importer.importDirectory(QFileInfo(m_filePath).canonicalPath());
m_rootScopeImports.insert(imported);
- const QStringList warnings = m_importer.takeWarnings();
- for (const QString &warning : warnings)
- m_colorOut.writePrefixedMessage(warning, Warning);
-
+ m_errors.append(m_importer.takeWarnings());
return true;
}
@@ -252,12 +257,14 @@ void FindWarningVisitor::endVisit(QQmlJS::AST::Catch *)
bool FindWarningVisitor::visit(QQmlJS::AST::WithStatement *withStatement)
{
if (m_warnWithStatement) {
- m_colorOut.writePrefixedMessage(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),
- Warning);
+ m_errors.append({
+ QStringLiteral(
+ "with statements are strongly discouraged in QML "
+ "and might cause false positives when analysing unqalified "
+ "identifiers\n"),
+ QtWarningMsg,
+ withStatement->firstSourceLocation()
+ });
}
enterEnvironment(QQmlJSScope::JSLexicalScope, "with");
@@ -309,12 +316,12 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiScriptBinding *uisb)
return true;
if (!m_currentScope->methods().contains(signal) && m_warnUnqualified) {
- const auto location = uisb->firstSourceLocation();
- m_colorOut.writePrefixedMessage(QString::fromLatin1(
- "no matching signal found for handler \"%1\" at %2:%3:%4\n")
- .arg(name.toString()).arg(m_filePath).arg(location.startLine)
- .arg(location.startColumn), Warning);
- CheckIdentifiers::printContext(m_code, &m_colorOut, location);
+ m_errors.append({
+ QStringLiteral("no matching signal found for handler \"%1\"\n")
+ .arg(name.toString()),
+ QtWarningMsg,
+ uisb->firstSourceLocation()
+ });
return true;
}
@@ -430,8 +437,36 @@ FindWarningVisitor::FindWarningVisitor(
m_currentScope->insertJSIdentifier(jsGlobVar, globalJavaScript);
}
+static MessageColors messageColor(QtMsgType type)
+{
+ switch (type) {
+ case QtDebugMsg:
+ return Normal;
+ case QtWarningMsg:
+ return Warning;
+ case QtCriticalMsg:
+ case QtFatalMsg:
+ return Error;
+ case QtInfoMsg:
+ return Info;
+ }
+
+ return Normal;
+}
+
bool FindWarningVisitor::check()
{
+ for (const auto &error : qAsConst(m_errors)) {
+ if (error.loc.isValid()) {
+ m_colorOut.writePrefixedMessage(
+ QStringLiteral("%1:%2: %3")
+ .arg(error.loc.startLine).arg(error.loc.startColumn).arg(error.message),
+ messageColor(error.type));
+ } else {
+ m_colorOut.writePrefixedMessage(error.message, messageColor(error.type));
+ }
+ }
+
if (m_visitFailed)
return false;
@@ -561,10 +596,7 @@ bool FindWarningVisitor::visit(QQmlJS::AST::UiImport *import)
m_rootScopeImports.insert(imported);
- const QStringList warnings = m_importer.takeWarnings();
- for (const QString &warning : warnings)
- m_colorOut.writePrefixedMessage(warning, Warning);
-
+ m_errors.append(m_importer.takeWarnings());
return true;
}
diff --git a/tools/qmllint/findwarnings.h b/tools/qmllint/findwarnings.h
index 74d86222b5..7d07114b32 100644
--- a/tools/qmllint/findwarnings.h
+++ b/tools/qmllint/findwarnings.h
@@ -79,6 +79,7 @@ private:
QString m_rootId;
QString m_filePath;
QSet<QString> m_unknownImports;
+ QList<QQmlJS::DiagnosticMessage> m_errors;
ColorOutput m_colorOut;
bool m_visitFailed = false;