Skip to content

Commit b0eda00

Browse files
vouillonhhugo
authored andcommitted
Debug info output improvements
- Do not repeat the output if the location has not changed - Start a new source map mapping before outputing the debug info as a comment (we need to start a mapping right after a return statement, moving it after a comment would not work) - Outputting an identifier name will not change the location (there is no point in doing that)
1 parent 7a48dc0 commit b0eda00

File tree

8 files changed

+9690
-17346
lines changed

8 files changed

+9690
-17346
lines changed

compiler/lib/js_output.ml

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,59 +146,86 @@ struct
146146

147147
let debug_enabled = Config.Flag.debuginfo ()
148148

149+
let current_loc = ref U
150+
151+
let on_ident = ref false
152+
149153
let output_debug_info f loc =
150-
(if debug_enabled
151-
then
152-
match loc with
153-
| Pi { Parse_info.src = None | Some ""; name = None | Some ""; _ } | N -> ()
154-
| U ->
155-
PP.non_breaking_space f;
156-
PP.string f "/*<<?>>*/";
157-
PP.non_breaking_space f
158-
| Pi { Parse_info.src; name; line; col; _ } ->
159-
let file =
160-
match name, src with
161-
| (None | Some ""), Some file -> file
162-
| Some file, (None | Some "") -> file
163-
| Some file, Some _file -> file
164-
| None, None -> assert false
165-
in
166-
PP.non_breaking_space f;
167-
PP.string f (Format.sprintf "/*<<%s:%d:%d>>*/" file line col);
168-
PP.non_breaking_space f);
169-
if source_map_enabled
170-
then
154+
let loc =
155+
(* We force a new mapping after an identifier, to avoid its name
156+
to bleed over other identifiers, using the current location
157+
when none is provided. *)
171158
match loc with
172-
| N -> ()
173-
| U | Pi { Parse_info.src = None | Some ""; _ } ->
174-
push_mapping (PP.pos f) (Source_map.Gen { gen_line = -1; gen_col = -1 })
175-
| Pi { Parse_info.src = Some file; line; col; _ } ->
176-
push_mapping
177-
(PP.pos f)
178-
(Source_map.Gen_Ori
179-
{ gen_line = -1
180-
; gen_col = -1
181-
; ori_source = get_file_index file
182-
; ori_line = line
183-
; ori_col = col
184-
})
159+
| N when !on_ident -> !current_loc
160+
| _ -> loc
161+
in
162+
match loc with
163+
| N -> ()
164+
| _ ->
165+
let location_changed = Poly.(loc <> !current_loc) in
166+
(if source_map_enabled && (!on_ident || location_changed)
167+
then
168+
match loc with
169+
| N | U | Pi { Parse_info.src = None | Some ""; _ } ->
170+
push_mapping (PP.pos f) (Source_map.Gen { gen_line = -1; gen_col = -1 })
171+
| Pi { Parse_info.src = Some file; line; col; _ } ->
172+
push_mapping
173+
(PP.pos f)
174+
(Source_map.Gen_Ori
175+
{ gen_line = -1
176+
; gen_col = -1
177+
; ori_source = get_file_index file
178+
; ori_line = line
179+
; ori_col = col
180+
}));
181+
(if debug_enabled && location_changed
182+
then
183+
match loc with
184+
| N | U ->
185+
PP.non_breaking_space f;
186+
PP.string f "/*<<?>>*/";
187+
PP.non_breaking_space f
188+
| Pi pi ->
189+
PP.non_breaking_space f;
190+
PP.string f (Format.sprintf "/*<<%s>>*/" (Parse_info.to_string pi));
191+
PP.non_breaking_space f);
192+
current_loc := loc;
193+
on_ident := false
185194

186195
let output_debug_info_ident f nm loc =
187196
if source_map_enabled
188-
then
189-
match loc with
190-
| None | Some { Parse_info.src = Some "" | None; _ } -> ()
191-
| Some { Parse_info.src = Some file; line; col; _ } ->
192-
push_mapping
193-
(PP.pos f)
194-
(Source_map.Gen_Ori_Name
195-
{ gen_line = -1
196-
; gen_col = -1
197-
; ori_source = get_file_index file
198-
; ori_line = line
199-
; ori_col = col
200-
; ori_name = get_name_index nm
201-
})
197+
then (
198+
let loc =
199+
(* Keep the current location if possible, since we don't care
200+
about the actual identifier's location *)
201+
match !current_loc, loc with
202+
| (N | U | Pi { Parse_info.src = Some "" | None; _ }), Some _ -> loc
203+
| Pi ({ Parse_info.src = Some _; _ } as loc), _ -> Some loc
204+
| _, None -> None
205+
in
206+
on_ident := true;
207+
push_mapping
208+
(PP.pos f)
209+
(match loc with
210+
| None | Some { Parse_info.src = Some "" | None; _ } ->
211+
(* Use a dummy location. It is going to be ignored anyway *)
212+
Source_map.Gen_Ori_Name
213+
{ gen_line = -1
214+
; gen_col = -1
215+
; ori_source = 0
216+
; ori_line = 1
217+
; ori_col = 0
218+
; ori_name = get_name_index nm
219+
}
220+
| Some { Parse_info.src = Some file; line; col; _ } ->
221+
Source_map.Gen_Ori_Name
222+
{ gen_line = -1
223+
; gen_col = -1
224+
; ori_source = get_file_index file
225+
; ori_line = line
226+
; ori_col = col
227+
; ori_name = get_name_index nm
228+
}))
202229

203230
let ident f ~kind = function
204231
| S { name = Utf8 name; var = Some v; _ } ->

compiler/lib/parse_info.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,16 @@ let t_of_position ~src pos =
5151
; col = pos.Lexing.pos_cnum - pos.Lexing.pos_bol
5252
; idx = 0
5353
}
54+
55+
let to_string { name; src; line; col; _ } =
56+
match name, src with
57+
| (None | Some ""), (None | Some "") -> "?"
58+
| _ ->
59+
let file =
60+
match name, src with
61+
| (None | Some ""), Some file -> file
62+
| Some file, (None | Some "") -> file
63+
| Some file, Some _file -> file
64+
| None, None -> assert false
65+
in
66+
Format.sprintf "%s:%d:%d" file line col

compiler/lib/parse_info.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ val t_of_pos : Lexing.position -> t
3434
val start_position : t -> Lexing.position
3535

3636
val t_of_position : src:string option -> Lexing.position -> t
37+
38+
val to_string : t -> string

0 commit comments

Comments
 (0)