From 496823b9a856d649c468d03b64241562807f3c16 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 12 Nov 2015 10:16:22 +0100 Subject: QtWidgets: use Q_UNLIKELY for every qWarning() (1) If, after checking a condition, we issue a qWarning(), by definition that check is unlikely to be true. Tell the compiler so it can move the error handling code out of the normal code path to increase the effective icache size. This change contains the changes to the util/, dialogs/ and widgets/ subdirs. Moved conditional code around where possible so that we could always use Q_UNLIKELY, instead of having to revert to Q_LIKELY here and there. In QSystemTrayIcon::setVisible(), as a drive-by, I swapped the evaluation order of an &&-expression (newly wrapped in Q_UNLIKELY) to be more readable and more efficient (cheaper check first) at the same time. Change-Id: I3564c5a5deacba49d67d3989fb0b53e680c57fcb Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/widgets/dialogs/qdialog.cpp | 2 +- src/widgets/dialogs/qfiledialog.cpp | 2 +- src/widgets/dialogs/qprogressdialog.cpp | 10 +++--- src/widgets/dialogs/qwizard.cpp | 38 +++++++++++------------ src/widgets/util/qcolormap.cpp | 2 +- src/widgets/util/qcompleter.cpp | 8 ++--- src/widgets/util/qscroller.cpp | 4 +-- src/widgets/util/qsystemtrayicon.cpp | 2 +- src/widgets/util/qundostack.cpp | 12 +++---- src/widgets/widgets/qabstractscrollarea.cpp | 4 +-- src/widgets/widgets/qabstractspinbox.cpp | 4 +-- src/widgets/widgets/qcalendarwidget.cpp | 2 +- src/widgets/widgets/qcombobox.cpp | 14 ++++----- src/widgets/widgets/qdatetimeedit.cpp | 8 ++--- src/widgets/widgets/qdialogbuttonbox.cpp | 11 +++---- src/widgets/widgets/qdockarealayout.cpp | 8 ++--- src/widgets/widgets/qlcdnumber.cpp | 4 +-- src/widgets/widgets/qlineedit.cpp | 2 +- src/widgets/widgets/qmaccocoaviewcontainer_mac.mm | 2 +- src/widgets/widgets/qmacnativewidget_mac.mm | 2 +- src/widgets/widgets/qmainwindow.cpp | 2 +- src/widgets/widgets/qmdiarea.cpp | 20 ++++++------ src/widgets/widgets/qmdisubwindow.cpp | 4 +-- src/widgets/widgets/qmenu_mac.mm | 2 +- src/widgets/widgets/qplaintextedit.cpp | 2 +- src/widgets/widgets/qspinbox.cpp | 2 +- src/widgets/widgets/qsplitter.cpp | 6 ++-- src/widgets/widgets/qstackedwidget.cpp | 2 +- src/widgets/widgets/qstatusbar.cpp | 4 +-- src/widgets/widgets/qtextbrowser.cpp | 2 +- src/widgets/widgets/qtoolbararealayout.cpp | 4 +-- src/widgets/widgets/qtoolbox.cpp | 6 ++-- src/widgets/widgets/qwidgetlinecontrol.cpp | 2 +- 33 files changed, 99 insertions(+), 100 deletions(-) diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp index 5124960ab4..4ba26a90f7 100644 --- a/src/widgets/dialogs/qdialog.cpp +++ b/src/widgets/dialogs/qdialog.cpp @@ -516,7 +516,7 @@ int QDialog::exec() { Q_D(QDialog); - if (d->eventLoop) { + if (Q_UNLIKELY(d->eventLoop)) { qWarning("QDialog::exec: Recursive call detected"); return -1; } diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index cd26a6759f..11067544c3 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -976,7 +976,7 @@ void QFileDialog::setDirectoryUrl(const QUrl &directory) d->setDirectory_sys(directory); else if (directory.isLocalFile()) setDirectory(directory.toLocalFile()); - else if (d->usingWidgets()) + else if (Q_UNLIKELY(d->usingWidgets())) qWarning("Non-native QFileDialog supports only local files"); } diff --git a/src/widgets/dialogs/qprogressdialog.cpp b/src/widgets/dialogs/qprogressdialog.cpp index bbb251c8b2..619d0051ea 100644 --- a/src/widgets/dialogs/qprogressdialog.cpp +++ b/src/widgets/dialogs/qprogressdialog.cpp @@ -353,7 +353,7 @@ void QProgressDialog::setLabel(QLabel *label) { Q_D(QProgressDialog); if (label == d->label) { - if (label) + if (Q_UNLIKELY(label)) qWarning("QProgressDialog::setLabel: Attempt to set the same label again"); return; } @@ -402,7 +402,7 @@ void QProgressDialog::setCancelButton(QPushButton *cancelButton) { Q_D(QProgressDialog); if (d->cancel == cancelButton) { - if (cancelButton) + if (Q_UNLIKELY(cancelButton)) qWarning("QProgressDialog::setCancelButton: Attempt to set the same button again"); return; } @@ -465,16 +465,16 @@ void QProgressDialogPrivate::setCancelButtonText(const QString &cancelButtonText void QProgressDialog::setBar(QProgressBar *bar) { Q_D(QProgressDialog); - if (!bar) { + if (Q_UNLIKELY(!bar)) { qWarning("QProgressDialog::setBar: Cannot set a null progress bar"); return; } #ifndef QT_NO_DEBUG - if (value() > 0) + if (Q_UNLIKELY(value() > 0)) qWarning("QProgressDialog::setBar: Cannot set a new progress bar " "while the old one is active"); #endif - if (bar == d->bar) { + if (Q_UNLIKELY(bar == d->bar)) { qWarning("QProgressDialog::setBar: Attempt to set the same progress bar again"); return; } diff --git a/src/widgets/dialogs/qwizard.cpp b/src/widgets/dialogs/qwizard.cpp index 996e5819ff..c050b5b097 100644 --- a/src/widgets/dialogs/qwizard.cpp +++ b/src/widgets/dialogs/qwizard.cpp @@ -799,7 +799,7 @@ void QWizardPrivate::addField(const QWizardField &field) QWizardField myField = field; myField.resolve(defaultPropertyTable); - if (fieldIndexMap.contains(myField.name)) { + if (Q_UNLIKELY(fieldIndexMap.contains(myField.name))) { qWarning("QWizardPage::addField: Duplicate field '%ls'", qUtf16Printable(myField.name)); return; } @@ -2256,17 +2256,17 @@ void QWizard::setPage(int theid, QWizardPage *page) { Q_D(QWizard); - if (!page) { + if (Q_UNLIKELY(!page)) { qWarning("QWizard::setPage: Cannot insert null page"); return; } - if (theid == -1) { + if (Q_UNLIKELY(theid == -1)) { qWarning("QWizard::setPage: Cannot insert page with ID -1"); return; } - if (d->pageMap.contains(theid)) { + if (Q_UNLIKELY(d->pageMap.contains(theid))) { qWarning("QWizard::setPage: Page with duplicate ID %d ignored", theid); return; } @@ -2450,7 +2450,7 @@ void QWizard::setStartId(int theid) return; } - if (!d->pageMap.contains(newStart)) { + if (Q_UNLIKELY(!d->pageMap.contains(newStart))) { qWarning("QWizard::setStartId: Invalid page ID %d", newStart); return; } @@ -2508,15 +2508,15 @@ void QWizard::setField(const QString &name, const QVariant &value) Q_D(QWizard); int index = d->fieldIndexMap.value(name, -1); - if (index != -1) { - const QWizardField &field = d->fields.at(index); - if (!field.object->setProperty(field.property, value)) - qWarning("QWizard::setField: Couldn't write to property '%s'", - field.property.constData()); + if (Q_UNLIKELY(index == -1)) { + qWarning("QWizard::setField: No such field '%ls'", qUtf16Printable(name)); return; } - qWarning("QWizard::setField: No such field '%ls'", qUtf16Printable(name)); + const QWizardField &field = d->fields.at(index); + if (Q_UNLIKELY(!field.object->setProperty(field.property, value))) + qWarning("QWizard::setField: Couldn't write to property '%s'", + field.property.constData()); } /*! @@ -2531,13 +2531,13 @@ QVariant QWizard::field(const QString &name) const Q_D(const QWizard); int index = d->fieldIndexMap.value(name, -1); - if (index != -1) { - const QWizardField &field = d->fields.at(index); - return field.object->property(field.property); + if (Q_UNLIKELY(index == -1)) { + qWarning("QWizard::field: No such field '%ls'", qUtf16Printable(name)); + return QVariant(); } - qWarning("QWizard::field: No such field '%ls'", qUtf16Printable(name)); - return QVariant(); + const QWizardField &field = d->fields.at(index); + return field.object->property(field.property); } /*! @@ -2763,7 +2763,7 @@ void QWizard::setButtonLayout(const QList &layout) // O(n^2), but n is very small for (int j = 0; j < i; ++j) { WizardButton button2 = layout.at(j); - if (button2 == button1) { + if (Q_UNLIKELY(button2 == button1)) { qWarning("QWizard::setButtonLayout: Duplicate button in layout"); return; } @@ -3140,11 +3140,11 @@ void QWizard::next() if (validateCurrentPage()) { int next = nextId(); if (next != -1) { - if (d->history.contains(next)) { + if (Q_UNLIKELY(d->history.contains(next))) { qWarning("QWizard::next: Page %d already met", next); return; } - if (!d->pageMap.contains(next)) { + if (Q_UNLIKELY(!d->pageMap.contains(next))) { qWarning("QWizard::next: No such page %d", next); return; } diff --git a/src/widgets/util/qcolormap.cpp b/src/widgets/util/qcolormap.cpp index fc4d511384..4bcf1501d2 100644 --- a/src/widgets/util/qcolormap.cpp +++ b/src/widgets/util/qcolormap.cpp @@ -58,7 +58,7 @@ static QColormapPrivate *screenMap = 0; void QColormap::initialize() { screenMap = new QColormapPrivate; - if (!QGuiApplication::primaryScreen()) { + if (Q_UNLIKELY(!QGuiApplication::primaryScreen())) { qWarning("no screens available, assuming 24-bit color"); screenMap->depth = 24; screenMap->mode = QColormap::Direct; diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp index ba56f004b7..55ceeca919 100644 --- a/src/widgets/util/qcompleter.cpp +++ b/src/widgets/util/qcompleter.cpp @@ -1144,9 +1144,9 @@ void QCompleter::setFilterMode(Qt::MatchFlags filterMode) if (d->filterMode == filterMode) return; - if (filterMode != Qt::MatchStartsWith - && filterMode != Qt::MatchContains - && filterMode != Qt::MatchEndsWith) { + if (Q_UNLIKELY(filterMode != Qt::MatchStartsWith && + filterMode != Qt::MatchContains && + filterMode != Qt::MatchEndsWith)) { qWarning("Unhandled QCompleter::filterMode flag is used."); return; } @@ -1641,7 +1641,7 @@ int QCompleter::maxVisibleItems() const void QCompleter::setMaxVisibleItems(int maxItems) { Q_D(QCompleter); - if (maxItems < 0) { + if (Q_UNLIKELY(maxItems < 0)) { qWarning("QCompleter::setMaxVisibleItems: " "Invalid max visible items (%d) must be >= 0", maxItems); return; diff --git a/src/widgets/util/qscroller.cpp b/src/widgets/util/qscroller.cpp index 0065ccf6b3..c540c3cdf0 100644 --- a/src/widgets/util/qscroller.cpp +++ b/src/widgets/util/qscroller.cpp @@ -159,8 +159,8 @@ static qreal differentialForProgress(const QEasingCurve &curve, qreal pos) static qreal progressForValue(const QEasingCurve &curve, qreal value) { - if (curve.type() >= QEasingCurve::InElastic && - curve.type() < QEasingCurve::Custom) { + if (Q_UNLIKELY(curve.type() >= QEasingCurve::InElastic && + curve.type() < QEasingCurve::Custom)) { qWarning("progressForValue(): QEasingCurves of type %d do not have an inverse, since they are not injective.", curve.type()); return value; } diff --git a/src/widgets/util/qsystemtrayicon.cpp b/src/widgets/util/qsystemtrayicon.cpp index d22791048f..20f5c95ae9 100644 --- a/src/widgets/util/qsystemtrayicon.cpp +++ b/src/widgets/util/qsystemtrayicon.cpp @@ -264,7 +264,7 @@ void QSystemTrayIcon::setVisible(bool visible) Q_D(QSystemTrayIcon); if (visible == d->visible) return; - if (d->icon.isNull() && visible) + if (Q_UNLIKELY(visible && d->icon.isNull())) qWarning("QSystemTrayIcon::setVisible: No Icon set"); d->visible = visible; if (d->visible) diff --git a/src/widgets/util/qundostack.cpp b/src/widgets/util/qundostack.cpp index 0272870537..35d8c10578 100644 --- a/src/widgets/util/qundostack.cpp +++ b/src/widgets/util/qundostack.cpp @@ -633,7 +633,7 @@ void QUndoStack::push(QUndoCommand *cmd) void QUndoStack::setClean() { Q_D(QUndoStack); - if (!d->macro_stack.isEmpty()) { + if (Q_UNLIKELY(!d->macro_stack.isEmpty())) { qWarning("QUndoStack::setClean(): cannot set clean in the middle of a macro"); return; } @@ -688,7 +688,7 @@ void QUndoStack::undo() if (d->index == 0) return; - if (!d->macro_stack.isEmpty()) { + if (Q_UNLIKELY(!d->macro_stack.isEmpty())) { qWarning("QUndoStack::undo(): cannot undo in the middle of a macro"); return; } @@ -714,7 +714,7 @@ void QUndoStack::redo() if (d->index == d->command_list.size()) return; - if (!d->macro_stack.isEmpty()) { + if (Q_UNLIKELY(!d->macro_stack.isEmpty())) { qWarning("QUndoStack::redo(): cannot redo in the middle of a macro"); return; } @@ -761,7 +761,7 @@ int QUndoStack::index() const void QUndoStack::setIndex(int idx) { Q_D(QUndoStack); - if (!d->macro_stack.isEmpty()) { + if (Q_UNLIKELY(!d->macro_stack.isEmpty())) { qWarning("QUndoStack::setIndex(): cannot set index in the middle of a macro"); return; } @@ -981,7 +981,7 @@ void QUndoStack::beginMacro(const QString &text) void QUndoStack::endMacro() { Q_D(QUndoStack); - if (d->macro_stack.isEmpty()) { + if (Q_UNLIKELY(d->macro_stack.isEmpty())) { qWarning("QUndoStack::endMacro(): no matching beginMacro()"); return; } @@ -1049,7 +1049,7 @@ void QUndoStack::setUndoLimit(int limit) { Q_D(QUndoStack); - if (!d->command_list.isEmpty()) { + if (Q_UNLIKELY(!d->command_list.isEmpty())) { qWarning("QUndoStack::setUndoLimit(): an undo limit can only be set when the stack is empty"); return; } diff --git a/src/widgets/widgets/qabstractscrollarea.cpp b/src/widgets/widgets/qabstractscrollarea.cpp index 65d06eafc5..ed6b039c7c 100644 --- a/src/widgets/widgets/qabstractscrollarea.cpp +++ b/src/widgets/widgets/qabstractscrollarea.cpp @@ -705,7 +705,7 @@ QScrollBar *QAbstractScrollArea::verticalScrollBar() const void QAbstractScrollArea::setVerticalScrollBar(QScrollBar *scrollBar) { Q_D(QAbstractScrollArea); - if (!scrollBar) { + if (Q_UNLIKELY(!scrollBar)) { qWarning("QAbstractScrollArea::setVerticalScrollBar: Cannot set a null scroll bar"); return; } @@ -766,7 +766,7 @@ QScrollBar *QAbstractScrollArea::horizontalScrollBar() const void QAbstractScrollArea::setHorizontalScrollBar(QScrollBar *scrollBar) { Q_D(QAbstractScrollArea); - if (!scrollBar) { + if (Q_UNLIKELY(!scrollBar)) { qWarning("QAbstractScrollArea::setHorizontalScrollBar: Cannot set a null scroll bar"); return; } diff --git a/src/widgets/widgets/qabstractspinbox.cpp b/src/widgets/widgets/qabstractspinbox.cpp index ba4bbe40a8..40c148f4c6 100644 --- a/src/widgets/widgets/qabstractspinbox.cpp +++ b/src/widgets/widgets/qabstractspinbox.cpp @@ -1931,7 +1931,7 @@ void QSpinBoxValidator::fixup(QString &input) const QVariant operator+(const QVariant &arg1, const QVariant &arg2) { QVariant ret; - if (arg1.type() != arg2.type()) + if (Q_UNLIKELY(arg1.type() != arg2.type())) qWarning("QAbstractSpinBox: Internal error: Different types (%s vs %s) (%s:%d)", arg1.typeName(), arg2.typeName(), __FILE__, __LINE__); switch (arg1.type()) { @@ -1970,7 +1970,7 @@ QVariant operator+(const QVariant &arg1, const QVariant &arg2) QVariant operator-(const QVariant &arg1, const QVariant &arg2) { QVariant ret; - if (arg1.type() != arg2.type()) + if (Q_UNLIKELY(arg1.type() != arg2.type())) qWarning("QAbstractSpinBox: Internal error: Different types (%s vs %s) (%s:%d)", arg1.typeName(), arg2.typeName(), __FILE__, __LINE__); switch (arg1.type()) { diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index 5e6db46a63..eb216b1ad0 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -2917,7 +2917,7 @@ void QCalendarWidget::setDateEditAcceptDelay(int delay) */ void QCalendarWidget::updateCell(const QDate &date) { - if (!date.isValid()) { + if (Q_UNLIKELY(!date.isValid())) { qWarning("QCalendarWidget::updateCell: Invalid date"); return; } diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index deffad432f..401f365426 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -1360,7 +1360,7 @@ int QComboBox::maxVisibleItems() const void QComboBox::setMaxVisibleItems(int maxItems) { Q_D(QComboBox); - if (maxItems < 0) { + if (Q_UNLIKELY(maxItems < 0)) { qWarning("QComboBox::setMaxVisibleItems: " "Invalid max visible items (%d) must be >= 0", maxItems); return; @@ -1395,7 +1395,7 @@ int QComboBox::count() const void QComboBox::setMaxCount(int max) { Q_D(QComboBox); - if (max < 0) { + if (Q_UNLIKELY(max < 0)) { qWarning("QComboBox::setMaxCount: Invalid count (%d) must be >= 0", max); return; } @@ -1448,7 +1448,7 @@ void QComboBox::setAutoCompletion(bool enable) Q_D(QComboBox); #ifdef QT_KEYPAD_NAVIGATION - if (QApplication::keypadNavigationEnabled() && !enable && isEditable()) + if (Q_UNLIKELY(QApplication::keypadNavigationEnabled() && !enable && isEditable())) qWarning("QComboBox::setAutoCompletion: auto completion is mandatory when combo box editable"); #endif @@ -1758,7 +1758,7 @@ void QComboBox::setEditable(bool editable) void QComboBox::setLineEdit(QLineEdit *edit) { Q_D(QComboBox); - if (!edit) { + if (Q_UNLIKELY(!edit)) { qWarning("QComboBox::setLineEdit: cannot set a 0 line edit"); return; } @@ -1917,7 +1917,7 @@ QAbstractItemDelegate *QComboBox::itemDelegate() const */ void QComboBox::setItemDelegate(QAbstractItemDelegate *delegate) { - if (!delegate) { + if (Q_UNLIKELY(!delegate)) { qWarning("QComboBox::setItemDelegate: cannot set a 0 delegate"); return; } @@ -1949,7 +1949,7 @@ void QComboBox::setModel(QAbstractItemModel *model) { Q_D(QComboBox); - if (!model) { + if (Q_UNLIKELY(!model)) { qWarning("QComboBox::setModel: cannot set a 0 model"); return; } @@ -2395,7 +2395,7 @@ QAbstractItemView *QComboBox::view() const void QComboBox::setView(QAbstractItemView *itemView) { Q_D(QComboBox); - if (!itemView) { + if (Q_UNLIKELY(!itemView)) { qWarning("QComboBox::setView: cannot set a 0 view"); return; } diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 42987df3ec..bff1ff6c67 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -760,17 +760,17 @@ QCalendarWidget *QDateTimeEdit::calendarWidget() const void QDateTimeEdit::setCalendarWidget(QCalendarWidget *calendarWidget) { Q_D(QDateTimeEdit); - if (!calendarWidget) { + if (Q_UNLIKELY(!calendarWidget)) { qWarning("QDateTimeEdit::setCalendarWidget: Cannot set a null calendar widget"); return; } - if (!d->calendarPopup) { + if (Q_UNLIKELY(!d->calendarPopup)) { qWarning("QDateTimeEdit::setCalendarWidget: calendarPopup is set to false"); return; } - if (!(d->display & QDateTimeParser::DateSectionMask)) { + if (Q_UNLIKELY(!(d->display & QDateTimeParser::DateSectionMask))) { qWarning("QDateTimeEdit::setCalendarWidget: no date sections specified"); return; } @@ -1864,7 +1864,7 @@ void QDateTimeEditPrivate::clearSection(int index) const QSignalBlocker blocker(edit); QString t = edit->text(); const int pos = sectionPos(index); - if (pos == -1) { + if (Q_UNLIKELY(pos == -1)) { qWarning("QDateTimeEdit: Internal error (%s:%d)", __FILE__, __LINE__); return; } diff --git a/src/widgets/widgets/qdialogbuttonbox.cpp b/src/widgets/widgets/qdialogbuttonbox.cpp index 5b6bfb3b3c..e262f2db22 100644 --- a/src/widgets/widgets/qdialogbuttonbox.cpp +++ b/src/widgets/widgets/qdialogbuttonbox.cpp @@ -405,11 +405,10 @@ QPushButton *QDialogButtonBoxPrivate::createButton(QDialogButtonBox::StandardBut button->setStyle(style); standardButtonHash.insert(button, sbutton); QPlatformDialogHelper::ButtonRole role = QPlatformDialogHelper::buttonRole(static_cast(sbutton)); - if (role != QPlatformDialogHelper::InvalidRole) { - addButton(button, static_cast(role), doLayout); - } else { + if (Q_UNLIKELY(role == QPlatformDialogHelper::InvalidRole)) qWarning("QDialogButtonBox::createButton: Invalid ButtonRole, button not added"); - } + else + addButton(button, static_cast(role), doLayout); #ifdef Q_DEAD_CODE_FROM_QT4_MAC // Since mnemonics is off by default on Mac, we add a Cmd-D @@ -753,7 +752,7 @@ void QDialogButtonBox::removeButton(QAbstractButton *button) void QDialogButtonBox::addButton(QAbstractButton *button, ButtonRole role) { Q_D(QDialogButtonBox); - if (role <= InvalidRole || role >= NRoles) { + if (Q_UNLIKELY(role <= InvalidRole || role >= NRoles)) { qWarning("QDialogButtonBox::addButton: Invalid ButtonRole, button not added"); return; } @@ -772,7 +771,7 @@ void QDialogButtonBox::addButton(QAbstractButton *button, ButtonRole role) QPushButton *QDialogButtonBox::addButton(const QString &text, ButtonRole role) { Q_D(QDialogButtonBox); - if (role <= InvalidRole || role >= NRoles) { + if (Q_UNLIKELY(role <= InvalidRole || role >= NRoles)) { qWarning("QDialogButtonBox::addButton: Invalid ButtonRole, button not added"); return 0; } diff --git a/src/widgets/widgets/qdockarealayout.cpp b/src/widgets/widgets/qdockarealayout.cpp index 6c38043e01..b12aa65dcd 100644 --- a/src/widgets/widgets/qdockarealayout.cpp +++ b/src/widgets/widgets/qdockarealayout.cpp @@ -1802,7 +1802,7 @@ void QDockAreaLayoutInfo::saveState(QDataStream &stream) const stream << (uchar) WidgetMarker; QWidget *w = item.widgetItem->widget(); QString name = w->objectName(); - if (name.isEmpty()) { + if (Q_UNLIKELY(name.isEmpty())) { qWarning("QMainWindow::saveState(): 'objectName' not set for QDockWidget %p '%ls;", w, qUtf16Printable(w->windowTitle())); } @@ -3109,7 +3109,7 @@ void QDockAreaLayout::tabifyDockWidget(QDockWidget *first, QDockWidget *second) void QDockAreaLayout::resizeDocks(const QList &docks, const QList &sizes, Qt::Orientation o) { - if (docks.count() != sizes.count()) { + if (Q_UNLIKELY(docks.count() != sizes.count())) { qWarning("QMainWidget::resizeDocks: size of the lists are not the same"); return; } @@ -3117,12 +3117,12 @@ void QDockAreaLayout::resizeDocks(const QList &docks, fallbackToSizeHints = false; for (int i = 0; i < count; ++i) { QList path = indexOf(docks[i]); - if (path.isEmpty()) { + if (Q_UNLIKELY(path.isEmpty())) { qWarning("QMainWidget::resizeDocks: one QDockWidget is not part of the layout"); continue; } int size = sizes[i]; - if (size <= 0) { + if (Q_UNLIKELY(size <= 0)) { qWarning("QMainWidget::resizeDocks: all sizes need to be larger than 0"); size = 1; } diff --git a/src/widgets/widgets/qlcdnumber.cpp b/src/widgets/widgets/qlcdnumber.cpp index d17e3e2470..b9c209ed3b 100644 --- a/src/widgets/widgets/qlcdnumber.cpp +++ b/src/widgets/widgets/qlcdnumber.cpp @@ -402,12 +402,12 @@ QLCDNumber::~QLCDNumber() void QLCDNumber::setDigitCount(int numDigits) { Q_D(QLCDNumber); - if (numDigits > 99) { + if (Q_UNLIKELY(numDigits > 99)) { qWarning("QLCDNumber::setNumDigits: (%s) Max 99 digits allowed", objectName().toLocal8Bit().constData()); numDigits = 99; } - if (numDigits < 0) { + if (Q_UNLIKELY(numDigits < 0)) { qWarning("QLCDNumber::setNumDigits: (%s) Min 0 digits allowed", objectName().toLocal8Bit().constData()); numDigits = 0; diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp index 6b32665065..b8283a0818 100644 --- a/src/widgets/widgets/qlineedit.cpp +++ b/src/widgets/widgets/qlineedit.cpp @@ -982,7 +982,7 @@ int QLineEdit::selectionStart() const void QLineEdit::setSelection(int start, int length) { Q_D(QLineEdit); - if (start < 0 || start > (int)d->control->end()) { + if (Q_UNLIKELY(start < 0 || start > (int)d->control->end())) { qWarning("QLineEdit::setSelection: Invalid start position (%d)", start); return; } diff --git a/src/widgets/widgets/qmaccocoaviewcontainer_mac.mm b/src/widgets/widgets/qmaccocoaviewcontainer_mac.mm index 7dad6604b9..7bd539033a 100644 --- a/src/widgets/widgets/qmaccocoaviewcontainer_mac.mm +++ b/src/widgets/widgets/qmaccocoaviewcontainer_mac.mm @@ -93,7 +93,7 @@ inline QPlatformNativeInterface::NativeResourceForIntegrationFunction resolvePla QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); QPlatformNativeInterface::NativeResourceForIntegrationFunction function = nativeInterface->nativeResourceFunctionForIntegration(functionName); - if (!function) + if (Q_UNLIKELY(!function)) qWarning() << "Qt could not resolve function" << functionName << "from QGuiApplication::platformNativeInterface()->nativeResourceFunctionForIntegration()"; return function; diff --git a/src/widgets/widgets/qmacnativewidget_mac.mm b/src/widgets/widgets/qmacnativewidget_mac.mm index f855777837..729aa9aba0 100644 --- a/src/widgets/widgets/qmacnativewidget_mac.mm +++ b/src/widgets/widgets/qmacnativewidget_mac.mm @@ -82,7 +82,7 @@ inline QPlatformNativeInterface::NativeResourceForIntegrationFunction resolvePla QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); QPlatformNativeInterface::NativeResourceForIntegrationFunction function = nativeInterface->nativeResourceFunctionForIntegration(functionName); - if (!function) + if (Q_UNLIKELY(!function)) qWarning() << "Qt could not resolve function" << functionName << "from QGuiApplication::platformNativeInterface()->nativeResourceFunctionForIntegration()"; return function; diff --git a/src/widgets/widgets/qmainwindow.cpp b/src/widgets/widgets/qmainwindow.cpp index ff4bb3cc98..2fbd83ef54 100644 --- a/src/widgets/widgets/qmainwindow.cpp +++ b/src/widgets/widgets/qmainwindow.cpp @@ -693,7 +693,7 @@ void QMainWindow::setCorner(Qt::Corner corner, Qt::DockWidgetArea area) valid = (area == Qt::BottomDockWidgetArea || area == Qt::RightDockWidgetArea); break; } - if (!valid) + if (Q_UNLIKELY(!valid)) qWarning("QMainWindow::setCorner(): 'area' is not valid for 'corner'"); else d_func()->layout->setCorner(corner, area); diff --git a/src/widgets/widgets/qmdiarea.cpp b/src/widgets/widgets/qmdiarea.cpp index 1864b42a80..55ae866358 100644 --- a/src/widgets/widgets/qmdiarea.cpp +++ b/src/widgets/widgets/qmdiarea.cpp @@ -179,7 +179,7 @@ using namespace QMdi; // Asserts in debug mode, gives warning otherwise. static bool sanityCheck(const QMdiSubWindow * const child, const char *where) { - if (!child) { + if (Q_UNLIKELY(!child)) { const char error[] = "null pointer"; Q_ASSERT_X(false, where, error); qWarning("%s:%s", where, error); @@ -190,13 +190,13 @@ static bool sanityCheck(const QMdiSubWindow * const child, const char *where) static bool sanityCheck(const QList &widgets, const int index, const char *where) { - if (index < 0 || index >= widgets.size()) { + if (Q_UNLIKELY(index < 0 || index >= widgets.size())) { const char error[] = "index out of range"; Q_ASSERT_X(false, where, error); qWarning("%s:%s", where, error); return false; } - if (!widgets.at(index)) { + if (Q_UNLIKELY(!widgets.at(index))) { const char error[] = "null pointer"; Q_ASSERT_X(false, where, error); qWarning("%s:%s", where, error); @@ -1831,12 +1831,12 @@ void QMdiArea::setActiveSubWindow(QMdiSubWindow *window) return; } - if (d->childWindows.isEmpty()) { + if (Q_UNLIKELY(d->childWindows.isEmpty())) { qWarning("QMdiArea::setActiveSubWindow: workspace is empty"); return; } - if (d->childWindows.indexOf(window) == -1) { + if (Q_UNLIKELY(d->childWindows.indexOf(window) == -1)) { qWarning("QMdiArea::setActiveSubWindow: window is not inside workspace"); return; } @@ -1960,7 +1960,7 @@ void QMdiArea::activatePreviousSubWindow() */ QMdiSubWindow *QMdiArea::addSubWindow(QWidget *widget, Qt::WindowFlags windowFlags) { - if (!widget) { + if (Q_UNLIKELY(!widget)) { qWarning("QMdiArea::addSubWindow: null pointer to widget"); return 0; } @@ -1972,7 +1972,7 @@ QMdiSubWindow *QMdiArea::addSubWindow(QWidget *widget, Qt::WindowFlags windowFla // Widget is already a QMdiSubWindow if (child) { - if (d->childWindows.indexOf(child) != -1) { + if (Q_UNLIKELY(d->childWindows.indexOf(child) != -1)) { qWarning("QMdiArea::addSubWindow: window is already added"); return child; } @@ -2003,7 +2003,7 @@ QMdiSubWindow *QMdiArea::addSubWindow(QWidget *widget, Qt::WindowFlags windowFla */ void QMdiArea::removeSubWindow(QWidget *widget) { - if (!widget) { + if (Q_UNLIKELY(!widget)) { qWarning("QMdiArea::removeSubWindow: null pointer to widget"); return; } @@ -2014,7 +2014,7 @@ void QMdiArea::removeSubWindow(QWidget *widget) if (QMdiSubWindow *child = qobject_cast(widget)) { int index = d->childWindows.indexOf(child); - if (index == -1) { + if (Q_UNLIKELY(index == -1)) { qWarning("QMdiArea::removeSubWindow: window is not inside workspace"); return; } @@ -2038,7 +2038,7 @@ void QMdiArea::removeSubWindow(QWidget *widget) } } - if (!found) + if (Q_UNLIKELY(!found)) qWarning("QMdiArea::removeSubWindow: widget is not child of any window inside QMdiArea"); } diff --git a/src/widgets/widgets/qmdisubwindow.cpp b/src/widgets/widgets/qmdisubwindow.cpp index be6cb7c54c..0f38eee6cd 100644 --- a/src/widgets/widgets/qmdisubwindow.cpp +++ b/src/widgets/widgets/qmdisubwindow.cpp @@ -2309,7 +2309,7 @@ void QMdiSubWindow::setWidget(QWidget *widget) return; } - if (widget == d->baseWidget) { + if (Q_UNLIKELY(widget == d->baseWidget)) { qWarning("QMdiSubWindow::setWidget: widget is already set"); return; } @@ -2507,7 +2507,7 @@ void QMdiSubWindow::setKeyboardPageStep(int step) void QMdiSubWindow::setSystemMenu(QMenu *systemMenu) { Q_D(QMdiSubWindow); - if (systemMenu && systemMenu == d->systemMenu) { + if (Q_UNLIKELY(systemMenu && systemMenu == d->systemMenu)) { qWarning("QMdiSubWindow::setSystemMenu: system menu is already set"); return; } diff --git a/src/widgets/widgets/qmenu_mac.mm b/src/widgets/widgets/qmenu_mac.mm index 9278099e18..58dee37bd9 100644 --- a/src/widgets/widgets/qmenu_mac.mm +++ b/src/widgets/widgets/qmenu_mac.mm @@ -55,7 +55,7 @@ inline QPlatformNativeInterface::NativeResourceForIntegrationFunction resolvePla QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); QPlatformNativeInterface::NativeResourceForIntegrationFunction function = nativeInterface->nativeResourceFunctionForIntegration(functionName); - if (!function) + if (Q_UNLIKELY(!function)) qWarning() << "Qt could not resolve function" << functionName << "from QGuiApplication::platformNativeInterface()->nativeResourceFunctionForIntegration()"; return function; diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 91788a3383..5e4e348e3b 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -1291,7 +1291,7 @@ void QPlainTextEdit::setDocument(QTextDocument *document) document->setDocumentLayout(documentLayout); } else { documentLayout = qobject_cast(document->documentLayout()); - if (!documentLayout) { + if (Q_UNLIKELY(!documentLayout)) { qWarning("QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout"); return; } diff --git a/src/widgets/widgets/qspinbox.cpp b/src/widgets/widgets/qspinbox.cpp index 457e2e1e4c..13076ad870 100644 --- a/src/widgets/widgets/qspinbox.cpp +++ b/src/widgets/widgets/qspinbox.cpp @@ -438,7 +438,7 @@ void QSpinBox::setDisplayIntegerBase(int base) { Q_D(QSpinBox); // Falls back to base 10 on invalid bases (like QString) - if (base < 2 || base > 36) { + if (Q_UNLIKELY(base < 2 || base > 36)) { qWarning("QSpinBox::setDisplayIntegerBase: Invalid base (%d)", base); base = 10; } diff --git a/src/widgets/widgets/qsplitter.cpp b/src/widgets/widgets/qsplitter.cpp index c2081c15f8..9162ba3c12 100644 --- a/src/widgets/widgets/qsplitter.cpp +++ b/src/widgets/widgets/qsplitter.cpp @@ -1058,7 +1058,7 @@ void QSplitter::setCollapsible(int index, bool collapse) { Q_D(QSplitter); - if (index < 0 || index >= d->list.size()) { + if (Q_UNLIKELY(index < 0 || index >= d->list.size())) { qWarning("QSplitter::setCollapsible: Index %d out of range", index); return; } @@ -1071,7 +1071,7 @@ void QSplitter::setCollapsible(int index, bool collapse) bool QSplitter::isCollapsible(int index) const { Q_D(const QSplitter); - if (index < 0 || index >= d->list.size()) { + if (Q_UNLIKELY(index < 0 || index >= d->list.size())) { qWarning("QSplitter::isCollapsible: Index %d out of range", index); return false; } @@ -1215,7 +1215,7 @@ void QSplitter::childEvent(QChildEvent *c) { Q_D(QSplitter); if (!c->child()->isWidgetType()) { - if (c->type() == QEvent::ChildAdded && qobject_cast(c->child())) + if (Q_UNLIKELY(c->type() == QEvent::ChildAdded && qobject_cast(c->child()))) qWarning("Adding a QLayout to a QSplitter is not supported."); return; } diff --git a/src/widgets/widgets/qstackedwidget.cpp b/src/widgets/widgets/qstackedwidget.cpp index 19a2edf0f2..8a2eff4e39 100644 --- a/src/widgets/widgets/qstackedwidget.cpp +++ b/src/widgets/widgets/qstackedwidget.cpp @@ -237,7 +237,7 @@ QWidget *QStackedWidget::currentWidget() const void QStackedWidget::setCurrentWidget(QWidget *widget) { Q_D(QStackedWidget); - if (d->layout->indexOf(widget) == -1) { + if (Q_UNLIKELY(d->layout->indexOf(widget) == -1)) { qWarning("QStackedWidget::setCurrentWidget: widget %p not contained in stack", widget); return; } diff --git a/src/widgets/widgets/qstatusbar.cpp b/src/widgets/widgets/qstatusbar.cpp index 19361fc793..2e8ca8510b 100644 --- a/src/widgets/widgets/qstatusbar.cpp +++ b/src/widgets/widgets/qstatusbar.cpp @@ -296,7 +296,7 @@ int QStatusBar::insertWidget(int index, QWidget *widget, int stretch) QStatusBarPrivate::SBItem* item = new QStatusBarPrivate::SBItem(widget, stretch, false); int idx = d->indexToLastNonPermanentWidget(); - if (index < 0 || index > d->items.size() || (idx >= 0 && index > idx + 1)) { + if (Q_UNLIKELY(index < 0 || index > d->items.size() || (idx >= 0 && index > idx + 1))) { qWarning("QStatusBar::insertWidget: Index out of range (%d), appending widget", index); index = idx + 1; } @@ -361,7 +361,7 @@ int QStatusBar::insertPermanentWidget(int index, QWidget *widget, int stretch) QStatusBarPrivate::SBItem* item = new QStatusBarPrivate::SBItem(widget, stretch, true); int idx = d->indexToLastNonPermanentWidget(); - if (index < 0 || index > d->items.size() || (idx >= 0 && index <= idx)) { + if (Q_UNLIKELY(index < 0 || index > d->items.size() || (idx >= 0 && index <= idx))) { qWarning("QStatusBar::insertPermanentWidget: Index out of range (%d), appending widget", index); index = d->items.size(); } diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index 2c073342b0..7db9a74afd 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -292,7 +292,7 @@ void QTextBrowserPrivate::setSource(const QUrl &url) txt = data.toString(); #endif } - if (txt.isEmpty()) + if (Q_UNLIKELY(txt.isEmpty())) qWarning("QTextBrowser: No document for %s", url.toString().toLatin1().constData()); if (q->isVisible()) { diff --git a/src/widgets/widgets/qtoolbararealayout.cpp b/src/widgets/widgets/qtoolbararealayout.cpp index 89e3da1383..dc39221b60 100644 --- a/src/widgets/widgets/qtoolbararealayout.cpp +++ b/src/widgets/widgets/qtoolbararealayout.cpp @@ -1128,7 +1128,7 @@ QRect QToolBarAreaLayout::itemRect(const QList &path) const QLayoutItem *QToolBarAreaLayout::plug(const QList &path) { QToolBarAreaLayoutItem *item = this->item(path); - if (!item) { + if (Q_UNLIKELY(!item)) { qWarning() << Q_FUNC_INFO << "No item at" << path; return 0; } @@ -1260,7 +1260,7 @@ void QToolBarAreaLayout::saveState(QDataStream &stream) const const QToolBarAreaLayoutItem &item = line.toolBarItems.at(k); QWidget *widget = const_cast(item.widgetItem)->widget(); QString objectName = widget->objectName(); - if (objectName.isEmpty()) { + if (Q_UNLIKELY(objectName.isEmpty())) { qWarning("QMainWindow::saveState(): 'objectName' not set for QToolBar %p '%s'", widget, widget->windowTitle().toLocal8Bit().constData()); } diff --git a/src/widgets/widgets/qtoolbox.cpp b/src/widgets/widgets/qtoolbox.cpp index beb70f1283..a37747e138 100644 --- a/src/widgets/widgets/qtoolbox.cpp +++ b/src/widgets/widgets/qtoolbox.cpp @@ -521,10 +521,10 @@ QWidget * QToolBox::currentWidget() const void QToolBox::setCurrentWidget(QWidget *widget) { int i = indexOf(widget); - if (i >= 0) - setCurrentIndex(i); - else + if (Q_UNLIKELY(i < 0)) qWarning("QToolBox::setCurrentWidget: widget not contained in tool box"); + else + setCurrentIndex(i); } /*! diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index 436937be72..6fbe0f9da4 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -309,7 +309,7 @@ void QWidgetLineControl::setSelection(int start, int length) { commitPreedit(); - if(start < 0 || start > (int)m_text.length()){ + if (Q_UNLIKELY(start < 0 || start > m_text.size())) { qWarning("QWidgetLineControl::setSelection: Invalid start position"); return; } -- cgit v1.2.3