diff options
author | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-10-11 14:27:17 +0200 |
---|---|---|
committer | Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> | 2021-10-16 14:41:37 +0200 |
commit | 826e076ef53a5809dee7d887b16ed961895049ea (patch) | |
tree | bc1f943014038dc481dea68884d6be2c32b6feb0 | |
parent | 95dfef4481c48ca457dec91c166291c1c022c95b (diff) |
Add suffixes to loop variables for Q_FOREACH
Currently using nested Q_FOREACH loops produces warnings about
shadowing local variables (if enabled). For example, this code:
QList<int> aList;
QList<int> bList;
foreach (int a, aList) {
Q_UNUSED(a);
foreach (int b, bList)
Q_UNUSED(b);
}
produces the following warning with MSVC:
warning C4456: declaration of '_container_' hides previous local
declaration
This is due to using variable _container_ as loop variable for both
loops.
This patch appends current line number to the name of the loop
variables so that most of the uses of Q_FOREACH will not result
in shadowing.
The patch originally by Benoit Regrain.
Fixes: QTBUG-79081
Change-Id: I55235e917e920171b138c8b3239a95dfe2ac7988
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(adapted from commit 9940e4812ba3448a997cb8fb1b08187a84c797f2)
-rw-r--r-- | src/corelib/global/qglobal.h | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index a2fe723bd8..66aeb449ad 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1102,13 +1102,16 @@ QForeachContainer<typename std::decay<T>::type> qMakeForeachContainer(T &&t) } +#define Q_FOREACH_JOIN(A, B) Q_FOREACH_JOIN_IMPL(A, B) +#define Q_FOREACH_JOIN_IMPL(A, B) A ## B + #if __cplusplus >= 201703L // Use C++17 if statement with initializer. User's code ends up in a else so // scoping of different ifs is not broken -#define Q_FOREACH(variable, container) \ -for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \ - _container_.i != _container_.e; ++_container_.i) \ - if (variable = *_container_.i; false) {} else +#define Q_FOREACH_IMPL(variable, name, container) \ + for (auto name = QtPrivate::qMakeForeachContainer(container); \ + name.i != name.e; ++name.i) \ + if (variable = *name.i; false) {} else #else // Explanation of the control word: // - it's initialized to 1 @@ -1119,12 +1122,15 @@ for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \ // the outer loop to continue executing // - if there was a break inside the inner loop, it will exit with control still // set to 1; in that case, the outer loop will invert it to 0 and will exit too -#define Q_FOREACH(variable, container) \ -for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \ - _container_.control && _container_.i != _container_.e; \ - ++_container_.i, _container_.control ^= 1) \ - for (variable = *_container_.i; _container_.control; _container_.control = 0) +#define Q_FOREACH_IMPL(variable, name, container) \ +for (auto name = QtPrivate::qMakeForeachContainer(container); \ + name.control && name.i != name.e; \ + ++name.i, name.control ^= 1) \ + for (variable = *name.i; name.control; name.control = 0) #endif + +#define Q_FOREACH(variable, container) \ + Q_FOREACH_IMPL(variable, Q_FOREACH_JOIN(_container_, __LINE__), container) #endif // QT_NO_FOREACH #define Q_FOREVER for(;;) |