Move set_function_span earlier

This commit is contained in:
bjorn3 2022-08-18 17:23:36 +00:00
parent 01be0ddacf
commit 1e57774011
3 changed files with 41 additions and 24 deletions

View file

@ -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
};

View file

@ -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<SourceInfo>,
) -> 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
}

View file

@ -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,