For qdebug operator<< of pointer types, always check for nullptr

Also, use a consistent syntax for when the pointer is null.

Fixes: QTBUG-135856
Change-Id: I2f5c80a5650b1be6cc0d70cde7cd1e1c1990df9a
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Daniel Trevitz 2025-04-09 21:46:49 -04:00
parent bc20065cf3
commit 1cd7e01c90
17 changed files with 49 additions and 52 deletions

View File

@ -1972,10 +1972,9 @@ const char *qAccessibleEventString(QAccessible::Event event)
Q_GUI_EXPORT QDebug operator<<(QDebug d, const QAccessibleInterface *iface) Q_GUI_EXPORT QDebug operator<<(QDebug d, const QAccessibleInterface *iface)
{ {
QDebugStateSaver saver(d); QDebugStateSaver saver(d);
if (!iface) { if (!iface)
d << "QAccessibleInterface(null)"; return d << "QAccessibleInterface(0x0)";
return d;
}
d.nospace(); d.nospace();
d << "QAccessibleInterface(" << Qt::hex << (const void *) iface << Qt::dec; d << "QAccessibleInterface(" << Qt::hex << (const void *) iface << Qt::dec;
if (iface->isValid()) { if (iface->isValid()) {

View File

@ -441,8 +441,8 @@ QStringList QIconTheme::parents() const
QDebug operator<<(QDebug debug, const std::unique_ptr<QIconLoaderEngineEntry> &entry) QDebug operator<<(QDebug debug, const std::unique_ptr<QIconLoaderEngineEntry> &entry)
{ {
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.noquote() << entry->filename; if (entry) return debug.noquote() << entry->filename;
return debug; return debug << "QIconLoaderEngineEntry(0x0)";
} }
QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName, QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName,

View File

@ -4055,10 +4055,9 @@ static void formatTabletEvent(QDebug d, const QTabletEvent *e)
QDebug operator<<(QDebug dbg, const QEventPoint *tp) QDebug operator<<(QDebug dbg, const QEventPoint *tp)
{ {
if (!tp) { if (!tp)
dbg << "QEventPoint(0x0)"; return dbg << "QEventPoint(0x0)";
return dbg;
}
return operator<<(dbg, *tp); return operator<<(dbg, *tp);
} }
@ -4098,10 +4097,9 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
{ {
QDebugStateSaver saver(dbg); QDebugStateSaver saver(dbg);
dbg.nospace(); dbg.nospace();
if (!e) { if (!e)
dbg << "QEvent(this = 0x0)"; return dbg << "QEvent(0x0)";
return dbg;
}
// More useful event output could be added here // More useful event output could be added here
const QEvent::Type type = e->type(); const QEvent::Type type = e->type();
bool isMouse = false; bool isMouse = false;

View File

@ -377,10 +377,8 @@ QDebug operator<<(QDebug debug, const QInputDevice *device)
debug.noquote(); debug.noquote();
debug << "QInputDevice("; debug << "QInputDevice(";
if (!device) { if (!device)
debug << "0)"; return debug << "0x0)";
return debug;
}
const QInputDevicePrivate *d = QInputDevicePrivate::get(device); const QInputDevicePrivate *d = QInputDevicePrivate::get(device);

View File

@ -1310,7 +1310,7 @@ QDebug operator<<(QDebug debug, const QOpenGLContext *ctx)
debug << ", invalid"; debug << ", invalid";
} }
} else { } else {
debug << '0'; debug << "0x0";
} }
debug << ')'; debug << ')';
return debug; return debug;
@ -1324,7 +1324,7 @@ QDebug operator<<(QDebug debug, const QOpenGLContextGroup *cg)
if (cg) if (cg)
debug << cg->shares(); debug << cg->shares();
else else
debug << '0'; debug << "0x0";
debug << ')'; debug << ')';
return debug; return debug;
} }

View File

