[PATCH 1/2] Fix compilation with Clang

lockalsash@gmail.com lockalsash@gmail.com
Thu Jun 4 03:49:12 GMT 2026


From: "Sv. Lockal" <lockalsash@gmail.com>

Clang-22 fails to compile with errors like:
```
bpfinterp.cxx:1098:18: error: case value is not a constant expression
 1098 |             case bpf::BPF_FUNC_str_concat:
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~
```

GCC accepts const variables, but Clang enforces the standard more
strictly. Using enum allows both compilers to accept the code, but
requires cast from bpf::(unnamed enum) to bpf_func_id.

Signed-off-by: Sv. Lockal <lockalsash@gmail.com>
---
 bpf-base.cxx      |  2 +-
 bpf-internal.h    | 24 +++++++++++++-----------
 bpf-translate.cxx | 10 +++++-----
 3 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/bpf-base.cxx b/bpf-base.cxx
index 740e8da24..e3ec08dd0 100644
--- a/bpf-base.cxx
+++ b/bpf-base.cxx
@@ -402,7 +402,7 @@ void
 init_bpf_helper_tables ()
 {
 #define __BPF_SET_FUNC_NAME(x) bpf_func_name_map[BPF_FUNC_ ## x] = #x
-#define __BPF_SET_FUNC_ID(x) bpf_func_id_map[#x] = BPF_FUNC_ ## x
+#define __BPF_SET_FUNC_ID(x) bpf_func_id_map[#x] = (bpf_func_id)(BPF_FUNC_ ## x)
   __BPF_FUNC_MAPPER(__BPF_SET_FUNC_NAME)
   __STAPBPF_FUNC_MAPPER(__BPF_SET_FUNC_NAME)
   __BPF_FUNC_MAPPER(__BPF_SET_FUNC_ID)
diff --git a/bpf-internal.h b/bpf-internal.h
index c53bda4e2..7774f1c1d 100644
--- a/bpf-internal.h
+++ b/bpf-internal.h
@@ -224,17 +224,19 @@ const opcode BPF_LD_MAP = BPF_LD | BPF_IMM | BPF_DW | (BPF_PSEUDO_MAP_FD << 8);
   FN(text_str),                   \
   FN(string_quoted),
  
-const bpf_func_id BPF_FUNC_map_get_next_key    = (bpf_func_id) -1;
-const bpf_func_id BPF_FUNC_sprintf             = (bpf_func_id) -2;
-const bpf_func_id BPF_FUNC_stapbpf_stat_get    = (bpf_func_id) -3;
-const bpf_func_id BPF_FUNC_gettimeofday_ns     = (bpf_func_id) -4;
-const bpf_func_id BPF_FUNC_get_target          = (bpf_func_id) -5;
-const bpf_func_id BPF_FUNC_set_procfs_value    = (bpf_func_id) -6;
-const bpf_func_id BPF_FUNC_append_procfs_value = (bpf_func_id) -7;
-const bpf_func_id BPF_FUNC_get_procfs_value    = (bpf_func_id) -8;
-const bpf_func_id BPF_FUNC_str_concat          = (bpf_func_id) -9;
-const bpf_func_id BPF_FUNC_text_str            = (bpf_func_id) -10;
-const bpf_func_id BPF_FUNC_string_quoted       = (bpf_func_id) -11;
+enum {
+  BPF_FUNC_map_get_next_key    = (bpf_func_id) -1,
+  BPF_FUNC_sprintf             = (bpf_func_id) -2,
+  BPF_FUNC_stapbpf_stat_get    = (bpf_func_id) -3,
+  BPF_FUNC_gettimeofday_ns     = (bpf_func_id) -4,
+  BPF_FUNC_get_target          = (bpf_func_id) -5,
+  BPF_FUNC_set_procfs_value    = (bpf_func_id) -6,
+  BPF_FUNC_append_procfs_value = (bpf_func_id) -7,
+  BPF_FUNC_get_procfs_value    = (bpf_func_id) -8,
+  BPF_FUNC_str_concat          = (bpf_func_id) -9,
+  BPF_FUNC_text_str            = (bpf_func_id) -10,
+  BPF_FUNC_string_quoted       = (bpf_func_id) -11
+};
 
 struct insn
 {
diff --git a/bpf-translate.cxx b/bpf-translate.cxx
index b77ccd6eb..cd1eeaf03 100644
--- a/bpf-translate.cxx
+++ b/bpf-translate.cxx
@@ -2309,7 +2309,7 @@ bpf_unparser::visit_foreach_loop(foreach_loop* s)
                        frame, newkey_ofs);
   this_prog.mk_mov (this_ins, this_prog.lookup_reg(BPF_REG_4), id);
   this_prog.mk_mov (this_ins, this_prog.lookup_reg(BPF_REG_5), limit);
-  this_prog.mk_call (this_ins, BPF_FUNC_map_get_next_key, 5);
+  this_prog.mk_call (this_ins, (bpf_func_id)BPF_FUNC_map_get_next_key, 5);
   this_prog.mk_jcond (this_ins, NE, this_prog.lookup_reg(BPF_REG_0), i0,
                       join_block, load_block_1);
 
@@ -2333,7 +2333,7 @@ bpf_unparser::visit_foreach_loop(foreach_loop* s)
                        frame, newkey_ofs);
   this_prog.mk_mov (this_ins, this_prog.lookup_reg(BPF_REG_4), id);
   this_prog.mk_mov (this_ins, this_prog.lookup_reg(BPF_REG_5), limit);
