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)
{
QDebugStateSaver saver(d);
if (!iface) {
d << "QAccessibleInterface(null)";
return d;
}
if (!iface)
return d << "QAccessibleInterface(0x0)";
d.nospace();
d << "QAccessibleInterface(" << Qt::hex << (const void *) iface << Qt::dec;
if (iface->isValid()) {

View File

@ -441,8 +441,8 @@ QStringList QIconTheme::parents() const
QDebug operator<<(QDebug debug, const std::unique_ptr<QIconLoaderEngineEntry> &entry)
{
QDebugStateSaver saver(debug);
debug.noquote() << entry->filename;
return debug;
if (entry) return debug.noquote() << entry->filename;
return debug << "QIconLoaderEngineEntry(0x0)";
}
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)
{
if (!tp) {
dbg << "QEventPoint(0x0)";
return dbg;
}
if (!tp)
return dbg << "QEventPoint(0x0)";
return operator<<(dbg, *tp);
}
@ -4098,10 +4097,9 @@ QDebug operator<<(QDebug dbg, const QEvent *e)
{
QDebugStateSaver saver(dbg);
dbg.nospace();
if (!e) {
dbg << "QEvent(this = 0x0)";
return dbg;
}
if (!e)
return dbg << "QEvent(0x0)";
// More useful event output could be added here
const QEvent::Type type = e->type();
bool isMouse = false;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1080,6 +1080,10 @@ Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGesture *gesture)
{
QDebugStateSaver saver(d);
d.nospace();
if (!gesture)
return d << "QGesture(0x0)";
switch (gesture->gestureType()) {
case Qt::TapGesture:
formatGestureHeader(d, "QTapGesture", gesture);
@ -1148,8 +1152,12 @@ Q_WIDGETS_EXPORT QDebug operator<<(QDebug d, const QGestureEvent *gestureEvent)
{
QDebugStateSaver saver(d);
d.nospace();
d << "QGestureEvent(" << gestureEvent->gestures() << ')';
return d;
d << "QGestureEvent(";
if (gestureEvent)
d << gestureEvent->gestures();
else
d << "0x0";
return d << ')';
}
#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)
{
debug << layout->layoutState.dockAreaLayout;
return debug;
if (layout)
return debug << layout->layoutState.dockAreaLayout;
return debug << "QMainWindowLayout(0x0)";
}
// 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)
{
Q_UNUSED(e);
return d << e->toString();
if (e)
return d << e->toString();
return d << "QNativeEvent(0x0)";
}
QDebug operator<<(QDebug d, const QNativeEvent &e)
{
Q_UNUSED(e);
return d << e.toString();
}

View File

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