Improve output of Q_ENUM

When we have the named keys and not just integer values, we can output
something unambiously that closer match how the enums should be used.

Output of enums without proper metadata is left unchanged

Before:
QSurfaceFormat::ColorSpace(DefaultColorSpace)
QPainter::CompositionMode(3)

After:
QSurfaceFormat::DefaultColorSpace
QPainter::CompositionMode(3)

Change-Id: I537e879ba8b5c555b2aae9ba831facc88d430443
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Allan Sandfeld Jensen 2018-07-25 16:34:03 +02:00
parent 056fbf03a5
commit 89f784757e
3 changed files with 23 additions and 11 deletions

View File

@ -939,14 +939,18 @@ void qt_QMetaEnum_flagDebugOperator(QDebug &debug, size_t sizeofT, int value)
QDebug qt_QMetaEnum_debugOperator(QDebug &dbg, int value, const QMetaObject *meta, const char *name)
{
QDebugStateSaver saver(dbg);
dbg.nospace();
QMetaEnum me = meta->enumerator(meta->indexOfEnumerator(name));
const char *key = me.valueToKey(value);
dbg.nospace() << meta->className() << "::" << name << '(';
if (key)
if (key) {
if (const char *scope = me.scope())
dbg << scope << "::";
if (me.isScoped())
dbg << name << "::";
dbg << key;
else
dbg << value;
dbg << ')';
} else {
dbg << meta->className() << "::" << name << "(" << value << ")";
}
return dbg;
}

View File

@ -65,7 +65,7 @@ void tst_QNoDebug::streaming() const
{
QDateTime dt(QDate(1,2,3),QTime(4,5,6));
const QByteArray debugString = dt.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t")).toLatin1();
const QByteArray message = "QDateTime(" + debugString + " Qt::TimeSpec(LocalTime))";
const QByteArray message = "QDateTime(" + debugString + " Qt::LocalTime)";
QTest::ignoreMessage(QtWarningMsg, message.constData());
qWarning() << dt;
}

View File

@ -54,6 +54,11 @@ namespace MyNamespace {
MyEnum2,
MyEnum3
};
enum class MyScopedEnum {
Enum1,
Enum2,
Enum3
};
enum MyAnotherEnum {
MyAnotherEnum1 = 1,
MyAnotherEnum2 = 2,
@ -79,6 +84,7 @@ namespace MyNamespace {
{ }
private:
Q_ENUM(MyEnum)
Q_ENUM(MyScopedEnum)
Q_ENUM(MyAnotherEnum)
Q_FLAG(MyFlags)
@ -1730,12 +1736,14 @@ void tst_QMetaObject::signalIndex()
void tst_QMetaObject::enumDebugStream()
{
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyEnum(MyEnum2) world ");
MyNamespace::MyClass::MyEnum e = MyNamespace::MyClass::MyEnum2;
qDebug() << "hello" << e << "world";
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyEnum2 world ");
qDebug() << "hello" << MyNamespace::MyClass::MyEnum2 << "world";
QTest::ignoreMessage(QtDebugMsg, "Qt::WindowType(WindowTitleHint) Qt::WindowType(Window) Qt::WindowType(Desktop) Qt::WindowType(WindowSystemMenuHint)");
qDebug() << Qt::WindowTitleHint << Qt::Window <<Qt::Desktop << Qt::WindowSystemMenuHint;
QTest::ignoreMessage(QtDebugMsg, "hello MyNamespace::MyClass::MyScopedEnum::Enum3 scoped world ");
qDebug() << "hello" << MyNamespace::MyClass::MyScopedEnum::Enum3 << "scoped world";
QTest::ignoreMessage(QtDebugMsg, "Qt::WindowTitleHint Qt::Window Qt::Desktop Qt::WindowSystemMenuHint");
qDebug() << Qt::WindowTitleHint << Qt::Window << Qt::Desktop << Qt::WindowSystemMenuHint;
QTest::ignoreMessage(QtDebugMsg, "hello QFlags<MyNamespace::MyClass::MyFlags>(MyFlag1) world");
MyNamespace::MyClass::MyFlags f1 = MyNamespace::MyClass::MyFlag1;