diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-09-25 16:56:17 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-10-01 13:15:34 +0000 |
commit | f5f9ad6b7af6ae85202e3107a94a0d762c0ee3f3 (patch) | |
tree | 11caca7ab8e5498a64a371e14a9f074a6f4cbc8e /sources/shiboken2/generator/generator.cpp | |
parent | 9d4aefc1f1d1fb4dfa76baa0d59e96d7193b88b4 (diff) |
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 <eike.ziller@qt.io>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/generator/generator.cpp')
-rw-r--r-- | sources/shiboken2/generator/generator.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
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) { |