diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-03-19 17:24:00 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-04-19 12:51:14 +0000 |
commit | fb7386c18c15ca83aebe8f973c1b018d016baaa9 (patch) | |
tree | 91f0c2988af2e4133a2fdc702fccce69a623b9bf /sources/shiboken2/ApiExtractor/tests | |
parent | a89690409972501741c846ac8ad4a499f2982809 (diff) |
shiboken: Use enum values from Clang
Remove a lot of ugly value parsing code in favor of
clang_getEnumConstantDeclValue() and
clang_getEnumConstantDeclUnsignedValue() depending on the
type.
Introduce a class EnumValue containing a union of qint64 and quint64 values
to represent signed/unsigned values correctly and use that in the code model
and meta language classes.
Change-Id: If2efb7cfd560237907678b8f6fdfb0bc689c0c93
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'sources/shiboken2/ApiExtractor/tests')
-rw-r--r-- | sources/shiboken2/ApiExtractor/tests/testarrayargument.cpp | 4 | ||||
-rw-r--r-- | sources/shiboken2/ApiExtractor/tests/testenum.cpp | 69 |
2 files changed, 44 insertions, 29 deletions
diff --git a/sources/shiboken2/ApiExtractor/tests/testarrayargument.cpp b/sources/shiboken2/ApiExtractor/tests/testarrayargument.cpp index cbeb216c9..eb10fc8a0 100644 --- a/sources/shiboken2/ApiExtractor/tests/testarrayargument.cpp +++ b/sources/shiboken2/ApiExtractor/tests/testarrayargument.cpp @@ -133,7 +133,7 @@ void TestArrayArgument::testArrayArgumentWithSizeDefinedByEnumValue() const AbstractMetaArgument* arg = classA->functions().last()->arguments().first(); QVERIFY(arg->type()->isArray()); - QCOMPARE(arg->type()->arrayElementCount(), nvalues->value()); + QCOMPARE(arg->type()->arrayElementCount(), nvalues->value().value()); QCOMPARE(arg->type()->arrayElementType()->name(), QLatin1String("double")); }; @@ -164,7 +164,7 @@ void TestArrayArgument::testArrayArgumentWithSizeDefinedByEnumValueFromGlobalEnu const AbstractMetaArgument* arg = classA->functions().last()->arguments().first(); QVERIFY(arg->type()->isArray()); - QCOMPARE(arg->type()->arrayElementCount(), nvalues->value()); + QCOMPARE(arg->type()->arrayElementCount(), nvalues->value().value()); QCOMPARE(arg->type()->arrayElementType()->name(), QLatin1String("double")); }; diff --git a/sources/shiboken2/ApiExtractor/tests/testenum.cpp b/sources/shiboken2/ApiExtractor/tests/testenum.cpp index 55c098840..970f6d00c 100644 --- a/sources/shiboken2/ApiExtractor/tests/testenum.cpp +++ b/sources/shiboken2/ApiExtractor/tests/testenum.cpp @@ -149,12 +149,12 @@ void TestEnum::testAnonymousEnum() AbstractMetaEnumValue* enumValueA0 = anonEnumA1->values().first(); QCOMPARE(enumValueA0->name(), QLatin1String("A0")); - QCOMPARE(enumValueA0->value(), 0); + QCOMPARE(enumValueA0->value().value(), 0); QCOMPARE(enumValueA0->stringValue(), QString()); AbstractMetaEnumValue* enumValueA1 = anonEnumA1->values().last(); QCOMPARE(enumValueA1->name(), QLatin1String("A1")); - QCOMPARE(enumValueA1->value(), 1); + QCOMPARE(enumValueA1->value().value(), 1); QCOMPARE(enumValueA1->stringValue(), QString()); AbstractMetaEnum* anonEnumIsThis = classes[0]->findEnum(QLatin1String("isThis")); @@ -164,12 +164,12 @@ void TestEnum::testAnonymousEnum() AbstractMetaEnumValue* enumValueIsThis = anonEnumIsThis->values().first(); QCOMPARE(enumValueIsThis->name(), QLatin1String("isThis")); - QCOMPARE(enumValueIsThis->value(), static_cast<int>(true)); + QCOMPARE(enumValueIsThis->value().value(), static_cast<int>(true)); QCOMPARE(enumValueIsThis->stringValue(), QLatin1String("true")); AbstractMetaEnumValue* enumValueIsThat = anonEnumIsThis->values().last(); QCOMPARE(enumValueIsThat->name(), QLatin1String("isThat")); - QCOMPARE(enumValueIsThat->value(), static_cast<int>(false)); + QCOMPARE(enumValueIsThat->value().value(), static_cast<int>(false)); QCOMPARE(enumValueIsThat->stringValue(), QLatin1String("false")); } @@ -195,12 +195,12 @@ void TestEnum::testGlobalEnums() AbstractMetaEnumValue* enumValueA0 = enumA->values().first(); QCOMPARE(enumValueA0->name(), QLatin1String("A0")); - QCOMPARE(enumValueA0->value(), 0); + QCOMPARE(enumValueA0->value().value(), 0); QCOMPARE(enumValueA0->stringValue(), QString()); AbstractMetaEnumValue* enumValueA1 = enumA->values().last(); QCOMPARE(enumValueA1->name(), QLatin1String("A1")); - QCOMPARE(enumValueA1->value(), 1); + QCOMPARE(enumValueA1->value().value(), 1); QCOMPARE(enumValueA1->stringValue(), QString()); AbstractMetaEnum* enumB = globalEnums.last(); @@ -208,12 +208,12 @@ void TestEnum::testGlobalEnums() AbstractMetaEnumValue* enumValueB0 = enumB->values().first(); QCOMPARE(enumValueB0->name(), QLatin1String("B0")); - QCOMPARE(enumValueB0->value(), 2); + QCOMPARE(enumValueB0->value().value(), 2); QCOMPARE(enumValueB0->stringValue(), QLatin1String("2")); AbstractMetaEnumValue* enumValueB1 = enumB->values().last(); QCOMPARE(enumValueB1->name(), QLatin1String("B1")); - QCOMPARE(enumValueB1->value(), 4); + QCOMPARE(enumValueB1->value().value(), 4); QCOMPARE(enumValueB1->stringValue(), QLatin1String("0x4")); } @@ -245,12 +245,12 @@ void TestEnum::testEnumValueFromNeighbourEnum() AbstractMetaEnumValue* enumValueA0 = enumA->values().first(); QCOMPARE(enumValueA0->name(), QLatin1String("ValueA0")); - QCOMPARE(enumValueA0->value(), 0); + QCOMPARE(enumValueA0->value().value(), 0); QCOMPARE(enumValueA0->stringValue(), QString()); AbstractMetaEnumValue* enumValueA1 = enumA->values().last(); QCOMPARE(enumValueA1->name(), QLatin1String("ValueA1")); - QCOMPARE(enumValueA1->value(), 1); + QCOMPARE(enumValueA1->value().value(), 1); QCOMPARE(enumValueA1->stringValue(), QString()); AbstractMetaEnum* enumB = classes[0]->findEnum(QLatin1String("EnumB")); @@ -259,12 +259,12 @@ void TestEnum::testEnumValueFromNeighbourEnum() AbstractMetaEnumValue* enumValueB0 = enumB->values().first(); QCOMPARE(enumValueB0->name(), QLatin1String("ValueB0")); - QCOMPARE(enumValueB0->value(), 1); + QCOMPARE(enumValueB0->value().value(), 1); QCOMPARE(enumValueB0->stringValue(), QLatin1String("A::ValueA1")); AbstractMetaEnumValue* enumValueB1 = enumB->values().last(); QCOMPARE(enumValueB1->name(), QLatin1String("ValueB1")); - QCOMPARE(enumValueB1->value(), 0); + QCOMPARE(enumValueB1->value().value(), 0); QCOMPARE(enumValueB1->stringValue(), QLatin1String("ValueA0")); } @@ -272,21 +272,25 @@ void TestEnum::testEnumValueFromExpression() { const char* cppCode ="\ struct A {\n\ - enum EnumA {\n\ + enum EnumA : unsigned {\n\ ValueA0 = 3u,\n\ ValueA1 = ~3u,\n\ - ValueA2 = ~3,\n\ + ValueA2 = 0xffffffff,\n\ ValueA3 = 0xf0,\n\ ValueA4 = 8 |ValueA3,\n\ ValueA5 = ValueA3|32,\n\ ValueA6 = ValueA3 >> 1,\n\ ValueA7 = ValueA3 << 1\n\ };\n\ + enum EnumB : int {\n\ + ValueB0 = ~3,\n\ + };\n\ };\n"; const char* xmlCode = "\ <typesystem package=\"Foo\">\n\ <value-type name='A'>\n\ <enum-type name='EnumA'/>\n\ + <enum-type name='EnumB'/>\n\ </value-type>\n\ </typesystem>\n"; @@ -298,47 +302,58 @@ void TestEnum::testEnumValueFromExpression() AbstractMetaEnum* enumA = classA->findEnum(QLatin1String("EnumA")); QVERIFY(enumA); + QVERIFY(!enumA->isSigned()); QCOMPARE(enumA->typeEntry()->qualifiedCppName(), QLatin1String("A::EnumA")); AbstractMetaEnumValue* valueA0 = enumA->values().at(0); QCOMPARE(valueA0->name(), QLatin1String("ValueA0")); QCOMPARE(valueA0->stringValue(), QLatin1String("3u")); - QCOMPARE(valueA0->value(), (int) 3u); + QCOMPARE(valueA0->value().unsignedValue(), 3u); AbstractMetaEnumValue* valueA1 = enumA->values().at(1); QCOMPARE(valueA1->name(), QLatin1String("ValueA1")); QCOMPARE(valueA1->stringValue(), QLatin1String("~3u")); - QCOMPARE(valueA1->value(), (int) ~3u); + QCOMPARE(valueA1->value().unsignedValue(), ~3u); AbstractMetaEnumValue* valueA2 = enumA->values().at(2); QCOMPARE(valueA2->name(), QLatin1String("ValueA2")); - QCOMPARE(valueA2->stringValue(), QLatin1String("~3")); - QCOMPARE(valueA2->value(), ~3); + QCOMPARE(valueA2->stringValue(), QLatin1String("0xffffffff")); + QCOMPARE(valueA2->value().unsignedValue(), 0xffffffffu); AbstractMetaEnumValue* valueA3 = enumA->values().at(3); QCOMPARE(valueA3->name(), QLatin1String("ValueA3")); QCOMPARE(valueA3->stringValue(), QLatin1String("0xf0")); - QCOMPARE(valueA3->value(), 0xf0); + QCOMPARE(valueA3->value().unsignedValue(), 0xf0u); AbstractMetaEnumValue* valueA4 = enumA->values().at(4); QCOMPARE(valueA4->name(), QLatin1String("ValueA4")); QCOMPARE(valueA4->stringValue(), QLatin1String("8 |ValueA3")); - QCOMPARE(valueA4->value(), 8|0xf0); + QCOMPARE(valueA4->value().unsignedValue(), 8|0xf0u); AbstractMetaEnumValue* valueA5 = enumA->values().at(5); QCOMPARE(valueA5->name(), QLatin1String("ValueA5")); QCOMPARE(valueA5->stringValue(), QLatin1String("ValueA3|32")); - QCOMPARE(valueA5->value(), 0xf0|32); + QCOMPARE(valueA5->value().unsignedValue(), 0xf0u|32); AbstractMetaEnumValue* valueA6 = enumA->values().at(6); QCOMPARE(valueA6->name(), QLatin1String("ValueA6")); QCOMPARE(valueA6->stringValue(), QLatin1String("ValueA3 >> 1")); - QCOMPARE(valueA6->value(), 0xf0 >> 1); + QCOMPARE(valueA6->value().unsignedValue(), 0xf0u >> 1); AbstractMetaEnumValue* valueA7 = enumA->values().at(7); QCOMPARE(valueA7->name(), QLatin1String("ValueA7")); QCOMPARE(valueA7->stringValue(), QLatin1String("ValueA3 << 1")); - QCOMPARE(valueA7->value(), 0xf0 << 1); + QCOMPARE(valueA7->value().unsignedValue(), 0xf0u << 1); + + const AbstractMetaEnum *enumB = classA->findEnum(QLatin1String("EnumB")); + QVERIFY(enumB); + QVERIFY(enumB->isSigned()); + QCOMPARE(enumB->typeEntry()->qualifiedCppName(), QLatin1String("A::EnumB")); + QCOMPARE(enumB->values().size(), 1); + const AbstractMetaEnumValue *valueB0 = enumB->values().at(0); + QCOMPARE(valueB0->name(), QLatin1String("ValueB0")); + QCOMPARE(valueB0->stringValue(), QLatin1String("~3")); + QCOMPARE(valueB0->value().value(), ~3); } void TestEnum::testPrivateEnum() @@ -375,12 +390,12 @@ void TestEnum::testPrivateEnum() AbstractMetaEnumValue* pub0 = publicEnum->values().first(); QCOMPARE(pub0->name(), QLatin1String("Pub0")); - QCOMPARE(pub0->value(), 0x0f); + QCOMPARE(pub0->value().value(), 0x0f); QCOMPARE(pub0->stringValue(), QLatin1String("Priv0")); AbstractMetaEnumValue* pub1 = publicEnum->values().last(); QCOMPARE(pub1->name(), QLatin1String("Pub1")); - QCOMPARE(pub1->value(), 0xf0); + QCOMPARE(pub1->value().value(), 0xf0); QCOMPARE(pub1->stringValue(), QLatin1String("A::Priv1")); } @@ -408,12 +423,12 @@ void TestEnum::testTypedefEnum() AbstractMetaEnumValue* enumValueA0 = enumA->values().first(); QCOMPARE(enumValueA0->name(), QLatin1String("A0")); - QCOMPARE(enumValueA0->value(), 0); + QCOMPARE(enumValueA0->value().value(), 0); QCOMPARE(enumValueA0->stringValue(), QLatin1String("")); AbstractMetaEnumValue* enumValueA1 = enumA->values().last(); QCOMPARE(enumValueA1->name(), QLatin1String("A1")); - QCOMPARE(enumValueA1->value(), 1); + QCOMPARE(enumValueA1->value().value(), 1); QCOMPARE(enumValueA1->stringValue(), QString()); } |