Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmlsa.h
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
4#ifndef QQMLSA_H
5#define QQMLSA_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is part of the qmllint plugin API, with limited compatibility guarantees.
12// Usage of this API may make your code source and binary incompatible with
13// future versions of Qt.
14//
15
16#include "qqmlsaconstants.h"
17#include "qqmljsloggingutils.h"
18
19#include <qtqmlcompilerexports.h>
20
21#include <QtCore/QMultiHash>
22#include <QtQmlCompiler/qqmlsasourcelocation.h>
23
24#include <unordered_map>
25
27
28class QQmlJSImportVisitor; // needed for PassManager
29class QQmlJSTypeResolver; // needed for PassManager
30struct QQmlJSTypePropagator; // needed for PassManager
31
32namespace QQmlJS {
33class ConstPtrWrapperIterator; // needed for Element's child scope iterators
34} // namespace QQmlJS
35
36namespace QQmlSA {
37
38class BindingPrivate;
39class BindingsPrivate;
40class Element;
41class ElementPass;
42class FixSuggestion;
43class FixSuggestionPrivate;
44class GenericPassPrivate;
45class MethodPrivate;
46class MethodsPrivate;
47class PassManager;
48class PassManagerPrivate;
49class PropertyPass;
50class PropertyPrivate;
51enum class AccessSemantics;
52struct BindingInfo;
53struct PropertyPassInfo;
54
56
57class Q_QMLCOMPILER_EXPORT Binding
58{
59 Q_DECLARE_PRIVATE(Binding)
60
61public:
62 class Q_QMLCOMPILER_EXPORT Bindings
63 {
64 Q_DECLARE_PRIVATE(Bindings)
65
66 public:
67 Bindings();
68 Bindings(const Bindings &);
70
71 QMultiHash<QString, Binding>::const_iterator begin() const { return constBegin(); }
75
76 private:
77 std::unique_ptr<BindingsPrivate> d_ptr;
78 };
79
80 Binding();
81 Binding(const Binding &);
82 Binding(Binding &&) noexcept;
83 Binding &operator=(const Binding &);
84 Binding &operator=(Binding &&) noexcept;
86
87 Element groupType() const;
88 BindingType bindingType() const;
89 QString stringValue() const;
90 QString propertyName() const;
91 Element attachingType() const;
92 QQmlSA::SourceLocation sourceLocation() const;
93 double numberValue() const;
94 ScriptBindingKind scriptKind() const;
95 bool hasObject() const;
96 Element objectType() const;
97 Element literalType(const QQmlJSTypeResolver *) const;
98 bool hasUndefinedScriptValue() const;
99
100 friend bool operator==(const Binding &lhs, const Binding &rhs)
101 {
102 return operatorEqualsImpl(lhs, rhs);
103 }
104 friend bool operator!=(const Binding &lhs, const Binding &rhs)
105 {
106 return !operatorEqualsImpl(lhs, rhs);
107 }
108
109 static bool isLiteralBinding(BindingType);
110
111private:
112 static bool operatorEqualsImpl(const Binding &, const Binding &);
113
114 std::unique_ptr<BindingPrivate> d_ptr;
115};
116
117class Q_QMLCOMPILER_EXPORT Method
118{
119 Q_DECLARE_PRIVATE(Method)
120
121public:
122 class Q_QMLCOMPILER_EXPORT Methods
123 {
124 Q_DECLARE_PRIVATE(Methods)
125
126 public:
127 Methods();
128 Methods(const Methods &);
130
131 QMultiHash<QString, Method>::const_iterator begin() const { return constBegin(); }
135
136 private:
137 std::unique_ptr<MethodsPrivate> d_ptr;
138 };
139
140 Method();
141 Method(const Method &);
142 Method(Method &&) noexcept;
143 Method &operator=(const Method &);
144 Method &operator=(Method &&) noexcept;
146
147 QString methodName() const;
148 MethodType methodType() const;
149
150 friend bool operator==(const Method &lhs, const Method &rhs)
151 {
152 return operatorEqualsImpl(lhs, rhs);
153 }
154 friend bool operator!=(const Method &lhs, const Method &rhs)
155 {
156 return !operatorEqualsImpl(lhs, rhs);
157 }
158
159private:
160 static bool operatorEqualsImpl(const Method &, const Method &);
161
162 std::unique_ptr<MethodPrivate> d_ptr;
163};
164
165class Q_QMLCOMPILER_EXPORT Property
166{
167 Q_DECLARE_PRIVATE(Property)
168
169public:
170 Property();
171 Property(const Property &);
172 Property(Property &&) noexcept;
173 Property &operator=(const Property &);
174 Property &operator=(Property &&) noexcept;
176
177 QString typeName() const;
178 bool isValid() const;
179
180 friend bool operator==(const Property &lhs, const Property &rhs)
181 {
182 return operatorEqualsImpl(lhs, rhs);
183 }
184
185 friend bool operator!=(const Property &lhs, const Property &rhs)
186 {
187 return !operatorEqualsImpl(lhs, rhs);
188 }
189
190private:
191 static bool operatorEqualsImpl(const Property &, const Property &);
192
193 std::unique_ptr<PropertyPrivate> d_ptr;
194};
195
196class Q_QMLCOMPILER_EXPORT Element
197{
198 friend class QT_PREPEND_NAMESPACE(QQmlJSScope);
199
200public:
201 Element();
202 Element(const Element &);
204 {
205 memcpy(m_data, other.m_data, sizeofElement);
206 memset(other.m_data, 0, sizeofElement);
207 }
208 Element &operator=(const Element &);
209 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Element)
210 ~Element();
211
212 ScopeType scopeType() const;
213 Element baseType() const;
214 QString baseTypeName() const;
215 Element parentScope() const;
216 bool inherits(const Element &) const;
217
218 bool isNull() const;
219 QString internalId() const;
220 AccessSemantics accessSemantics() const;
221 bool isComposite() const;
222
223 bool hasProperty(const QString &propertyName) const;
224 Property property(const QString &propertyName) const;
225 bool isPropertyRequired(const QString &propertyName) const;
226 QString defaultPropertyName() const;
227
228 bool hasMethod(const QString &methodName) const;
229 Method::Methods ownMethods() const;
230
231 QQmlSA::SourceLocation sourceLocation() const;
232 QString filePath() const;
233
234 bool hasPropertyBindings(const QString &name) const;
235 bool hasOwnPropertyBindings(const QString &propertyName) const;
236
237 Binding::Bindings ownPropertyBindings() const;
238 Binding::Bindings ownPropertyBindings(const QString &propertyName) const;
239 QList<Binding> propertyBindings(const QString &propertyName) const;
240
241 QQmlJS::ConstPtrWrapperIterator childScopesBegin() const;
242 QQmlJS::ConstPtrWrapperIterator childScopesEnd() const;
243
244 explicit operator bool() const;
245 bool operator!() const;
246
247 QString name() const;
248
249 friend inline bool operator==(const QQmlSA::Element &lhs, const QQmlSA::Element &rhs)
250 {
251 return operatorEqualsImpl(lhs, rhs);
252 }
253 friend inline bool operator!=(const Element &lhs, const Element &rhs) { return !(lhs == rhs); }
254
255 friend inline qsizetype qHash(const Element &key, qsizetype seed = 0) noexcept
256 {
257 return qHashImpl(key, seed);
258 }
259
260private:
261 static bool operatorEqualsImpl(const Element &, const Element &);
262 static qsizetype qHashImpl(const Element &key, qsizetype seed) noexcept;
263
264 static constexpr qsizetype sizeofElement = 2 * sizeof(QSharedPointer<int>);
265 alignas(QSharedPointer<int>) char m_data[sizeofElement];
266
267 void swap(Element &other) noexcept
268 {
269 char t[sizeofElement];
270 memcpy(t, m_data, sizeofElement);
271 memcpy(m_data, other.m_data, sizeofElement);
272 memcpy(other.m_data, t, sizeofElement);
273 }
274 friend void swap(Element &lhs, Element &rhs) noexcept { lhs.swap(rhs); }
275};
276
277class Q_QMLCOMPILER_EXPORT GenericPass
278{
279 Q_DECLARE_PRIVATE(GenericPass)
280 Q_DISABLE_COPY_MOVE(GenericPass)
281
282public:
284 virtual ~GenericPass();
285
286 void emitWarning(QAnyStringView diagnostic, QQmlJS::LoggerWarningId id);
287 void emitWarning(QAnyStringView diagnostic, QQmlJS::LoggerWarningId id,
288 QQmlSA::SourceLocation srcLocation);
289 void emitWarning(QAnyStringView diagnostic, QQmlJS::LoggerWarningId id,
290 QQmlSA::SourceLocation srcLocation, const QQmlSA::FixSuggestion &fix);
291
292 Element resolveTypeInFileScope(QAnyStringView typeName);
293 Element resolveAttachedInFileScope(QAnyStringView typeName);
294 Element resolveType(QAnyStringView moduleName, QAnyStringView typeName); // #### TODO: revisions
295 Element resolveBuiltinType(QAnyStringView typeName) const;
296 Element resolveAttached(QAnyStringView moduleName, QAnyStringView typeName);
297 Element resolveLiteralType(const Binding &binding);
298
299 Element resolveIdToElement(QAnyStringView id, const Element &context);
300 QString resolveElementToId(const Element &element, const Element &context);
301
303
304private:
305 std::unique_ptr<GenericPassPrivate> d_ptr;
306};
307
308class Q_QMLCOMPILER_EXPORT PassManager
309{
310 friend struct ::QQmlJSTypePropagator;
311 Q_DECLARE_PRIVATE(PassManager)
312
313public:
316
317 void registerElementPass(std::unique_ptr<ElementPass> pass);
318 bool registerPropertyPass(std::shared_ptr<PropertyPass> pass, QAnyStringView moduleName,
320 QAnyStringView propertyName = QAnyStringView(),
321 bool allowInheritance = true);
322 void analyze(const Element &root);
323
324 bool hasImportedModule(QAnyStringView name) const;
325
326 bool isCategoryEnabled(QQmlJS::LoggerWarningId category) const;
327
328 std::vector<std::shared_ptr<ElementPass>> elementPasses() const;
329 std::multimap<QString, PropertyPassInfo> propertyPasses() const;
330 std::unordered_map<quint32, BindingInfo> bindingsByLocation() const;
331
332private:
333 std::unique_ptr<PassManagerPrivate> d_ptr;
334};
335
336class Q_QMLCOMPILER_EXPORT LintPlugin
337{
338public:
339 LintPlugin() = default;
340 virtual ~LintPlugin() = default;
341
342 Q_DISABLE_COPY_MOVE(LintPlugin)
343
344 virtual void registerPasses(PassManager *manager, const Element &rootElement) = 0;
345};
346
347class Q_QMLCOMPILER_EXPORT PropertyPass : public GenericPass
348{
349public:
351
352 virtual void onBinding(const QQmlSA::Element &element, const QString &propertyName,
353 const QQmlSA::Binding &binding, const QQmlSA::Element &bindingScope,
354 const QQmlSA::Element &value);
355 virtual void onRead(const QQmlSA::Element &element, const QString &propertyName,
357 virtual void onWrite(const QQmlSA::Element &element, const QString &propertyName,
358 const QQmlSA::Element &value, const QQmlSA::Element &writeScope,
360};
361
362class Q_QMLCOMPILER_EXPORT ElementPass : public GenericPass
363{
364public:
366
367 virtual bool shouldRun(const Element &element);
368 virtual void run(const Element &element) = 0;
369};
370
371class Q_QMLCOMPILER_EXPORT DebugElementPass : public ElementPass
372{
373 void run(const Element &element) override;
374};
375
376class Q_QMLCOMPILER_EXPORT DebugPropertyPass : public QQmlSA::PropertyPass
377{
378public:
380
381 void onRead(const QQmlSA::Element &element, const QString &propertyName,
382 const QQmlSA::Element &readScope, QQmlSA::SourceLocation location) override;
383 void onBinding(const QQmlSA::Element &element, const QString &propertyName,
384 const QQmlSA::Binding &binding, const QQmlSA::Element &bindingScope,
385 const QQmlSA::Element &value) override;
386 void onWrite(const QQmlSA::Element &element, const QString &propertyName,
387 const QQmlSA::Element &value, const QQmlSA::Element &writeScope,
389};
390
391class Q_QMLCOMPILER_EXPORT FixSuggestion
392{
393 Q_DECLARE_PRIVATE(FixSuggestion)
394
395public:
396 FixSuggestion(const QString &fixDescription, const QQmlSA::SourceLocation &location,
397 const QString &replacement = QString());
399 FixSuggestion(FixSuggestion &&) noexcept;
400 FixSuggestion &operator=(const FixSuggestion &);
401 FixSuggestion &operator=(FixSuggestion &&) noexcept;
403
404 QString fixDescription() const;
406 QString replacement() const;
407
408 void setFileName(const QString &);
409 QString fileName() const;
410
411 void setHint(const QString &);
412 QString hint() const;
413
414 void setAutoApplicable(bool autoApplicable = true);
415 bool isAutoApplicable() const;
416
417 friend bool operator==(const FixSuggestion &lhs, const FixSuggestion &rhs)
418 {
419 return operatorEqualsImpl(lhs, rhs);
420 }
421
422 friend bool operator!=(const FixSuggestion &lhs, const FixSuggestion &rhs)
423 {
424 return !operatorEqualsImpl(lhs, rhs);
425 }
426
427private:
428 static bool operatorEqualsImpl(const FixSuggestion &, const FixSuggestion &);
429
430 std::unique_ptr<FixSuggestionPrivate> d_ptr;
431};
432
433} // namespace QQmlSA
434
435#define QmlLintPluginInterface_iid "org.qt-project.Qt.Qml.SA.LintPlugin/1.0"
436
438
440
441#endif // QQMLSA_H
NSData * m_data
\inmodule QtCore
Definition qlist.h:74
\inmodule QtCore
Definition qhash.h:1748
\inmodule QtQmlCompiler
Definition qqmlsa.h:63
QMultiHash< QString, Binding >::const_iterator begin() const
Definition qqmlsa.h:71
QMultiHash< QString, Binding >::const_iterator end() const
Definition qqmlsa.h:72
\inmodule QtQmlCompiler
Definition qqmlsa.h:58
friend bool operator!=(const Binding &lhs, const Binding &rhs)
Definition qqmlsa.h:104
\inmodule QtQmlCompiler
Definition qqmlsa.h:363
virtual void run(const Element &element)=0
Executes if shouldRun() returns true.
ElementPass(PassManager *manager)
Definition qqmlsa.h:365
\inmodule QtQmlCompiler
Definition qqmlsa.h:197
friend void swap(Element &lhs, Element &rhs) noexcept
Definition qqmlsa.h:274
friend bool operator!=(const Element &lhs, const Element &rhs)
Definition qqmlsa.h:253
Element(Element &&other) noexcept
Definition qqmlsa.h:203
friend qsizetype qHash(const Element &key, qsizetype seed=0) noexcept
Definition qqmlsa.h:255
friend bool operator==(const QQmlSA::Element &lhs, const QQmlSA::Element &rhs)
Definition qqmlsa.h:249
\inmodule QtQmlCompiler
Definition qqmlsa.h:392
friend bool operator==(const FixSuggestion &lhs, const FixSuggestion &rhs)
Definition qqmlsa.h:417
friend bool operator!=(const FixSuggestion &lhs, const FixSuggestion &rhs)
Definition qqmlsa.h:422
\inmodule QtQmlCompiler
Definition qqmlsa.h:278
virtual ~GenericPass()
\inmodule QtQmlCompiler
Definition qqmlsa.h:337
LintPlugin()=default
virtual ~LintPlugin()=default
\inmodule QtQmlCompiler
Definition qqmlsa.h:123
QMultiHash< QString, Method >::const_iterator end() const
Definition qqmlsa.h:132
QMultiHash< QString, Method >::const_iterator begin() const
Definition qqmlsa.h:131
\inmodule QtQmlCompiler
Definition qqmlsa.h:118
friend bool operator!=(const Method &lhs, const Method &rhs)
Definition qqmlsa.h:154
\inmodule QtQmlCompiler
Definition qqmlsa.h:309
\inmodule QtQmlCompiler
Definition qqmlsa.h:348
\inmodule QtQmlCompiler
Definition qqmlsa.h:166
friend bool operator==(const Property &lhs, const Property &rhs)
Definition qqmlsa.h:180
friend bool operator!=(const Property &lhs, const Property &rhs)
Definition qqmlsa.h:185
\inmodule QtQmlCompiler
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
const QLoggingCategory & category()
[1]
\inmodule QtQmlCompiler
MethodType
Definition qqmlsa.h:55
AccessSemantics
Definition qqmlsa_p.h:40
Combined button and popup list for selecting options.
static void * context
static QString methodName(const QDBusIntrospection::Method &method)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
const char * typeName
#define Q_DECLARE_INTERFACE(IFace, IId)
Definition qobject.h:420
GLint location
GLuint64 key
GLuint name
GLdouble GLdouble t
Definition qopenglext.h:243
static bool isComposite(const QQmlJSScope::ConstPtr &scope)
#define QmlLintPluginInterface_iid
Definition qqmlsa.h:435
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
ptrdiff_t qsizetype
Definition qtypes.h:70
const char property[13]
Definition qwizard.cpp:101
file setFileName("readme.txt")
timer inherits("QTimer")
QSharedPointer< T > other(t)
[5]
this swap(other)
QNetworkAccessManager manager