Skip to content

Commit 12c4aed

Browse files
committed
typeof cleanup
1 parent 5a1d6bf commit 12c4aed

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/decode.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stddef.h>
33
#include <stdint.h>
44

5+
#include "platform.h"
6+
57
int read_u8(const uint8_t **pp, const uint8_t *ep, uint8_t *resultp);
68
int read_u32(const uint8_t **pp, const uint8_t *ep, uint32_t *resultp);
79
int read_u64(const uint8_t **pp, const uint8_t *ep, uint64_t *resultp);
@@ -21,14 +23,21 @@ int read_vec_with_ctx(const uint8_t **pp, const uint8_t *ep, size_t elem_size,
2123
void (*clear_elem)(void *elem), void *ctx,
2224
uint32_t *countp, void **resultp);
2325

26+
#if defined(toywasm_typeof)
2427
/* a version with non-standard checks */
2528
#define read_vec_with_ctx2(pp, ep, elem_size, read_elem, clear_elem, ctx, \
2629
countp, resultp) \
2730
({ \
2831
int (*_r)(const uint8_t **, const uint8_t *, uint32_t, \
29-
__typeof__(*resultp), void *) = read_elem; \
30-
void (*_c)(__typeof__(*resultp)) = clear_elem; \
32+
toywasm_typeof(*resultp), void *) = read_elem; \
33+
void (*_c)(toywasm_typeof(*resultp)) = clear_elem; \
3134
assert(sizeof(**resultp) == elem_size); \
3235
read_vec_with_ctx(pp, ep, elem_size, (void *)_r, (void *)_c, \
3336
ctx, countp, (void **)resultp); \
3437
})
38+
#else
39+
#define read_vec_with_ctx2(pp, ep, elem_size, read_elem, clear_elem, ctx, \
40+
countp, resultp) \
41+
read_vec_with_ctx(pp, ep, elem_size, (void *)read_elem, \
42+
(void *)clear_elem, ctx, countp, (void **)resultp)
43+
#endif

lib/list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ void list_insert_tail(struct list_head *h, void *elem, struct list_entry *e);
3131
#define LIST_FOREACH(VAR, HEAD, NAME) \
3232
for (VAR = (HEAD)->first; VAR != NULL; VAR = (VAR)->NAME.next)
3333

34-
#if defined(__clang__)
34+
#if defined(toywasm_typeof)
3535
#define CHECK_TYPE(a, b) \
3636
do { \
37-
__attribute__((__unused__)) __typeof__(a) __dummy = b; \
37+
__attribute__((__unused__)) toywasm_typeof(a) __dummy = b; \
3838
} while (0)
3939
#else
4040
#define CHECK_TYPE(a, b)

lib/platform.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@
5656
#else
5757
#define ctassert(e)
5858
#endif
59+
60+
/*
61+
* https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
62+
* clang also has it.
63+
*
64+
* Note: we don't want to rely on typeof.
65+
* any use of it in toywasm should have a fallback implementation.
66+
*/
67+
#if defined(__GNUC__)
68+
#define toywasm_typeof(a) __typeof__(a)
69+
#else
70+
#undef toywasm_typeof
71+
#endif

0 commit comments

Comments
 (0)