cast: fix cast of void * when we don't have GCC features.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 01:23:17 +0000 (11:53 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 22 Mar 2011 01:23:17 +0000 (11:53 +1030)
I thought using sizeof() comparison to compare the types was clever,
but it doesn't work on void pointers, as sizeof(void) is illegal.

ccan/cast/cast.h
ccan/cast/test/compile_fail-cast_const-sizesame.c [deleted file]
ccan/cast/test/compile_fail-cast_const.c
ccan/cast/test/compile_fail-cast_const2-sizesame.c [deleted file]
ccan/cast/test/compile_fail-cast_const2.c
ccan/cast/test/compile_fail-cast_const3-sizesame.c [deleted file]
ccan/cast/test/compile_fail-cast_const3.c
ccan/cast/test/compile_fail-cast_signed-const.c
ccan/cast/test/compile_ok-cast_void.c [new file with mode: 0644]

index dfb95b59772611bf53582b4b4ef132970104a829..daebd8572391f07dfb17ae20c341c114c2507c3d 100644 (file)
 #else
 #define cast_sign_compatible(type, expr)               \
        (sizeof(*(type)0) == 1 && sizeof(*(expr)) == 1)
-#define cast_const_compat1(expr, type)         \
-       (sizeof(*(expr)) == sizeof(*(type)0))
-#define cast_const_compat2(expr, type)         \
-       (sizeof(**(expr)) == sizeof(**(type)0))
-#define cast_const_compat3(expr, type)                 \
-       (sizeof(***(expr)) == sizeof(***(type)0))
+#define cast_const_compat1(expr, type)         (1)
+#define cast_const_compat2(expr, type)         (1)
+#define cast_const_compat3(expr, type)         (1)
 #endif
 #endif /* CCAN_CAST_H */
diff --git a/ccan/cast/test/compile_fail-cast_const-sizesame.c b/ccan/cast/test/compile_fail-cast_const-sizesame.c
deleted file mode 100644 (file)
index d401cc8..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <ccan/cast/cast.h>
-#include <stdlib.h>
-
-/* Note: this *isn't* sizeof(char) on all platforms. */
-struct char_struct {
-       char c;
-};
-
-int main(int argc, char *argv[])
-{
-       char *uc;
-       const
-#ifdef FAIL
-               struct char_struct
-#else
-               char
-#endif
-               *p = NULL;
-
-       uc = cast_const(char *, p);
-       return 0;
-}
-
-#ifdef FAIL
-#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
-#error "Unfortunately we don't fail if cast_const can only use size"
-#endif
-#endif
index bb8de94f4c2212e0a5e1a27d41b0d2cb0d01017f..d401cc8ca91c488c159fb8f0d0bdb63bb0fd9df4 100644 (file)
@@ -1,12 +1,17 @@
 #include <ccan/cast/cast.h>
 #include <stdlib.h>
 
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+       char c;
+};
+
 int main(int argc, char *argv[])
 {
        char *uc;
-       const 
+       const
 #ifdef FAIL
-               int
+               struct char_struct
 #else
                char
 #endif
@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
        uc = cast_const(char *, p);
        return 0;
 }
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/ccan/cast/test/compile_fail-cast_const2-sizesame.c b/ccan/cast/test/compile_fail-cast_const2-sizesame.c
deleted file mode 100644 (file)
index a16cfac..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <ccan/cast/cast.h>
-#include <stdlib.h>
-
-/* Note: this *isn't* sizeof(char) on all platforms. */
-struct char_struct {
-       char c;
-};
-
-int main(int argc, char *argv[])
-{
-       char **uc;
-       const
-#ifdef FAIL
-               struct char_struct
-#else
-               char
-#endif
-               **p = NULL;
-
-       uc = cast_const2(char **, p);
-       return 0;
-}
-
-#ifdef FAIL
-#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
-#error "Unfortunately we don't fail if cast_const can only use size"
-#endif
-#endif
index 88df4b7a853168141dd3e07fb2bf045fe41c9874..a16cfacf15354c468189d50a4a87d6550da30442 100644 (file)
@@ -1,12 +1,17 @@
 #include <ccan/cast/cast.h>
 #include <stdlib.h>
 
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+       char c;
+};
+
 int main(int argc, char *argv[])
 {
        char **uc;
-       const 
+       const
 #ifdef FAIL
-               int
+               struct char_struct
 #else
                char
 #endif
@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
        uc = cast_const2(char **, p);
        return 0;
 }
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/ccan/cast/test/compile_fail-cast_const3-sizesame.c b/ccan/cast/test/compile_fail-cast_const3-sizesame.c
deleted file mode 100644 (file)
index f782f9d..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <ccan/cast/cast.h>
-#include <stdlib.h>
-
-/* Note: this *isn't* sizeof(char) on all platforms. */
-struct char_struct {
-       char c;
-};
-
-int main(int argc, char *argv[])
-{
-       char ***uc;
-       const
-#ifdef FAIL
-               struct char_struct
-#else
-               char
-#endif
-               ***p = NULL;
-
-       uc = cast_const3(char ***, p);
-       return 0;
-}
-
-#ifdef FAIL
-#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
-#error "Unfortunately we don't fail if cast_const can only use size"
-#endif
-#endif
index 6232c863db040e9ad6e23b2e3a6ac8acad54af97..f782f9d9ec5d10fc317021179934b17057ae60e0 100644 (file)
@@ -1,12 +1,17 @@
 #include <ccan/cast/cast.h>
 #include <stdlib.h>
 
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+       char c;
+};
+
 int main(int argc, char *argv[])
 {
        char ***uc;
-       const 
+       const
 #ifdef FAIL
-               int
+               struct char_struct
 #else
                char
 #endif
@@ -15,3 +20,9 @@ int main(int argc, char *argv[])
        uc = cast_const3(char ***, p);
        return 0;
 }
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
index 4e67dc6264491b5e9e61ca15c874884f97b7cc24..791d9b84529aa4bad527ec18294ff079a1b547bd 100644 (file)
@@ -13,3 +13,9 @@ int main(int argc, char *argv[])
        uc = cast_signed(unsigned char *, p);
        return 0;
 }
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/ccan/cast/test/compile_ok-cast_void.c b/ccan/cast/test/compile_ok-cast_void.c
new file mode 100644 (file)
index 0000000..c649d28
--- /dev/null
@@ -0,0 +1,12 @@
+#include <ccan/cast/cast.h>
+
+static void *remove_void(const void *p)
+{
+       return cast_const(void *, p);
+}
+
+int main(void)
+{
+       void *p = remove_void("foo");
+       return !p;
+}