2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2023-08-10 21:36:57 +00:00
|
|
|
#include <QtCore/qcompilerdetection.h>
|
|
|
|
// GCC 11 thinks diagMsg.fixSuggestion.fixes.d.ptr is somehow uninitialized in
|
|
|
|
// QList::emplaceBack(), probably called from QQmlJsLogger::log()
|
|
|
|
// Ditto for GCC 12, but it emits a different warning
|
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_GCC("-Wuninitialized")
|
|
|
|
QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized")
|
|
|
|
#include <QtCore/qlist.h>
|
|
|
|
QT_WARNING_POP
|
|
|
|
|
2023-06-05 08:47:43 +00:00
|
|
|
#include <private/qqmljslogger_p.h>
|
|
|
|
#include <private/qqmlsa_p.h>
|
|
|
|
|
|
|
|
#include <QtQmlCompiler/qqmljsloggingutils.h>
|
|
|
|
|
|
|
|
#include <QtCore/qglobal.h>
|
|
|
|
#include <QtCore/qfile.h>
|
2022-01-12 17:10:41 +00:00
|
|
|
|
|
|
|
|
2021-11-22 12:41:14 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2025-03-20 17:41:31 +00:00
|
|
|
// don't forget to forward-declare your logging category ID in qqmljsloggingutils.h!
|
|
|
|
#define QMLLINT_DEFAULT_CATEGORIES \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlAccessSingleton, "access-singleton-via-object", "AccessSingletonViaObject", \
|
|
|
|
"Warn if a singleton is accessed via an object", QtWarningMsg, false, false) \
|
2025-03-21 11:11:28 +00:00
|
|
|
X(qmlAliasCycle, "alias-cycle", "AliasCycle", "Warn about alias cycles", QtWarningMsg, false, \
|
|
|
|
false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlAssignmentInCondition, "assignment-in-condition", "AssignmentInCondition", \
|
|
|
|
"Warn about using assignment in conditions.", QtWarningMsg, false, false) \
|
|
|
|
X(qmlAttachedPropertyReuse, "attached-property-reuse", "AttachedPropertyReuse", \
|
|
|
|
"Warn if attached types from parent components aren't reused. This is handled by the " \
|
|
|
|
"QtQuick lint plugin. Use Quick.AttachedPropertyReuse instead.", \
|
|
|
|
QtCriticalMsg, true, false) \
|
2025-04-25 10:24:11 +00:00
|
|
|
X(qmlComma, "comma", "Comma", "Warn about using comma expressions.", QtWarningMsg, false, \
|
|
|
|
false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlCompiler, "compiler", "CompilerWarnings", "Warn about compiler issues", QtWarningMsg, \
|
|
|
|
true, false) \
|
2025-05-20 08:25:13 +00:00
|
|
|
X(qmlComponentChildrenCount, "component-children-count", "ComponentChildrenCount", \
|
|
|
|
"Warn about Components that don't have exactly one child", QtWarningMsg, false, false) \
|
2025-04-25 14:21:58 +00:00
|
|
|
X(qmlConfusingExpressionStatement, "confusing-expression-statement", \
|
|
|
|
"ConfusingExpressionStatement", \
|
|
|
|
"Warn about expression statement that has no obvious effect.", QtWarningMsg, false, false) \
|
2025-04-25 12:27:27 +00:00
|
|
|
X(qmlConfusingMinuses, "confusing-minuses", "ConfusingMinuses", \
|
|
|
|
"Warn about confusing minuses.", QtWarningMsg, false, false) \
|
|
|
|
X(qmlConfusingPluses, "confusing-pluses", "ConfusingPluses", \
|
|
|
|
"Warn about confusing pluses.", QtWarningMsg, false, false) \
|
2025-04-07 14:57:53 +00:00
|
|
|
X(qmlContextProperties, "context-properties", "ContextProperties", \
|
|
|
|
"Warn about using context properties.", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlDeferredPropertyId, "deferred-property-id", "DeferredPropertyId", \
|
|
|
|
"Warn about making deferred properties immediate by giving them an id.", QtInfoMsg, true, \
|
|
|
|
true) \
|
|
|
|
X(qmlDeprecated, "deprecated", "Deprecated", "Warn about deprecated properties and types", \
|
2025-03-20 17:41:31 +00:00
|
|
|
QtWarningMsg, false, false) \
|
2025-05-05 09:39:07 +00:00
|
|
|
X(qmlDuplicateEnumEntries, "duplicate-enum-entries", "DuplicateEnumEntries", \
|
|
|
|
"Warn about duplicate enum entries", QtWarningMsg, false, false) \
|
2025-05-05 08:56:40 +00:00
|
|
|
X(qmlDuplicateImport, "duplicate-import", "DuplicateImport", "Warn about duplicate imports", \
|
|
|
|
QtWarningMsg, false, false) \
|
2025-04-29 14:35:10 +00:00
|
|
|
X(qmlDuplicateInlineComponent, "duplicate-inline-component", "DuplicateInlineComponent", \
|
|
|
|
"Warn about duplicate inline components", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlDuplicatePropertyBinding, "duplicate-property-binding", "DuplicatePropertyBinding", \
|
|
|
|
"Warn about duplicate property bindings", QtWarningMsg, false, false) \
|
|
|
|
X(qmlDuplicatedName, "duplicated-name", "DuplicatedName", \
|
|
|
|
"Warn about duplicated property/signal names", QtWarningMsg, false, false) \
|
2025-05-06 13:49:25 +00:00
|
|
|
X(qmlEnumEntryMatchesEnum, "enum-entry-matches-enum", "EnumEntryMatchesEnum", \
|
|
|
|
"Warn about enum entries named the same as the enum itself", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlEval, "eval", "Eval", "Warn about uses of eval()", QtWarningMsg, false, false) \
|
|
|
|
X(qmlFunctionUsedBeforeDeclaration, "function-used-before-declaration", \
|
|
|
|
"FunctionUsedBeforeDeclaration", "Warn if a function is used before declaration", \
|
|
|
|
QtWarningMsg, true, false) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlImport, "import", "ImportFailure", "Warn about failing imports and deprecated qmltypes", \
|
|
|
|
QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlIncompatibleType, "incompatible-type", "IncompatibleType", \
|
|
|
|
"Warn about incompatible types", QtWarningMsg, false, false) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlInheritanceCycle, "inheritance-cycle", "InheritanceCycle", \
|
|
|
|
"Warn about inheritance cycles", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlInvalidLintDirective, "invalid-lint-directive", "InvalidLintDirective", \
|
|
|
|
"Warn if an invalid qmllint comment is found", QtWarningMsg, false, false) \
|
2025-04-25 11:20:52 +00:00
|
|
|
X(qmlLiteralConstructor, "literal-constructor", "LiteralConstructor", \
|
|
|
|
"Warn about using literal constructors, like Boolean or String for example.", QtWarningMsg, \
|
|
|
|
false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlMissingEnumEntry, "missing-enum-entry", "MissingEnumEntry", \
|
|
|
|
"Warn about using missing enum values.", QtWarningMsg, false, false) \
|
|
|
|
X(qmlMissingProperty, "missing-property", "MissingProperty", "Warn about missing properties", \
|
2025-03-20 17:41:31 +00:00
|
|
|
QtWarningMsg, false, false) \
|
|
|
|
X(qmlMissingType, "missing-type", "MissingType", "Warn about missing types", QtWarningMsg, \
|
|
|
|
false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlMultilineStrings, "multiline-strings", "MultilineStrings", \
|
|
|
|
"Warn about multiline strings", QtInfoMsg, false, false) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlNonListProperty, "non-list-property", "NonListProperty", \
|
|
|
|
"Warn about non-list properties", QtWarningMsg, false, false) \
|
2025-05-06 13:50:12 +00:00
|
|
|
X(qmlNonRootEnums, "non-root-enum", "NonRootEnum", \
|
|
|
|
"Warn about enums defined outside the root component", QtWarningMsg, false, false) \
|
2025-05-15 08:46:53 +00:00
|
|
|
X(qmlUnterminatedCase, "unterminated-case", "UnterminatedCase", "Warn about non-empty case " \
|
|
|
|
"blocks that are not terminated by control flow or by a fallthrough comment", \
|
|
|
|
QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlPlugin, "plugin", "LintPluginWarnings", "Warn if a qmllint plugin finds an issue", \
|
|
|
|
QtWarningMsg, true, false) \
|
2025-05-20 11:45:56 +00:00
|
|
|
X(qmlPreferNonVarProperties, "prefer-non-var-properties", "PreferNonVarProperties", \
|
|
|
|
"Warn about var properties that could use a more specific type", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlPrefixedImportType, "prefixed-import-type", "PrefixedImportType", \
|
|
|
|
"Warn about prefixed import types", QtWarningMsg, false, false) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlReadOnlyProperty, "read-only-property", "ReadOnlyProperty", \
|
|
|
|
"Warn about writing to read-only properties", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlRecursionDepthErrors, "recursion-depth-errors", "", "", QtWarningMsg, false, true) \
|
|
|
|
X(qmlRedundantOptionalChaining, "redundant-optional-chaining", "RedundantOptionalChaining", \
|
|
|
|
"Warn about optional chaining on non-voidable and non-nullable base", QtWarningMsg, false, \
|
|
|
|
false) \
|
|
|
|
X(qmlRequired, "required", "RequiredProperty", "Warn about required properties", QtWarningMsg, \
|
|
|
|
false, false) \
|
|
|
|
X(qmlRestrictedType, "restricted-type", "RestrictedType", "Warn about restricted types", \
|
|
|
|
QtWarningMsg, false, false) \
|
|
|
|
X(qmlSignalParameters, "signal-handler-parameters", "BadSignalHandlerParameters", \
|
|
|
|
"Warn about bad signal handler parameters", QtWarningMsg, false, false) \
|
|
|
|
X(qmlStalePropertyRead, "stale-property-read", "StalePropertyRead", \
|
|
|
|
"Warn about bindings reading non-constant and non-notifiable properties", QtWarningMsg, \
|
2025-03-20 17:41:31 +00:00
|
|
|
false, false) \
|
|
|
|
X(qmlSyntax, "syntax", "", "Syntax errors", QtWarningMsg, false, true) \
|
|
|
|
X(qmlSyntaxDuplicateIds, "syntax.duplicate-ids", "", "ID duplication", QtCriticalMsg, false, \
|
|
|
|
true) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlSyntaxIdQuotation, "syntax.id-quotation", "", "ID quotation", QtWarningMsg, false, true) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlTopLevelComponent, "top-level-component", "TopLevelComponent", \
|
2025-03-21 14:09:39 +00:00
|
|
|
"Warn if a top level Component is encountered", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlTranslationFunctionMismatch, "translation-function-mismatch", \
|
|
|
|
"TranslationFunctionMismatch", \
|
|
|
|
"Warn about usages of ID and non-ID translation functions in the same file.", QtWarningMsg, \
|
|
|
|
false, false) \
|
2025-03-20 17:41:31 +00:00
|
|
|
X(qmlUncreatableType, "uncreatable-type", "UncreatableType", \
|
2025-03-20 17:41:31 +00:00
|
|
|
"Warn if uncreatable types are created", QtWarningMsg, false, false) \
|
2025-05-28 13:00:38 +00:00
|
|
|
X(qmlUnintentionalEmptyBlock, "unintentional-empty-block", "UnintentionalEmptyBlock", \
|
|
|
|
"Warn about bindings that contain only an empty block", QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlUnqualified, "unqualified", "UnqualifiedAccess", \
|
|
|
|
"Warn about unqualified identifiers and how to fix them", QtWarningMsg, false, false) \
|
2025-03-26 14:23:42 +00:00
|
|
|
X(qmlUnreachableCode, "unreachable-code", "UnreachableCode", "Warn about unreachable code.", \
|
2025-04-25 16:54:39 +00:00
|
|
|
QtWarningMsg, false, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlUnresolvedAlias, "unresolved-alias", "UnresolvedAlias", "Warn about unresolved aliases", \
|
|
|
|
QtWarningMsg, false, false) \
|
|
|
|
X(qmlUnresolvedType, "unresolved-type", "UnresolvedType", "Warn about unresolved types", \
|
|
|
|
QtWarningMsg, false, false) \
|
|
|
|
X(qmlUnusedImports, "unused-imports", "UnusedImports", "Warn about unused imports", QtInfoMsg, \
|
|
|
|
false, false) \
|
|
|
|
X(qmlUseProperFunction, "use-proper-function", "UseProperFunction", \
|
|
|
|
"Warn if var is used for storing functions", QtWarningMsg, false, false) \
|
|
|
|
X(qmlVarUsedBeforeDeclaration, "var-used-before-declaration", "VarUsedBeforeDeclaration", \
|
|
|
|
"Warn if a variable is used before declaration", QtWarningMsg, false, false) \
|
2025-04-25 11:37:45 +00:00
|
|
|
X(qmlVoid, "void", "Void", "Warn about void expressions.", QtWarningMsg, true, false) \
|
2025-05-06 07:36:39 +00:00
|
|
|
X(qmlWith, "with", "WithStatement", \
|
|
|
|
"Warn about with statements as they can cause false " \
|
|
|
|
"positives when checking for unqualified access", \
|
|
|
|
QtWarningMsg, false, false)
|
2025-03-20 17:41:31 +00:00
|
|
|
|
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) \
|
|
|
|
const QQmlSA::LoggerWarningId category{ name };
|
|
|
|
QMLLINT_DEFAULT_CATEGORIES
|
|
|
|
#undef X
|
2023-05-05 07:30:27 +00:00
|
|
|
|
2025-03-21 16:32:33 +00:00
|
|
|
|
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) ++i;
|
|
|
|
constexpr size_t numCategories = [] { size_t i = 0; QMLLINT_DEFAULT_CATEGORIES return i; }();
|
|
|
|
#undef X
|
|
|
|
|
|
|
|
constexpr bool isUnique(const std::array<std::string_view, numCategories>& fields) {
|
|
|
|
for (std::size_t i = 0; i < fields.size(); ++i) {
|
|
|
|
for (std::size_t j = i + 1; j < fields.size(); ++j) {
|
|
|
|
if (!fields[i].empty() && fields[i] == fields[j]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) std::string_view(name),
|
|
|
|
static_assert(isUnique(std::array{ QMLLINT_DEFAULT_CATEGORIES }), "Duplicate names found!");
|
|
|
|
#undef X
|
|
|
|
|
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) std::string_view(setting),
|
|
|
|
static_assert(isUnique(std::array{ QMLLINT_DEFAULT_CATEGORIES }), "Duplicate settings found!");
|
|
|
|
#undef X
|
|
|
|
|
2025-05-06 07:36:39 +00:00
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) std::string_view(description),
|
2025-03-21 16:32:33 +00:00
|
|
|
static_assert(isUnique(std::array{ QMLLINT_DEFAULT_CATEGORIES }), "Duplicate description found!");
|
|
|
|
#undef X
|
|
|
|
|
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
QQmlJSLogger::QQmlJSLogger()
|
2022-05-30 12:52:03 +00:00
|
|
|
{
|
2023-05-05 07:30:27 +00:00
|
|
|
static const QList<QQmlJS::LoggerCategory> cats = defaultCategories();
|
|
|
|
|
|
|
|
for (const QQmlJS::LoggerCategory &category : cats)
|
|
|
|
registerCategory(category);
|
|
|
|
|
|
|
|
// setup color output
|
|
|
|
m_output.insertMapping(QtCriticalMsg, QColorOutput::RedForeground);
|
|
|
|
m_output.insertMapping(QtWarningMsg, QColorOutput::PurpleForeground); // Yellow?
|
|
|
|
m_output.insertMapping(QtInfoMsg, QColorOutput::BlueForeground);
|
|
|
|
m_output.insertMapping(QtDebugMsg, QColorOutput::GreenForeground); // None?
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<QQmlJS::LoggerCategory> &QQmlJSLogger::defaultCategories()
|
|
|
|
{
|
|
|
|
static const QList<QQmlJS::LoggerCategory> cats = {
|
2025-03-20 17:41:31 +00:00
|
|
|
#define X(category, name, setting, description, level, ignored, isDefault) \
|
|
|
|
QQmlJS::LoggerCategory{ name##_L1, setting##_L1, description##_L1, level, ignored, isDefault },
|
|
|
|
QMLLINT_DEFAULT_CATEGORIES
|
|
|
|
#undef X
|
2021-04-19 14:54:45 +00:00
|
|
|
};
|
2021-03-30 10:58:26 +00:00
|
|
|
|
2022-05-30 12:52:03 +00:00
|
|
|
return cats;
|
2021-03-30 10:58:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
bool QQmlJSFixSuggestion::operator==(const QQmlJSFixSuggestion &other) const
|
2022-05-30 12:52:03 +00:00
|
|
|
{
|
2023-05-05 07:30:27 +00:00
|
|
|
return m_location == other.m_location && m_fixDescription == other.m_fixDescription
|
|
|
|
&& m_replacement == other.m_replacement && m_filename == other.m_filename
|
|
|
|
&& m_hint == other.m_hint && m_autoApplicable == other.m_autoApplicable;
|
2022-05-30 12:52:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
bool QQmlJSFixSuggestion::operator!=(const QQmlJSFixSuggestion &other) const
|
2021-03-25 14:34:42 +00:00
|
|
|
{
|
2023-05-05 07:30:27 +00:00
|
|
|
return !(*this == other);
|
2022-05-30 12:52:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
QList<QQmlJS::LoggerCategory> QQmlJSLogger::categories() const
|
2022-05-30 12:52:03 +00:00
|
|
|
{
|
2023-05-05 07:30:27 +00:00
|
|
|
return m_categories.values();
|
|
|
|
}
|
2022-05-30 12:52:03 +00:00
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
void QQmlJSLogger::registerCategory(const QQmlJS::LoggerCategory &category)
|
|
|
|
{
|
|
|
|
if (m_categories.contains(category.name())) {
|
|
|
|
qWarning() << "Trying to re-register existing logger category" << category.name();
|
|
|
|
return;
|
|
|
|
}
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
m_categoryLevels[category.name()] = category.level();
|
|
|
|
m_categoryIgnored[category.name()] = category.isIgnored();
|
|
|
|
m_categories.insert(category.name(), category);
|
2021-03-25 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 15:30:42 +00:00
|
|
|
static bool isMsgTypeLess(QtMsgType a, QtMsgType b)
|
2021-03-25 14:34:42 +00:00
|
|
|
{
|
2021-07-01 15:30:42 +00:00
|
|
|
static QHash<QtMsgType, int> level = { { QtDebugMsg, 0 },
|
|
|
|
{ QtInfoMsg, 1 },
|
|
|
|
{ QtWarningMsg, 2 },
|
|
|
|
{ QtCriticalMsg, 3 },
|
|
|
|
{ QtFatalMsg, 4 } };
|
|
|
|
return level[a] < level[b];
|
|
|
|
}
|
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
void QQmlJSLogger::log(
|
|
|
|
Message diagMsg, bool showContext, bool showFileName, const QString overrideFileName)
|
2021-07-01 15:30:42 +00:00
|
|
|
{
|
2025-01-20 17:08:23 +00:00
|
|
|
Q_ASSERT(m_categoryLevels.contains(diagMsg.id.toString()));
|
2022-05-30 12:52:03 +00:00
|
|
|
|
2025-01-30 13:25:52 +00:00
|
|
|
if (isCategoryIgnored(diagMsg.id) || isDisabled())
|
2021-03-25 14:34:42 +00:00
|
|
|
return;
|
|
|
|
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
// Note: assume \a type is the type we should prefer for logging
|
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
if (diagMsg.loc.isValid()
|
2025-03-31 14:27:06 +00:00
|
|
|
&& m_ignoredWarnings[diagMsg.lineForDisabling()].contains(diagMsg.id.toString())) {
|
2021-05-27 12:32:43 +00:00
|
|
|
return;
|
2025-01-20 17:08:23 +00:00
|
|
|
}
|
2021-05-27 12:32:43 +00:00
|
|
|
|
2021-03-25 14:34:42 +00:00
|
|
|
QString prefix;
|
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
if ((!overrideFileName.isEmpty() || !m_filePath.isEmpty()) && showFileName) {
|
|
|
|
prefix = (!overrideFileName.isEmpty() ? overrideFileName : m_filePath)
|
|
|
|
+ QStringLiteral(":");
|
|
|
|
}
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
if (diagMsg.loc.isValid())
|
|
|
|
prefix += QStringLiteral("%1:%2: ").arg(diagMsg.loc.startLine).arg(diagMsg.loc.startColumn);
|
2024-09-04 06:40:08 +00:00
|
|
|
else if (!prefix.isEmpty())
|
|
|
|
prefix += QStringLiteral(": "); // produce double colon for Qt Creator's issues pane
|
2021-03-25 14:34:42 +00:00
|
|
|
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
// Note: we do the clamping to [Info, Critical] range since our logger only
|
|
|
|
// supports 3 categories
|
2025-01-20 17:08:23 +00:00
|
|
|
diagMsg.type = std::clamp(diagMsg.type, QtInfoMsg, QtCriticalMsg, isMsgTypeLess);
|
2021-07-01 15:30:42 +00:00
|
|
|
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
// Note: since we clamped our \a type, the output message is not printed
|
|
|
|
// exactly like it was requested, bear with us
|
2025-01-20 17:08:23 +00:00
|
|
|
m_output.writePrefixedMessage(
|
|
|
|
u"%1%2 [%3]"_s.arg(prefix, diagMsg.message, diagMsg.id.toString()), diagMsg.type);
|
|
|
|
|
|
|
|
if (diagMsg.loc.length > 0 && !m_code.isEmpty() && showContext)
|
|
|
|
printContext(overrideFileName, diagMsg.loc);
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
if (diagMsg.fixSuggestion.has_value())
|
|
|
|
printFix(diagMsg.fixSuggestion.value());
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2025-01-20 13:02:11 +00:00
|
|
|
if (m_inTransaction) {
|
|
|
|
m_pendingMessages.push_back(std::move(diagMsg));
|
|
|
|
} else {
|
|
|
|
countMessage(diagMsg);
|
2025-01-20 17:08:23 +00:00
|
|
|
m_currentFunctionMessages.push_back(std::move(diagMsg));
|
2021-03-25 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2025-01-20 13:02:11 +00:00
|
|
|
if (!m_inTransaction)
|
|
|
|
m_output.flushBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QQmlJSLogger::countMessage(const Message &message)
|
|
|
|
{
|
|
|
|
switch (message.type) {
|
|
|
|
case QtWarningMsg:
|
|
|
|
++m_numWarnings;
|
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
|
|
|
++m_numErrors;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-01 11:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 15:30:42 +00:00
|
|
|
void QQmlJSLogger::processMessages(const QList<QQmlJS::DiagnosticMessage> &messages,
|
2024-12-05 09:02:29 +00:00
|
|
|
QQmlJS::LoggerWarningId id,
|
|
|
|
const QQmlJS::SourceLocation &sourceLocation)
|
2021-03-25 14:34:42 +00:00
|
|
|
{
|
2025-01-30 13:25:52 +00:00
|
|
|
if (messages.isEmpty() || isCategoryIgnored(id) || isDisabled())
|
2021-03-30 08:16:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_output.write(QStringLiteral("---\n"));
|
|
|
|
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
// TODO: we should instead respect message's category here (potentially, it
|
|
|
|
// should hold a category instead of type)
|
2021-03-25 14:34:42 +00:00
|
|
|
for (const QQmlJS::DiagnosticMessage &message : messages)
|
2024-12-05 09:02:29 +00:00
|
|
|
log(message.message, id, sourceLocation, false, false);
|
2021-03-30 08:16:03 +00:00
|
|
|
|
|
|
|
m_output.write(QStringLiteral("---\n\n"));
|
2021-03-25 14:34:42 +00:00
|
|
|
}
|
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
void QQmlJSLogger::finalizeFuction()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!m_inTransaction);
|
|
|
|
m_archivedMessages.append(std::exchange(m_currentFunctionMessages, {}));
|
|
|
|
m_hasCompileError = false;
|
|
|
|
}
|
|
|
|
|
2025-01-20 13:02:11 +00:00
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Starts a transaction for a compile pass. This buffers all messages until the
|
|
|
|
transaction completes. If you commit the transaction, the messages are printed
|
|
|
|
and added to the list of committed messages. If you roll it back, the logger
|
|
|
|
reverts to the state before the start of the transaction.
|
|
|
|
|
|
|
|
This is useful for compile passes that potentially have to be repeated, such
|
|
|
|
as the type propagator. We don't want to see the same messages logged multiple
|
|
|
|
times.
|
|
|
|
*/
|
|
|
|
void QQmlJSLogger::startTransaction()
|
|
|
|
{
|
|
|
|
Q_ASSERT(!m_inTransaction);
|
|
|
|
m_inTransaction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Commit the current transaction. Print all pending messages, and add them to
|
|
|
|
the list of committed messages. Then, clear the transaction flag.
|
|
|
|
*/
|
|
|
|
void QQmlJSLogger::commit()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_inTransaction);
|
|
|
|
for (const Message &message : std::as_const(m_pendingMessages))
|
|
|
|
countMessage(message);
|
|
|
|
|
2025-01-20 17:08:23 +00:00
|
|
|
m_currentFunctionMessages.append(std::exchange(m_pendingMessages, {}));
|
|
|
|
m_hasCompileError = m_hasCompileError || std::exchange(m_hasPendingCompileError, false);
|
2025-01-20 13:02:11 +00:00
|
|
|
m_output.flushBuffer();
|
|
|
|
m_inTransaction = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Roll back the current transaction and revert the logger to the state before
|
|
|
|
it was started.
|
|
|
|
*/
|
|
|
|
void QQmlJSLogger::rollback()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_inTransaction);
|
|
|
|
m_pendingMessages.clear();
|
2025-01-20 17:08:23 +00:00
|
|
|
m_hasPendingCompileError = false;
|
2025-01-20 13:02:11 +00:00
|
|
|
m_output.discardBuffer();
|
|
|
|
m_inTransaction = false;
|
|
|
|
}
|
|
|
|
|
2022-02-24 09:28:52 +00:00
|
|
|
void QQmlJSLogger::printContext(const QString &overrideFileName,
|
|
|
|
const QQmlJS::SourceLocation &location)
|
2021-03-25 14:34:42 +00:00
|
|
|
{
|
2022-02-24 09:28:52 +00:00
|
|
|
QString code = m_code;
|
|
|
|
|
2024-10-14 10:37:54 +00:00
|
|
|
if (!overrideFileName.isEmpty() && overrideFileName != m_filePath) {
|
2022-02-24 09:28:52 +00:00
|
|
|
QFile file(overrideFileName);
|
|
|
|
const bool success = file.open(QFile::ReadOnly);
|
|
|
|
Q_ASSERT(success);
|
|
|
|
code = QString::fromUtf8(file.readAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
IssueLocationWithContext issueLocationWithContext { code, location };
|
2021-03-25 14:34:42 +00:00
|
|
|
if (const QStringView beforeText = issueLocationWithContext.beforeText(); !beforeText.isEmpty())
|
|
|
|
m_output.write(beforeText);
|
2021-04-19 14:54:45 +00:00
|
|
|
|
|
|
|
bool locationMultiline = issueLocationWithContext.issueText().contains(QLatin1Char('\n'));
|
|
|
|
|
2021-07-05 10:48:49 +00:00
|
|
|
if (!issueLocationWithContext.issueText().isEmpty())
|
|
|
|
m_output.write(issueLocationWithContext.issueText().toString(), QtCriticalMsg);
|
2021-08-13 07:44:42 +00:00
|
|
|
m_output.write(issueLocationWithContext.afterText().toString() + QLatin1Char('\n'));
|
2021-04-19 14:54:45 +00:00
|
|
|
|
|
|
|
// Do not draw location indicator for multiline locations
|
|
|
|
if (locationMultiline)
|
|
|
|
return;
|
|
|
|
|
2021-03-25 14:34:42 +00:00
|
|
|
int tabCount = issueLocationWithContext.beforeText().count(QLatin1Char('\t'));
|
2021-07-05 10:48:49 +00:00
|
|
|
int locationLength = location.length == 0 ? 1 : location.length;
|
2022-10-05 05:29:16 +00:00
|
|
|
m_output.write(QString::fromLatin1(" ").repeated(issueLocationWithContext.beforeText().size()
|
2021-07-05 10:48:49 +00:00
|
|
|
- tabCount)
|
|
|
|
+ QString::fromLatin1("\t").repeated(tabCount)
|
|
|
|
+ QString::fromLatin1("^").repeated(locationLength) + QLatin1Char('\n'));
|
2021-03-25 14:34:42 +00:00
|
|
|
}
|
2021-07-01 11:55:11 +00:00
|
|
|
|
2023-02-06 11:14:38 +00:00
|
|
|
void QQmlJSLogger::printFix(const QQmlJSFixSuggestion &fixItem)
|
2021-07-01 11:55:11 +00:00
|
|
|
{
|
2024-10-14 10:37:54 +00:00
|
|
|
const QString currentFileAbsPath = m_filePath;
|
2022-02-24 09:28:52 +00:00
|
|
|
QString code = m_code;
|
|
|
|
QString currentFile;
|
2023-02-06 11:14:38 +00:00
|
|
|
m_output.writePrefixedMessage(fixItem.fixDescription(), QtInfoMsg);
|
|
|
|
|
|
|
|
if (!fixItem.location().isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QString filename = fixItem.filename();
|
|
|
|
if (filename == currentFile) {
|
|
|
|
// Nothing to do in this case, we've already read the code
|
|
|
|
} else if (filename.isEmpty() || filename == currentFileAbsPath) {
|
|
|
|
code = m_code;
|
|
|
|
} else {
|
|
|
|
QFile file(filename);
|
|
|
|
const bool success = file.open(QFile::ReadOnly);
|
|
|
|
Q_ASSERT(success);
|
|
|
|
code = QString::fromUtf8(file.readAll());
|
|
|
|
currentFile = filename;
|
2021-07-01 11:55:11 +00:00
|
|
|
}
|
2023-02-06 11:14:38 +00:00
|
|
|
|
|
|
|
IssueLocationWithContext issueLocationWithContext { code, fixItem.location() };
|
|
|
|
|
|
|
|
if (const QStringView beforeText = issueLocationWithContext.beforeText();
|
|
|
|
!beforeText.isEmpty()) {
|
|
|
|
m_output.write(beforeText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The replacement string can be empty if we're only pointing something out to the user
|
|
|
|
const QString replacement = fixItem.replacement();
|
|
|
|
QStringView replacementString = replacement.isEmpty()
|
|
|
|
? issueLocationWithContext.issueText()
|
|
|
|
: replacement;
|
|
|
|
|
|
|
|
// But if there's nothing to change it cannot be auto-applied
|
|
|
|
Q_ASSERT(!replacement.isEmpty() || !fixItem.isAutoApplicable());
|
|
|
|
|
|
|
|
m_output.write(replacementString, QtDebugMsg);
|
|
|
|
m_output.write(issueLocationWithContext.afterText().toString() + u'\n');
|
|
|
|
|
|
|
|
int tabCount = issueLocationWithContext.beforeText().count(u'\t');
|
|
|
|
|
|
|
|
// Do not draw location indicator for multiline replacement strings
|
|
|
|
if (replacementString.contains(u'\n'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_output.write(u" "_s.repeated(
|
|
|
|
issueLocationWithContext.beforeText().size() - tabCount)
|
|
|
|
+ u"\t"_s.repeated(tabCount)
|
|
|
|
+ u"^"_s.repeated(replacement.size()) + u'\n');
|
2021-07-01 11:55:11 +00:00
|
|
|
}
|
2021-11-22 12:41:14 +00:00
|
|
|
|
2023-05-05 07:30:27 +00:00
|
|
|
QQmlJSFixSuggestion::QQmlJSFixSuggestion(const QString &fixDescription,
|
|
|
|
const QQmlJS::SourceLocation &location,
|
|
|
|
const QString &replacement)
|
|
|
|
: m_location{ location }, m_fixDescription{ fixDescription }, m_replacement{ replacement }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:41:14 +00:00
|
|
|
QT_END_NAMESPACE
|