From 892448b6e027236228b2c6429a6453c038b5c403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20K=C3=B6hne?= Date: Tue, 11 Jul 2023 16:14:07 +0200 Subject: [PATCH] Make Qt headers work with MSVC /W4 This requires explicitly marking constexpr if conditions to fix C4127 issues: qtbase/src/corelib/text/qstringbuilder.h(112): error C2220: the following warning is treated as an error qtbase/src/corelib/text/qstringbuilder.h(112): warning C4127: conditional expression is constant qtbase/src/corelib/text/qstringbuilder.h(112): note: consider using 'if constexpr' statement instead Change-Id: I9787fb37099f811c52f93c94c9edb4da8aafdfe5 Reviewed-by: Thiago Macieira --- cmake/QtHeadersClean.cmake | 2 +- src/corelib/kernel/qiterable.h | 36 +++++++++++++++++-------------- src/corelib/text/qstringbuilder.h | 11 +++++++--- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/cmake/QtHeadersClean.cmake b/cmake/QtHeadersClean.cmake index 955f325e3f..5f1cc9fd0c 100644 --- a/cmake/QtHeadersClean.cmake +++ b/cmake/QtHeadersClean.cmake @@ -186,7 +186,7 @@ function(qt_internal_add_headersclean_target module_target module_headers) # Note we can't enable -Za, as it does not support certain key Microsoft SDK header files # we use. Microsoft suggests to use /permissive- instead, which is implicity set by # -std:c++latest. - set(hcleanFLAGS -std:c++latest -Zc:__cplusplus -WX -W3 -EHsc) + set(hcleanFLAGS -std:c++latest -Zc:__cplusplus -WX -W4 -EHsc) # Because we now add `-DNOMINMAX` to `PlatformCommonInternal`. set(hcleanUDEFS -UNOMINMAX) diff --git a/src/corelib/kernel/qiterable.h b/src/corelib/kernel/qiterable.h index 1178c5e8a3..4adcdfd76f 100644 --- a/src/corelib/kernel/qiterable.h +++ b/src/corelib/kernel/qiterable.h @@ -71,28 +71,32 @@ public: QTaggedIterator(Iterator &&it) : Iterator(std::move(it)) { const QMetaContainer metaContainer = this->metaContainer(); - if (std::is_base_of_v - && !metaContainer.hasRandomAccessIterator()) { - qFatal("You cannot use this iterator as a random access iterator"); - this->clearIterator(); + if constexpr (std::is_base_of_v) { + if (!metaContainer.hasRandomAccessIterator()) { + qFatal("You cannot use this iterator as a random access iterator"); + this->clearIterator(); + } } - if (std::is_base_of_v - && !metaContainer.hasBidirectionalIterator()) { - qFatal("You cannot use this iterator as a bidirectional iterator"); - this->clearIterator(); + if constexpr (std::is_base_of_v) { + if (!metaContainer.hasBidirectionalIterator()) { + qFatal("You cannot use this iterator as a bidirectional iterator"); + this->clearIterator(); + } } - if (std::is_base_of_v - && !metaContainer.hasForwardIterator()) { - qFatal("You cannot use this iterator as a forward iterator"); - this->clearIterator(); + if constexpr (std::is_base_of_v) { + if (!metaContainer.hasForwardIterator()) { + qFatal("You cannot use this iterator as a forward iterator"); + this->clearIterator(); + } } - if (std::is_base_of_v - && !metaContainer.hasInputIterator()) { - qFatal("You cannot use this iterator as an input iterator"); - this->clearIterator(); + if constexpr (std::is_base_of_v) { + if (!metaContainer.hasInputIterator()) { + qFatal("You cannot use this iterator as an input iterator"); + this->clearIterator(); + } } } diff --git a/src/corelib/text/qstringbuilder.h b/src/corelib/text/qstringbuilder.h index fb44a56633..53f8865edf 100644 --- a/src/corelib/text/qstringbuilder.h +++ b/src/corelib/text/qstringbuilder.h @@ -106,10 +106,15 @@ private: // we abuse const_cast / constData here because we know we've just // allocated the data and we're the only reference count typename T::iterator d = const_cast(s.constData()); - typename T::const_iterator const start = d; - QConcatenable< QStringBuilder >::appendTo(*this, d); - if (!QConcatenable< QStringBuilder >::ExactSize && len != d - start) { + if constexpr (QConcatenable>::ExactSize) { + QConcatenable>::appendTo(*this, d); + return s; + } + + typename T::const_iterator const start = d; + QConcatenable>::appendTo(*this, d); + if (len != d - start) { // this resize is necessary since we allocate a bit too much // when dealing with variable sized 8-bit encodings s.resize(d - start);