diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-03-31 15:46:20 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-04-01 10:29:29 +0200 |
commit | eb5aa8d9c18535ecd0aacc42fe3af954faf01c04 (patch) | |
tree | ee76e6358c9ae18fcdce6a6ae4fead3173ea6780 /tools/shared | |
parent | a88e5c1711bbef1af00ca9a9e0db71b8a24223aa (diff) |
qmllint: Consistently use shared pointers for ScopeTree
ScopeTree keeps a weak pointer to its parent scope and strong shared
pointers to its children. Avoid passing bare pointers around, so that we
cannot create multiple shared pointers independently from the same bare
pointer.
Change-Id: Id0faece550b1878363004d843a8fa5c1164794ae
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/shared')
-rw-r--r-- | tools/shared/metatypes.h | 7 | ||||
-rw-r--r-- | tools/shared/scopetree.cpp | 72 | ||||
-rw-r--r-- | tools/shared/scopetree.h | 17 | ||||
-rw-r--r-- | tools/shared/typedescriptionreader.cpp | 2 |
4 files changed, 59 insertions, 39 deletions
diff --git a/tools/shared/metatypes.h b/tools/shared/metatypes.h index d67de2edcd..0ab37d72ca 100644 --- a/tools/shared/metatypes.h +++ b/tools/shared/metatypes.h @@ -41,6 +41,7 @@ #include <QtCore/qstring.h> #include <QtCore/qstringlist.h> +#include <QtCore/qsharedpointer.h> class MetaEnum { @@ -119,7 +120,7 @@ class MetaProperty { QString m_propertyName; QString m_typeName; - const ScopeTree *m_type = nullptr; + QWeakPointer<const ScopeTree> m_type; bool m_isList; bool m_isWritable; bool m_isPointer; @@ -142,8 +143,8 @@ public: QString propertyName() const { return m_propertyName; } QString typeName() const { return m_typeName; } - void setType(const ScopeTree *type) { m_type = type; } - const ScopeTree *type() const { return m_type; } + void setType(const QSharedPointer<const ScopeTree> &type) { m_type = type; } + QSharedPointer<const ScopeTree> type() const { return m_type.toStrongRef(); } bool isList() const { return m_isList; } bool isWritable() const { return m_isWritable; } diff --git a/tools/shared/scopetree.cpp b/tools/shared/scopetree.cpp index 870ba13fc4..ea5175d2d6 100644 --- a/tools/shared/scopetree.cpp +++ b/tools/shared/scopetree.cpp @@ -33,17 +33,20 @@ #include <algorithm> -ScopeTree::ScopeTree(ScopeType type, QString name, ScopeTree *parentScope) - : m_parentScope(parentScope), m_name(std::move(name)), m_scopeType(type) {} - -ScopeTree::Ptr ScopeTree::createNewChildScope(ScopeType type, const QString &name) -{ - Q_ASSERT(type != ScopeType::QMLScope - || !m_parentScope - || m_parentScope->m_scopeType == ScopeType::QMLScope - || m_parentScope->m_name == QLatin1String("global")); - auto childScope = ScopeTree::Ptr(new ScopeTree{type, name, this}); - m_childScopes.push_back(childScope); +ScopeTree::ScopeTree(ScopeType type, const QString &name, const ScopeTree::Ptr &parentScope) + : m_parentScope(parentScope), m_name(name), m_scopeType(type) {} + +ScopeTree::Ptr ScopeTree::create(ScopeType type, const QString &name, + const ScopeTree::Ptr &parentScope) +{ + ScopeTree::Ptr childScope(new ScopeTree{type, name, parentScope}); + if (parentScope) { + Q_ASSERT(type != ScopeType::QMLScope + || !parentScope->m_parentScope + || parentScope->parentScope()->m_scopeType == ScopeType::QMLScope + || parentScope->parentScope()->m_name == QLatin1String("global")); + parentScope->m_childScopes.push_back(childScope); + } return childScope; } @@ -51,13 +54,13 @@ void ScopeTree::insertJSIdentifier(const QString &id, ScopeType scope) { Q_ASSERT(m_scopeType != ScopeType::QMLScope); Q_ASSERT(scope != ScopeType::QMLScope); - if (scope == ScopeType::JSFunctionScope) { - auto targetScope = this; - while (targetScope->scopeType() != ScopeType::JSFunctionScope) - targetScope = targetScope->m_parentScope; - targetScope->m_jsIdentifiers.insert(id); - } else { + if (scope != ScopeType::JSFunctionScope || m_scopeType == ScopeType::JSFunctionScope) { m_jsIdentifiers.insert(id); + } else { + auto targetScope = parentScope(); + while (targetScope->m_scopeType != ScopeType::JSFunctionScope) + targetScope = targetScope->parentScope(); + targetScope->m_jsIdentifiers.insert(id); } } @@ -101,13 +104,22 @@ void ScopeTree::accessMember(const QString &name, const QString &parentType, bool ScopeTree::isVisualRootScope() const { - return m_parentScope && m_parentScope->m_parentScope - && m_parentScope->m_parentScope->m_parentScope == nullptr; + if (!m_parentScope) + return false; + + const auto grandParent = parentScope()->m_parentScope.toStrongRef(); + if (!grandParent) + return false; + + return grandParent->m_parentScope == nullptr; } bool ScopeTree::isIdInCurrentQMlScopes(const QString &id) const { - const auto *qmlScope = currentQMLScope(); + if (m_scopeType == ScopeType::QMLScope) + return m_properties.contains(id) || m_methods.contains(id) || m_enums.contains(id); + + const auto qmlScope = findCurrentQMLScope(parentScope()); return qmlScope->m_properties.contains(id) || qmlScope->m_methods.contains(id) || qmlScope->m_enums.contains(id); @@ -115,25 +127,29 @@ bool ScopeTree::isIdInCurrentQMlScopes(const QString &id) const bool ScopeTree::isIdInCurrentJSScopes(const QString &id) const { - auto jsScope = this; - while (jsScope) { + if (m_scopeType != ScopeType::QMLScope && m_jsIdentifiers.contains(id)) + return true; + + for (auto jsScope = parentScope(); jsScope; jsScope = jsScope->parentScope()) { if (jsScope->m_scopeType != ScopeType::QMLScope && jsScope->m_jsIdentifiers.contains(id)) return true; - jsScope = jsScope->m_parentScope; } + return false; } bool ScopeTree::isIdInjectedFromSignal(const QString &id) const { - return currentQMLScope()->m_injectedSignalIdentifiers.contains(id); + if (m_scopeType == ScopeType::QMLScope) + return m_injectedSignalIdentifiers.contains(id); + return findCurrentQMLScope(parentScope())->m_injectedSignalIdentifiers.contains(id); } -const ScopeTree *ScopeTree::currentQMLScope() const +ScopeTree::ConstPtr ScopeTree::findCurrentQMLScope(const ScopeTree::ConstPtr &scope) { - auto qmlScope = this; + auto qmlScope = scope; while (qmlScope && qmlScope->m_scopeType != ScopeType::QMLScope) - qmlScope = qmlScope->m_parentScope; + qmlScope = qmlScope->parentScope(); return qmlScope; } @@ -148,7 +164,7 @@ void ScopeTree::setExportMetaObjectRevision(int exportIndex, int metaObjectRevis m_exports[exportIndex].setMetaObjectRevision(metaObjectRevision); } -void ScopeTree::updateParentProperty(const ScopeTree *scope) +void ScopeTree::updateParentProperty(const ScopeTree::ConstPtr &scope) { auto it = m_properties.find(QLatin1String("parent")); if (it != m_properties.end() diff --git a/tools/shared/scopetree.h b/tools/shared/scopetree.h index f28f2fa841..90f772ab98 100644 --- a/tools/shared/scopetree.h +++ b/tools/shared/scopetree.h @@ -67,7 +67,9 @@ class ScopeTree Q_DISABLE_COPY_MOVE(ScopeTree) public: using Ptr = QSharedPointer<ScopeTree>; + using WeakPtr = QWeakPointer<ScopeTree>; using ConstPtr = QSharedPointer<const ScopeTree>; + using WeakConstPtr = QWeakPointer<const ScopeTree>; class Export { public: @@ -93,11 +95,11 @@ public: int m_metaObjectRevision = 0; }; - ScopeTree(ScopeType type, QString name = QString(), - ScopeTree *parentScope = nullptr); + static ScopeTree::Ptr create(ScopeType type = ScopeType::QMLScope, const QString &name = QString(), + const ScopeTree::Ptr &parentScope = ScopeTree::Ptr()); + static ScopeTree::ConstPtr findCurrentQMLScope(const ScopeTree::ConstPtr &scope); - ScopeTree::Ptr createNewChildScope(ScopeType type, const QString &name); - ScopeTree *parentScope() const { return m_parentScope; } + ScopeTree::Ptr parentScope() const { return m_parentScope.toStrongRef(); } void insertJSIdentifier(const QString &id, ScopeType scope); void insertSignalIdentifier(const QString &id, const MetaMethod &method, @@ -139,7 +141,7 @@ public: void addProperty(const MetaProperty &prop) { m_properties.insert(prop.propertyName(), prop); } QHash<QString, MetaProperty> properties() const { return m_properties; } - void updateParentProperty(const ScopeTree *scope); + void updateParentProperty(const ScopeTree::ConstPtr &scope); QString defaultPropertyName() const { return m_defaultPropertyName; } void setDefaultPropertyName(const QString &name) { m_defaultPropertyName = name; } @@ -174,7 +176,6 @@ public: bool isIdInCurrentQMlScopes(const QString &id) const; bool isIdInCurrentJSScopes(const QString &id) const; bool isIdInjectedFromSignal(const QString &id) const; - const ScopeTree *currentQMLScope() const; QVector<ScopeTree::Ptr> childScopes() const { @@ -187,6 +188,8 @@ public: } private: + ScopeTree(ScopeType type, const QString &name = QString(), + const ScopeTree::Ptr &parentScope = ScopeTree::Ptr()); QSet<QString> m_jsIdentifiers; QMultiHash<QString, MethodUsage> m_injectedSignalIdentifiers; @@ -199,7 +202,7 @@ private: QVector<QPair<QString, QQmlJS::SourceLocation>> m_unmatchedSignalHandlers; QVector<ScopeTree::Ptr> m_childScopes; - ScopeTree *m_parentScope; + ScopeTree::WeakPtr m_parentScope; QString m_fileName; QString m_name; diff --git a/tools/shared/typedescriptionreader.cpp b/tools/shared/typedescriptionreader.cpp index 495ee57f32..3ca8e7e44a 100644 --- a/tools/shared/typedescriptionreader.cpp +++ b/tools/shared/typedescriptionreader.cpp @@ -188,7 +188,7 @@ void TypeDescriptionReader::readDependencies(UiScriptBinding *ast) void TypeDescriptionReader::readComponent(UiObjectDefinition *ast) { - ScopeTree::Ptr scope(new ScopeTree(ScopeType::QMLScope)); + ScopeTree::Ptr scope = ScopeTree::create(); for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) { UiObjectMember *member = it->member; |