From 5a9f24b4e739b6ef830f526845dd4d1557d0adee Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Thu, 3 Nov 2022 16:02:44 +0100 Subject: Ignore debug statements before the first case of a `switch` This can occur in debug mode if there are declarations before the first case, as in ``` switch (x) { int x; case 0: ... } ``` Without this commit, the code above is a structured switch if -g is not given, and a non-structured switch if -g is given. Co-authored-by: Michael Schmidt --- cparser/SwitchNorm.ml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cparser/SwitchNorm.ml b/cparser/SwitchNorm.ml index 2493c63d..65d10cb2 100644 --- a/cparser/SwitchNorm.ml +++ b/cparser/SwitchNorm.ml @@ -118,6 +118,12 @@ let substitute_cases case_table body end_label = | sd -> sd } in transf false body +let rec is_skip_or_debug s = + match s.sdesc with + | Sseq (a, b) -> is_skip_or_debug a && is_skip_or_debug b + | Sskip -> true + | _ -> Cutil.is_debug_stmt s + let new_label = ref 0 let gen_label () = incr new_label; sprintf "@%d" !new_label @@ -125,7 +131,7 @@ let gen_label () = incr new_label; sprintf "@%d" !new_label let normalize_switch loc e body = let (init, cases) = [body] |> flatten_switch |> group_switch and allcases = List.rev (all_cases [] body) in - if init.sdesc = Sskip && List.length cases = List.length allcases then + if is_skip_or_debug init && List.length cases = List.length allcases then (* This is a structured switch *) make_normalized_switch e cases else begin -- cgit