diff options
Diffstat (limited to 'sources/shiboken6/ApiExtractor')
3 files changed, 27 insertions, 23 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); +} |