Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljsloggingutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
7#include <QtQmlToolingSettings/private/qqmltoolingsettings_p.h>
8#include <QtCore/qcommandlineparser.h>
9
11
12using namespace Qt::StringLiterals;
13
14/*!
15 \class QQmlSA::LoggerWarningId
16 \inmodule QtQmlCompiler
17
18 \brief A wrapper around a string literal to uniquely identify
19 warning categories in the \c{QQmlSA} framework.
20*/
21
22/*!
23 \fn QQmlSA::LoggerWarningId::LoggerWarningId(QAnyStringView name)
24 Constructs a LoggerWarningId object with logging catergory name \a name.
25*/
26
27/*!
28 \fn QAnyStringView QQmlSA::LoggerWarningId::name() const
29 Returns the name of the wrapped warning category.
30*/
31
32namespace QQmlJS {
33
35
42
47
48LoggerCategory::LoggerCategory(LoggerCategory &&) noexcept = default;
49
51{
52 *d_func() = *other.d_func();
53 return *this;
54}
55
57
58LoggerCategory::~LoggerCategory() = default;
59
61{
62 Q_D(const LoggerCategory);
63 return d->name();
64}
65
67{
68 Q_D(const LoggerCategory);
69 return d->settingsName();
70}
71
73{
74 Q_D(const LoggerCategory);
75 return d->description();
76}
77
79{
80 Q_D(const LoggerCategory);
81 return d->severity();
82}
83
85{
86 Q_D(const LoggerCategory);
87 return d->isEssential();
88}
89
91{
92 Q_D(const LoggerCategory);
93 return d->id();
94}
95
101
109
111{
112 if (m_severity == severity)
113 return;
114
116 m_changed = true;
117}
118
124
125namespace LoggingUtils {
126
128{
129 switch (severity) {
131 return QStringLiteral("disable");
133 return QStringLiteral("info");
135 return QStringLiteral("warning");
137 return QStringLiteral("error");
138 default:
139 Q_UNREACHABLE();
140 break;
141 }
142};
143
144static QStringList settingsNamesForCategory(const LoggerCategory &category)
145{
146 const QString name = category.settingsName();
147 QStringList result{ QStringLiteral("Warnings/") + name };
148 const QString sliced = "Warnings/"_L1 + name.sliced(name.indexOf(u'.') + 1);
149 if (sliced != result.last())
150 result.append(sliced);
151 return result;
152}
153
154static QString lookInSettings(const LoggerCategory &category, const QQmlToolingSettings &settings,
155 const QString &settingsName)
156{
157 if (settings.isSet(settingsName))
158 return settings.value(settingsName).toString();
159 static constexpr QLatin1String propertyAliasCyclesKey = "Warnings/PropertyAliasCycles"_L1;
160
161 // keep compatibility to deprecated settings
162 if (category.name() == qmlAliasCycle.name() || category.name() == qmlUnresolvedAlias.name()) {
163 if (settings.isSet(propertyAliasCyclesKey)) {
164 qWarning()
165 << "Detected deprecated setting name \"PropertyAliasCycles\". Use %1 or %2 instead."_L1
166 .arg(qmlAliasCycle.name(), qmlUnresolvedAlias.name());
167 return settings.value(propertyAliasCyclesKey).toString();
168 }
169 }
170 return {};
171}
172
173static QString severityValueForCategory(const LoggerCategory &category,
174 const QQmlToolingSettings &settings,
175 QCommandLineParser *parser)
176{
177 const QString key = category.id().name().toString();
178 if (parser && parser->isSet(key))
179 return parser->value(key);
180
181 const QStringList settingsName = settingsNamesForCategory(category);
182 for (const QString &settingsName : settingsName) {
183 const QString value = lookInSettings(category, settings, settingsName);
184 if (value.isEmpty())
185 continue;
186
187 // Do not try to set the severity if it's due to a default config option.
188 // This way we can tell which options have actually been overwritten by the user.
189 if (severityToString(category.severity()) == value)
190 return QString();
191
192 return value;
193 }
194 return QString();
195}
196
198{
199 if (s == "disable"_L1)
201 if (s == "info"_L1)
203 if (s == "warning"_L1)
205 if (s == "error"_L1)
207 return {};
208}
209
210/*!
211\internal
212Sets the category severity from a settings file and an optional parser.
213Calls \c {parser->showHelp(-1)} for an invalid logging severity.
214*/
215void updateLogSeverities(QList<LoggerCategory> &categories,
216 const QQmlToolingSettings &settings,
217 QCommandLineParser *parser,
218 CategorySelection categorySelection)
219{
220 bool success = true;
221 for (auto &category : categories) {
222 // Disable all non-essential categories, they may still be overriden by settings or command line options
223 if (categorySelection == CategorySelection::Explicit && !category.isEssential())
224 category.setSeverity(QQmlSA::WarningSeverity::Disable);
225
226 const QString value = severityValueForCategory(category, settings, parser);
227 if (value.isEmpty())
228 continue;
229
230 const QString &name = category.id().name().toString();
231 const std::optional<QQmlJS::WarningSeverity> severity = severityFromString(value);
232 if (!severity.has_value()) {
233 qWarning() << "Invalid logging severity" << value << "provided for" << name
234 << "(allowed are: disable, info, warning, error).";
235 success = false;
236 continue;
237 }
238
239 if (category.isEssential() && severity.value() < category.severity()) {
240 qWarning() << "In order to ensure the proper function of qmllint, the severity of the "
241 "essential category %1 cannot be lowered."_L1.arg(name);
242 continue;
243 }
244
245 category.setSeverity(*severity);
246 }
247
248 if (!success && parser)
249 parser->showHelp(-1);
250}
251} // namespace LoggingUtils
252
253} // namespace QQmlJS
254
255QT_END_NAMESPACE
static QString severityValueForCategory(const LoggerCategory &category, const QQmlToolingSettings &settings, QCommandLineParser *parser)
std::optional< QQmlJS::WarningSeverity > severityFromString(const QString &s)
static QString lookInSettings(const LoggerCategory &category, const QQmlToolingSettings &settings, const QString &settingsName)
void updateLogSeverities(QList< LoggerCategory > &categories, const QQmlToolingSettings &settings, QCommandLineParser *parser, CategorySelection categorySelection)
QString severityToString(QQmlJS::WarningSeverity severity)
static QStringList settingsNamesForCategory(const LoggerCategory &category)
QQmlSA::WarningSeverity WarningSeverity
Combined button and popup list for selecting options.