-  this_prog.mk_call (this_ins, BPF_FUNC_map_get_next_key, 5);
+  this_prog.mk_call (this_ins, (bpf_func_id)BPF_FUNC_map_get_next_key, 5);
   this_prog.mk_jcond (this_ins, NE, this_prog.lookup_reg(BPF_REG_0), i0,
                       join_block, load_block_1);
 
@@ -2848,7 +2848,7 @@ bpf_unparser::visit_concatenation (concatenation* e)
       this_prog.mk_mov(this_ins, this_prog.lookup_reg(BPF_REG_2), placeholder_next);
 
       // Call function to concatenate. 
-      this_prog.mk_call(this_ins, BPF_FUNC_str_concat, 2);
+      this_prog.mk_call(this_ins, (bpf_func_id)BPF_FUNC_str_concat, 2);
 
       result_str = this_prog.new_reg();
       this_prog.mk_mov(this_ins, result_str, this_prog.lookup_reg(BPF_REG_0));
@@ -3948,7 +3948,7 @@ bpf_unparser::emit_print_format (const std::string& format,
       for (size_t i = 0; i < nargs; ++i)
         emit_mov(this_prog.lookup_reg(BPF_REG_3 + i), actual[i]);
 
-      this_prog.mk_call(this_ins, BPF_FUNC_sprintf, nargs + 2);
+      this_prog.mk_call(this_ins, (bpf_func_id)BPF_FUNC_sprintf, nargs + 2);
       return this_prog.lookup_reg(BPF_REG_0);
     }
 
@@ -4172,7 +4172,7 @@ bpf_unparser::visit_stat_op (stat_op* e)
   uint64_t sc_type = globals::intern_sc_type(e->ctype);
   emit_mov(this_prog.lookup_reg(BPF_REG_3), this_prog.new_imm(sc_type));
 
-  this_prog.mk_call (this_ins, BPF_FUNC_stapbpf_stat_get, 3);
+  this_prog.mk_call (this_ins, (bpf_func_id)BPF_FUNC_stapbpf_stat_get, 3);
 
   result = this_prog.new_reg();
   emit_mov(result, this_prog.lookup_reg(BPF_REG_0));
-- 
2.54.0



More information about the Systemtap mailing list