summaryrefslogtreecommitdiffstats
path: root/tests/auto/tools/moc/tst_moc.cpp
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-06-07 16:34:45 +0300
committerAhmad Samir <a.samirh78@gmail.com>2023-06-18 21:28:58 +0300
commit7779400ba6fee98b1f90702f92c17a5a4089c5ce (patch)
treee66be9f45a8981bfcda6eb83fd5042b374acc76f /tests/auto/tools/moc/tst_moc.cpp
parente603661c48903fa674332a218b21cb35b288de4c (diff)
Moc: fix generated code for nested enum class corner case
Fixes an issue with generated code where the name of an enclosing namespace is identical to an enum class type, when Q_ENUM_NS is used. Consider: namespace a { Q_NAMESPACE namespace b { enum class b { Key, Key2 }; Q_ENUM_NS(b); } } moc generated code such as: Q_CONSTINIT const QMetaObject a::b::staticMetaObject = { { ... qt_incomplete_metaTypeArray<qt_meta_stringdata_CLASSaSCOPEbENDCLASS_t, // enum 'TestEnum' QtPrivate::TypeAndForceComplete<b::b, std::true_type>, // Q_OBJECT / Q_GADGET QtPrivate::TypeAndForceComplete<void, std::true_type> >, nullptr } }; which confused the compiler: error: ‘b’ is not a member of ‘a::b::b 83 | QtPrivate::TypeAndForceComplete<b::b, std::true_type>, Fixes: QTBUG-112996 Pick-to: 6.6 Change-Id: I37aee83c32efe96cc9d6c2bd0bdb9ba80bb7b8a7 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/tools/moc/tst_moc.cpp')
-rw-r--r--tests/auto/tools/moc/tst_moc.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp
index a509c0ddae..1929ad3fba 100644
--- a/tests/auto/tools/moc/tst_moc.cpp
+++ b/tests/auto/tools/moc/tst_moc.cpp
@@ -203,6 +203,27 @@ namespace TestQNamespace {
Q_FLAG_NS(TestFlag2)
}
+namespace TestSameEnumNamespace {
+ Q_NAMESPACE
+
+ enum class TestSameEnumNamespace {
+ Key1 = 1,
+ Key2 = 2,
+ };
+ Q_ENUM_NS(TestSameEnumNamespace)
+}
+
+namespace TestNestedSameEnumNamespace {
+namespace a {
+ Q_NAMESPACE
+ // enum class with the same name as the enclosing nested namespace
+ enum class a {
+ Key11 = 11,
+ Key12 = 12,
+ };
+ Q_ENUM_NS(a)
+}
+}
namespace TestExportNamespace {
Q_NAMESPACE_EXPORT(TESTEXPORTMACRO)
@@ -789,6 +810,7 @@ private slots:
void optionsFileError_data();
void optionsFileError();
void testQNamespace();
+ void testNestedQNamespace();
void cxx17Namespaces();
void cxxAttributes();
void mocJsonOutput();
@@ -4081,6 +4103,28 @@ public:
FooNamespace::Enum1 prop() { return FooNamespace::Enum1::Key2; }
};
+void tst_Moc::testNestedQNamespace()
+{
+ QCOMPARE(TestSameEnumNamespace::staticMetaObject.enumeratorCount(), 1);
+ checkEnum(TestSameEnumNamespace::staticMetaObject.enumerator(0), "TestSameEnumNamespace",
+ {{"Key1", 1}, {"Key2", 2}});
+ QMetaEnum meta1 = QMetaEnum::fromType<TestSameEnumNamespace::TestSameEnumNamespace>();
+ QVERIFY(meta1.isValid());
+ QCOMPARE(meta1.name(), "TestSameEnumNamespace");
+ QCOMPARE(meta1.enclosingMetaObject(), &TestSameEnumNamespace::staticMetaObject);
+ QCOMPARE(meta1.keyCount(), 2);
+
+ // QTBUG-112996
+ QCOMPARE(TestNestedSameEnumNamespace::a::staticMetaObject.enumeratorCount(), 1);
+ checkEnum(TestNestedSameEnumNamespace::a::staticMetaObject.enumerator(0), "a",
+ {{"Key11", 11}, {"Key12", 12}});
+ QMetaEnum meta2 = QMetaEnum::fromType<TestNestedSameEnumNamespace::a::a>();
+ QVERIFY(meta2.isValid());
+ QCOMPARE(meta2.name(), "a");
+ QCOMPARE(meta2.enclosingMetaObject(), &TestNestedSameEnumNamespace::a::staticMetaObject);
+ QCOMPARE(meta2.keyCount(), 2);
+}
+
void tst_Moc::testQNamespace()
{
QCOMPARE(TestQNamespace::staticMetaObject.enumeratorCount(), 5);