QMetaMethod::typeName() should return "void" if the return type is void

QMetaMethod::typeName() is documented to return an empty string if
the return type is void. But after the introduction of
QMetaType::UnknownType (where void was made a distinct type),
returning an empty string causes the idiom

   QMetaType::type(method.typeName())

to break; the result will be QMetaType::UnknownType rather than
the expected QMetaType::Void for methods that return void.

New code should use the new function QMetaMethod::returnType()
instead, but it would be good if existing code still did the right
thing.

The consequence of returning "void" instead of an empty string is
that it breaks existing logic that uses the typeName() length to
determine whether a method returns void. But we judge this as the
lesser of the two evils; it's better to have a typeName() function
that is consistent and keeps the QMetaType::type(method.typeName())
idiom working, than to force the typeName() inconsistency for void
only to keep code that does "strlen(method.typeName()) == 0"
working.

The places in Qt that were relying on a zero-length typeName()
(testlib, dbus, declarative) have already been changed to use
returnType().

Also adapt QMetaObjectBuilder, which is internal API.

Change-Id: I70249174029811c5b5d2a08c24b6db33b3723d19
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Kent Hansen 2012-03-20 19:27:02 +01:00 committed by Qt by Nokia
parent b8d71ed60b
commit 22d621dd99
5 changed files with 46 additions and 46 deletions

13
dist/changes-5.0.0 vendored
View File

@ -58,10 +58,15 @@ information about a particular change.
* QMetaType::construct() has been renamed to QMetaType::create().
* QMetaType::unregisterType() has been removed.
- QMetaMethod::signature() has been renamed to QMetaMethod::methodSignature(),
and the return type has been changed to QByteArray. This was done to be able
to generate the signature string on demand, rather than always storing it in
the meta-data.
- QMetaMethod:
* QMetaMethod::signature() has been renamed to QMetaMethod::methodSignature(),
and the return type has been changed to QByteArray. This was done to be able
to generate the signature string on demand, rather than always storing it in
the meta-data.
* QMetaMethod::typeName() no longer returns an empty string when the return
type is void; it returns "void". The recommended way of checking whether a
method returns void is to compare the return value of QMetaMethod::returnType()
to QMetaType::Void.
- QTestLib:
* The plain-text, xml and lightxml test output formats have been changed to

View File

@ -1675,15 +1675,8 @@ const char *QMetaMethodPrivate::rawReturnTypeName() const
uint typeInfo = mobj->d.data[typesDataIndex()];
if (typeInfo & IsUnresolvedType)
return rawStringData(mobj, typeInfo & TypeNameIndexMask);
else {
if (typeInfo == QMetaType::Void) {
// QMetaMethod::typeName() is documented to return an empty string
// if the return type is void, but QMetaType::typeName() returns
// "void".
return "";
}
else
return QMetaType::typeName(typeInfo);
}
}
int QMetaMethodPrivate::returnType() const
@ -1907,8 +1900,9 @@ QList<QByteArray> QMetaMethod::parameterNames() const
/*!
Returns the return type name of this method, or an empty string if the
return type is \e void.
Returns the return type name of this method.
\sa returnType(), QMetaType::type()
*/
const char *QMetaMethod::typeName() const
{

View File

@ -96,7 +96,7 @@ public:
QMetaMethodBuilderPrivate
(QMetaMethod::MethodType _methodType,
const QByteArray& _signature,
const QByteArray& _returnType = QByteArray(),
const QByteArray& _returnType = QByteArray("void"),
QMetaMethod::Access _access = QMetaMethod::Public,
int _revision = 0)
: signature(QMetaObject::normalizedSignature(_signature.constData())),
@ -435,8 +435,7 @@ QMetaMethodBuilder QMetaObjectBuilder::addMethod(const QByteArray& signature)
\a signature and \a returnType. Returns an object that can be
used to adjust the other attributes of the method. The \a signature
and \a returnType will be normalized before they are added to
the class. If \a returnType is empty, then it indicates that
the method has \c{void} as its return type.
the class.
\sa method(), methodCount(), removeMethod(), indexOfMethod()
*/
@ -507,7 +506,7 @@ QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
{
int index = d->methods.size();
d->methods.append(QMetaMethodBuilderPrivate
(QMetaMethod::Signal, signature, QByteArray(), QMetaMethod::Protected));
(QMetaMethod::Signal, signature, QByteArray("void"), QMetaMethod::Protected));
return QMetaMethodBuilder(this, index);
}
@ -523,7 +522,8 @@ QMetaMethodBuilder QMetaObjectBuilder::addSignal(const QByteArray& signature)
QMetaMethodBuilder QMetaObjectBuilder::addConstructor(const QByteArray& signature)
{
int index = d->constructors.size();
d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature));
d->constructors.append(QMetaMethodBuilderPrivate(QMetaMethod::Constructor, signature,
/*returnType=*/QByteArray()));
return QMetaMethodBuilder(this, -(index + 1));
}

