diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-01-12 13:22:47 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-01-24 18:29:28 +0000 |
commit | 8c17e3a19f436bb1d309e0c36c79f00a90971488 (patch) | |
tree | dba9ffa6d1c402602a4da22026f0043d59ab46c3 | |
parent | 29bdff9f1466ea46f0a4bd6205c5e39d000666a5 (diff) |
shiboken6/Type system parser: Use a QHash for element type lookups
This allows for the reverse lookup, helping with error messages
and debugging.
Task-number: PYSIDE-1766
Change-Id: I8d2a9db246e306042af34522c245fb11c151e892
Reviewed-by: Christian Tismer <tismer@stackless.com>
(cherry picked from commit 60a797c9959a6ae3e18b68b7ff06ffad0a2c6f5e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/shiboken6/ApiExtractor/typesystemparser.cpp | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/sources/shiboken6/ApiExtractor/typesystemparser.cpp b/sources/shiboken6/ApiExtractor/typesystemparser.cpp index 684800719..1f1b52756 100644 --- a/sources/shiboken6/ApiExtractor/typesystemparser.cpp +++ b/sources/shiboken6/ApiExtractor/typesystemparser.cpp @@ -33,6 +33,7 @@ #include "sourcelocation.h" #include "conditionalstreamreader.h" +#include <QtCore/QDebug> #include <QtCore/QDir> #include <QtCore/QFile> #include <QtCore/QFileInfo> @@ -365,10 +366,30 @@ ENUM_LOOKUP_BEGIN(TypeSystem::ExceptionHandling, Qt::CaseSensitive, }; ENUM_LOOKUP_LINEAR_SEARCH() -ENUM_LOOKUP_BEGIN(StackElement, Qt::CaseInsensitive, - elementFromTag) - { - {u"add-conversion", StackElement::AddConversion}, // sorted! +template <class EnumType> +static std::optional<EnumType> + lookupHashElement(const QHash<QStringView, EnumType> &hash, + QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) +{ + auto end = hash.cend(); + auto it = hash.constFind(needle); + if (it != end) + return it.value(); + if (cs == Qt::CaseInsensitive) { // brute force search for the unlikely case mismatch + for (it = hash.cbegin(); it != end; ++it) { + if (it.key().compare(needle, cs) == 0) + return it.value(); + } + } + return std::nullopt; +} + +using StackElementHash = QHash<QStringView, StackElement>; + +static const StackElementHash &stackElementHash() +{ + static const StackElementHash result{ + {u"add-conversion", StackElement::AddConversion}, {u"add-function", StackElement::AddFunction}, {u"array", StackElement::Array}, {u"container-type", StackElement::ContainerTypeEntry}, @@ -417,8 +438,30 @@ ENUM_LOOKUP_BEGIN(StackElement, Qt::CaseInsensitive, {u"typesystem", StackElement::Root}, {u"value-type", StackElement::ValueTypeEntry}, }; -ENUM_LOOKUP_BINARY_SEARCH() + return result; +} +static std::optional<StackElement> elementFromTag(QStringView needle) +{ + return lookupHashElement(stackElementHash(), needle, + Qt::CaseInsensitive); // FIXME PYSIDE-7: case sensitive +} + +static QStringView tagFromElement(StackElement st) +{ + return stackElementHash().key(st); +} + +#ifndef QT_NO_DEBUG_STREAM +QDebug operator<<(QDebug d, StackElement st) +{ + QDebugStateSaver saver(d); + d.noquote(); + d.nospace(); + d << tagFromElement(st); + return d; +} +#endif // QT_NO_DEBUG_STREAM ENUM_LOOKUP_BEGIN(TypeSystem::SnakeCase, Qt::CaseSensitive, snakeCaseFromAttribute) @@ -2181,7 +2224,7 @@ bool TypeSystemParser::parseModifyArgument(const ConditionalStreamReader &, && topElement != StackElement::AddFunction) { m_error = QString::fromLatin1("argument modification requires function" " modification as parent, was %1") - .arg(uint64_t(topElement), 0, 16); + .arg(tagFromElement(topElement)); return false; } @@ -2368,7 +2411,7 @@ bool TypeSystemParser::parseAddFunction(const ConditionalStreamReader &, if (!(topElement & (StackElement::ComplexTypeEntryMask | StackElement::Root | StackElement::ContainerTypeEntry))) { m_error = QString::fromLatin1("Add/Declare function requires a complex/container type or a root tag as parent" - ", was=%1").arg(uint64_t(topElement), 0, 16); + ", was=%1").arg(tagFromElement(topElement)); return false; } QString originalSignature; @@ -2452,7 +2495,7 @@ bool TypeSystemParser::parseProperty(const ConditionalStreamReader &, StackEleme { if ((topElement & StackElement::ComplexTypeEntryMask) == 0) { m_error = QString::fromLatin1("Add property requires a complex type as parent" - ", was=%1").arg(uint64_t(topElement), 0, 16); + ", was=%1").arg(tagFromElement(topElement)); return false; } @@ -2487,7 +2530,7 @@ bool TypeSystemParser::parseModifyFunction(const ConditionalStreamReader &reader { if (!(topElement & StackElement::ComplexTypeEntryMask)) { m_error = QString::fromLatin1("Modify function requires complex type as parent" - ", was=%1").arg(uint64_t(topElement), 0, 16); + ", was=%1").arg(tagFromElement(topElement)); return false; } |