Skip to content

Commit 3a6c8e0

Browse files
committed
Long live QT_ENABLE_STRICT_MODE_UP_TO
We already have fine-grained macros to individually disable APIs that we consider "suboptimal" or "dangerous". This commit adds a shortcut for the user to set all such macros in one go. QT_ENABLE_STRICT_MODE_UP_TO is versioned, just like QT_DISABLE_DEPRECATED_UP_TO; the idea is that users should set it to the minimum Qt version they want to support. Also, if QT_DISABLE_DEPRECATED_UP_TO is not set, then QT_ENABLE_STRICT_MODE_UP_TO will set it as well, to the same value. [ChangeLog][QtCore][QtGlobal] Added the QT_ENABLE_STRICT_MODE_UP_TO macro. Change-Id: I5466465986104e047a6a86369928be9294f24ab7 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
1 parent 03baf08 commit 3a6c8e0

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/corelib/doc/src/foreach-keyword.qdoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,15 @@
7373
\c foreach would not. But using \c foreach always copies the container,
7474
which is usually not cheap for STL containers. If in doubt, prefer
7575
\c foreach for Qt containers, and range based \c for for STL ones.
76+
77+
You can remove the availability of the Qt's \c foreach loop by
78+
defining the \c{QT_NO_FOREACH} macro.
79+
*/
80+
81+
/*!
82+
\macro QT_NO_FOREACH
83+
\since 6.0
84+
85+
Defining this macro removes the availability of Qt's \c foreach
86+
loop.
7687
*/

src/corelib/global/qglobal.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,51 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
310310
qMove takes an rvalue reference to its parameter \a x, and converts it to an xvalue.
311311
*/
312312

313+
/*!
314+
\macro QT_ENABLE_STRICT_MODE_UP_TO
315+
\relates <QtGlobal>
316+
\since 6.8
317+
318+
Defining this macro will disable a number of Qt APIs that are
319+
deemed suboptimal or dangerous.
320+
321+
This macro's value must be set to a Qt version, using
322+
\l{QT_VERSION_CHECK}'s encoding. For instance, in order to set it
323+
to Qt 6.6, define \c{QT_ENABLE_STRICT_MODE_UP_TO=0x060600}.
324+
This will disable only the APIs introduced in versions up to (and
325+
including) the specified Qt version.
326+
327+
If the \l QT_DISABLE_DEPRECATED_UP_TO macro is \e not defined,
328+
then QT_ENABLE_STRICT_MODE_UP_TO will define it as well,
329+
to the same value.
330+
331+
This macro should always be set to the minimum Qt version that
332+
your project wants to support.
333+
334+
The APIs disabled by this macro are listed in the table below,
335+
together with the minimum value to use in order to disable them.
336+
337+
\table
338+
\header \li Version \li Disabled APIs
339+
\row \li 6.0.0 \li \l{foreach-keyword}{foreach} (see \l{QT_NO_FOREACH})
340+
\row \li 6.0.0 \li QString constructors from \c{const char *} (see \l{QT_NO_CAST_FROM_ASCII})
341+
\row \li 6.0.0 \li QString conversions towards \c{const char *} / QByteArray (see \l{QT_NO_CAST_TO_ASCII})
342+
\row \li 6.0.0 \li QByteArray implicit conversions towards \c{const char *} (see \l{QT_NO_CAST_FROM_BYTEARRAY})
343+
\row \li 6.0.0 \li QUrl implicit conversions from QString (see \l{QT_NO_URL_CAST_FROM_STRING})
344+
\row \li 6.0.0 \li Allowing narrowing conversions in signal-slot connections (see \l{QT_NO_NARROWING_CONVERSIONS_IN_CONNECT})
345+
\row \li 6.0.0 \li Java-style iterators for Qt containers
346+
\row \li 6.6.0 \li The qExchange() function (see \l{QT_NO_QEXCHANGE})
347+
\row \li 6.7.0 \li Overloads of QObject::connect that do not take a context object (see \l{QT_NO_CONTEXTLESS_CONNECT})
348+
\row \li 6.8.0 \li The qAsConst() function (see \l{QT_NO_QASCONST})
349+
\endtable
350+
351+
Moreover, individual APIs may also get disabled as part of the
352+
setting of QT_DISABLE_DEPRECATED_UP_TO. Please refer to each class'
353+
documentation for more details.
354+
355+
\sa QT_DISABLE_DEPRECATED_UP_TO, QT_NO_KEYWORDS, QT_VERSION_CHECK
356+
*/
357+
313358
namespace QtPrivate {
314359
Q_LOGGING_CATEGORY(lcNativeInterface, "qt.nativeinterface")
315360
}

src/corelib/global/qtconfigmacros.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,35 @@ namespace QT_NAMESPACE {}
155155
# define QT_END_MOC_NAMESPACE
156156
#endif
157157

158+
/*
159+
Strict mode
160+
*/
161+
#ifdef QT_ENABLE_STRICT_MODE_UP_TO
162+
#ifndef QT_DISABLE_DEPRECATED_UP_TO
163+
# define QT_DISABLE_DEPRECATED_UP_TO QT_ENABLE_STRICT_MODE_UP_TO
164+
#endif
165+
166+
#if QT_ENABLE_STRICT_MODE_UP_TO >= QT_VERSION_CHECK(6, 0, 0)
167+
# define QT_NO_FOREACH
168+
# define QT_NO_CAST_FROM_ASCII
169+
# define QT_NO_CAST_TO_ASCII
170+
# define QT_NO_CAST_FROM_BYTEARRAY
171+
# define QT_NO_URL_CAST_FROM_STRING
172+
# define QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
173+
# define QT_NO_JAVA_STYLE_ITERATORS
174+
#endif // 6.0.0
175+
176+
#if QT_ENABLE_STRICT_MODE_UP_TO >= QT_VERSION_CHECK(6, 6, 0)
177+
# define QT_NO_QEXCHANGE
178+
#endif // 6.6.0
179+
180+
#if QT_ENABLE_STRICT_MODE_UP_TO >= QT_VERSION_CHECK(6, 7, 0)
181+
# define QT_NO_CONTEXTLESS_CONNECT
182+
#endif // 6.7.0
183+
184+
#if QT_ENABLE_STRICT_MODE_UP_TO >= QT_VERSION_CHECK(6, 8, 0)
185+
# define QT_NO_QASCONST
186+
#endif // 6.8.0
187+
#endif // QT_ENABLE_STRICT_MODE_UP_TO
188+
158189
#endif /* QTCONFIGMACROS_H */

0 commit comments

Comments
 (0)