Commit da72623a authored by Rusty Russell's avatar Rusty Russell

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...
parent ad3f309e
...@@ -97,11 +97,14 @@ ...@@ -97,11 +97,14 @@
//We do just fine by ourselves //We do just fine by ourselves
#define array_pop(array) ((array).item[--(array).size]) #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 #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_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) #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 #endif
......
...@@ -218,7 +218,7 @@ static void unbreak_backslash_broken_lines(struct token_list *tl, tok_message_qu ...@@ -218,7 +218,7 @@ static void unbreak_backslash_broken_lines(struct token_list *tl, tok_message_qu
txt.item[txt.size] = 0; txt.item[txt.size] = 0;
//convert the line start offsets to pointers //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 = olines.item;
tl->olines_size = olines.size; tl->olines_size = olines.size;
...@@ -401,7 +401,7 @@ struct token_list *tokenize(const char *orig, size_t orig_size, ...@@ -401,7 +401,7 @@ struct token_list *tokenize(const char *orig, size_t orig_size,
s = tl->txt; s = tl->txt;
e = s + tl->txt_size; e = s + tl->txt_size;
array_appends(array, { array_appends_t(array, struct token, {
.type = TOK_STARTLINE, .type = TOK_STARTLINE,
.txt = s, .txt = s,
.txt_size = 0 .txt_size = 0
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
#define check_types_match(expr1, expr2) \ #define check_types_match(expr1, expr2) \
((typeof(expr1) *)0 != (typeof(expr2) *)0) ((typeof(expr1) *)0 != (typeof(expr2) *)0)
#else #else
#include "build_assert/build_assert.h" #include <ccan/build_assert/build_assert.h>
/* Without typeof, we can only test the sizes. */ /* Without typeof, we can only test the sizes. */
#define check_type(expr, type) \ #define check_type(expr, type) \
EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type)) EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type))
......
...@@ -51,12 +51,13 @@ ...@@ -51,12 +51,13 @@
* return i; * return i;
* } * }
*/ */
#ifdef HAVE_TYPEOF #if HAVE_TYPEOF
#define container_of_var(member_ptr, var, member) \ #define container_of_var(member_ptr, var, member) \
container_of(member_ptr, typeof(*var), member) container_of(member_ptr, typeof(*var), member)
#else #else
#define container_of_var(member_ptr, var, member) \ #define container_of_var(member_ptr, var, member) \
((void *)((char *)(member_ptr) - offsetof(containing_type, member))) ((void *)((char *)(member_ptr) \
- ((char *)&(var)->member - (char *)(var))))
#endif #endif
#endif /* CCAN_CONTAINER_OF_H */ #endif /* CCAN_CONTAINER_OF_H */
...@@ -14,6 +14,9 @@ int main(int argc, char *argv[]) ...@@ -14,6 +14,9 @@ int main(int argc, char *argv[])
#ifdef FAIL #ifdef FAIL
/* b is a char, but intp is an int * */ /* b is a char, but intp is an int * */
foop = container_of_var(intp, foop, b); foop = container_of_var(intp, foop, b);
#if !HAVE_TYPEOF
#error "Unfortunately we don't fail if we don't have typeof."
#endif
#else #else
foop = NULL; foop = NULL;
#endif #endif
......
...@@ -29,6 +29,9 @@ int main(int argc, char *argv[]) ...@@ -29,6 +29,9 @@ int main(int argc, char *argv[])
{ {
#ifdef FAIL #ifdef FAIL
struct other 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 #else
struct foo struct foo
#endif #endif
......
...@@ -23,9 +23,12 @@ static void take_any(struct any *any) ...@@ -23,9 +23,12 @@ static void take_any(struct any *any)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
#if HAVE_TYPEOF
/* Otherwise we get unused warnings for these. */
struct foo *foo = NULL; struct foo *foo = NULL;
struct bar *bar = NULL; struct bar *bar = NULL;
struct baz *baz = NULL; struct baz *baz = NULL;
#endif
struct other *arg = NULL; struct other *arg = NULL;
take_any(cast_if_any(struct any *, arg, foo, take_any(cast_if_any(struct any *, arg, foo,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment