Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: Use __typeof__() instead of typeof() in BPF skeleton

When generated BPF skeleton header is included in C++ code base, some
compiler setups will emit warning about using language extensions due to
typeof() usage, resulting in something like:

error: extension used [-Werror,-Wlanguage-extension-token]
obj->struct_ops.empty_tcp_ca = (typeof(obj->struct_ops.empty_tcp_ca))
^

It looks like __typeof__() is a preferred way to do typeof() with better
C++ compatibility behavior, so switch to that. With __typeof__() we get
no such warning.

Fixes: c2a0257c1edf ("bpftool: Cast pointers for shadow types explicitly.")
Fixes: 00389c58ffe9 ("bpftool: Add support for subskeletons")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Kui-Feng Lee <thinker.li@gmail.com>
Acked-by: Quentin Monnet <qmo@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20240401170713.2081368-1-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
2a24e248 965c6167

+2 -2
+2 -2
tools/bpf/bpftool/gen.c
··· 386 386 */ 387 387 needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var); 388 388 if (needs_typeof) 389 - printf("typeof("); 389 + printf("__typeof__("); 390 390 391 391 err = btf_dump__emit_type_decl(d, var_type_id, &opts); 392 392 if (err) ··· 1131 1131 continue; 1132 1132 codegen("\ 1133 1133 \n\ 1134 - obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\ 1134 + obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\ 1135 1135 bpf_map__initial_value(obj->maps.%1$s, NULL);\n\ 1136 1136 \n\ 1137 1137 ", ident);