Add QT_NO_FOREACH to disable foreach and Q_FOREACH

It has been known for a long time that Q_FOREACH produces
inferior code to other looping constructs, and the use of it
in Qt library code was informally frowned upon since forever
(pun intended).

Yet, to this day, several thousand foreach/Q_FOREACH loops
have been added to Qt libraries, and while many were ported
to range-for in Qt 5.7, there are still new ones added every
day, which is a nuisance, to say the least.

This patch introduces a technical way to prevent new foreach
use to creep into Qt libraries after they have been cleaned,
by simply not defining either Q_FOREACH or foreach when the
QT_NO_FOREACH macro is defined. This way, one library at a
time can be ported away, and, once ported, is guaranteed to
actually stay ported.

Change-Id: Ie042e84d6c7d766bd16095f9bc1118a8e0ce0c7a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2016-05-10 10:49:11 +02:00
parent e64b2234e8
commit 81793b8b58

View File

@ -932,6 +932,8 @@ QT_WARNING_DISABLE_MSVC(4530) /* C++ exception handler used, but unwind semantic
# endif # endif
#endif #endif
#ifndef QT_NO_FOREACH
template <typename T> template <typename T>
class QForeachContainer { class QForeachContainer {
QForeachContainer &operator=(const QForeachContainer &) Q_DECL_EQ_DELETE; QForeachContainer &operator=(const QForeachContainer &) Q_DECL_EQ_DELETE;
@ -957,11 +959,15 @@ for (QForeachContainer<typename QtPrivate::remove_reference<decltype(container)>
++_container_.i, _container_.control ^= 1) \ ++_container_.i, _container_.control ^= 1) \
for (variable = *_container_.i; _container_.control; _container_.control = 0) for (variable = *_container_.i; _container_.control; _container_.control = 0)
#endif // QT_NO_FOREACH
#define Q_FOREVER for(;;) #define Q_FOREVER for(;;)
#ifndef QT_NO_KEYWORDS #ifndef QT_NO_KEYWORDS
# ifndef QT_NO_FOREACH
# ifndef foreach # ifndef foreach
# define foreach Q_FOREACH # define foreach Q_FOREACH
# endif # endif
# endif // QT_NO_FOREACH
# ifndef forever # ifndef forever
# define forever Q_FOREVER # define forever Q_FOREVER
# endif # endif