View File

@ -197,7 +197,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignal")
<< QByteArray("voidSignal()")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>())
<< (QList<QByteArray>())
<< (QList<QByteArray>())
@ -206,7 +206,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokable")
<< QByteArray("voidInvokable()")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>())
<< (QList<QByteArray>())
<< (QList<QByteArray>())
@ -215,7 +215,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlot")
<< QByteArray("voidSlot()")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>())
<< (QList<QByteArray>())
<< (QList<QByteArray>())
@ -233,7 +233,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalVoid")
<< QByteArray("voidSignalVoid()")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< QList<int>()
<< QList<QByteArray>()
<< QList<QByteArray>()
@ -242,7 +242,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalInt")
<< QByteArray("voidSignalInt(int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("voidSignalIntArg"))
@ -251,7 +251,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableInt")
<< QByteArray("voidInvokableInt(int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("voidInvokableIntArg"))
@ -260,7 +260,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotInt")
<< QByteArray("voidSlotInt(int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("voidSlotIntArg"))
@ -278,7 +278,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalQReal")
<< QByteArray("voidSignalQReal(qreal)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<qreal>())
<< (QList<QByteArray>() << QByteArray("qreal"))
<< (QList<QByteArray>() << QByteArray("voidSignalQRealArg"))
@ -287,7 +287,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableQReal")
<< QByteArray("voidInvokableQReal(qreal)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<qreal>())
<< (QList<QByteArray>() << QByteArray("qreal"))
<< (QList<QByteArray>() << QByteArray("voidInvokableQRealArg"))
@ -296,7 +296,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotQReal")
<< QByteArray("voidSlotQReal(qreal)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<qreal>())
<< (QList<QByteArray>() << QByteArray("qreal"))
<< (QList<QByteArray>() << QByteArray("voidSlotQRealArg"))
@ -314,7 +314,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalQString")
<< QByteArray("voidSignalQString(QString)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::QString))
<< (QList<QByteArray>() << QByteArray("QString"))
<< (QList<QByteArray>() << QByteArray("voidSignalQStringArg"))
@ -323,7 +323,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableQString")
<< QByteArray("voidInvokableQString(QString)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::QString))
<< (QList<QByteArray>() << QByteArray("QString"))
<< (QList<QByteArray>() << QByteArray("voidInvokableQStringArg"))
@ -332,7 +332,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotQString")
<< QByteArray("voidSlotQString(QString)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::QString))
<< (QList<QByteArray>() << QByteArray("QString"))
<< (QList<QByteArray>() << QByteArray("voidSlotQStringArg"))
@ -350,7 +350,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalCustomType")
<< QByteArray("voidSignalCustomType(CustomType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<CustomType>())
<< (QList<QByteArray>() << QByteArray("CustomType"))
<< (QList<QByteArray>() << QByteArray("voidSignalCustomTypeArg"))
@ -359,7 +359,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableCustomType")
<< QByteArray("voidInvokableCustomType(CustomType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<CustomType>())
<< (QList<QByteArray>() << QByteArray("CustomType"))
<< (QList<QByteArray>() << QByteArray("voidInvokableCustomTypeArg"))
@ -368,7 +368,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotCustomType")
<< QByteArray("voidSlotCustomType(CustomType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << qMetaTypeId<CustomType>())
<< (QList<QByteArray>() << QByteArray("CustomType"))
<< (QList<QByteArray>() << QByteArray("voidSlotCustomTypeArg"))
@ -386,7 +386,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalCustomUnregisteredType")
<< QByteArray("voidSignalCustomUnregisteredType(CustomUnregisteredType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << 0)
<< (QList<QByteArray>() << QByteArray("CustomUnregisteredType"))
<< (QList<QByteArray>() << QByteArray("voidSignalCustomUnregisteredTypeArg"))
@ -395,7 +395,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableCustomUnregisteredType")
<< QByteArray("voidInvokableCustomUnregisteredType(CustomUnregisteredType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << 0)
<< (QList<QByteArray>() << QByteArray("CustomUnregisteredType"))
<< (QList<QByteArray>() << QByteArray("voidInvokableCustomUnregisteredTypeArg"))
@ -404,7 +404,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotCustomUnregisteredType")
<< QByteArray("voidSlotCustomUnregisteredType(CustomUnregisteredType)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << 0)
<< (QList<QByteArray>() << QByteArray("CustomUnregisteredType"))
<< (QList<QByteArray>() << QByteArray("voidSlotCustomUnregisteredTypeArg"))
@ -554,7 +554,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSignalNoParameterNames")
<< QByteArray("voidSignalNoParameterNames(bool,int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Bool) << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("bool") << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("") << QByteArray(""))
@ -563,7 +563,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidInvokableNoParameterNames")
<< QByteArray("voidInvokableNoParameterNames(bool,int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Bool) << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("bool") << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("") << QByteArray(""))
@ -572,7 +572,7 @@ void tst_QMetaMethod::method_data()
QTest::newRow("voidSlotNoParameterNames")
<< QByteArray("voidSlotNoParameterNames(bool,int)")
<< int(QMetaType::Void) << QByteArray("")
<< int(QMetaType::Void) << QByteArray("void")
<< (QList<int>() << int(QMetaType::Bool) << int(QMetaType::Int))
<< (QList<QByteArray>() << QByteArray("bool") << QByteArray("int"))
<< (QList<QByteArray>() << QByteArray("") << QByteArray(""))
@ -627,6 +627,7 @@ void tst_QMetaMethod::method()
QCOMPARE(method.tag(), "");
QCOMPARE(method.returnType(), returnType);
QVERIFY(method.typeName() != 0);
if (QByteArray(method.typeName()) != returnTypeName) {
// QMetaMethod should always produce a semantically equivalent typename
QCOMPARE(QMetaType::type(method.typeName()), QMetaType::type(returnTypeName));

View File

@ -229,7 +229,7 @@ void tst_QMetaObjectBuilder::method()
QMetaMethodBuilder method1 = builder.addMethod("foo(const QString&, int)");
QCOMPARE(method1.signature(), QByteArray("foo(QString,int)"));
QVERIFY(method1.methodType() == QMetaMethod::Method);
QVERIFY(method1.returnType().isEmpty());
QCOMPARE(method1.returnType(), QByteArray("void"));
QCOMPARE(method1.parameterTypes(), QList<QByteArray>() << "QString" << "int");
QVERIFY(method1.parameterNames().isEmpty());
QVERIFY(method1.tag().isEmpty());
@ -354,7 +354,7 @@ void tst_QMetaObjectBuilder::slot()
QMetaMethodBuilder method1 = builder.addSlot("foo(const QString&, int)");
QCOMPARE(method1.signature(), QByteArray("foo(QString,int)"));
QVERIFY(method1.methodType() == QMetaMethod::Slot);
QVERIFY(method1.returnType().isEmpty());
QCOMPARE(method1.returnType(), QByteArray("void"));
QCOMPARE(method1.parameterTypes(), QList<QByteArray>() << "QString" << "int");
QVERIFY(method1.parameterNames().isEmpty());
QVERIFY(method1.tag().isEmpty());
@ -367,7 +367,7 @@ void tst_QMetaObjectBuilder::slot()
QMetaMethodBuilder method2 = builder.addSlot("bar(QString)");
QCOMPARE(method2.signature(), QByteArray("bar(QString)"));
QVERIFY(method2.methodType() == QMetaMethod::Slot);
QVERIFY(method2.returnType().isEmpty());
QCOMPARE(method2.returnType(), QByteArray("void"));
QCOMPARE(method2.parameterTypes(), QList<QByteArray>() << "QString");
QVERIFY(method2.parameterNames().isEmpty());
QVERIFY(method2.tag().isEmpty());
@ -393,7 +393,7 @@ void tst_QMetaObjectBuilder::signal()
QMetaMethodBuilder method1 = builder.addSignal("foo(const QString&, int)");
QCOMPARE(method1.signature(), QByteArray("foo(QString,int)"));
QVERIFY(method1.methodType() == QMetaMethod::Signal);
QVERIFY(method1.returnType().isEmpty());
QCOMPARE(method1.returnType(), QByteArray("void"));
QCOMPARE(method1.parameterTypes(), QList<QByteArray>() << "QString" << "int");
QVERIFY(method1.parameterNames().isEmpty());
QVERIFY(method1.tag().isEmpty());
@ -406,7 +406,7 @@ void tst_QMetaObjectBuilder::signal()
QMetaMethodBuilder method2 = builder.addSignal("bar(QString)");
QCOMPARE(method2.signature(), QByteArray("bar(QString)"));
QVERIFY(method2.methodType() == QMetaMethod::Signal);
QVERIFY(method2.returnType().isEmpty());
QCOMPARE(method2.returnType(), QByteArray("void"));
QCOMPARE(method2.parameterTypes(), QList<QByteArray>() << "QString");
QVERIFY(method2.parameterNames().isEmpty());
QVERIFY(method2.tag().isEmpty());