1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#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
#include <private/qqmljslogger_p.h>
#include <private/qqmlsa_p.h>
#include <QtQmlCompiler/qqmljsloggingutils.h>
#include <QtCore/qglobal.h>
#include <QtCore/qfile.h>
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
// don't forget to forward-declare your logging category ID in qqmljsloggingutils.h!
#define QMLLINT_DEFAULT_CATEGORIES \
X(qmlAccessSingleton, "access-singleton-via-object", "AccessSingletonViaObject", \
"Warn if a singleton is accessed via an object", QtWarningMsg, false, false) \
X(qmlAliasCycle, "alias-cycle", "AliasCycle", "Warn about alias cycles", QtWarningMsg, false, \
false) \
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) \
X(qmlComma, "comma", "Comma", "Warn about using comma expressions.", QtWarningMsg, false, \
false) \
X(qmlCompiler, "compiler", "CompilerWarnings", "Warn about compiler issues", QtWarningMsg, \
true, false) \
X(qmlComponentChildrenCount, "component-children-count", "ComponentChildrenCount", \
"Warn about Components that don't have exactly one child", QtWarningMsg, false, false) \
X(qmlConfusingExpressionStatement, "confusing-expression-statement", \
"ConfusingExpressionStatement", \
"Warn about expression statement that has no obvious effect.", QtWarningMsg, false, false) \
X(qmlConfusingMinuses, "confusing-minuses", "ConfusingMinuses", \
"Warn about confusing minuses.", QtWarningMsg, false, false) \
X(qmlConfusingPluses, "confusing-pluses", "ConfusingPluses", \
"Warn about confusing pluses.", QtWarningMsg, false, false) \
X(qmlContextProperties, "context-properties", "ContextProperties", \
"Warn about using context properties.", QtWarningMsg, false, false) \
X(qmlDeferredPropertyId, "deferred-property-id", "DeferredPropertyId", \
"Warn about making deferred properties immediate by giving them an id.", QtInfoMsg, true, \
true) \
X(qmlEqualityTypeCoercion, "equality-type-coercion", "EqualityTypeCoercion", \
"Warn about coercions due to usages of '==' and '!='", QtWarningMsg, false, false) \
X(qmlDeprecated, "deprecated", "Deprecated", "Warn about deprecated properties and types", \
QtWarningMsg, false, false) \
X(qmlDuplicateEnumEntries, "duplicate-enum-entries", "DuplicateEnumEntries", \
"Warn about duplicate enum entries", QtWarningMsg, false, false) \
X(qmlDuplicateImport, "duplicate-import", "DuplicateImport", "Warn about duplicate imports", \
QtWarningMsg, false, false) \
X(qmlDuplicateInlineComponent, "duplicate-inline-component", "DuplicateInlineComponent", \
"Warn about duplicate inline components", QtWarningMsg, false, false) \
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) \
X(qmlEnumEntryMatchesEnum, "enum-entry-matches-enum", "EnumEntryMatchesEnum", \
"Warn about enum entries named the same as the enum itself", QtWarningMsg, false, false) \
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) \
X(qmlImport, "import", "ImportFailure", "Warn about failing imports and deprecated qmltypes", \
QtWarningMsg, false, false) \
X(qmlIncompatibleType, "incompatible-type", "IncompatibleType", \
"Warn about incompatible types", QtWarningMsg, false, false) \
X(qmlInheritanceCycle, "inheritance-cycle", "InheritanceCycle", \
"Warn about inheritance cycles", QtWarningMsg, false, false) \
X(qmlInvalidLintDirective, "invalid-lint-directive", "InvalidLintDirective", \
"Warn if an invalid qmllint comment is found", QtWarningMsg, false, false) \
X(qmlLiteralConstructor, "literal-constructor", "LiteralConstructor", \
"Warn about using literal constructors, like Boolean or String for example.", QtWarningMsg, \
false, false) \
X(qmlMissingEnumEntry, "missing-enum-entry", "MissingEnumEntry", \
"Warn about using missing enum values.", QtWarningMsg, false, false) \
X(qmlMissingProperty, "missing-property", "MissingProperty", "Warn about missing properties", \
QtWarningMsg, false, false) \
X(qmlMissingType, "missing-type", "MissingType", "Warn about missing types", QtWarningMsg, \
false, false) \
X(qmlMultilineStrings, "multiline-strings", "MultilineStrings", \
"Warn about multiline strings", QtInfoMsg, false, false) \
X(qmlNonListProperty, "non-list-property", "NonListProperty", \
"Warn about non-list properties", QtWarningMsg, false, false) \
X(qmlNonRootEnums, "non-root-enum", "NonRootEnum", \
"Warn about enums defined outside the root component", QtWarningMsg, false, false) \
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) \
X(qmlPlugin, "plugin", "LintPluginWarnings", "Warn if a qmllint plugin finds an issue", \
QtWarningMsg, true, false) \
X(qmlPreferNonVarProperties, "prefer-non-var-properties", "PreferNonVarProperties", \
"Warn about var properties that could use a more specific type", QtWarningMsg, false, false) \
X(qmlPrefixedImportType, "prefixed-import-type", "PrefixedImportType", \
"Warn about prefixed import types", QtWarningMsg, false, false) \
X(qmlReadOnlyProperty, "read-only-property", "ReadOnlyProperty", \
"Warn about writing to read-only properties", QtWarningMsg, false, false) \
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, \
false, false) \
X(qmlSyntax, "syntax", "", "Syntax errors", QtWarningMsg, false, true) \
X(qmlSyntaxDuplicateIds, "syntax.duplicate-ids", "", "ID duplication", QtCriticalMsg, false, \
true) \
X(qmlSyntaxIdQuotation, "syntax.id-quotation", "", "ID quotation", QtWarningMsg, false, true) \
X(qmlTopLevelComponent, "top-level-component", "TopLevelComponent", \
"Warn if a top level Component is encountered", QtWarningMsg, false, false) \
X(qmlTranslationFunctionMismatch, "translation-function-mismatch", \
"TranslationFunctionMismatch", \
"Warn about usages of ID and non-ID translation functions in the same file.", QtWarningMsg, \
false, false) \
X(qmlUncreatableType, "uncreatable-type", "UncreatableType", \
"Warn if uncreatable types are created", QtWarningMsg, false, false) \
X(qmlUnintentionalEmptyBlock, "unintentional-empty-block", "UnintentionalEmptyBlock", \
"Warn about bindings that contain only an empty block", QtWarningMsg, false, false) \
X(qmlUnqualified, "unqualified", "UnqualifiedAccess", \
"Warn about unqualified identifiers and how to fix them", QtWarningMsg, false, false) \
X(qmlUnreachableCode, "unreachable-code", "UnreachableCode", "Warn about unreachable code.", \
QtWarningMsg, false, false) \
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) \
X(qmlVoid, "void", "Void", "Warn about void expressions.", QtWarningMsg, true, false) \
X(qmlWith, "with", "WithStatement", \
"Warn about with statements as they can cause false " \
"positives when checking for unqualified access", \
QtWarningMsg, false, false)
#define X(category, name, setting, description, level, ignored, isDefault) \
const QQmlSA::LoggerWarningId category{ name };
QMLLINT_DEFAULT_CATEGORIES
#undef X
#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
#define X(category, name, setting, description, level, ignored, isDefault) std::string_view(description),
static_assert(isUnique(std::array{ QMLLINT_DEFAULT_CATEGORIES }), "Duplicate description found!");
#undef X
QQmlJSLogger::QQmlJSLogger()
{
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 = {
#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
};
return cats;
}
bool QQmlJSFixSuggestion::operator==(const QQmlJSFixSuggestion &other) const
{
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;
}
bool QQmlJSFixSuggestion::operator!=(const QQmlJSFixSuggestion &other) const
{
return !(*this == other);
}
QList<QQmlJS::LoggerCategory> QQmlJSLogger::categories() const
{
return m_categories.values();
}
void QQmlJSLogger::registerCategory(const QQmlJS::LoggerCategory &category)
{
if (m_categories.contains(category.name())) {
qWarning() << "Trying to re-register existing logger category" << category.name();
return;
}
m_categoryLevels[category.name()] = category.level();
m_categoryIgnored[category.name()] = category.isIgnored();
m_categories.insert(category.name(), category);
}
static bool isMsgTypeLess(QtMsgType a, QtMsgType b)
{
static QHash<QtMsgType, int> level = { { QtDebugMsg, 0 },
{ QtInfoMsg, 1 },
{ QtWarningMsg, 2 },
{ QtCriticalMsg, 3 },
{ QtFatalMsg, 4 } };
return level[a] < level[b];
}
void QQmlJSLogger::log(
Message diagMsg, bool showContext, bool showFileName, const QString overrideFileName)
{
Q_ASSERT(m_categoryLevels.contains(diagMsg.id.toString()));
if (isCategoryIgnored(diagMsg.id) || isDisabled())
return;
// Note: assume \a type is the type we should prefer for logging
if (diagMsg.loc.isValid()
&& m_ignoredWarnings[diagMsg.lineForDisabling()].contains(diagMsg.id.toString())) {
return;
}
QString prefix;
if ((!overrideFileName.isEmpty() || !m_filePath.isEmpty()) && showFileName) {
prefix = (!overrideFileName.isEmpty() ? overrideFileName : m_filePath)
+ QStringLiteral(":");
}
if (diagMsg.loc.isValid())
prefix += QStringLiteral("%1:%2: ").arg(diagMsg.loc.startLine).arg(diagMsg.loc.startColumn);
else if (!prefix.isEmpty())
prefix += QStringLiteral(": "); // produce double colon for Qt Creator's issues pane
// Note: we do the clamping to [Info, Critical] range since our logger only
// supports 3 categories
diagMsg.type = std::clamp(diagMsg.type, QtInfoMsg, QtCriticalMsg, isMsgTypeLess);
// Note: since we clamped our \a type, the output message is not printed
// exactly like it was requested, bear with us
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);
if (diagMsg.fixSuggestion.has_value())
printFix(diagMsg.fixSuggestion.value());
if (m_inTransaction) {
m_pendingMessages.push_back(std::move(diagMsg));
} else {
countMessage(diagMsg);
m_currentFunctionMessages.push_back(std::move(diagMsg));
}
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;
}
}
void QQmlJSLogger::processMessages(const QList<QQmlJS::DiagnosticMessage> &messages,
QQmlJS::LoggerWarningId id,
const QQmlJS::SourceLocation &sourceLocation)
{
if (messages.isEmpty() || isCategoryIgnored(id) || isDisabled())
return;
m_output.write(QStringLiteral("---\n"));
// TODO: we should instead respect message's category here (potentially, it
// should hold a category instead of type)
for (const QQmlJS::DiagnosticMessage &message : messages)
log(message.message, id, sourceLocation, false, false);
m_output.write(QStringLiteral("---\n\n"));
}
void QQmlJSLogger::finalizeFuction()
{
Q_ASSERT(!m_inTransaction);
m_archivedMessages.append(std::exchange(m_currentFunctionMessages, {}));
m_hasCompileError = false;
}
/*!
\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);
m_currentFunctionMessages.append(std::exchange(m_pendingMessages, {}));
m_hasCompileError = m_hasCompileError || std::exchange(m_hasPendingCompileError, false);
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();
m_hasPendingCompileError = false;
m_output.discardBuffer();
m_inTransaction = false;
}
void QQmlJSLogger::printContext(const QString &overrideFileName,
const QQmlJS::SourceLocation &location)
{
QString code = m_code;
if (!overrideFileName.isEmpty() && overrideFileName != m_filePath) {
QFile file(overrideFileName);
const bool success = file.open(QFile::ReadOnly);
Q_ASSERT(success);
code = QString::fromUtf8(file.readAll());
}
IssueLocationWithContext issueLocationWithContext { code, location };
if (const QStringView beforeText = issueLocationWithContext.beforeText(); !beforeText.isEmpty())
m_output.write(beforeText);
bool locationMultiline = issueLocationWithContext.issueText().contains(QLatin1Char('\n'));
if (!issueLocationWithContext.issueText().isEmpty())
m_output.write(issueLocationWithContext.issueText().toString(), QtCriticalMsg);
m_output.write(issueLocationWithContext.afterText().toString() + QLatin1Char('\n'));
// Do not draw location indicator for multiline locations
if (locationMultiline)
return;
int tabCount = issueLocationWithContext.beforeText().count(QLatin1Char('\t'));
int locationLength = location.length == 0 ? 1 : location.length;
m_output.write(QString::fromLatin1(" ").repeated(issueLocationWithContext.beforeText().size()
- tabCount)
+ QString::fromLatin1("\t").repeated(tabCount)
+ QString::fromLatin1("^").repeated(locationLength) + QLatin1Char('\n'));
}
void QQmlJSLogger::printFix(const QQmlJSFixSuggestion &fixItem)
{
const QString currentFileAbsPath = m_filePath;
QString code = m_code;
QString currentFile;
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;
}
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');
}
QQmlJSFixSuggestion::QQmlJSFixSuggestion(const QString &fixDescription,
const QQmlJS::SourceLocation &location,
const QString &replacement)
: m_location{ location }, m_fixDescription{ fixDescription }, m_replacement{ replacement }
{
}
QT_END_NAMESPACE
|