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 @@
//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
......
......@@ -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
......
......@@ -51,7 +51,7 @@
#define check_types_match(expr1, expr2) \
((typeof(expr1) *)0 != (typeof(expr2) *)0)
#else
#include "build_assert/build_assert.h"
#include <ccan/build_assert/build_assert.h>
/* Without typeof, we can only test the sizes. */
#define check_type(expr, type) \
EXPR_BUILD_ASSERT(sizeof(expr) == sizeof(type))
......
......@@ -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 */
......@@ -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
......
......@@ -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
......
......@@ -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,
......
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