Skip to content

Commit a7b5d93

Browse files
committed
Fix C expression generation to pre-generate string literals for match arms, improving code clarity and avoiding mid-chain declarations.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 0dda866 commit a7b5d93

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/ebpf_c_codegen.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,9 +2060,14 @@ let generate_c_expression ctx ir_expr =
20602060
(* Generate temporary variable for the result *)
20612061
emit_line ctx (sprintf "%s %s;" result_type temp_var);
20622062

2063-
(* Generate if-else chain *)
2064-
let generate_match_arm is_first arm =
2063+
(* Pre-generate all string literals to avoid declarations in the middle of if-else chain *)
2064+
let arm_value_strings = List.map (fun arm ->
20652065
let arm_val_str = generate_c_value ctx arm.ir_arm_value in
2066+
(arm, arm_val_str)
2067+
) arms in
2068+
2069+
(* Generate if-else chain *)
2070+
let generate_match_arm is_first (arm, arm_val_str) =
20662071
match arm.ir_arm_pattern with
20672072
| IRConstantPattern const_val ->
20682073
let const_str = generate_c_value ctx const_val in
@@ -2081,7 +2086,7 @@ let generate_c_expression ctx ir_expr =
20812086
in
20822087

20832088
(* Generate all arms *)
2084-
(match arms with
2089+
(match arm_value_strings with
20852090
| [] -> () (* No arms - should not happen *)
20862091
| first_arm :: rest_arms ->
20872092
generate_match_arm true first_arm;

0 commit comments

Comments
 (0)