From f5f9ad6b7af6ae85202e3107a94a0d762c0ee3f3 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 25 Sep 2018 16:56:17 +0200 Subject: shiboken: Check whether enum should be converted to int for the protected hack Find the AbstractMetaEnum belonging to the type entry and perform some checks. Generally do not use int for public enums. Warn when a protected scoped enum is encountered as this cannot be converted. Task-number: PYSIDE-817 Change-Id: I02b566093b331ea2ea627bf72964aad0a1a51c83 Reviewed-by: Eike Ziller Reviewed-by: Christian Tismer --- sources/shiboken2/ApiExtractor/messages.cpp | 9 +++++++++ sources/shiboken2/ApiExtractor/messages.h | 2 ++ sources/shiboken2/generator/generator.cpp | 22 +++++++++++++++++++++- sources/shiboken2/generator/generator.h | 2 ++ .../shiboken2/tests/libsample/samplenamespace.cpp | 7 +++++++ .../shiboken2/tests/libsample/samplenamespace.h | 6 +++++- .../tests/samplebinding/typesystem_sample.xml | 1 + 7 files changed, 47 insertions(+), 2 deletions(-) diff --git a/sources/shiboken2/ApiExtractor/messages.cpp b/sources/shiboken2/ApiExtractor/messages.cpp index b81585933..fa4c75743 100644 --- a/sources/shiboken2/ApiExtractor/messages.cpp +++ b/sources/shiboken2/ApiExtractor/messages.cpp @@ -291,6 +291,15 @@ QString msgCannotOpenForWriting(const QFile &f) .arg(QDir::toNativeSeparators(f.fileName()), f.errorString()); } +// generator.cpp + +QString msgCannotUseEnumAsInt(const QString &name) +{ + return QLatin1String("Cannot convert the protected scoped enum \"") + name + + QLatin1String("\" to type int when generating wrappers for the protected hack. " + "Compilation errors may occur when used as a function argument."); +} + // main.cpp QString msgLeftOverArguments(const QMap &remainingArgs) diff --git a/sources/shiboken2/ApiExtractor/messages.h b/sources/shiboken2/ApiExtractor/messages.h index cf6800b5d..539332aef 100644 --- a/sources/shiboken2/ApiExtractor/messages.h +++ b/sources/shiboken2/ApiExtractor/messages.h @@ -109,6 +109,8 @@ QString msgCannotOpenForReading(const QFile &f); QString msgCannotOpenForWriting(const QFile &f); +QString msgCannotUseEnumAsInt(const QString &name); + QString msgLeftOverArguments(const QMap &remainingArgs); QString msgInvalidVersion(const QString &package, const QString &version); diff --git a/sources/shiboken2/generator/generator.cpp b/sources/shiboken2/generator/generator.cpp index e5df11dc8..456172ece 100644 --- a/sources/shiboken2/generator/generator.cpp +++ b/sources/shiboken2/generator/generator.cpp @@ -28,6 +28,7 @@ #include "generator.h" #include "abstractmetalang.h" +#include "messages.h" #include "reporthandler.h" #include "fileout.h" #include "apiextractor.h" @@ -809,6 +810,25 @@ DefaultValue Generator::minimalConstructor(const AbstractMetaClass* metaClass) c return DefaultValue(DefaultValue::Error); } +// Should int be used for a (protected) enum when generating the public wrapper? +bool Generator::useEnumAsIntForProtectedHack(const AbstractMetaType *metaType) const +{ + if (metaType->isFlags()) + return true; + if (!metaType->isEnum()) + return false; + const AbstractMetaEnum *metaEnum = findAbstractMetaEnum(metaType); + if (!metaEnum) + return true; + if (metaEnum->attributes() & AbstractMetaAttributes::Public) // No reason, type is public + return false; + // Only ordinary C-enums can be used as int, scoped enums fail when used + // as function arguments. + if (metaEnum->enumKind() == EnumKind::EnumClass) + qWarning(lcShiboken, "%s", qPrintable(msgCannotUseEnumAsInt(metaEnum->name()))); + return true; +} + QString Generator::translateType(const AbstractMetaType *cType, const AbstractMetaClass *context, Options options) const @@ -826,7 +846,7 @@ QString Generator::translateType(const AbstractMetaType *cType, s = QLatin1String("void"); } else if (cType->isArray()) { s = translateType(cType->arrayElementType(), context, options) + QLatin1String("[]"); - } else if (options & Generator::EnumAsInts && (cType->isEnum() || cType->isFlags())) { + } else if ((options & Generator::EnumAsInts) && useEnumAsIntForProtectedHack(cType)) { s = QLatin1String("int"); } else { if (options & Generator::OriginalName) { diff --git a/sources/shiboken2/generator/generator.h b/sources/shiboken2/generator/generator.h index 1af934871..be9a789bd 100644 --- a/sources/shiboken2/generator/generator.h +++ b/sources/shiboken2/generator/generator.h @@ -404,6 +404,8 @@ protected: const QString &context); private: + bool useEnumAsIntForProtectedHack(const AbstractMetaType *cType) const; + struct GeneratorPrivate; GeneratorPrivate* m_d; void collectInstantiatedContainersAndSmartPointers(const AbstractMetaFunction* func); diff --git a/sources/shiboken2/tests/libsample/samplenamespace.cpp b/sources/shiboken2/tests/libsample/samplenamespace.cpp index ec3da3dbf..e066869d2 100644 --- a/sources/shiboken2/tests/libsample/samplenamespace.cpp +++ b/sources/shiboken2/tests/libsample/samplenamespace.cpp @@ -36,6 +36,13 @@ using namespace std; namespace SampleNamespace { +// PYSIDE-817, scoped enums must not be converted to int in the wrappers generated +// for the protected hacks +SomeClass::PublicScopedEnum SomeClass::protectedMethodReturningPublicScopedEnum() const +{ + return PublicScopedEnum::v1; +} + OutValue enumInEnumOut(InValue in) { diff --git a/sources/shiboken2/tests/libsample/samplenamespace.h b/sources/shiboken2/tests/libsample/samplenamespace.h index 37a445e51..27fa11290 100644 --- a/sources/shiboken2/tests/libsample/samplenamespace.h +++ b/sources/shiboken2/tests/libsample/samplenamespace.h @@ -96,9 +96,11 @@ LIBSAMPLE_API void doSomethingWithArray(const unsigned char* data, unsigned int LIBSAMPLE_API int enumItemAsDefaultValueToIntArgument(int value = ZeroIn); -class SomeClass +class LIBSAMPLE_API SomeClass { public: + enum class PublicScopedEnum { v1, v2 }; + class SomeInnerClass { public: @@ -131,6 +133,8 @@ protected: ProtectedItem0, ProtectedItem1 }; + + PublicScopedEnum protectedMethodReturningPublicScopedEnum() const; }; class DerivedFromNamespace : public SomeClass::SomeInnerClass::OkThisIsRecursiveEnough diff --git a/sources/shiboken2/tests/samplebinding/typesystem_sample.xml b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml index 334f0162d..9b967fc53 100644 --- a/sources/shiboken2/tests/samplebinding/typesystem_sample.xml +++ b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml @@ -549,6 +549,7 @@ + -- cgit v1.2.3