diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-22 13:43:07 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-10-25 15:13:41 +0200 |
commit | 6c2c4c00bc38bc9dc7b3c2f82fa8b12053902e71 (patch) | |
tree | 8fc11e858d7075cbfd78fed2cdde566d93ee19e9 | |
parent | f863ce7cc68a4def0fe9929c7f50e4e101fd8a1e (diff) |
shiboken6: Extend checks when not to fix a default value
Exclude numeric constants, boolean literals, initializer lists and
similar to prevent scope lookups for them.
Remove the conversion of integers to boolean literals from
AbstractMetaBuilderPrivate::fixDefaultValue(), since the type name was
misspelt ("boolean" instead of "bool") and bool constructs from
integers anyways.
Rename helper isNumericConstantt() to isIntegerConstant() for clarity.
Amends 2efc3669d07f77a08e334cf37913017523b8099b.
Task-number: PYSIDE-1691
Pick-to: 6.2
Change-Id: If74858ed0a4f16653d73220f33c4a98254dc5173
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
4 files changed, 30 insertions, 26 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp b/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp index d365a285a..fef5b06c9 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder.cpp @@ -2542,27 +2542,12 @@ QString AbstractMetaBuilder::fixEnumDefault(const AbstractMetaType &type, QString AbstractMetaBuilderPrivate::fixDefaultValue(QString expr, const AbstractMetaType &type, const AbstractMetaClass *implementingClass) const { - if (expr.isEmpty() || expr == u"{}" || expr == u"nullptr" || expr == u"NULL") - return expr; - expr.replace(u'\n', u' '); // breaks signature parser - if (type.isPrimitive()) { - if (type.name() == QLatin1String("boolean")) { - if (expr != QLatin1String("false") && expr != QLatin1String("true")) { - bool ok = false; - int number = expr.toInt(&ok); - if (ok && number) - expr = QLatin1String("true"); - else - expr = QLatin1String("false"); - } - } else { - // This can be an enum or flag so I need to delay the - // translation until all namespaces are completely - // processed. This is done in figureOutEnumValues() - } - } else if (type.isFlags() || type.isEnum()) { + if (AbstractMetaBuilder::dontFixDefaultValue(expr)) + return expr; + + if (type.isFlags() || type.isEnum()) { expr = fixEnumDefault(type, expr); } else if (type.isContainer() && expr.contains(QLatin1Char('<'))) { static const QRegularExpression typeRegEx(QStringLiteral("[^<]*<(.*)>")); diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder.h b/sources/shiboken6/ApiExtractor/abstractmetabuilder.h index 06c8c0bc4..1f6527b7d 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder.h +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder.h @@ -112,6 +112,8 @@ public: static QString searchForEnumScope(const AbstractMetaClass *metaClass, QStringView value); + static bool dontFixDefaultValue(QStringView expr); + // For testing purposes QString fixDefaultValue(const QString &expr, const AbstractMetaType &type, const AbstractMetaClass *) const; diff --git a/sources/shiboken6/ApiExtractor/abstractmetabuilder_helpers.cpp b/sources/shiboken6/ApiExtractor/abstractmetabuilder_helpers.cpp index 068c67241..1a2e7d2ac 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetabuilder_helpers.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetabuilder_helpers.cpp @@ -105,7 +105,7 @@ static bool isQualifiedCppIdentifier(QStringView e) [](QChar c) { return c.isLetterOrNumber() || c == u'_' || c == u':'; }); } -static bool isNumericConstant(const QStringView expr) +static bool isIntegerConstant(const QStringView expr) { bool isNumber; auto n = expr.toInt(&isNumber, /* guess base: 0x or decimal */ 0); @@ -113,6 +113,14 @@ static bool isNumericConstant(const QStringView expr) return isNumber; } +static bool isFloatConstant(const QStringView expr) +{ + bool isNumber; + auto d = expr.toDouble(&isNumber); + Q_UNUSED(d); + return isNumber; +} + // Fix an enum default value: Add the enum/flag scope or fully qualified name // to the default value, making it usable from Python wrapper code outside the // owner class hierarchy. See TestEnum::testEnumDefaultValues(). @@ -120,7 +128,7 @@ QString AbstractMetaBuilderPrivate::fixEnumDefault(const AbstractMetaType &type, const QString &expr) const { // QFlags construct from integers, do not fix that - if (isNumericConstant(expr)) + if (isIntegerConstant(expr)) return expr; const auto *typeEntry = type.typeEntry(); @@ -163,7 +171,7 @@ QString AbstractMetaBuilderPrivate::fixEnumDefault(const AbstractMetaType &type, : QStringView{result}; // Quick check for number "Options(0x4)" - if (isNumericConstant(innerExpression)) + if (isIntegerConstant(innerExpression)) return result; // Quick check for single enum value "Options(Option1)" @@ -187,7 +195,7 @@ QString AbstractMetaBuilderPrivate::fixEnumDefault(const AbstractMetaType &type, for (const auto &tokenIn : tokens) { const auto token = tokenIn.trimmed(); QString qualified = token.toString(); - if (!isNumericConstant(token) && isQualifiedCppIdentifier(token)) + if (!isIntegerConstant(token) && isQualifiedCppIdentifier(token)) qualified.prepend(resolveEnumValueScopePrefix(metaEnum, token)); qualifiedTokens.append(qualified); } @@ -198,3 +206,12 @@ QString AbstractMetaBuilderPrivate::fixEnumDefault(const AbstractMetaType &type, result.replace(parenPos + 1, innerExpression.size(), qualifiedExpression); return result; } + +bool AbstractMetaBuilder::dontFixDefaultValue(QStringView expr) +{ + return expr.isEmpty() || expr == u"{}" || expr == u"nullptr" + || expr == u"NULL" || expr == u"true" || expr == u"false" + || (expr.startsWith(u'{') && expr.startsWith(u'}')) // initializer list + || (expr.startsWith(u'[') && expr.startsWith(u']')) // array + || isIntegerConstant(expr) || isFloatConstant(expr); +} diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 8f3017832..8dbf6b68a 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -382,9 +382,9 @@ QString ShibokenGenerator::guessScopeForDefaultValue(const AbstractMetaFunctionC { QString value = arg.defaultValueExpression(); - if (value.isEmpty() || value == u"{}" || value == u"nullptr" || value == u"NULL" - || arg.hasModifiedDefaultValueExpression() - || arg.type().isPointer()) { + if (arg.hasModifiedDefaultValueExpression() + || arg.type().isPointer() + || AbstractMetaBuilder::dontFixDefaultValue(value)) { return value; } |