foreach: more const iteration fixes.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 03:35:33 +0000 (14:05 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 03:35:33 +0000 (14:05 +1030)
Shown up by -Wwrite-strings.

ccan/foreach/foreach.h
ccan/foreach/test/compile_ok-const.c [new file with mode: 0644]
ccan/foreach/test/compile_ok-nonconst.c [new file with mode: 0644]

index 73c4c693ebf7f58297b092f943716d07701f809b..8e08548f5e33874715ad6bcd65f4b9ab40169fd8 100644 (file)
@@ -44,7 +44,7 @@
  */
 #define foreach_ptr(i, ...)                                    \
        for (unsigned _foreach_i                                \
-                    = (((i) = ((const void *[]){ __VA_ARGS__ })[0]), 0); \
+            = (((i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0]), 0); \
             (i);                                                       \
             (i) = (void *)((FOREACH_TYPEOF(i)[])                       \
                     { __VA_ARGS__, NULL})[++_foreach_i],               \
             (i) = (int[]) { __VA_ARGS__, 0 }[_foreach_iter_inc(&(i))])
 
 #define foreach_ptr(i, ...)                                            \
-       for ((i) = ((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0],           \
+       for ((i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__ })[0],   \
                     _foreach_iter_init(&(i));                          \
             (i);                                                       \
             (i) = (void *)((FOREACH_TYPEOF(i)[]){ __VA_ARGS__, NULL }) \
                     [_foreach_iter_inc(&(i))],                         \
                 _foreach_no_nullval(_foreach_iter(&(i)), i,            \
-                                    ((void *[]){ __VA_ARGS__})))
+                                    ((const void *[]){ __VA_ARGS__})))
 
 void _foreach_iter_init(const void *i);
 unsigned int _foreach_iter(const void *i);
diff --git a/ccan/foreach/test/compile_ok-const.c b/ccan/foreach/test/compile_ok-const.c
new file mode 100644 (file)
index 0000000..d1c858a
--- /dev/null
@@ -0,0 +1,14 @@
+#include <ccan/foreach/foreach.h>
+#include <ccan/foreach/foreach.c>
+
+/* Iterating over const pointers should work fine. */
+int main(int argc, char *argv[])
+{
+       const char *s1 = "hello", *s2 = "world", *p;
+       unsigned int i = 0;
+
+       foreach_ptr(p, s1, s2)
+               i++;
+
+       return i == 2 ? 0 : 1;
+}
diff --git a/ccan/foreach/test/compile_ok-nonconst.c b/ccan/foreach/test/compile_ok-nonconst.c
new file mode 100644 (file)
index 0000000..e533a9d
--- /dev/null
@@ -0,0 +1,14 @@
+#include <ccan/foreach/foreach.h>
+#include <ccan/foreach/foreach.c>
+
+/* Iterating const over non-const pointers should work fine. */
+int main(int argc, char *argv[])
+{
+       char *p;
+       unsigned int i = 0;
+
+       foreach_ptr(p, argv[0], argv[1])
+               i++;
+
+       return i == 2 ? 0 : 1;
+}