Skip to content

Commit 685927a

Browse files
committed
Auto merge of rust-lang#122450 - Urgau:simplify-trim-paths-feature, r=michaelwoerister
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 rust-lang#111540 (comment). And also do some correctness fixes found during the review. cc `@weihanglo` r? `@michaelwoerister`
2 parents 45796d1 + fefb8f1 commit 685927a

File tree

18 files changed

+141
-183
lines changed

18 files changed

+141
-183
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl DebugContext {
8989
match &source_file.name {
9090
FileName::Real(path) => {
9191
let (dir_path, file_name) =
92-
split_path_dir_and_file(if self.should_remap_filepaths {
93-
path.remapped_path_if_available()
94-
} else {
95-
path.local_path_if_available()
96-
});
92+
split_path_dir_and_file(path.to_path(self.filename_display_preference));
9793
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
9894
let file_name = osstr_as_utf8_bytes(file_name);
9995

@@ -115,14 +111,7 @@ impl DebugContext {
115111
filename => {
116112
let dir_id = line_program.default_directory();
117113
let dummy_file_name = LineString::new(
118-
filename
119-
.display(if self.should_remap_filepaths {
120-
FileNameDisplayPreference::Remapped
121-
} else {
122-
FileNameDisplayPreference::Local
123-
})
124-
.to_string()
125-
.into_bytes(),
114+
filename.display(self.filename_display_preference).to_string().into_bytes(),
126115
line_program.encoding(),
127116
line_strings,
128117
);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) struct DebugContext {
4242
namespace_map: DefIdMap<UnitEntryId>,
4343
array_size_type: UnitEntryId,
4444

45-
should_remap_filepaths: bool,
45+
filename_display_preference: FileNameDisplayPreference,
4646
}
4747

4848
pub(crate) struct FunctionDebugContext {
@@ -84,22 +84,18 @@ impl DebugContext {
8484

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

87-
let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
87+
use rustc_session::config::RemapPathScopeComponents;
88+
89+
let filename_display_preference =
90+
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
8891

8992
let producer = producer(tcx.sess);
90-
let comp_dir = tcx
91-
.sess
92-
.opts
93-
.working_dir
94-
.to_string_lossy(if should_remap_filepaths {
95-
FileNameDisplayPreference::Remapped
96-
} else {
97-
FileNameDisplayPreference::Local
98-
})
99-
.into_owned();
93+
let comp_dir =
94+
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();
95+
10096
let (name, file_info) = match tcx.sess.local_crate_source_file() {
10197
Some(path) => {
102-
let name = path.to_string_lossy().into_owned();
98+
let name = path.to_string_lossy(filename_display_preference).to_string();
10399
(name, None)
104100
}
105101
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@@ -156,7 +152,7 @@ impl DebugContext {
156152
stack_pointer_register,
157153
namespace_map: DefIdMap::default(),
158154
array_size_type,
159-
should_remap_filepaths,
155+
filename_display_preference,
160156
}
161157
}
162158

compiler/rustc_codegen_llvm/src/back/write.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use rustc_data_structures::small_c_str::SmallCStr;
2929
use rustc_errors::{DiagCtxt, FatalError, Level};
3030
use rustc_fs_util::{link_or_copy, path_to_c_string};
3131
use rustc_middle::ty::TyCtxt;
32-
use rustc_session::config::{self, Lto, OutputType, Passes, SplitDwarfKind, SwitchWithOptPath};
32+
use rustc_session::config::{self, Lto, OutputType, Passes};
33+
use rustc_session::config::{RemapPathScopeComponents, SplitDwarfKind, SwitchWithOptPath};
3334
use rustc_session::Session;
3435
use rustc_span::symbol::sym;
3536
use rustc_span::InnerSpan;
@@ -257,18 +258,17 @@ pub fn target_machine_factory(
257258
};
258259
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
259260

260-
let should_prefer_remapped_for_split_debuginfo_paths =
261-
sess.should_prefer_remapped_for_split_debuginfo_paths();
261+
let file_name_display_preference =
262+
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
262263

263264
Arc::new(move |config: TargetMachineFactoryConfig| {
264265
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
265266
let path = path.unwrap_or_default();
266-
let path = if should_prefer_remapped_for_split_debuginfo_paths {
267-
path_mapping.map_prefix(path).0
268-
} else {
269-
path.into()
270-
};
271-
CString::new(path.to_str().unwrap()).unwrap()
267+
let path = path_mapping
268+
.to_real_filename(path)
269+
.to_string_lossy(file_name_display_preference)
270+
.into_owned();
271+
CString::new(path).unwrap()
272272
};
273273

274274
let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file);

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ impl GlobalFileTable {
173173
// Since rustc generates coverage maps with relative paths, the
174174
// compilation directory can be combined with the relative paths
175175
// to get absolute paths, if needed.
176+
use rustc_session::config::RemapPathScopeComponents;
176177
use rustc_session::RemapFileNameExt;
177-
let working_dir: &str = &tcx.sess.opts.working_dir.for_codegen(tcx.sess).to_string_lossy();
178+
let working_dir: &str = &tcx
179+
.sess
180+
.opts
181+
.working_dir
182+
.for_scope(tcx.sess, RemapPathScopeComponents::MACRO)
183+
.to_string_lossy();
178184

179185
llvm::build_byte_buffer(|buffer| {
180186
coverageinfo::write_filenames_section_to_buffer(

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,16 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
554554
) -> &'ll DIFile {
555555
debug!(?source_file.name);
556556

557-
use rustc_session::RemapFileNameExt;
557+
let filename_display_preference =
558+
cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
559+
560+
use rustc_session::config::RemapPathScopeComponents;
558561
let (directory, file_name) = match &source_file.name {
559562
FileName::Real(filename) => {
560563
let working_directory = &cx.sess().opts.working_dir;
561564
debug!(?working_directory);
562565

563-
if cx.sess().should_prefer_remapped_for_codegen() {
566+
if filename_display_preference == FileNameDisplayPreference::Remapped {
564567
let filename = cx
565568
.sess()
566569
.source_map()
@@ -623,7 +626,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
623626
}
624627
other => {
625628
debug!(?other);
626-
("".into(), other.for_codegen(cx.sess()).to_string_lossy().into_owned())
629+
("".into(), other.display(filename_display_preference).to_string())
627630
}
628631
};
629632

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

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

865-
use rustc_session::RemapFileNameExt;
866870
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
867-
let work_dir = tcx.sess.opts.working_dir.for_codegen(tcx.sess).to_string_lossy();
871+
let work_dir = tcx
872+
.sess
873+
.opts
874+
.working_dir
875+
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
876+
.to_string_lossy();
868877
let output_filenames = tcx.output_filenames(());
869-
let split_name = if tcx.sess.target_can_use_split_dwarf() {
870-
output_filenames
871-
.split_dwarf_path(
872-
tcx.sess.split_debuginfo(),
873-
tcx.sess.opts.unstable_opts.split_dwarf_kind,
874-
Some(codegen_unit_name),
875-
)
876-
// We get a path relative to the working directory from split_dwarf_path
877-
.map(|f| {
878-
if tcx.sess.should_prefer_remapped_for_split_debuginfo_paths() {
879-
tcx.sess.source_map().path_mapping().map_prefix(f).0
880-
} else {
881-
f.into()
882-
}
883-
})
878+
let split_name = if tcx.sess.target_can_use_split_dwarf()
879+
&& let Some(f) = output_filenames.split_dwarf_path(
880+
tcx.sess.split_debuginfo(),
881+
tcx.sess.opts.unstable_opts.split_dwarf_kind,
882+
Some(codegen_unit_name),
883+
) {
884+
// We get a path relative to the working directory from split_dwarf_path
885+
Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
884886
} else {
885887
None
886-
}
887-
.unwrap_or_default();
888-
let split_name = split_name.to_str().unwrap();
888+
};
889+
let split_name = split_name
890+
.as_ref()
891+
.map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
892+
.unwrap_or_default();
889893
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
890894

891895
let dwarf_version =

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
549549

550550
match source_file.name {
551551
FileName::Real(ref original_file_name) => {
552-
let adapted_file_name = if self.tcx.sess.should_prefer_remapped_for_codegen() {
553-
source_map.path_mapping().to_embeddable_absolute_path(
554-
original_file_name.clone(),
555-
working_directory,
556-
)
557-
} else {
558-
source_map.path_mapping().to_local_embeddable_absolute_path(
559-
original_file_name.clone(),
560-
working_directory,
561-
)
562-
};
552+
// FIXME: This should probably to conditionally remapped under
553+
// a RemapPathScopeComponents but which one?
554+
let adapted_file_name = source_map
555+
.path_mapping()
556+
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);
563557

564558
adapted_source_file.name = FileName::Real(adapted_file_name);
565559
}

compiler/rustc_middle/src/mir/consts.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt::{self, Debug, Display, Formatter};
22

33
use rustc_hir::def_id::DefId;
4-
use rustc_session::RemapFileNameExt;
4+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
55
use rustc_span::{Span, DUMMY_SP};
66
use rustc_target::abi::{HasDataLayout, Size};
77

@@ -516,7 +516,11 @@ impl<'tcx> TyCtxt<'tcx> {
516516
let caller = self.sess.source_map().lookup_char_pos(topmost.lo());
517517
self.const_caller_location(
518518
rustc_span::symbol::Symbol::intern(
519-
&caller.file.name.for_codegen(self.sess).to_string_lossy(),
519+
&caller
520+
.file
521+
.name
522+
.for_scope(self.sess, RemapPathScopeComponents::MACRO)
523+
.to_string_lossy(),
520524
),
521525
caller.line as u32,
522526
caller.col_display as u32 + 1,

compiler/rustc_mir_transform/src/coverage/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ fn create_mappings<'tcx>(
123123
let body_span = hir_info.body_span;
124124

125125
let source_file = source_map.lookup_source_file(body_span.lo());
126-
use rustc_session::RemapFileNameExt;
127-
let file_name = Symbol::intern(&source_file.name.for_codegen(tcx.sess).to_string_lossy());
126+
127+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
128+
let file_name = Symbol::intern(
129+
&source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
130+
);
128131

129132
let term_for_bcb = |bcb| {
130133
coverage_counters

compiler/rustc_passes/src/entry.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::{ItemId, Node, CRATE_HIR_ID};
77
use rustc_middle::query::Providers;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_session::config::{sigpipe, CrateType, EntryFnType};
10+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
1011
use rustc_span::symbol::sym;
1112
use rustc_span::{Span, Symbol};
1213

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

166167
// There is no main function.
167168
let mut has_filename = true;
168-
let filename = tcx.sess.local_crate_source_file().unwrap_or_else(|| {
169-
has_filename = false;
170-
Default::default()
171-
});
169+
let filename = tcx
170+
.sess
171+
.local_crate_source_file()
172+
.map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DIAGNOSTICS).to_path_buf())
173+
.unwrap_or_else(|| {
174+
has_filename = false;
175+
Default::default()
176+
});
172177
let main_def_opt = tcx.resolutions(()).main_def;
173178
let code = E0601;
174179
let add_teach_note = tcx.sess.teach(code);

compiler/rustc_session/src/config.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -990,22 +990,12 @@ bitflags::bitflags! {
990990
const MACRO = 1 << 0;
991991
/// Apply remappings to printed compiler diagnostics
992992
const DIAGNOSTICS = 1 << 1;
993-
/// Apply remappings to debug information only when they are written to
994-
/// compiled executables or libraries, but not when they are in split
995-
/// debuginfo files
996-
const UNSPLIT_DEBUGINFO = 1 << 2;
997-
/// Apply remappings to debug information only when they are written to
998-
/// split debug information files, but not in compiled executables or
999-
/// libraries
1000-
const SPLIT_DEBUGINFO = 1 << 3;
1001-
/// Apply remappings to the paths pointing to split debug information
1002-
/// files. Does nothing when these files are not generated.
1003-
const SPLIT_DEBUGINFO_PATH = 1 << 4;
993+
/// Apply remappings to debug informations
994+
const DEBUGINFO = 1 << 3;
1004995

1005-
/// An alias for macro,unsplit-debuginfo,split-debuginfo-path. This
1006-
/// ensures all paths in compiled executables or libraries are remapped
1007-
/// but not elsewhere.
1008-
const OBJECT = Self::MACRO.bits() | Self::UNSPLIT_DEBUGINFO.bits() | Self::SPLIT_DEBUGINFO_PATH.bits();
996+
/// An alias for `macro` and `debuginfo`. This ensures all paths in compiled
997+
/// executables or libraries are remapped but not elsewhere.
998+
const OBJECT = Self::MACRO.bits() | Self::DEBUGINFO.bits();
1009999
}
10101000
}
10111001

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

2855-
let remap = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2856-
let (path, remapped) = remap.map_prefix(&working_dir);
2857-
let working_dir = if remapped {
2858-
RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) }
2859-
} else {
2860-
RealFileName::LocalPath(path.into_owned())
2861-
};
2845+
let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2846+
let working_dir = file_mapping.to_real_filename(&working_dir);
28622847

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

compiler/rustc_session/src/options.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ mod desc {
433433
"a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
434434
pub const parse_proc_macro_execution_strategy: &str =
435435
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
436-
pub const parse_remap_path_scope: &str = "comma separated list of scopes: `macro`, `diagnostics`, `unsplit-debuginfo`, `split-debuginfo`, `split-debuginfo-path`, `object`, `all`";
436+
pub const parse_remap_path_scope: &str =
437+
"comma separated list of scopes: `macro`, `diagnostics`, `debuginfo`, `object`, `all`";
437438
pub const parse_inlining_threshold: &str =
438439
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
439440
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`)";
@@ -1156,9 +1157,7 @@ mod parse {
11561157
*slot |= match s {
11571158
"macro" => RemapPathScopeComponents::MACRO,
11581159
"diagnostics" => RemapPathScopeComponents::DIAGNOSTICS,
1159-
"unsplit-debuginfo" => RemapPathScopeComponents::UNSPLIT_DEBUGINFO,
1160-
"split-debuginfo" => RemapPathScopeComponents::SPLIT_DEBUGINFO,
1161-
"split-debuginfo-path" => RemapPathScopeComponents::SPLIT_DEBUGINFO_PATH,
1160+
"debuginfo" => RemapPathScopeComponents::DEBUGINFO,
11621161
"object" => RemapPathScopeComponents::OBJECT,
11631162
"all" => RemapPathScopeComponents::all(),
11641163
_ => return false,

0 commit comments

Comments
 (0)