Skip to contentSkip to conversation

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

PATCH Applied Andrii Nakryiko 5 participants 5 messages Apr 1, 2024 – Apr 2, 2024

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 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")
---
 tools/bpf/bpftool/gen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +2 -2
index 786268f1a483..b3979ddc0189 100644
@@ -386,7 +386,7 @@ static int codegen_subskel_datasecs(struct bpf_object *obj, const char *obj_name
 			 */
 			needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var);
 			if (needs_typeof)
-				printf("typeof(");
+				printf("__typeof__(");
 
 			err = btf_dump__emit_type_decl(d, var_type_id, &opts);
 			if (err)
@@ -1131,7 +1131,7 @@ static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
 			continue;
 		codegen("\
 			\n\
-				obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\
+				obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\
 					bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
 			\n\
 			", ident);
Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 33 lines · Andrii Nakryiko

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")
---
  tools/bpf/bpftool/gen.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +1 -1
index 786268f1a483..b3979ddc0189 100644
@@ -386,7 +386,7 @@ static int codegen_subskel_datasecs(struct bpf_object *obj, const char *obj_name
  			 */
  			needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var);
  			if (needs_typeof)
-				printf("typeof(");
+				printf("__typeof__(");
 
  			err = btf_dump__emit_type_decl(d, var_type_id, &opts);
  			if (err)

tools/bpf/bpftool/gen.c static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj) · line 1137

Quoted 8 lines
tools/bpf/bpftool/gen.c +1 -1
@@ -1131,7 +1131,7 @@ static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
  			continue;
  		codegen("\
  			\n\
-				obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\
+				obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\
  					bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
  			\n\
  			", ident);

LGTM!

Thanks!

maintainer Acked-by
#2 lore raw
Quoted 52 lines · Kui-Feng Lee
Quoted 43 lines · Andrii Nakryiko

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")

---
  tools/bpf/bpftool/gen.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +0 -0
index 786268f1a483..b3979ddc0189 100644
@@ -386,7 +386,7 @@ static int codegen_subskel_datasecs(struct

bpf_object *obj, const char *obj_name
               */
              needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var);
              if (needs_typeof)
-                printf("typeof(");
+                printf("__typeof__(");
                err = btf_dump__emit_type_decl(d, var_type_id, &opts); if (err)

tools/bpf/bpftool/gen.c +1 -1
@@ -1131,7 +1131,7 @@ static void gen_st_ops_shadow_init(struct btf
 *btf, struct bpf_object *obj)
               continue;
           codegen("\
               \n\
-                obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\
+                obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.
 %1$s))\n\
                       bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
Quoted 2 lines

              \n\
              ", ident);

LGTM!

Quoted 4 lines

Thanks!

Acked-by on v1 1/1
#3 lore raw
In reply to Andrii Nakryiko
Quoted 45 lines · Andrii Nakryiko

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")
---
 tools/bpf/bpftool/gen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/bpf/bpftool/gen.c +2 -2
index 786268f1a483..b3979ddc0189 100644
@@ -386,7 +386,7 @@ static int codegen_subskel_datasecs(struct bpf_object *obj, const char *obj_name
 			 */
 			needs_typeof = btf_is_array(var) || btf_is_ptr_to_func_proto(btf, var);
 			if (needs_typeof)
-				printf("typeof(");
+				printf("__typeof__(");
 
 			err = btf_dump__emit_type_decl(d, var_type_id, &opts);
 			if (err)
@@ -1131,7 +1131,7 @@ static void gen_st_ops_shadow_init(struct btf *btf, struct bpf_object *obj)
 			continue;
 		codegen("\
 			\n\
-				obj->struct_ops.%1$s = (typeof(obj->struct_ops.%1$s))\n\
+				obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\
 					bpf_map__initial_value(obj->maps.%1$s, NULL);\n\
 			\n\
 			", ident);
Signature · 1 line
2.43.0

sure.

#4 lore raw
In reply to Andrii Nakryiko
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 9 lines · On Mon, 1 Apr 2024 10:07:13 -0700 you wrote:

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))

                                  ^

[...]

Here is the summary with links:
- [bpf-next] bpftool: use __typeof__() instead of typeof() in BPF skeleton

    https://git.kernel.org/bpf/bpf-next/c/2a24e2485722

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html