@ -726,7 +726,7 @@ QDebug operator<<(QDebug debug, const QPointingDevice *device)
if (device->uniqueId().isValid()) if (device->uniqueId().isValid())
debug << " uniqueId=" << Qt::hex << device->uniqueId().numericId() << Qt::dec; debug << " uniqueId=" << Qt::hex << device->uniqueId().numericId() << Qt::dec;
} else { } else {
debug << '0'; debug << "0x0";
} }
debug << ')'; debug << ')';
return debug; return debug;

View File

@ -4835,7 +4835,7 @@ QDebug operator<<(QDebug debug, const QOpenGLTexture *t)
<< ", features=" << d->features << ", minificationFilter=" << d->minFilter << ", features=" << d->features << ", minificationFilter=" << d->minFilter
<< ", magnificationFilter=" << d->magFilter << ", wrapMode=" << d->wrapModes[0]; << ", magnificationFilter=" << d->magFilter << ", wrapMode=" << d->wrapModes[0];
} else { } else {
debug << '0'; debug << "0x0";
} }
debug << ')'; debug << ')';
return debug; return debug;

View File

@ -57,7 +57,7 @@ static QDebug operator<<(QDebug d, const QMimeData *mimeData)
if (mimeData->hasUrls()) if (mimeData->hasUrls())
d << ", urls=" << mimeData->urls(); d << ", urls=" << mimeData->urls();
} else { } else {
d << '0'; d << "0x0";
} }
d << ')'; d << ')';
return d; return d;

View File

@ -876,7 +876,7 @@ QDebug operator<<(QDebug d, const QPlatformMenuItem *i)
if (i) if (i)
static_cast<const QWindowsMenuItem *>(i)->formatDebug(d); static_cast<const QWindowsMenuItem *>(i)->formatDebug(d);
else else
d << '0'; d << "0x0";
d << ')'; d << ')';
return d; return d;
} }
@ -915,7 +915,7 @@ QDebug operator<<(QDebug d, const QPlatformMenu *m)
static_cast<const QWindowsMenu *>(m)->formatDebug(d); static_cast<const QWindowsMenu *>(m)->formatDebug(d);
d << ')'; d << ')';
} else { } else {
d << "QPlatformMenu(0)"; d << "QPlatformMenu(0x0)";
} }
return d; return d;
} }
@ -929,7 +929,7 @@ QDebug operator<<(QDebug d, const QPlatformMenuBar *mb)
if (mb) if (mb)
static_cast<const QWindowsMenuBar *>(mb)->formatDebug(d); static_cast<const QWindowsMenuBar *>(mb)->formatDebug(d);
else else
d << '0'; d << "0x0";
d << ')'; d << ')';
return d; return d;
} }

View File

@ -326,7 +326,7 @@ QDebug operator<<(QDebug d, IDataObject *dataObj)
} }
} }
} else { } else {
d << '0'; d << "0x0";
} }
d << ')'; d << ')';
return d; return d;

View File

@ -464,7 +464,7 @@ QDebug operator<<(QDebug d, const QWindowsSystemTrayIcon *t)
if (t) if (t)
t->formatDebug(d); t->formatDebug(d);
else else
d << '0'; d << "0x0";
d << ')'; d << ')';
return d; return d;
} }

View File

@ -11161,10 +11161,8 @@ QDebug operator<<(QDebug debug, const QGraphicsItem *item)
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.nospace(); debug.nospace();
if (!item) { if (!item)
debug << "QGraphicsItem(0)"; return debug << "QGraphicsItem(0x0)";
return debug;
}
if (const QGraphicsObject *o = item->toGraphicsObject()) if (const QGraphicsObject *o = item->toGraphicsObject())
debug << o->metaObject()->className(); debug << o->metaObject()->className();
@ -11179,7 +11177,7 @@ QDebug operator<<(QDebug debug, const QGraphicsItem *item)
debug << ", name=" << w->objectName(); debug << ", name=" << w->objectName();
debug << ')'; debug << ')';
} else { } else {
debug << "QWidget(0)"; debug << "QWidget(0x0)";
} }
} }
formatGraphicsItemHelper(debug, item); formatGraphicsItemHelper(debug, item);
@ -11192,10 +11190,8 @@ QDebug operator<<(QDebug debug, const QGraphicsObject *item)
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.nospace(); debug.nospace();
if (!item) { if (!item)
debug << "QGraphicsObject(0)"; return debug << "QGraphicsObject(0x0)";
return debug;
}
debug << item->metaObject()->className() << '(' << static_cast<const void *>(item); debug << item->metaObject()->className() << '(' << static_cast<const void *>(item);
if (!item->objectName().isEmpty()) if (!item->objectName().isEmpty())

