]> git.ozlabs.org Git - ccan/commitdiff
ccan_tokenizer, check_type, container_of, typesafe_cb: handle !HAVE_TYPEOF
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 10 Nov 2010 11:40:03 +0000 (22:10 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 10 Nov 2010 11:40:03 +0000 (22:10 +1030)
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
ccan/ccan_tokenizer/ccan_tokenizer.c
ccan/check_type/check_type.h
ccan/container_of/container_of.h
ccan/container_of/test/compile_fail-var-types.c
ccan/typesafe_cb/test/compile_fail-cast_if_any.c
ccan/typesafe_cb/test/compile_ok-cast_if_any.c

index c2efa3ce707d74678ca51e503e2e6b7644b62774..35248409371a1683b2b1f23ce3a1bb50cb9c65f0 100644 (file)
 //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
 
index 52858feaa8797a80422a032d0f7f12ead5ff09e6..cb1b2716a71e722f0578d1417e68609d4e9c3c51 100644 (file)
@@ -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
index d0dc0d4b295fb9bf800bf4e1d69ac59a7ed5615f..884056719cc718eb075acd1a8848c8aef8640265 100644 (file)
@@ -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))
index de3f4505162a5416f477d5624e0049da6d2abeec..2a6b1cd54e9dcf97f57b0b282e23bb7de82ae0db 100644 (file)
  *             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 */
index f9312b69b3a0486f0b07506781638c3e09cfce89..b7f395c761e7e91a327ef45bc86bec3d14454430 100644 (file)
@@ -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
index 9ead3de562f3309b9c4df70be550b57a74845d61..dfb51167ffbe7c01c384aab5be0eb4eabf1d0586 100644 (file)
@@ -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
index 2955f60c93efb7ca29ef174b7f46203bd61f61f7..e8f3c49406c02d31312d298c38d4860c843f4b1c 100644 (file)
@@ -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,