From 0ec1ae8bf5f4cad3f4b90f2615909c3e7bc6270d Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 20 Jan 2022 09:32:56 -0500 Subject: [PATCH] Use __typeof__ instead of typeof MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When compiling the ctf plugin as C++, a common error was: In file included from /home/simark/src/babeltrace/src/plugins/ctf/common/bfcr/bfcr.cpp:23: /home/simark/src/babeltrace/src/plugins/ctf/common/bfcr/bfcr.cpp: In function ‘void read_unsigned_bitfield(bt_bfcr*, const uint8_t*, size_t, unsigned int, ctf_byte_order, uint64_t*)’: /home/simark/src/babeltrace/src/compat/bitfield.h:343:9: error: ‘typeof’ was not declared in this scope 343 | typeof(*(vptr)) *_vptr = (vptr); \ | ^~~~~~ Use __typeof__ instead, gcc and clang seem happy with that in both C and C++. Using `decltype` or `auto` would be more C++-y, but using __typeof__ works and we don't need separate paths for both languages. Change-Id: Iec503420f27dd2fe0a246d02af9380a2cff1c96e Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/7098 Reviewed-by: Philippe Proulx --- src/common/align.h | 10 +++++----- src/common/list.h | 14 +++++++------- src/common/macros.h | 2 +- src/compat/compiler.h | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/common/align.h b/src/common/align.h index a9c88de8..0d4c0150 100644 --- a/src/common/align.h +++ b/src/common/align.h @@ -10,14 +10,14 @@ #include "compat/compiler.h" #include "compat/limits.h" -#define BT_ALIGN(x, a) __BT_ALIGN_MASK(x, (typeof(x))(a) - 1) +#define BT_ALIGN(x, a) __BT_ALIGN_MASK(x, (__typeof__(x))(a) - 1) #define __BT_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) -#define BT_PTR_ALIGN(p, a) ((typeof(p)) BT_ALIGN((unsigned long) (p), a)) -#define BT_ALIGN_FLOOR(x, a) __BT_ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1) +#define BT_PTR_ALIGN(p, a) ((__typeof__(p)) BT_ALIGN((unsigned long) (p), a)) +#define BT_ALIGN_FLOOR(x, a) __BT_ALIGN_FLOOR_MASK(x, (__typeof__(x)) (a) - 1) #define __BT_ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask)) #define BT_PTR_ALIGN_FLOOR(p, a) \ - ((typeof(p)) BT_ALIGN_FLOOR((unsigned long) (p), a)) -#define BT_IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0) + ((__typeof__(p)) BT_ALIGN_FLOOR((unsigned long) (p), a)) +#define BT_IS_ALIGNED(x, a) (((x) & ((__typeof__(x)) (a) - 1)) == 0) /* * Align pointer on natural object alignment. diff --git a/src/common/list.h b/src/common/list.h index 565fc687..193bf2d7 100644 --- a/src/common/list.h +++ b/src/common/list.h @@ -130,20 +130,20 @@ bt_list_splice (struct bt_list_head *add, struct bt_list_head *head) pos = p, p = pos->prev) #define bt_list_for_each_entry(pos, head, member) \ - for (pos = bt_list_entry((head)->next, typeof(*pos), member); \ + for (pos = bt_list_entry((head)->next, __typeof__(*pos), member); \ &pos->member != (head); \ - pos = bt_list_entry(pos->member.next, typeof(*pos), member)) + pos = bt_list_entry(pos->member.next, __typeof__(*pos), member)) #define bt_list_for_each_entry_reverse(pos, head, member) \ - for (pos = bt_list_entry((head)->prev, typeof(*pos), member); \ + for (pos = bt_list_entry((head)->prev, __typeof__(*pos), member); \ &pos->member != (head); \ - pos = bt_list_entry(pos->member.prev, typeof(*pos), member)) + pos = bt_list_entry(pos->member.prev, __typeof__(*pos), member)) #define bt_list_for_each_entry_safe(pos, p, head, member) \ - for (pos = bt_list_entry((head)->next, typeof(*pos), member), \ - p = bt_list_entry(pos->member.next,typeof(*pos), member); \ + for (pos = bt_list_entry((head)->next, __typeof__(*pos), member), \ + p = bt_list_entry(pos->member.next, __typeof__(*pos), member); \ &pos->member != (head); \ - pos = p, p = bt_list_entry(pos->member.next, typeof(*pos), member)) + pos = p, p = bt_list_entry(pos->member.next, __typeof__(*pos), member)) static inline int bt_list_empty(struct bt_list_head *head) { diff --git a/src/common/macros.h b/src/common/macros.h index 9014afc2..5549b857 100644 --- a/src/common/macros.h +++ b/src/common/macros.h @@ -43,7 +43,7 @@ extern "C" { #define BT_MOVE_REF(ref) \ ({ \ - typeof(ref) _ref = ref; \ + __typeof__(ref) _ref = ref; \ ref = NULL; \ _ref; \ }) diff --git a/src/compat/compiler.h b/src/compat/compiler.h index 2c852742..90e80da8 100644 --- a/src/compat/compiler.h +++ b/src/compat/compiler.h @@ -14,7 +14,7 @@ #ifndef container_of #define container_of(ptr, type, member) \ ({ \ - const typeof(((type *)NULL)->member) * __ptr = (ptr); \ + const __typeof__(((type *)NULL)->member) * __ptr = (ptr); \ (type *)((char *)__ptr - offsetof(type, member)); \ }) #endif -- 2.34.1