//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
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;
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
#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))
* 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 */
#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
{
#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
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,