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
qqmltcvisitor.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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 <private/qqmltcvisitor_p.h>
8
9#include <QtCore/qfileinfo.h>
10#include <QtCore/qstack.h>
11#include <QtCore/qdir.h>
12#include <QtCore/qloggingcategory.h>
13#include <QtQml/private/qqmlsignalnames_p.h>
14
15#include <private/qqmljsutils_p.h>
16
17#include <algorithm>
18#include <array>
19
20QT_BEGIN_NAMESPACE
21
22using namespace Qt::StringLiterals;
23
24Q_LOGGING_CATEGORY(lcQmltcCompiler, "qml.qmltc.compiler", QtWarningMsg);
25
26namespace QQmltc {
27
29{
30 const auto Error = std::make_optional(QQmlJS::WarningSeverity::Error);
31
32 // We want qmltc to be stricter than qmllint. Override certain categories.
33 // Kept in sync via tst_qmltc::ensureWarningCategoryOverridesSync
34 static const std::array overrides{
35 QmltcLoggingCategoryOverride{ qmlAccessSingleton, std::nullopt },
36 QmltcLoggingCategoryOverride{ qmlAliasCycle, Error },
37 QmltcLoggingCategoryOverride{ qmlAssignmentInCondition, std::nullopt },
38 QmltcLoggingCategoryOverride{ qmlAttachedPropertyReuse, std::nullopt },
39 QmltcLoggingCategoryOverride{ qmlBlockScopeVarDeclaration, std::nullopt },
40 QmltcLoggingCategoryOverride{ qmlComma, std::nullopt },
41 QmltcLoggingCategoryOverride{ qmlCompiler, Error },
42 QmltcLoggingCategoryOverride{ qmlComponentChildrenCount, Error },
43 QmltcLoggingCategoryOverride{ qmlConfusingExpressionStatement, std::nullopt },
44 QmltcLoggingCategoryOverride{ qmlConfusingMinuses, std::nullopt },
45 QmltcLoggingCategoryOverride{ qmlConfusingPluses, std::nullopt },
46 QmltcLoggingCategoryOverride{ qmlContextProperties, std::nullopt },
47 QmltcLoggingCategoryOverride{ qmlDeferredPropertyId, std::nullopt },
48 QmltcLoggingCategoryOverride{ qmlEnumsAreNotTypes, Error },
49 QmltcLoggingCategoryOverride{ qmlEqualityTypeCoercion, std::nullopt },
50 QmltcLoggingCategoryOverride{ qmlDeprecated, std::nullopt },
51 QmltcLoggingCategoryOverride{ qmlDuplicateEnumEntries, std::nullopt },
52 QmltcLoggingCategoryOverride{ qmlDuplicateImport, std::nullopt },
53 QmltcLoggingCategoryOverride{ qmlDuplicateInlineComponent, Error },
54 QmltcLoggingCategoryOverride{ qmlDuplicatePropertyBinding, Error },
55 QmltcLoggingCategoryOverride{ qmlDuplicatedName, Error },
56 QmltcLoggingCategoryOverride{ qmlEnumEntryMatchesEnum, std::nullopt },
57 QmltcLoggingCategoryOverride{ qmlEnumKeyCase, std::nullopt },
58 QmltcLoggingCategoryOverride{ qmlEval, std::nullopt },
59 QmltcLoggingCategoryOverride{ qmlFunctionUsedBeforeDeclaration, std::nullopt },
60 QmltcLoggingCategoryOverride{ qmlIdShadowsMember, std::nullopt },
61 QmltcLoggingCategoryOverride{ qmlImport, Error },
62 QmltcLoggingCategoryOverride{ qmlImportFileSelector, std::nullopt },
63 QmltcLoggingCategoryOverride{ qmlIncompatibleType, Error },
64 QmltcLoggingCategoryOverride{ qmlInheritanceCycle, Error },
65 QmltcLoggingCategoryOverride{ qmlInlineComponentEnums, std::nullopt },
66 QmltcLoggingCategoryOverride{ qmlInvalidLintDirective, std::nullopt },
67 QmltcLoggingCategoryOverride{ qmlLiteralConstructor, Error },
68 QmltcLoggingCategoryOverride{ qmlMaxWarningsExceeded, std::nullopt },
69 QmltcLoggingCategoryOverride{ qmlMissingEnumEntry, std::nullopt },
70 QmltcLoggingCategoryOverride{ qmlMissingProperty, Error },
71 QmltcLoggingCategoryOverride{ qmlMissingType, Error },
72 QmltcLoggingCategoryOverride{ qmlMultilineStrings, std::nullopt },
73 QmltcLoggingCategoryOverride{ qmlNonListProperty, Error },
74 QmltcLoggingCategoryOverride{ qmlNonRootEnums, std::nullopt },
75 QmltcLoggingCategoryOverride{ qmlPropertyOverride, std::nullopt },
76 QmltcLoggingCategoryOverride{ qmlUnterminatedCase, std::nullopt },
77 QmltcLoggingCategoryOverride{ qmlPreferNonVarProperties, std::nullopt },
78 QmltcLoggingCategoryOverride{ qmlPrefixedImportType, std::nullopt },
79 QmltcLoggingCategoryOverride{ qmlReadOnlyProperty, Error },
80 QmltcLoggingCategoryOverride{ qmlRecursionDepthErrors, Error },
81 QmltcLoggingCategoryOverride{ qmlRedundantOptionalChaining, std::nullopt },
82 QmltcLoggingCategoryOverride{ qmlRenamedType, std::nullopt },
83 QmltcLoggingCategoryOverride{ qmlRequired, Error },
84 QmltcLoggingCategoryOverride{ qmlShadow, std::nullopt },
85 QmltcLoggingCategoryOverride{ qmlShadowableMember, std::nullopt },
86 QmltcLoggingCategoryOverride{ qmlSignalHandlerNameClash, Error },
87 QmltcLoggingCategoryOverride{ qmlSignalParameters, Error },
88 QmltcLoggingCategoryOverride{ qmlStalePropertyRead, std::nullopt },
89 QmltcLoggingCategoryOverride{ qmlSyntax, std::nullopt },
90 QmltcLoggingCategoryOverride{ qmlSyntaxDuplicateIds, Error },
91 QmltcLoggingCategoryOverride{ qmlSyntaxIdQuotation, std::nullopt },
92 QmltcLoggingCategoryOverride{ qmlTypeInstantiatedRecursively, Error },
93 QmltcLoggingCategoryOverride{ qmlTopLevelComponent, Error },
94 QmltcLoggingCategoryOverride{ qmlUncreatableType, Error },
95 QmltcLoggingCategoryOverride{ qmlUnintentionalEmptyBlock, std::nullopt },
96 QmltcLoggingCategoryOverride{ qmlUnqualified, std::nullopt },
97 QmltcLoggingCategoryOverride{ qmlUnreachableCode, std::nullopt },
98 QmltcLoggingCategoryOverride{ qmlUnresolvedAlias, Error },
99 QmltcLoggingCategoryOverride{ qmlUnresolvedType, Error },
100 QmltcLoggingCategoryOverride{ qmlUnusedImports, std::nullopt },
101 QmltcLoggingCategoryOverride{ qmlUseProperFunction, std::nullopt },
102 QmltcLoggingCategoryOverride{ qmlVarUsedBeforeDeclaration, std::nullopt },
103 QmltcLoggingCategoryOverride{ qmlVoid, std::nullopt },
104 QmltcLoggingCategoryOverride{ qmlWith, std::nullopt },
105 };
106
107 return overrides;
108}
109
110static QString uniqueNameFromPieces(const QStringList &pieces, QHash<QString, int> &repetitions)
111{
112 QString possibleName = pieces.join(u'_');
113 const int count = repetitions[possibleName]++;
114 if (count > 0)
115 possibleName.append(u"_" + QString::number(count));
116 return possibleName;
117}
118
119static bool isExplicitComponent(const QQmlJSScope::ConstPtr &type)
120{
121 if (!type->isComposite())
122 return false;
123 auto base = type->baseType();
124 return base && base->internalName() == u"QQmlComponent";
125}
126
127/*! \internal
128 Returns if type is an implicit component.
129 This method should only be called after implicit components
130 are detected, that is, after QQmlJSImportVisitor::endVisit(UiProgram *)
131 was called.
132 */
133static bool isImplicitComponent(const QQmlJSScope::ConstPtr &type)
134{
135 // root components and inline components are explicitly components.
136 if (!type->isComposite() || type->isFileRootComponent() || type->isInlineComponent())
137 return false;
138
139 const auto cppBase = QQmlJSScope::nonCompositeBaseType(type);
140 const bool isComponentBased = (cppBase && cppBase->internalName() == u"QQmlComponent");
141 return type->componentRootStatus() != QQmlJSScope::IsComponentRoot::No && !isComponentBased;
142}
143
144/*! \internal
145 Checks if type is inside or a (implicit or explicit) component.
146 This method should only be called after implicit components
147 are detected, that is, after QQmlJSImportVisitor::endVisit(UiProgram *)
148 was called.
149 */
151{
152 Q_ASSERT(type->isComposite()); // we're dealing with composite types here
153 for (; type; type = type->parentScope()) {
155 return true;
156 }
157 }
158 return false;
159}
160
167
169{
170 // TODO: this pass is slow: we have to do exhaustive search because some C++
171 // code could do forward declarations and they are hard to handle correctly
172 QSet<const QQmlJSScope *> visitedTypes; // we can still improve by walking all types only once
173 const auto visitType = [&visitedTypes](const QQmlJSScope::ConstPtr &type) -> bool {
175 return true;
177 return false;
178 };
179 const auto addCppInclude = [this](const QQmlJSScope::ConstPtr &type) {
182 };
183
184 const auto findInType = [&](const QQmlJSScope::ConstPtr &type) {
185 if (!type)
186 return;
187 if (visitType(type)) // optimization - don't call nonCompositeBaseType() needlessly
188 return;
189
190 // look in type
192
193 if (type->isListProperty())
195
196 // look in type's base type
197 auto base = type->baseType();
198 if (!base && type->isComposite())
199 // in this case, qqmljsimportvisitor would have already print an error message
200 // about the missing type, so just return silently without crashing
201 return;
202 if (!base || visitType(base))
203 return;
205 };
206
209 return QString();
210 Q_ASSERT(publicInclude.endsWith(u".h"_s) || publicInclude.endsWith(u".hpp"_s));
214 // check if "public" include is in fact already private
215 if (publicInclude.startsWith(u"private"))
216 return includeWithoutExtension + u"_p" + extension;
217 return u"private/" + includeWithoutExtension + u"_p" + extension;
218 };
219
220 // walk the whole type hierarchy
223 while (!types.isEmpty()) {
224 auto type = types.pop();
225 Q_ASSERT(type);
226
227 const auto scopeType = type->scopeType();
231 continue;
232 }
233
234 for (auto t = type; !type->isArrayScope() && t; t = t->baseType()) {
235 findInType(t);
236
237 // look in properties
238 const auto properties = t->ownProperties();
239 for (const QQmlJSMetaProperty &p : properties) {
240 findInType(p.type());
241
242 if (p.isPrivate()) {
245 if (!privateInclude.isEmpty())
247 }
248 }
249
250 // look in methods
251 const auto methods = t->ownMethods();
252 for (const QQmlJSMetaMethod &m : methods) {
254
255 const auto parameters = m.parameters();
256 for (const auto &param : parameters)
258 }
259 }
260
262 }
263
264 // remove own include
266}
267
268static void addCleanQmlTypeName(QStringList *names, const QQmlJSScope::ConstPtr &scope)
269{
270 Q_ASSERT(scope->scopeType() == QQmlSA::ScopeType::QMLScope);
271 Q_ASSERT(!scope->isArrayScope());
272 Q_ASSERT(!scope->baseTypeName().isEmpty());
273 // the scope is guaranteed to be a new QML type, so any prefixes (separated
274 // by dots) should be import namespaces
275 const std::optional<QString> &inlineComponentName = scope->inlineComponentName();
276 QString name = inlineComponentName ? *inlineComponentName : scope->baseTypeName();
277 names->append(name.replace(u'.', u'_'));
278}
279
281{
283 return false;
284
285 // we're not interested in non-QML scopes
287 return true;
288
292 }
293
294 if (m_currentScope != m_exportedRootScope) // not document root
296 // give C++-relevant internal names to QMLScopes, we can use them later in compiler
299
300 return true;
301}
302
309
311{
313 return false;
314
315 if (m_currentScope != m_exportedRootScope) // not document root
317 // give C++-relevant internal names to QMLScopes, we can use them later in compiler
319
321 return true;
322}
323
329
331{
333 return false;
334
335 // augment property: set its write/read/etc. methods
337 const auto name = publicMember->name.toString();
338
343 if (!property.isAlias()) { // aliases are covered separately
345 if (property.read().isEmpty())
347 if (!property.isList()) {
350 // Note: prefer BINDABLE to NOTIFY
351 if (property.bindable().isEmpty())
353 }
355 }
356
358 // also check that notify is already a method of the scope
359 {
362 if (methods.size() != 1) {
363 const QString errorString =
364 methods.isEmpty() ? u"no signal"_s : u"too many signals"_s;
365 m_logger->log(
366 u"internal error: %1 found for property '%2'"_s.arg(errorString, name),
368 return false;
369 } else if (methods[0].methodType() != QQmlJSMetaMethodType::Signal) {
370 m_logger->log(u"internal error: method %1 of property %2 must be a signal"_s.arg(
373 return false;
374 }
375 }
376 }
377
378 return true;
379}
380
382{
384 return false;
385
386 {
387 const auto id = scriptBinding->qualifiedId;
388 if (!id->next && id->name == QLatin1String("id"))
389 m_typesWithId[m_currentScope] = -1; // temporary value
390 }
391
392 return true;
393}
394
396{
398 return false;
399 return true;
400}
401
403{
405 if (!rootScopeIsValid()) // in case we failed badly
406 return;
407
409
410 // Yes, we want absolutely all bindings in the document.
411 // Not only the ones immediately assigned to QML types.
414
416 setupAliases();
417
418 if (m_mode != Mode::Compile)
419 return;
420
422
424 for (const QQmlJSScope::ConstPtr &type : qmlTypes)
426}
427
429{
431 m_seenCustomParsers = true;
432 return false;
433}
434
440
441QQmlJSScope::ConstPtr fetchType(const QQmlJSMetaPropertyBinding &binding)
442{
443 switch (binding.bindingType()) {
444 case QQmlSA::BindingType::Object:
445 return binding.objectType();
446 case QQmlSA::BindingType::Interceptor:
447 return binding.interceptorType();
448 case QQmlSA::BindingType::ValueSource:
449 return binding.valueSourceType();
450 case QQmlSA::BindingType::AttachedProperty:
451 return binding.attachedType();
452 case QQmlSA::BindingType::GroupedProperty:
453 return binding.groupType();
454 default:
455 return {};
456 }
457 Q_UNREACHABLE_RETURN({});
458}
459
460template<typename TypePredicate, typename BindingPredicate>
462 const QQmlJSScope::ConstPtr &root,
465{
466 // NB: depth-first-search is used here to mimic various QmlIR passes
467 QStack<QQmlJSScope::ConstPtr> types;
468 types.push(root);
469 while (!types.isEmpty()) {
470 auto current = types.pop();
471
472 if (typePredicate(current))
473 continue;
474
475 Q_ASSERT(qmlIrOrderedBindings.contains(current));
476 const auto &bindings = qmlIrOrderedBindings[current];
477 // reverse the binding order here, because stack processes right-most
478 // child first and we need left-most first
479 for (auto it = bindings.rbegin(); it != bindings.rend(); ++it) {
480 const auto &binding = *it;
481
482 if (bindingPredicate(current, binding))
483 continue;
484
485 if (auto type = fetchType(binding))
486 types.push(type);
487 }
488 }
489}
490
491template<typename Predicate>
493 const QQmlJSScope::ConstPtr &root,
496{
497 iterateBindings(root, qmlIrOrderedBindings, [predicate](const QQmlJSScope::ConstPtr &current) {
498 return predicate(current) || isOrUnderComponent(current);
499 }, [](const QQmlJSScope::ConstPtr &, const QQmlJSMetaPropertyBinding &) {
500 return false;
501 });
502}
503
504/*! \internal
505 This is a special function that must be called after
506 QQmlJSImportVisitor::endVisit(QQmlJS::AST::UiProgram *). It is used to
507 resolve things that couldn't be resolved during the AST traversal, such
508 as anything that is dependent on implicit or explicit components
509*/
512{
513
514 // add the document root (that is not an inline component), as we usually
515 // want to iterate on all inline components, followed by the document root
518
519 // match scopes to indices of QmlIR::Object from QmlIR::Document
520 qsizetype count = 0;
521 const auto setIndex = [&](const QQmlJSScope::Ptr &current) {
523 return;
526 ++count;
527 };
529
530 // find types that are part of the deferred bindings (we care about *types*
531 // exclusively here)
533 const auto findDeferred = [&](const QQmlJSScope::ConstPtr &type,
539
542 return true;
543 }
544 }
545 return false;
546 };
550 }
551
553 for (; type; type = type->parentScope()) {
555 return true;
556 }
557 return false;
558 };
559
560 // find all "pure" QML types
562 for (qsizetype i = 0; i < m_qmlTypes.size(); ++i) {
564
566 // root is special: we compile Component roots. root is also never
567 // deferred, so in case `isOrUnderDeferred(type)` returns true, we
568 // always continue here
569 if (type != m_exportedRootScope) {
570 // if a type is an explicit component, its id "leaks" into the
571 // document context
574 continue;
575 }
576 }
577
583 }
584
585 // update the typeCounts
586 for (const auto &inlineComponent : std::as_const(m_inlineComponentNames)) {
588 }
589
590 // add explicit components to the object creation indices
591 {
599 }
600 }
601
602 // m_qmlTypesWithQmlBases should contain the types to be compiled.
603 // Some types should not be compiled and are therefore filtered out:
604 // * deferred types
605 // * types inside of capital-c-Components (implicit and explicit)
606 // * non-composite types (that is, types not defined in qml)
607 //
608 // This can not be done earlier as implicitly wrapped Components are
609 // only known after visitation is over!
610
611 // filter step:
612 for (const auto &inlineComponent : std::as_const(m_inlineComponentNames)) {
619 [&](const QQmlJSScope::ConstPtr &type) {
620 auto base = type->baseType();
623 });
625 }
626
627 // count QmlIR::Objects in the document - the amount is used to calculate
628 // object indices of implicit components
630 const auto countQmlScopes = [&](const QQmlJSScope::ConstPtr &scope) {
631 if (scope->isArrayScope()) // special kind of QQmlJSScope::QMLScope
632 return;
633 switch (scope->scopeType()) {
638 break;
639 }
640 default:
641 return;
642 }
643 return;
644 };
645 // order doesn't matter (so re-use QQmlJSUtils)
647
648 // figure synthetic indices of QQmlComponent-wrapped types
650 const auto addSyntheticIndex = [&](const QQmlJSScope::ConstPtr &type) {
651 // explicit component
654 return true;
655 }
656 // implicit component
658 const int index =
661 return true;
662 }
663 return false;
664 };
665
667 syntheticCreationIndex = 0; // reset for each inline component
670 }
671
672 // figure runtime object ids for non-component wrapped types
673 int currentId;
674 const auto setRuntimeId = [&](const QQmlJSScope::ConstPtr &type) {
675 // any type wrapped in an implicit component shouldn't be processed
676 // here. even if it has id, it doesn't need to be set by qmltc
677 if (type->isInlineComponent()) {
678 // explicit inline component
679 } else if (type->isFileRootComponent()) {
680 // explicit root component
682 // Wrapped in implicit component, assigned to unknown property, or child of scope
683 // called "QQmlComponent". We consider this an "implicit component".
684 return true;
685 }
686
689 }
690
691 return false;
692 };
693
695 currentId = 0; // reset for each inline component
697 }
698}
699
700static void setAliasData(QQmlJSMetaProperty *alias, const QQmlJSUtils::ResolvedAlias &origin)
701{
702 Q_ASSERT(origin.kind != QQmlJSUtils::AliasTarget_Invalid);
703 PropertyData compiledData(*alias);
704 if (alias->read().isEmpty())
705 alias->setRead(compiledData.read);
706 if (origin.kind == QQmlJSUtils::AliasTarget_Object) // id-pointing aliases only have READ method
707 return;
708 if (origin.property.isWritable() && alias->write().isEmpty())
709 alias->setWrite(compiledData.write);
710
711 // the engine always compiles a notify for properties/aliases defined in qml code
712 // Yes, this generated notify will never be emitted.
713 if (alias->notify().isEmpty())
714 alias->setNotify(compiledData.notify);
715
716 if (!origin.property.bindable().isEmpty() && alias->bindable().isEmpty())
717 alias->setBindable(compiledData.bindable);
718}
719
720void Visitor::setupAliases()
721{
724
725 while (!types.isEmpty()) {
728
730 if (!p.isAlias())
731 continue;
732
736 m_logger->log(QStringLiteral("Cannot resolve alias \"%1\"").arg(p.propertyName()),
738 continue;
739 }
742 }
743 }
744}
745
747{
748 static const QString cppKeywords[] = {
749 u"alignas"_s,
750 u"alignof"_s,
751 u"and"_s,
752 u"and_eq"_s,
753 u"asm"_s,
754 u"atomic_cancel"_s,
755 u"atomic_commit"_s,
756 u"atomic_noexcept"_s,
757 u"auto"_s,
758 u"bitand"_s,
759 u"bitor"_s,
760 u"bool"_s,
761 u"break"_s,
762 u"case"_s,
763 u"catch"_s,
764 u"char"_s,
765 u"char16_t"_s,
766 u"char32_t"_s,
767 u"char8_t"_s,
768 u"class"_s,
769 u"co_await"_s,
770 u"co_return"_s,
771 u"co_yield"_s,
772 u"compl"_s,
773 u"concept"_s,
774 u"const"_s,
775 u"const_cast"_s,
776 u"consteval"_s,
777 u"constexpr"_s,
778 u"constinit"_s,
779 u"continue"_s,
780 u"decltype"_s,
781 u"default"_s,
782 u"delete"_s,
783 u"do"_s,
784 u"double"_s,
785 u"dynamic_cast"_s,
786 u"else"_s,
787 u"enum"_s,
788 u"explicit"_s,
789 u"export"_s,
790 u"extern"_s,
791 u"false"_s,
792 u"float"_s,
793 u"for"_s,
794 u"friend"_s,
795 u"goto"_s,
796 u"if"_s,
797 u"inline"_s,
798 u"int"_s,
799 u"long"_s,
800 u"mutable"_s,
801 u"namespace"_s,
802 u"new"_s,
803 u"noexcept"_s,
804 u"not"_s,
805 u"not_eq"_s,
806 u"nullptr"_s,
807 u"operator"_s,
808 u"or"_s,
809 u"or_eq"_s,
810 u"private"_s,
811 u"protected"_s,
812 u"public"_s,
813 u"reflexpr"_s,
814 u"register"_s,
815 u"reinterpret_cast"_s,
816 u"requires"_s,
817 u"return"_s,
818 u"short"_s,
819 u"signed"_s,
820 u"sizeof"_s,
821 u"static"_s,
822 u"static_assert"_s,
823 u"static_cast"_s,
824 u"struct"_s,
825 u"switch"_s,
826 u"synchronized"_s,
827 u"template"_s,
828 u"this"_s,
829 u"thread_local"_s,
830 u"throw"_s,
831 u"true"_s,
832 u"try"_s,
833 u"typedef"_s,
834 u"typeid"_s,
835 u"typename"_s,
836 u"union"_s,
837 u"unsigned"_s,
838 u"using"_s,
839 u"virtual"_s,
840 u"void"_s,
841 u"volatile"_s,
842 u"wchar_t"_s,
843 u"while"_s,
844 u"xor"_s,
845 u"xor_eq"_s,
846 };
848
849 const auto isReserved = [&](QStringView word) {
850 if (word.startsWith(QChar(u'_')) && word.size() >= 2
851 && (word[1].isUpper() || word[1] == QChar(u'_'))) {
852 return true; // Identifiers starting with underscore and uppercase are reserved in C++
853 }
855 };
856
857 const auto validate = [&](QStringView name, QStringView errorPrefix) {
858 if (!isReserved(name))
859 return;
860 m_logger->log(errorPrefix + u" '" + name + u"' is a reserved C++ word, consider renaming",
862 };
863
864 const auto validateType = [&type, this](const QQmlJSScope::ConstPtr &typeToCheck,
866 if (typeToCheck.isNull()) {
867 m_logger->log(
869 "Can't compile the %1 type \"%2\" to C++ because it cannot be resolved")
872 return;
873 }
874
875 if (type->moduleName().isEmpty())
876 return;
877
879 m_logger->log(
881 "Can't compile the %1 type \"%2\" to C++ because it "
882 "lives in \"%3\" instead of the current file's \"%4\" QML module.")
885 }
886 };
887
888 validateType(type->baseType(), type->baseTypeName(), u"QML base");
889
890 const auto enums = type->ownEnumerations();
891 for (auto it = enums.cbegin(); it != enums.cend(); ++it) {
892 const QQmlJSMetaEnum e = it.value();
893 validate(e.name(), u"Enumeration");
894
895 const auto enumKeys = e.keys();
896 for (const auto &key : enumKeys)
897 validate(key, u"Enumeration '%1' key"_s.arg(e.name()));
898 }
899
900 const auto properties = type->ownProperties();
901 for (auto it = properties.cbegin(); it != properties.cend(); ++it) {
902 const QQmlJSMetaProperty &p = it.value();
903 validate(p.propertyName(), u"Property");
904
905 if (!p.isAlias() && !p.typeName().isEmpty())
906 validateType(p.type(), p.typeName(), u"QML property");
907 }
908
909 const auto methods = type->ownMethods();
910 for (auto it = methods.cbegin(); it != methods.cend(); ++it) {
911 const QQmlJSMetaMethod &m = it.value();
912 validate(m.methodName(), u"Method");
913 if (!m.returnTypeName().isEmpty())
914 validateType(m.returnType(), m.returnTypeName(), u"QML method return");
915
916 for (const auto &parameter : m.parameters()) {
917 validate(parameter.name(), u"Method '%1' parameter"_s.arg(m.methodName()));
918 if (!parameter.typeName().isEmpty())
919 validateType(parameter.type(), parameter.typeName(), u"QML parameter");
920 }
921 }
922
923 // TODO: one could also test signal handlers' parameters but we do not store
924 // this information in QQmlJSMetaPropertyBinding currently
925}
926
927/*! \internal
928 * Returns the file path for the C++ header of \a scope or the header created
929 * by qmltc for it and its inline components.
930 */
932{
933 const QString filePath = scope->filePath();
934 if (!filePath.endsWith(u".qml")) // assume the correct path is set
935 return scope->filePath();
936
941 [](const QString &x) { return x.endsWith(u".h"_s); });
942 if (firstHeader == paths.cend()) {
943 const QString matchedPaths = paths.isEmpty() ? u"<none>"_s : paths.join(u", ");
945 "Failed to find a header file name for path %s. Paths checked:\n%s",
947 return QString();
948 }
949 // NB: get the file name to avoid prefixes
950 return QFileInfo(*firstHeader).fileName();
951}
952
954{
956 if (const QString *srcDirPath = std::get_if<QString>(&result))
957 return *srcDirPath;
958
962 // return input as a fallback
963 return path;
964}
965
966} // namespace QQmltc
967
968QT_END_NAMESPACE
static bool isImplicitComponent(const QQmlJSScope::ConstPtr &type)
void iterateBindings(const QQmlJSScope::ConstPtr &root, const QHash< QQmlJSScope::ConstPtr, QList< QQmlJSMetaPropertyBinding > > &qmlIrOrderedBindings, TypePredicate typePredicate, BindingPredicate bindingPredicate)
QSpan< const QmltcLoggingCategoryOverride > categoryOverrides()
static bool isOrUnderComponent(QQmlJSScope::ConstPtr type)
static void setAliasData(QQmlJSMetaProperty *alias, const QQmlJSUtils::ResolvedAlias &origin)
static void addCleanQmlTypeName(QStringList *names, const QQmlJSScope::ConstPtr &scope)
void iterateTypes(const QQmlJSScope::ConstPtr &root, const QHash< QQmlJSScope::ConstPtr, QList< QQmlJSMetaPropertyBinding > > &qmlIrOrderedBindings, Predicate predicate)
QQmlJSScope::ConstPtr fetchType(const QQmlJSMetaPropertyBinding &binding)
static bool isExplicitComponent(const QQmlJSScope::ConstPtr &type)
static QString uniqueNameFromPieces(const QStringList &pieces, QHash< QString, int > &repetitions)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")