summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2022-08-09 10:50:37 +0200
committerSami Shalayel <sami.shalayel@qt.io>2022-08-10 00:48:31 +0200
commitcec3b3b4f90a1db642b8c3e03c493ab7d6c5f8a5 (patch)
treec09635309c10920000078d6978e78d37c8f6bc56
parent395c9ac731e3789f336bad09cb4a8918a73be43d (diff)
Remove assertion from ownMethodIndex that breaks testswip/protobuf
ownMethodIndex works for all kind of methods, also for constructors. Therefore, remove the assertion there (that checks for non constructors) and add a test in qtbase so it does not happen again. The test broken by the assertion is in qtdeclarative: tst_QJSEngine::newQMetaObject(). Also rename QMetaMethodPrivate::ownConstructorIndex() to ownConstructorMethodIndex() as the previous naming implied that ownMethodIndex() could not be used for constructors. amends b73ab954dffffc462b6f6efe5a2dd97efeab0038 Task-number: QTBUG-105360 Change-Id: I0244993ed79bee055645b5443f5d02e1c089a6c6 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--src/corelib/kernel/qmetaobject.cpp7
-rw-r--r--tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp39
2 files changed, 42 insertions, 4 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp
index f96354f0d2..7849bce771 100644
--- a/src/corelib/kernel/qmetaobject.cpp
+++ b/src/corelib/kernel/qmetaobject.cpp
@@ -189,7 +189,7 @@ public:
inline QList<QByteArray> parameterNames() const;
inline QByteArray tag() const;
inline int ownMethodIndex() const;
- inline int ownConstructorIndex() const;
+ inline int ownConstructorMethodIndex() const;
// shadows the public function
enum class InvokeFailReason : int {
@@ -1917,11 +1917,10 @@ QByteArray QMetaMethodPrivate::tag() const
int QMetaMethodPrivate::ownMethodIndex() const
{
// recompute the methodIndex by reversing the arithmetic in QMetaObject::method()
- Q_ASSERT(methodType() != Constructor);
return ( data.d - mobj->d.data - priv(mobj->d.data)->methodData)/Data::Size;
}
-int QMetaMethodPrivate::ownConstructorIndex() const
+int QMetaMethodPrivate::ownConstructorMethodIndex() const
{
// recompute the methodIndex by reversing the arithmetic in QMetaObject::constructor()
Q_ASSERT(methodType() == Constructor);
@@ -2471,7 +2470,7 @@ auto QMetaMethodPrivate::invokeImpl(QMetaMethod self, void *target,
return InvokeFailReason::ConstructorCallWithoutResult;
}
- int idx = priv->ownConstructorIndex();
+ int idx = priv->ownConstructorMethodIndex();
if (priv->mobj->static_metacall(QMetaObject::CreateInstance, idx, param) >= 0)
return InvokeFailReason::ConstructorCallFailed;
return {};
diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
index 2b8ef8358d..4cc15159e7 100644
--- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
+++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp
@@ -32,6 +32,9 @@ private slots:
void parameterTypeName();
void isConst();
+
+ void methodIndexes_data();
+ void methodIndexes();
};
struct CustomType { };
@@ -862,6 +865,42 @@ void tst_QMetaMethod::isConst()
}
}
+void tst_QMetaMethod::methodIndexes_data()
+{
+ QTest::addColumn<QByteArray>("signature");
+ QTest::addColumn<QMetaMethod::MethodType>("methodType");
+
+ QTest::newRow("constructor1") << QByteArray("MethodTestObject()") << QMetaMethod::Constructor;
+ QTest::newRow("constructor5") << QByteArray("MethodTestObject(CustomUnregisteredType)")
+ << QMetaMethod::Constructor;
+ QTest::newRow("method0") << QByteArray("voidInvokable()") << QMetaMethod::Method;
+ QTest::newRow("method6") << QByteArray("boolInvokable()") << QMetaMethod::Method;
+}
+
+void tst_QMetaMethod::methodIndexes()
+{
+ QFETCH(QByteArray, signature);
+ QFETCH(QMetaMethod::MethodType, methodType);
+
+ const bool isConstructor = methodType == QMetaMethod::Constructor;
+
+ // roundtrip: index = QMetaObject::indexOfConstructor/Method()
+ // <-> method = QMetaObject::constructor/method()
+ // <-> indexThatShouldBeEqualToAboveIndex = QMetaMethod::methodIndex()
+
+ const QMetaObject *mo = &MethodTestObject::staticMetaObject;
+ const int index =
+ isConstructor ? mo->indexOfConstructor(signature) : mo->indexOfMethod(signature);
+ QVERIFY(index != -1);
+
+ QMetaMethod methodFromMetaObject =
+ mo->method(index); // should work on all methods (constructors, signals, ...)
+ const int absoluteMethodIndex =
+ methodFromMetaObject
+ .methodIndex(); // should work on all methods (constructors, signals, ...)
+
+ QCOMPARE(absoluteMethodIndex, index);
+}
QTEST_MAIN(tst_QMetaMethod)
#include "tst_qmetamethod.moc"