It was not clear whether hwloc_cpuset_foreach_end() needs a terminating ';'
or not. And hwloc itself had both cases.

This converts hwloc_cpuset_foreach_{begin,end}() to use the do-while-(0)
idiom to enforce a terminating semi-colon.

Note: it is still possible to break and continue this loop.

Regards,
Bert

---
 include/hwloc/cpuset.h |    9 ++++++---
 src/topology-osf.c     |    2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/include/hwloc/cpuset.h b/include/hwloc/cpuset.h
index bacaf92..ad2dab4 100644
--- a/include/hwloc/cpuset.h
+++ b/include/hwloc/cpuset.h
@@ -187,13 +187,16 @@ HWLOC_DECLSPEC int 
hwloc_cpuset_weight(hwloc_const_cpuset_t set) __hwloc_attribu
  * (the cpu set) and \p cpu (the loop variable)
  */
 #define hwloc_cpuset_foreach_begin(cpu, set) \
-        for (cpu = 0; cpu < HWLOC_NBMAXCPUS; cpu++) \
+do { \
+        for (cpu = 0; cpu < HWLOC_NBMAXCPUS; cpu++) { \
                 if (hwloc_cpuset_isset(set, cpu)) {
-/** \brief End of loop
+/** \brief End of loop. Needs a terminating ';'.
  *
  * \sa hwloc_cpuset_foreach_begin */
 #define hwloc_cpuset_foreach_end() \
-                }
+                } \
+        } \
+} while (0)

 /** @} */

diff --git a/src/topology-osf.c b/src/topology-osf.c
index 750cb28..711107b 100644
--- a/src/topology-osf.c
+++ b/src/topology-osf.c
@@ -37,7 +37,7 @@ prepare_radset(hwloc_topology_t topology, radset_t *radset, 
hwloc_const_cpuset_t
   cpuemptyset(target_cpuset);
   hwloc_cpuset_foreach_begin(cpu, hwloc_set)
     cpuaddset(target_cpuset, cpu);
-  hwloc_cpuset_foreach_end()
+  hwloc_cpuset_foreach_end();

   cpusetcreate(&cpuset);
   cpusetcreate(&xor_cpuset);
-- 
tg: (e66476e..) bw/semi-colon-safe-foreach (depends on: master)

Reply via email to