Skip to content

Commit 94c4e5c

Browse files
committed
Auto merge of #115214 - Urgau:rfc-3127-trim-paths, r=compiler-errors
Implement rustc part of RFC 3127 trim-paths This PR implements (or at least tries to) [RFC 3127 trim-paths](#111540), the rustc part. That is `-Zremap-path-scope` with all of it's components/scopes. `@rustbot` label: +F-trim-paths
2 parents e76cb8c + 5c41de1 commit 94c4e5c

File tree

34 files changed

+582
-94
lines changed

34 files changed

+582
-94
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4457,6 +4457,7 @@ dependencies = [
44574457
name = "rustc_session"
44584458
version = "0.0.0"
44594459
dependencies = [
4460+
"bitflags 1.3.2",
44604461
"getopts",
44614462
"libc",
44624463
"rustc_ast",

compiler/rustc_builtin_macros/src/source_util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ pub fn expand_file(
6161

6262
let topmost = cx.expansion_cause().unwrap_or(sp);
6363
let loc = cx.source_map().lookup_char_pos(topmost.lo());
64-
base::MacEager::expr(
65-
cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())),
66-
)
64+
65+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
66+
base::MacEager::expr(cx.expr_str(
67+
topmost,
68+
Symbol::intern(
69+
&loc.file.name.for_scope(cx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
70+
),
71+
))
6772
}
6873

6974
pub fn expand_stringify(

compiler/rustc_codegen_cranelift/src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,12 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
414414
// Note: must be kept in sync with get_caller_location from cg_ssa
415415
pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> {
416416
let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| {
417+
use rustc_session::RemapFileNameExt;
417418
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
418419
let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo());
419420
let const_loc = fx.tcx.const_caller_location((
420421
rustc_span::symbol::Symbol::intern(
421-
&caller.file.name.prefer_remapped().to_string_lossy(),
422+
&caller.file.name.for_codegen(&fx.tcx.sess).to_string_lossy(),
422423
),
423424
caller.line as u32,
424425
caller.col_display as u32 + 1,

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ impl DebugContext {
9595
match &source_file.name {
9696
FileName::Real(path) => {
9797
let (dir_path, file_name) =
98-
split_path_dir_and_file(path.remapped_path_if_available());
98+
split_path_dir_and_file(if self.should_remap_filepaths {
99+
path.remapped_path_if_available()
100+
} else {
101+
path.local_path_if_available()
102+
});
99103
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
100104
let file_name = osstr_as_utf8_bytes(file_name);
101105

@@ -116,7 +120,14 @@ impl DebugContext {
116120
filename => {
117121
let dir_id = line_program.default_directory();
118122
let dummy_file_name = LineString::new(
119-
filename.prefer_remapped().to_string().into_bytes(),
123+
filename
124+
.display(if self.should_remap_filepaths {
125+
FileNameDisplayPreference::Remapped
126+
} else {
127+
FileNameDisplayPreference::Local
128+
})
129+
.to_string()
130+
.into_bytes(),
120131
line_program.encoding(),
121132
line_strings,
122133
);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub(crate) struct DebugContext {
3131

3232
dwarf: DwarfUnit,
3333
unit_range_list: RangeList,
34+
35+
should_remap_filepaths: bool,
3436
}
3537

3638
pub(crate) struct FunctionDebugContext {
@@ -63,12 +65,18 @@ impl DebugContext {
6365

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

68+
let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
69+
6670
let producer = producer();
6771
let comp_dir = tcx
6872
.sess
6973
.opts
7074
.working_dir
71-
.to_string_lossy(FileNameDisplayPreference::Remapped)
75+
.to_string_lossy(if should_remap_filepaths {
76+
FileNameDisplayPreference::Remapped
77+
} else {
78+
FileNameDisplayPreference::Local
79+
})
7280
.into_owned();
7381
let (name, file_info) = match tcx.sess.local_crate_source_file() {
7482
Some(path) => {
@@ -102,7 +110,12 @@ impl DebugContext {
102110
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
103111
}
104112

105-
DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
113+
DebugContext {
114+
endian,
115+
dwarf,
116+
unit_range_list: RangeList(Vec::new()),
117+
should_remap_filepaths,
118+
}
106119
}
107120

108121
pub(crate) fn define_function(

compiler/rustc_codegen_llvm/src/back/write.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,17 @@ pub fn target_machine_factory(
259259
};
260260
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
261261

262+
let should_prefer_remapped_for_split_debuginfo_paths =
263+
sess.should_prefer_remapped_for_split_debuginfo_paths();
264+
262265
Arc::new(move |config: TargetMachineFactoryConfig| {
263266
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
264-
let path = path_mapping.map_prefix(path.unwrap_or_default()).0;
267+
let path = path.unwrap_or_default();
268+
let path = if should_prefer_remapped_for_split_debuginfo_paths {
269+
path_mapping.map_prefix(path).0
270+
} else {
271+
path.into()
272+
};
265273
CString::new(path.to_str().unwrap()).unwrap()
266274
};
267275

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ impl GlobalFileTable {
123123
// Since rustc generates coverage maps with relative paths, the
124124
// compilation directory can be combined with the relative paths
125125
// to get absolute paths, if needed.
126-
let working_dir = Symbol::intern(
127-
&tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy(),
128-
);
126+
use rustc_session::RemapFileNameExt;
127+
let working_dir =
128+
Symbol::intern(&tcx.sess.opts.working_dir.for_codegen(&tcx.sess).to_string_lossy());
129129
global_file_table.insert(working_dir);
130130
Self { global_file_table }
131131
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+72-36
Original file line numberDiff line numberDiff line change
@@ -547,48 +547,77 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
547547
) -> &'ll DIFile {
548548
debug!(?source_file.name);
549549

550+
use rustc_session::RemapFileNameExt;
550551
let (directory, file_name) = match &source_file.name {
551552
FileName::Real(filename) => {
552553
let working_directory = &cx.sess().opts.working_dir;
553554
debug!(?working_directory);
554555

555-
let filename = cx
556-
.sess()
557-
.source_map()
558-
.path_mapping()
559-
.to_embeddable_absolute_path(filename.clone(), working_directory);
560-
561-
// Construct the absolute path of the file
562-
let abs_path = filename.remapped_path_if_available();
563-
debug!(?abs_path);
564-
565-
if let Ok(rel_path) =
566-
abs_path.strip_prefix(working_directory.remapped_path_if_available())
567-
{
568-
// If the compiler's working directory (which also is the DW_AT_comp_dir of
569-
// the compilation unit) is a prefix of the path we are about to emit, then
570-
// only emit the part relative to the working directory.
571-
// Because of path remapping we sometimes see strange things here: `abs_path`
572-
// might actually look like a relative path
573-
// (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
574-
// taking the working directory into account, downstream tooling will
575-
// interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
576-
// which makes no sense. Usually in such cases the working directory will also
577-
// be remapped to `<crate-name-and-version>` or some other prefix of the path
578-
// we are remapping, so we end up with
579-
// `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
580-
// By moving the working directory portion into the `directory` part of the
581-
// DIFile, we allow LLVM to emit just the relative path for DWARF, while
582-
// still emitting the correct absolute path for CodeView.
583-
(
584-
working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
585-
rel_path.to_string_lossy().into_owned(),
586-
)
556+
if cx.sess().should_prefer_remapped_for_codegen() {
557+
let filename = cx
558+
.sess()
559+
.source_map()
560+
.path_mapping()
561+
.to_embeddable_absolute_path(filename.clone(), working_directory);
562+
563+
// Construct the absolute path of the file
564+
let abs_path = filename.remapped_path_if_available();
565+
debug!(?abs_path);
566+
567+
if let Ok(rel_path) =
568+
abs_path.strip_prefix(working_directory.remapped_path_if_available())
569+
{
570+
// If the compiler's working directory (which also is the DW_AT_comp_dir of
571+
// the compilation unit) is a prefix of the path we are about to emit, then
572+
// only emit the part relative to the working directory.
573+
// Because of path remapping we sometimes see strange things here: `abs_path`
574+
// might actually look like a relative path
575+
// (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
576+
// taking the working directory into account, downstream tooling will
577+
// interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
578+
// which makes no sense. Usually in such cases the working directory will also
579+
// be remapped to `<crate-name-and-version>` or some other prefix of the path
580+
// we are remapping, so we end up with
581+
// `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
582+
// By moving the working directory portion into the `directory` part of the
583+
// DIFile, we allow LLVM to emit just the relative path for DWARF, while
584+
// still emitting the correct absolute path for CodeView.
585+
(
586+
working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
587+
rel_path.to_string_lossy().into_owned(),
588+
)
589+
} else {
590+
("".into(), abs_path.to_string_lossy().into_owned())
591+
}
587592
} else {
588-
("".into(), abs_path.to_string_lossy().into_owned())
593+
let working_directory = working_directory.local_path_if_available();
594+
let filename = filename.local_path_if_available();
595+
596+
debug!(?working_directory, ?filename);
597+
598+
let abs_path: Cow<'_, Path> = if filename.is_absolute() {
599+
filename.into()
600+
} else {
601+
let mut p = PathBuf::new();
602+
p.push(working_directory);
603+
p.push(filename);
604+
p.into()
605+
};
606+
607+
if let Ok(rel_path) = abs_path.strip_prefix(working_directory) {
608+
(
609+
working_directory.to_string_lossy().into(),
610+
rel_path.to_string_lossy().into_owned(),
611+
)
612+
} else {
613+
("".into(), abs_path.to_string_lossy().into_owned())
614+
}
589615
}
590616
}
591-
other => ("".into(), other.prefer_remapped().to_string_lossy().into_owned()),
617+
other => {
618+
debug!(?other);
619+
("".into(), other.for_codegen(cx.sess()).to_string_lossy().into_owned())
620+
}
592621
};
593622

594623
let hash_kind = match source_file.src_hash.kind {
@@ -822,8 +851,9 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
822851
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
823852
let producer = format!("clang LLVM ({rustc_producer})");
824853

854+
use rustc_session::RemapFileNameExt;
825855
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
826-
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
856+
let work_dir = tcx.sess.opts.working_dir.for_codegen(&tcx.sess).to_string_lossy();
827857
let flags = "\0";
828858
let output_filenames = tcx.output_filenames(());
829859
let split_name = if tcx.sess.target_can_use_split_dwarf() {
@@ -834,7 +864,13 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
834864
Some(codegen_unit_name),
835865
)
836866
// We get a path relative to the working directory from split_dwarf_path
837-
.map(|f| tcx.sess.source_map().path_mapping().map_prefix(f).0)
867+
.map(|f| {
868+
if tcx.sess.should_prefer_remapped_for_split_debuginfo_paths() {
869+
tcx.sess.source_map().path_mapping().map_prefix(f).0
870+
} else {
871+
f.into()
872+
}
873+
})
838874
} else {
839875
None
840876
}

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1454,10 +1454,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14541454
let tcx = bx.tcx();
14551455

14561456
let mut span_to_caller_location = |span: Span| {
1457+
use rustc_session::RemapFileNameExt;
14571458
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
14581459
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
14591460
let const_loc = tcx.const_caller_location((
1460-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
1461+
Symbol::intern(&caller.file.name.for_codegen(self.cx.sess()).to_string_lossy()),
14611462
caller.line as u32,
14621463
caller.col_display as u32 + 1,
14631464
));

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
114114
pub(crate) fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
115115
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
116116
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
117+
118+
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
117119
(
118-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
120+
Symbol::intern(
121+
&caller
122+
.file
123+
.name
124+
.for_scope(&self.tcx.sess, RemapPathScopeComponents::DIAGNOSTICS)
125+
.to_string_lossy(),
126+
),
119127
u32::try_from(caller.line).unwrap(),
120128
u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(),
121129
)

compiler/rustc_metadata/src/rmeta/encoder.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
525525
// the remapped version -- as is necessary for reproducible builds.
526526
let mut source_file = match source_file.name {
527527
FileName::Real(ref original_file_name) => {
528-
let adapted_file_name = source_map
529-
.path_mapping()
530-
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);
528+
let adapted_file_name = if self.tcx.sess.should_prefer_remapped_for_codegen() {
529+
source_map.path_mapping().to_embeddable_absolute_path(
530+
original_file_name.clone(),
531+
working_directory,
532+
)
533+
} else {
534+
source_map.path_mapping().to_local_embeddable_absolute_path(
535+
original_file_name.clone(),
536+
working_directory,
537+
)
538+
};
531539

532540
if adapted_file_name != *original_file_name {
533541
let mut adapted: SourceFile = (**source_file).clone();

compiler/rustc_mir_transform/src/coverage/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
212212
let tcx = self.tcx;
213213
let source_map = tcx.sess.source_map();
214214
let body_span = self.body_span;
215-
let file_name = Symbol::intern(&self.source_file.name.prefer_remapped().to_string_lossy());
215+
216+
use rustc_session::RemapFileNameExt;
217+
let file_name =
218+
Symbol::intern(&self.source_file.name.for_codegen(self.tcx.sess).to_string_lossy());
216219

217220
for (bcb, spans) in coverage_spans.bcbs_with_coverage_spans() {
218221
let counter_kind = self.coverage_counters.take_bcb_counter(bcb).unwrap_or_else(|| {

compiler/rustc_session/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7+
bitflags = "1.2.1"
78
getopts = "0.2"
89
rustc_macros = { path = "../rustc_macros" }
910
tracing = "0.1"

0 commit comments

Comments
 (0)