Skip to content

Commit

Permalink
Auto merge of #122450 - Urgau:simplify-trim-paths-feature, r=michaelw…
Browse files Browse the repository at this point in the history
…oerister

Simplify trim-paths feature by merging all debuginfo options together

This PR simplifies the trim-paths feature by merging all debuginfo options together, as described in #111540 (comment).

And also do some correctness fixes found during the review.

cc `@weihanglo`
r? `@michaelwoerister`
  • Loading branch information
bors committed Mar 29, 2024
2 parents 45796d1 + fefb8f1 commit 685927a
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 183 deletions.
15 changes: 2 additions & 13 deletions compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
Expand Up @@ -89,11 +89,7 @@ impl DebugContext {
match &source_file.name {
FileName::Real(path) => {
let (dir_path, file_name) =
split_path_dir_and_file(if self.should_remap_filepaths {
path.remapped_path_if_available()
} else {
path.local_path_if_available()
});
split_path_dir_and_file(path.to_path(self.filename_display_preference));
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
let file_name = osstr_as_utf8_bytes(file_name);

Expand All @@ -115,14 +111,7 @@ impl DebugContext {
filename => {
let dir_id = line_program.default_directory();
let dummy_file_name = LineString::new(
filename
.display(if self.should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.to_string()
.into_bytes(),
filename.display(self.filename_display_preference).to_string().into_bytes(),
line_program.encoding(),
line_strings,
);
Expand Down
24 changes: 10 additions & 14 deletions compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Expand Up @@ -42,7 +42,7 @@ pub(crate) struct DebugContext {
namespace_map: DefIdMap<UnitEntryId>,
array_size_type: UnitEntryId,

should_remap_filepaths: bool,
filename_display_preference: FileNameDisplayPreference,
}

pub(crate) struct FunctionDebugContext {
Expand Down Expand Up @@ -84,22 +84,18 @@ impl DebugContext {

let mut dwarf = DwarfUnit::new(encoding);

let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
use rustc_session::config::RemapPathScopeComponents;

let filename_display_preference =
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);

let producer = producer(tcx.sess);
let comp_dir = tcx
.sess
.opts
.working_dir
.to_string_lossy(if should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.into_owned();
let comp_dir =
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();

let (name, file_info) = match tcx.sess.local_crate_source_file() {
Some(path) => {
let name = path.to_string_lossy().into_owned();
let name = path.to_string_lossy(filename_display_preference).to_string();
(name, None)
}
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
Expand Down Expand Up @@ -156,7 +152,7 @@ impl DebugContext {
stack_pointer_register,
namespace_map: DefIdMap::default(),
array_size_type,
should_remap_filepaths,
filename_display_preference,
}
}

Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Expand Up @@ -29,7 +29,8 @@ use rustc_data_structures::small_c_str::SmallCStr;
use rustc_errors::{DiagCtxt, FatalError, Level};
use rustc_fs_util::{link_or_copy, path_to_c_string};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, Lto, OutputType, Passes, SplitDwarfKind, SwitchWithOptPath};
use rustc_session::config::{self, Lto, OutputType, Passes};
use rustc_session::config::{RemapPathScopeComponents, SplitDwarfKind, SwitchWithOptPath};
use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::InnerSpan;
Expand Down Expand Up @@ -257,18 +258,17 @@ pub fn target_machine_factory(
};
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);

let should_prefer_remapped_for_split_debuginfo_paths =
sess.should_prefer_remapped_for_split_debuginfo_paths();
let file_name_display_preference =
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);

Arc::new(move |config: TargetMachineFactoryConfig| {
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
let path = path.unwrap_or_default();
let path = if should_prefer_remapped_for_split_debuginfo_paths {
path_mapping.map_prefix(path).0
} else {
path.into()
};
CString::new(path.to_str().unwrap()).unwrap()
let path = path_mapping
.to_real_filename(path)
.to_string_lossy(file_name_display_preference)
.into_owned();
CString::new(path).unwrap()
};

let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file);
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Expand Up @@ -173,8 +173,14 @@ impl GlobalFileTable {
// Since rustc generates coverage maps with relative paths, the
// compilation directory can be combined with the relative paths
// to get absolute paths, if needed.
use rustc_session::config::RemapPathScopeComponents;
use rustc_session::RemapFileNameExt;
let working_dir: &str = &tcx.sess.opts.working_dir.for_codegen(tcx.sess).to_string_lossy();
let working_dir: &str = &tcx
.sess
.opts
.working_dir
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
.to_string_lossy();

llvm::build_byte_buffer(|buffer| {
coverageinfo::write_filenames_section_to_buffer(
Expand Down
50 changes: 27 additions & 23 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Expand Up @@ -554,13 +554,16 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
) -> &'ll DIFile {
debug!(?source_file.name);

use rustc_session::RemapFileNameExt;
let filename_display_preference =
cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);

use rustc_session::config::RemapPathScopeComponents;
let (directory, file_name) = match &source_file.name {
FileName::Real(filename) => {
let working_directory = &cx.sess().opts.working_dir;
debug!(?working_directory);

if cx.sess().should_prefer_remapped_for_codegen() {
if filename_display_preference == FileNameDisplayPreference::Remapped {
let filename = cx
.sess()
.source_map()
Expand Down Expand Up @@ -623,7 +626,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
}
other => {
debug!(?other);
("".into(), other.for_codegen(cx.sess()).to_string_lossy().into_owned())
("".into(), other.display(filename_display_preference).to_string())
}
};

Expand Down Expand Up @@ -832,9 +835,11 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
codegen_unit_name: &str,
debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
) -> &'ll DIDescriptor {
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
let mut name_in_debuginfo = tcx
.sess
.local_crate_source_file()
.map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf())
.unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));

// To avoid breaking split DWARF, we need to ensure that each codegen unit
Expand Down Expand Up @@ -862,30 +867,29 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
let producer = format!("clang LLVM ({rustc_producer})");

use rustc_session::RemapFileNameExt;
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx.sess.opts.working_dir.for_codegen(tcx.sess).to_string_lossy();
let work_dir = tcx
.sess
.opts
.working_dir
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
.to_string_lossy();
let output_filenames = tcx.output_filenames(());
let split_name = if tcx.sess.target_can_use_split_dwarf() {
output_filenames
.split_dwarf_path(
tcx.sess.split_debuginfo(),
tcx.sess.opts.unstable_opts.split_dwarf_kind,
Some(codegen_unit_name),
)
// We get a path relative to the working directory from split_dwarf_path
.map(|f| {
if tcx.sess.should_prefer_remapped_for_split_debuginfo_paths() {
tcx.sess.source_map().path_mapping().map_prefix(f).0
} else {
f.into()
}
})
let split_name = if tcx.sess.target_can_use_split_dwarf()
&& let Some(f) = output_filenames.split_dwarf_path(
tcx.sess.split_debuginfo(),
tcx.sess.opts.unstable_opts.split_dwarf_kind,
Some(codegen_unit_name),
) {
// We get a path relative to the working directory from split_dwarf_path
Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
} else {
None
}
.unwrap_or_default();
let split_name = split_name.to_str().unwrap();
};
let split_name = split_name
.as_ref()
.map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
.unwrap_or_default();
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);

let dwarf_version =
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -549,17 +549,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

match source_file.name {
FileName::Real(ref original_file_name) => {
let adapted_file_name = if self.tcx.sess.should_prefer_remapped_for_codegen() {
source_map.path_mapping().to_embeddable_absolute_path(
original_file_name.clone(),
working_directory,
)
} else {
source_map.path_mapping().to_local_embeddable_absolute_path(
original_file_name.clone(),
working_directory,
)
};
// FIXME: This should probably to conditionally remapped under
// a RemapPathScopeComponents but which one?
let adapted_file_name = source_map
.path_mapping()
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);

adapted_source_file.name = FileName::Real(adapted_file_name);
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_middle/src/mir/consts.rs
@@ -1,7 +1,7 @@
use std::fmt::{self, Debug, Display, Formatter};

use rustc_hir::def_id::DefId;
use rustc_session::RemapFileNameExt;
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::{HasDataLayout, Size};

Expand Down Expand Up @@ -516,7 +516,11 @@ impl<'tcx> TyCtxt<'tcx> {
let caller = self.sess.source_map().lookup_char_pos(topmost.lo());
self.const_caller_location(
rustc_span::symbol::Symbol::intern(
&caller.file.name.for_codegen(self.sess).to_string_lossy(),
&caller
.file
.name
.for_scope(self.sess, RemapPathScopeComponents::MACRO)
.to_string_lossy(),
),
caller.line as u32,
caller.col_display as u32 + 1,
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Expand Up @@ -123,8 +123,11 @@ fn create_mappings<'tcx>(
let body_span = hir_info.body_span;

let source_file = source_map.lookup_source_file(body_span.lo());
use rustc_session::RemapFileNameExt;
let file_name = Symbol::intern(&source_file.name.for_codegen(tcx.sess).to_string_lossy());

use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
let file_name = Symbol::intern(
&source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
);

let term_for_bcb = |bcb| {
coverage_counters
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_passes/src/entry.rs
Expand Up @@ -7,6 +7,7 @@ use rustc_hir::{ItemId, Node, CRATE_HIR_ID};
use rustc_middle::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{sigpipe, CrateType, EntryFnType};
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol};

Expand Down Expand Up @@ -165,10 +166,14 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {

// There is no main function.
let mut has_filename = true;
let filename = tcx.sess.local_crate_source_file().unwrap_or_else(|| {
has_filename = false;
Default::default()
});
let filename = tcx
.sess
.local_crate_source_file()
.map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DIAGNOSTICS).to_path_buf())
.unwrap_or_else(|| {
has_filename = false;
Default::default()
});
let main_def_opt = tcx.resolutions(()).main_def;
let code = E0601;
let add_teach_note = tcx.sess.teach(code);
Expand Down
29 changes: 7 additions & 22 deletions compiler/rustc_session/src/config.rs
Expand Up @@ -990,22 +990,12 @@ bitflags::bitflags! {
const MACRO = 1 << 0;
/// Apply remappings to printed compiler diagnostics
const DIAGNOSTICS = 1 << 1;
/// Apply remappings to debug information only when they are written to
/// compiled executables or libraries, but not when they are in split
/// debuginfo files
const UNSPLIT_DEBUGINFO = 1 << 2;
/// Apply remappings to debug information only when they are written to
/// split debug information files, but not in compiled executables or
/// libraries
const SPLIT_DEBUGINFO = 1 << 3;
/// Apply remappings to the paths pointing to split debug information
/// files. Does nothing when these files are not generated.
const SPLIT_DEBUGINFO_PATH = 1 << 4;
/// Apply remappings to debug informations
const DEBUGINFO = 1 << 3;

/// An alias for macro,unsplit-debuginfo,split-debuginfo-path. This
/// ensures all paths in compiled executables or libraries are remapped
/// but not elsewhere.
const OBJECT = Self::MACRO.bits() | Self::UNSPLIT_DEBUGINFO.bits() | Self::SPLIT_DEBUGINFO_PATH.bits();
/// An alias for `macro` and `debuginfo`. This ensures all paths in compiled
/// executables or libraries are remapped but not elsewhere.
const OBJECT = Self::MACRO.bits() | Self::DEBUGINFO.bits();
}
}

Expand Down Expand Up @@ -2852,13 +2842,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
});

let remap = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
let (path, remapped) = remap.map_prefix(&working_dir);
let working_dir = if remapped {
RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) }
} else {
RealFileName::LocalPath(path.into_owned())
};
let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
let working_dir = file_mapping.to_real_filename(&working_dir);

let verbose = matches.opt_present("verbose") || unstable_opts.verbose_internals;

Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_session/src/options.rs
Expand Up @@ -433,7 +433,8 @@ mod desc {
"a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
pub const parse_proc_macro_execution_strategy: &str =
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
pub const parse_remap_path_scope: &str = "comma separated list of scopes: `macro`, `diagnostics`, `unsplit-debuginfo`, `split-debuginfo`, `split-debuginfo-path`, `object`, `all`";
pub const parse_remap_path_scope: &str =
"comma separated list of scopes: `macro`, `diagnostics`, `debuginfo`, `object`, `all`";
pub const parse_inlining_threshold: &str =
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
pub const parse_llvm_module_flag: &str = "<key>:<type>:<value>:<behavior>. Type must currently be `u32`. Behavior should be one of (`error`, `warning`, `require`, `override`, `append`, `appendunique`, `max`, `min`)";
Expand Down Expand Up @@ -1156,9 +1157,7 @@ mod parse {
*slot |= match s {
"macro" => RemapPathScopeComponents::MACRO,
"diagnostics" => RemapPathScopeComponents::DIAGNOSTICS,
"unsplit-debuginfo" => RemapPathScopeComponents::UNSPLIT_DEBUGINFO,
"split-debuginfo" => RemapPathScopeComponents::SPLIT_DEBUGINFO,
"split-debuginfo-path" => RemapPathScopeComponents::SPLIT_DEBUGINFO_PATH,
"debuginfo" => RemapPathScopeComponents::DEBUGINFO,
"object" => RemapPathScopeComponents::OBJECT,
"all" => RemapPathScopeComponents::all(),
_ => return false,
Expand Down

0 comments on commit 685927a

Please sign in to comment.