summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-03-22 11:53:17 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-03-22 11:53:17 +1030
commit4a084a9e956e6e5fec9f9ecb02ca56a79de4a422 (patch)
tree2680d93516179a7eff9d2a8ff2953d8ff92c8782
parentef54ea44cc261a59bf2d4b5f9898c89da409859d (diff)
cast: fix cast of void * when we don't have GCC features.
I thought using sizeof() comparison to compare the types was clever, but it doesn't work on void pointers, as sizeof(void) is illegal.
-rw-r--r--ccan/cast/cast.h9
-rw-r--r--ccan/cast/test/compile_fail-cast_const-sizesame.c28
-rw-r--r--ccan/cast/test/compile_fail-cast_const.c15
-rw-r--r--ccan/cast/test/compile_fail-cast_const2-sizesame.c28
-rw-r--r--ccan/cast/test/compile_fail-cast_const2.c15
-rw-r--r--ccan/cast/test/compile_fail-cast_const3-sizesame.c28
-rw-r--r--ccan/cast/test/compile_fail-cast_const3.c15
-rw-r--r--ccan/cast/test/compile_fail-cast_signed-const.c6
-rw-r--r--ccan/cast/test/compile_ok-cast_void.c12
9 files changed, 60 insertions, 96 deletions
diff --git a/ccan/cast/cast.h b/ccan/cast/cast.h
index dfb95b59..daebd857 100644
--- a/ccan/cast/cast.h
+++ b/ccan/cast/cast.h
@@ -122,11 +122,8 @@
#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
index d401cc8c..00000000
--- a/ccan/cast/test/compile_fail-cast_const-sizesame.c
+++ /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
diff --git a/ccan/cast/test/compile_fail-cast_const.c b/ccan/cast/test/compile_fail-cast_const.c
index bb8de94f..d401cc8c 100644
--- a/ccan/cast/test/compile_fail-cast_const.c
+++ b/ccan/cast/test/compile_fail-cast_const.c
@@ -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
index a16cfacf..00000000
--- a/ccan/cast/test/compile_fail-cast_const2-sizesame.c
+++ /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
diff --git a/ccan/cast/test/compile_fail-cast_const2.c b/ccan/cast/test/compile_fail-cast_const2.c
index 88df4b7a..a16cfacf 100644
--- a/ccan/cast/test/compile_fail-cast_const2.c
+++ b/ccan/cast/test/compile_fail-cast_const2.c
@@ -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
index f782f9d9..00000000
--- a/ccan/cast/test/compile_fail-cast_const3-sizesame.c
+++ /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
diff --git a/ccan/cast/test/compile_fail-cast_const3.c b/ccan/cast/test/compile_fail-cast_const3.c
index 6232c863..f782f9d9 100644
--- a/ccan/cast/test/compile_fail-cast_const3.c
+++ b/ccan/cast/test/compile_fail-cast_const3.c
@@ -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
diff --git a/ccan/cast/test/compile_fail-cast_signed-const.c b/ccan/cast/test/compile_fail-cast_signed-const.c
index 4e67dc62..791d9b84 100644
--- a/ccan/cast/test/compile_fail-cast_signed-const.c
+++ b/ccan/cast/test/compile_fail-cast_signed-const.c
@@ -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
index 00000000..c649d283
--- /dev/null
+++ b/ccan/cast/test/compile_ok-cast_void.c
@@ -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;
+}