View File

@ -1797,10 +1797,8 @@ QDebug operator<<(QDebug debug, const QGraphicsSceneEvent *event)
{ {
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.nospace(); debug.nospace();
if (!event) { if (!event)
debug << "QGraphicsSceneEvent(0)"; return debug << "QGraphicsSceneEvent(0x0)";
return debug;
}
const QEvent::Type type = event->type(); const QEvent::Type type = event->type();
switch (type) { switch (type) {

View File

@ -1080,6 +1080,10 @@ Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGesture *gesture)
{ {
QDebugStateSaver saver(d); QDebugStateSaver saver(d);
d.nospace(); d.nospace();
if (!gesture)
return d << "QGesture(0x0)";
switch (gesture->gestureType()) { switch (gesture->gestureType()) {
case Qt::TapGesture: case Qt::TapGesture:
formatGestureHeader(d, "QTapGesture", gesture); formatGestureHeader(d, "QTapGesture", gesture);
@ -1148,8 +1152,12 @@ Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGestureEvent *gestureEvent)
{ {
QDebugStateSaver saver(d); QDebugStateSaver saver(d);
d.nospace(); d.nospace();
d << "QGestureEvent(" << gestureEvent->gestures() << ')'; d << "QGestureEvent(";
return d; if (gestureEvent)
d << gestureEvent->gestures();
else
d << "0x0";
return d << ')';
} }
#endif // !QT_NO_DEBUG_STREAM #endif // !QT_NO_DEBUG_STREAM

View File

@ -146,8 +146,9 @@ QDebug operator<<(QDebug debug, const QDockAreaLayout &layout)
QDebug operator<<(QDebug debug, const QMainWindowLayout *layout) QDebug operator<<(QDebug debug, const QMainWindowLayout *layout)
{ {
debug << layout->layoutState.dockAreaLayout; if (layout)
return debug; return debug << layout->layoutState.dockAreaLayout;
return debug << "QMainWindowLayout(0x0)";
} }
// Use this to dump item lists of all populated main window docks. // Use this to dump item lists of all populated main window docks.

View File

@ -180,13 +180,13 @@ QString QNativeModifierEvent::toString() const
QDebug operator<<(QDebug d, QNativeEvent *e) QDebug operator<<(QDebug d, QNativeEvent *e)
{ {
Q_UNUSED(e); if (e)
return d << e->toString(); return d << e->toString();
return d << "QNativeEvent(0x0)";
} }
QDebug operator<<(QDebug d, const QNativeEvent &e) QDebug operator<<(QDebug d, const QNativeEvent &e)
{ {
Q_UNUSED(e);
return d << e.toString(); return d << e.toString();
} }

View File

@ -14,10 +14,9 @@ QDebug operator<<(QDebug debug, const QStyleOption *option)
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);
debug.noquote(); debug.noquote();
debug.nospace(); debug.nospace();
if (!option) { if (!option)
debug << "QStyleOption(0)"; return debug << "QStyleOption(0x0)";
return debug;
}
if (const QStyleOptionViewItem *ivo = qstyleoption_cast<const QStyleOptionViewItem *>(option)) { if (const QStyleOptionViewItem *ivo = qstyleoption_cast<const QStyleOptionViewItem *>(option)) {
debug << "QStyleOptionViewItem("; debug << "QStyleOptionViewItem(";
debug << ivo->index; debug << ivo->index;