From da72623aec30213a593bd23dca80c89416598f75 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 10 Nov 2010 22:10:03 +1030 Subject: [PATCH] ccan_tokenizer, check_type, container_of, typesafe_cb: handle !HAVE_TYPEOF Delio Brignoli points out that check_type fails when HAVE_TYPEOF is 0, so turn that off here and see what else breaks... --- ccan/array/array.h | 7 +++++-- ccan/ccan_tokenizer/ccan_tokenizer.c | 4 ++-- ccan/check_type/check_type.h | 2 +- ccan/container_of/container_of.h | 7 ++++--- ccan/container_of/test/compile_fail-var-types.c | 3 +++ ccan/typesafe_cb/test/compile_fail-cast_if_any.c | 3 +++ ccan/typesafe_cb/test/compile_ok-cast_if_any.c | 3 +++ 7 files changed, 21 insertions(+), 8 deletions(-) diff --git a/ccan/array/array.h b/ccan/array/array.h index c2efa3ce..35248409 100644 --- a/ccan/array/array.h +++ b/ccan/array/array.h @@ -97,11 +97,14 @@ //We do just fine by ourselves #define array_pop(array) ((array).item[--(array).size]) +#define array_for_t(var, array, type, ...) do {type *var=(void*)(array).item; size_t _r=(array).size, _i=0; for (;_r--;_i++, var++) { __VA_ARGS__ ;} } while(0) + +#define array_appends_t(array, type, ...) do {type __src[] = {__VA_ARGS__}; array_append_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0) #if HAVE_TYPEOF==1 -#define array_appends(array, ...) do {typeof((*(array).item)) __src[] = {__VA_ARGS__}; array_append_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0) +#define array_appends(array, ...) array_appends_t(array, typeof((*(array).item)), __VA_ARGS__)) #define array_prepends(array, ...) do {typeof((*(array).item)) __src[] = {__VA_ARGS__}; array_prepend_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0) -#define array_for(var, array, ...) do {typeof(*(array).item) *var=(void*)(array).item; size_t _r=(array).size, _i=0; for (;_r--;_i++, var++) { __VA_ARGS__ ;} } while(0) +#define array_for(var, array, ...) array_for_t(var, array, typeof(*(array).item), __VA_ARGS__) #define array_rof(var, array, ...) do {typeof(*(array).item) *var=(void*)(array).item; size_t _i=(array).size, _r=0; var += _i; for (;_i--;_r++) { var--; __VA_ARGS__ ;} } while(0) #endif diff --git a/ccan/ccan_tokenizer/ccan_tokenizer.c b/ccan/ccan_tokenizer/ccan_tokenizer.c index 52858fea..cb1b2716 100644 --- a/ccan/ccan_tokenizer/ccan_tokenizer.c +++ b/ccan/ccan_tokenizer/ccan_tokenizer.c @@ -218,7 +218,7 @@ static void unbreak_backslash_broken_lines(struct token_list *tl, tok_message_qu txt.item[txt.size] = 0; //convert the line start offsets to pointers - array_for(i, tlines, *i = txt.item + (size_t)*i); + array_for_t(i, tlines, const char *, *i = txt.item + (size_t)*i); tl->olines = olines.item; tl->olines_size = olines.size; @@ -401,7 +401,7 @@ struct token_list *tokenize(const char *orig, size_t orig_size, s = tl->txt; e = s + tl->txt_size; - array_appends(array, { + array_appends_t(array, struct token, { .type = TOK_STARTLINE, .txt = s, .txt_size = 0 diff --git a/ccan/check_type/check_type.h b/ccan/check_type/check_type.h index d0dc0d4b..88405671 100644 --- a/ccan/check_type/check_type.h +++ b/ccan/check_type/check_type.h @@ -51,7 +51,7 @@ #define check_types_match(expr1, expr2) \ ((typeof(expr1) *)0 != (typeof(expr2) *)0) #else -#include "build_assert/build_assert.h" +#include /* Without typeof, we can only test the sizes. */ #define check_type(expr, type) \ EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type)) diff --git a/ccan/container_of/container_of.h b/ccan/container_of/container_of.h index de3f4505..2a6b1cd5 100644 --- a/ccan/container_of/container_of.h +++ b/ccan/container_of/container_of.h @@ -51,12 +51,13 @@ * return i; * } */ -#ifdef HAVE_TYPEOF +#if HAVE_TYPEOF #define container_of_var(member_ptr, var, member) \ container_of(member_ptr, typeof(*var), member) #else -#define container_of_var(member_ptr, var, member) \ - ((void *)((char *)(member_ptr) - offsetof(containing_type, member))) +#define container_of_var(member_ptr, var, member) \ + ((void *)((char *)(member_ptr) \ + - ((char *)&(var)->member - (char *)(var)))) #endif #endif /* CCAN_CONTAINER_OF_H */ diff --git a/ccan/container_of/test/compile_fail-var-types.c b/ccan/container_of/test/compile_fail-var-types.c index f9312b69..b7f395c7 100644 --- a/ccan/container_of/test/compile_fail-var-types.c +++ b/ccan/container_of/test/compile_fail-var-types.c @@ -14,6 +14,9 @@ int main(int argc, char *argv[]) #ifdef FAIL /* b is a char, but intp is an int * */ foop = container_of_var(intp, foop, b); +#if !HAVE_TYPEOF +#error "Unfortunately we don't fail if we don't have typeof." +#endif #else foop = NULL; #endif diff --git a/ccan/typesafe_cb/test/compile_fail-cast_if_any.c b/ccan/typesafe_cb/test/compile_fail-cast_if_any.c index 9ead3de5..dfb51167 100644 --- a/ccan/typesafe_cb/test/compile_fail-cast_if_any.c +++ b/ccan/typesafe_cb/test/compile_fail-cast_if_any.c @@ -29,6 +29,9 @@ int main(int argc, char *argv[]) { #ifdef FAIL struct other +#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P +#error "Unfortunately we don't fail if cast_if_type is a noop." +#endif #else struct foo #endif diff --git a/ccan/typesafe_cb/test/compile_ok-cast_if_any.c b/ccan/typesafe_cb/test/compile_ok-cast_if_any.c index 2955f60c..e8f3c494 100644 --- a/ccan/typesafe_cb/test/compile_ok-cast_if_any.c +++ b/ccan/typesafe_cb/test/compile_ok-cast_if_any.c @@ -23,9 +23,12 @@ static void take_any(struct any *any) int main(int argc, char *argv[]) { +#if HAVE_TYPEOF + /* Otherwise we get unused warnings for these. */ struct foo *foo = NULL; struct bar *bar = NULL; struct baz *baz = NULL; +#endif struct other *arg = NULL; take_any(cast_if_any(struct any *, arg, foo, -- 2.39.2