@@ -48,30 +48,6 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
4848 }
4949}
5050
51- fn get_span_loc ( tcx : TyCtxt < ' _ > , function_span : Span , span : Span ) -> ( Lrc < SourceFile > , u64 , u64 ) {
52- // Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
53- // In order to have a good line stepping behavior in debugger, we overwrite debug
54- // locations of macro expansions with that of the outermost expansion site
55- // (unless the crate is being compiled with `-Z debug-macros`).
56- let span = if !span. from_expansion ( ) || tcx. sess . opts . unstable_opts . debug_macros {
57- span
58- } else {
59- // Walk up the macro expansion chain until we reach a non-expanded span.
60- // We also stop at the function body level because no line stepping can occur
61- // at the level above that.
62- rustc_span:: hygiene:: walk_chain ( span, function_span. ctxt ( ) )
63- } ;
64-
65- match tcx. sess . source_map ( ) . lookup_line ( span. lo ( ) ) {
66- Ok ( SourceFileAndLine { sf : file, line } ) => {
67- let line_pos = file. line_begin_pos ( span. lo ( ) ) ;
68-
69- ( file, u64:: try_from ( line) . unwrap ( ) + 1 , u64:: from ( ( span. lo ( ) - line_pos) . to_u32 ( ) ) + 1 )
70- }
71- Err ( file) => ( file, 0 , 0 ) ,
72- }
73- }
74-
7551const MD5_LEN : usize = 16 ;
7652
7753fn make_file_info ( hash : SourceFileHash ) -> Option < FileInfo > {
@@ -85,6 +61,38 @@ fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
8561}
8662
8763impl DebugContext {
64+ pub ( crate ) fn get_span_loc (
65+ tcx : TyCtxt < ' _ > ,
66+ function_span : Span ,
67+ span : Span ,
68+ ) -> ( Lrc < SourceFile > , u64 , u64 ) {
69+ // Based on https://github.com/rust-lang/rust/blob/e369d87b015a84653343032833d65d0545fd3f26/src/librustc_codegen_ssa/mir/mod.rs#L116-L131
70+ // In order to have a good line stepping behavior in debugger, we overwrite debug
71+ // locations of macro expansions with that of the outermost expansion site
72+ // (unless the crate is being compiled with `-Z debug-macros`).
73+ let span = if !span. from_expansion ( ) || tcx. sess . opts . unstable_opts . debug_macros {
74+ span
75+ } else {
76+ // Walk up the macro expansion chain until we reach a non-expanded span.
77+ // We also stop at the function body level because no line stepping can occur
78+ // at the level above that.
79+ rustc_span:: hygiene:: walk_chain ( span, function_span. ctxt ( ) )
80+ } ;
81+
82+ match tcx. sess . source_map ( ) . lookup_line ( span. lo ( ) ) {
83+ Ok ( SourceFileAndLine { sf : file, line } ) => {
84+ let line_pos = file. line_begin_pos ( span. lo ( ) ) ;
85+
86+ (
87+ file,
88+ u64:: try_from ( line) . unwrap ( ) + 1 ,
89+ u64:: from ( ( span. lo ( ) - line_pos) . to_u32 ( ) ) + 1 ,
90+ )
91+ }
92+ Err ( file) => ( file, 0 , 0 ) ,
93+ }
94+ }
95+
8896 pub ( crate ) fn add_source_file ( & mut self , source_file : & SourceFile ) -> FileId {
8997 let line_program: & mut LineProgram = & mut self . dwarf . unit . line_program ;
9098 let line_strings: & mut LineStringTable = & mut self . dwarf . line_strings ;
@@ -124,63 +132,26 @@ impl DebugContext {
124132}
125133
126134impl FunctionDebugContext {
127- pub ( super ) fn set_function_span (
128- & mut self ,
129- debug_context : & mut DebugContext ,
130- tcx : TyCtxt < ' _ > ,
131- span : Span ,
132- ) {
133- let ( file, line, column) = get_span_loc ( tcx, span, span) ;
134-
135- let file_id = debug_context. add_source_file ( & file) ;
136-
137- let entry = debug_context. dwarf . unit . get_mut ( self . entry_id ) ;
138- entry. set ( gimli:: DW_AT_decl_file , AttributeValue :: FileIndex ( Some ( file_id) ) ) ;
139- entry. set ( gimli:: DW_AT_decl_line , AttributeValue :: Udata ( line) ) ;
140- entry. set ( gimli:: DW_AT_decl_column , AttributeValue :: Udata ( column) ) ;
135+ pub ( crate ) fn add_dbg_loc ( & mut self , file_id : FileId , line : u64 , column : u64 ) -> SourceLoc {
136+ let ( index, _) = self . source_loc_set . insert_full ( ( file_id, line, column) ) ;
137+ SourceLoc :: new ( u32:: try_from ( index) . unwrap ( ) )
141138 }
142139
143140 pub ( super ) fn create_debug_lines (
144141 & mut self ,
145142 debug_context : & mut DebugContext ,
146- tcx : TyCtxt < ' _ > ,
147143 symbol : usize ,
148144 context : & Context ,
149- function_span : Span ,
150- source_info_set : & indexmap:: IndexSet < SourceInfo > ,
151145 ) -> CodeOffset {
152- let mut last_span = None ;
153- let mut last_file = None ;
154- let mut create_row_for_span = |debug_context : & mut DebugContext , span : Span | {
155- if let Some ( last_span) = last_span {
156- if span == last_span {
157- debug_context. dwarf . unit . line_program . generate_row ( ) ;
158- return ;
159- }
160- }
161- last_span = Some ( span) ;
162-
163- let ( file, line, col) = get_span_loc ( tcx, function_span, span) ;
146+ let create_row_for_span =
147+ |debug_context : & mut DebugContext , source_loc : ( FileId , u64 , u64 ) | {
148+ let ( file_id, line, col) = source_loc;
164149
165- // line_program_add_file is very slow.
166- // Optimize for the common case of the current file not being changed.
167- let current_file_changed = if let Some ( last_file) = & last_file {
168- // If the allocations are not equal, then the files may still be equal, but that
169- // is not a problem, as this is just an optimization.
170- !rustc_data_structures:: sync:: Lrc :: ptr_eq ( last_file, & file)
171- } else {
172- true
173- } ;
174- if current_file_changed {
175- let file_id = debug_context. add_source_file ( & file) ;
176150 debug_context. dwarf . unit . line_program . row ( ) . file = file_id;
177- last_file = Some ( file) ;
178- }
179-
180- debug_context. dwarf . unit . line_program . row ( ) . line = line;
181- debug_context. dwarf . unit . line_program . row ( ) . column = col;
182- debug_context. dwarf . unit . line_program . generate_row ( ) ;
183- } ;
151+ debug_context. dwarf . unit . line_program . row ( ) . line = line;
152+ debug_context. dwarf . unit . line_program . row ( ) . column = col;
153+ debug_context. dwarf . unit . line_program . generate_row ( ) ;
154+ } ;
184155
185156 debug_context
186157 . dwarf
@@ -194,10 +165,10 @@ impl FunctionDebugContext {
194165 for & MachSrcLoc { start, end, loc } in mcr. buffer . get_srclocs_sorted ( ) {
195166 debug_context. dwarf . unit . line_program . row ( ) . address_offset = u64:: from ( start) ;
196167 if !loc. is_default ( ) {
197- let source_info = * source_info_set . get_index ( loc. bits ( ) as usize ) . unwrap ( ) ;
198- create_row_for_span ( debug_context, source_info . span ) ;
168+ let source_loc = * self . source_loc_set . get_index ( loc. bits ( ) as usize ) . unwrap ( ) ;
169+ create_row_for_span ( debug_context, source_loc ) ;
199170 } else {
200- create_row_for_span ( debug_context, function_span ) ;
171+ create_row_for_span ( debug_context, self . function_source_loc ) ;
201172 }
202173 func_end = end;
203174 }
0 commit comments