From 1e57774011ab634a04c0efd3bf2e956ee8997245 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 18 Aug 2022 17:23:36 +0000 Subject: [PATCH] Move set_function_span earlier --- src/base.rs | 2 +- src/debuginfo/line_info.rs | 44 ++++++++++++++++++++++---------------- src/debuginfo/mod.rs | 19 +++++++++++----- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/base.rs b/src/base.rs index 34dbf96be7dd..8440a03335a4 100644 --- a/src/base.rs +++ b/src/base.rs @@ -85,7 +85,7 @@ fn codegen_fn<'tcx>( let clif_comments = crate::pretty_clif::CommentWriter::new(tcx, instance); let func_debug_cx = if let Some(debug_context) = &mut cx.debug_context { - Some(debug_context.define_function(symbol_name.name)) + Some(debug_context.define_function(tcx, symbol_name.name, mir.span)) } else { None }; diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index eb1365167717..5a13b9681c0f 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -3,6 +3,7 @@ use std::ffi::OsStr; use std::path::{Component, Path}; +use crate::debuginfo::FunctionDebugContext; use crate::prelude::*; use rustc_data_structures::sync::Lrc; @@ -15,7 +16,6 @@ use cranelift_codegen::MachSrcLoc; use gimli::write::{ Address, AttributeValue, FileId, FileInfo, LineProgram, LineString, LineStringTable, - UnitEntryId, }; // OPTIMIZATION: It is cheaper to do this in one pass than using `.parent()` and `.file_name()`. @@ -121,19 +121,39 @@ fn line_program_add_file( } } -impl DebugContext { +impl FunctionDebugContext { + pub(super) fn set_function_span( + &mut self, + debug_context: &mut DebugContext, + tcx: TyCtxt<'_>, + span: Span, + ) { + let (file, line, column) = get_span_loc(tcx, span, span); + + let file_id = line_program_add_file( + &mut debug_context.dwarf.unit.line_program, + &mut debug_context.dwarf.line_strings, + &file, + ); + + let entry = debug_context.dwarf.unit.get_mut(self.entry_id); + entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id))); + entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(line)); + entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(column)); + } + pub(super) fn create_debug_lines( &mut self, + debug_context: &mut DebugContext, tcx: TyCtxt<'_>, symbol: usize, - entry_id: UnitEntryId, context: &Context, function_span: Span, source_info_set: &indexmap::IndexSet, ) -> CodeOffset { - let line_program = &mut self.dwarf.unit.line_program; + let line_program = &mut debug_context.dwarf.unit.line_program; - let line_strings = &mut self.dwarf.line_strings; + let line_strings = &mut debug_context.dwarf.line_strings; let mut last_span = None; let mut last_file = None; let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| { @@ -189,24 +209,12 @@ impl DebugContext { assert_ne!(func_end, 0); - let (function_file, function_line, function_col) = - get_span_loc(tcx, function_span, function_span); - - let function_file_id = line_program_add_file( - &mut self.dwarf.unit.line_program, - &mut self.dwarf.line_strings, - &function_file, - ); - - let entry = self.dwarf.unit.get_mut(entry_id); + let entry = debug_context.dwarf.unit.get_mut(self.entry_id); entry.set( gimli::DW_AT_low_pc, AttributeValue::Address(Address::Symbol { symbol, addend: 0 }), ); entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end))); - entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(function_file_id))); - entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(function_line)); - entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(function_col)); func_end } diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 0726807000da..169b7d1ef4c0 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -99,7 +99,12 @@ impl DebugContext { DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) } } - pub(crate) fn define_function(&mut self, name: &str) -> FunctionDebugContext { + pub(crate) fn define_function( + &mut self, + tcx: TyCtxt<'_>, + name: &str, + function_span: Span, + ) -> FunctionDebugContext { // FIXME: add to appropriate scope instead of root let scope = self.dwarf.unit.root(); @@ -110,13 +115,17 @@ impl DebugContext { entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id)); entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id)); - FunctionDebugContext { entry_id } + let mut function_debug_context = FunctionDebugContext { entry_id }; + + function_debug_context.set_function_span(self, tcx, function_span); + + function_debug_context } } impl FunctionDebugContext { pub(crate) fn finalize( - self, + mut self, debug_context: &mut DebugContext, tcx: TyCtxt<'_>, func_id: FuncId, @@ -126,10 +135,10 @@ impl FunctionDebugContext { ) { let symbol = func_id.as_u32() as usize; - let end = debug_context.create_debug_lines( + let end = self.create_debug_lines( + debug_context, tcx, symbol, - self.entry_id, context, function_span, source_info_set,