Skip to content

Commit 9750e6d

Browse files
committed
[RFC 3127 - Trim Paths]: Condition remapped filepath on remap scopes
1 parent d4f4253 commit 9750e6d

File tree

16 files changed

+329
-81
lines changed

16 files changed

+329
-81
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,18 @@ 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+
65+
use rustc_session::{config::RemapPathScopeComponents, FileNameExt};
6466
base::MacEager::expr(
65-
cx.expr_str(topmost, Symbol::intern(&loc.file.name.prefer_remapped().to_string_lossy())),
67+
cx.expr_str(
68+
topmost,
69+
Symbol::intern(
70+
&loc.file
71+
.name
72+
.prefer_remapped(cx.sess, RemapPathScopeComponents::MACRO)
73+
.to_string_lossy(),
74+
),
75+
),
6676
)
6777
}
6878

compiler/rustc_codegen_cranelift/src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,12 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
415415
// Note: must be kept in sync with get_caller_location from cg_ssa
416416
pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> {
417417
let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| {
418+
use rustc_session::FileNameExt;
418419
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
419420
let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo());
420421
let const_loc = fx.tcx.const_caller_location((
421422
rustc_span::symbol::Symbol::intern(
422-
&caller.file.name.prefer_remapped().to_string_lossy(),
423+
&caller.file.name.prefer_remapped_codegen(&fx.tcx.sess).to_string_lossy(),
423424
),
424425
caller.line as u32,
425426
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
@@ -100,7 +100,11 @@ impl DebugContext {
100100
match &source_file.name {
101101
FileName::Real(path) => {
102102
let (dir_path, file_name) =
103-
split_path_dir_and_file(path.remapped_path_if_available());
103+
split_path_dir_and_file(if self.should_remap_filepaths {
104+
path.remapped_path_if_available()
105+
} else {
106+
path.local_path_if_available()
107+
});
104108
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
105109
let file_name = osstr_as_utf8_bytes(file_name);
106110

@@ -121,7 +125,14 @@ impl DebugContext {
121125
filename => {
122126
let dir_id = line_program.default_directory();
123127
let dummy_file_name = LineString::new(
124-
filename.prefer_remapped().to_string().into_bytes(),
128+
filename
129+
.display(if self.should_remap_filepaths {
130+
FileNameDisplayPreference::Remapped
131+
} else {
132+
FileNameDisplayPreference::Local
133+
})
134+
.to_string()
135+
.into_bytes(),
125136
line_program.encoding(),
126137
line_strings,
127138
);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

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

3434
dwarf: DwarfUnit,
3535
unit_range_list: RangeList,
36+
37+
should_remap_filepaths: bool,
3638
}
3739

3840
pub(crate) struct FunctionDebugContext {
@@ -65,12 +67,18 @@ impl DebugContext {
6567

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

70+
let should_remap_filepaths = tcx.sess.should_prefer_remapped_for_codegen();
71+
6872
let producer = producer();
6973
let comp_dir = tcx
7074
.sess
7175
.opts
7276
.working_dir
73-
.to_string_lossy(FileNameDisplayPreference::Remapped)
77+
.to_string_lossy(if should_remap_filepaths {
78+
FileNameDisplayPreference::Remapped
79+
} else {
80+
FileNameDisplayPreference::Local
81+
})
7482
.into_owned();
7583
let (name, file_info) = match tcx.sess.local_crate_source_file() {
7684
Some(path) => {
@@ -104,7 +112,12 @@ impl DebugContext {
104112
root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0)));
105113
}
106114

107-
DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()) }
115+
DebugContext {
116+
endian,
117+
dwarf,
118+
unit_range_list: RangeList(Vec::new()),
119+
should_remap_filepaths,
120+
}
108121
}
109122

110123
pub(crate) fn define_function(

compiler/rustc_codegen_llvm/src/back/write.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,16 @@ pub fn target_machine_factory(
216216

217217
let force_emulated_tls = sess.target.force_emulated_tls;
218218

219+
let should_prefer_remapped_for_split_debuginfo_paths =
220+
sess.should_prefer_remapped_for_split_debuginfo_paths();
221+
219222
Arc::new(move |config: TargetMachineFactoryConfig| {
220-
let split_dwarf_file =
221-
path_mapping.map_prefix(config.split_dwarf_file.unwrap_or_default()).0;
223+
let split_dwarf_file = config.split_dwarf_file.unwrap_or_default();
224+
let split_dwarf_file = if should_prefer_remapped_for_split_debuginfo_paths {
225+
path_mapping.map_prefix(split_dwarf_file).0
226+
} else {
227+
split_dwarf_file.into()
228+
};
222229
let split_dwarf_file = CString::new(split_dwarf_file.to_str().unwrap()).unwrap();
223230

224231
let tm = unsafe {

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ impl CoverageMapGenerator {
131131
// compilation directory can be combined with the relative paths
132132
// to get absolute paths, if needed.
133133
let working_dir = Symbol::intern(
134-
&tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy(),
134+
&if tcx.sess.should_prefer_remapped_for_codegen() {
135+
tcx.sess.opts.working_dir.remapped_path_if_available()
136+
} else {
137+
tcx.sess.opts.working_dir.local_path_if_available()
138+
}
139+
.to_string_lossy(),
135140
);
136141
filenames.insert(working_dir);
137142
Self { filenames }

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+80-36
Original file line numberDiff line numberDiff line change
@@ -539,48 +539,77 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
539539
) -> &'ll DIFile {
540540
debug!(?source_file.name);
541541

542+
use rustc_session::FileNameExt;
542543
let (directory, file_name) = match &source_file.name {
543544
FileName::Real(filename) => {
544545
let working_directory = &cx.sess().opts.working_dir;
545546
debug!(?working_directory);
546547

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

586615
let hash_kind = match source_file.src_hash.kind {
@@ -815,7 +844,13 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
815844
let producer = format!("clang LLVM ({rustc_producer})");
816845

817846
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
818-
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
847+
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(
848+
if tcx.sess.should_prefer_remapped_for_codegen() {
849+
FileNameDisplayPreference::Remapped
850+
} else {
851+
FileNameDisplayPreference::Local
852+
},
853+
);
819854
let flags = "\0";
820855
let output_filenames = tcx.output_filenames(());
821856
let split_name = if tcx.sess.target_can_use_split_dwarf() {
@@ -826,14 +861,23 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
826861
Some(codegen_unit_name),
827862
)
828863
// We get a path relative to the working directory from split_dwarf_path
829-
.map(|f| tcx.sess.source_map().path_mapping().map_prefix(f).0)
864+
.map(|f| {
865+
if tcx.sess.should_prefer_remapped_for_split_debuginfo_paths() {
866+
tcx.sess.source_map().path_mapping().map_prefix(f).0
867+
} else {
868+
f.into()
869+
}
870+
})
830871
} else {
831872
None
832873
}
833874
.unwrap_or_default();
834875
let split_name = split_name.to_str().unwrap();
835876
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
836877

878+
debug!(?name_in_debuginfo);
879+
debug!(?work_dir);
880+
debug!(?split_name);
837881
unsafe {
838882
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
839883
debug_context.builder,

compiler/rustc_codegen_ssa/src/mir/block.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14791479
let tcx = bx.tcx();
14801480

14811481
let mut span_to_caller_location = |span: Span| {
1482+
use rustc_session::FileNameExt;
14821483
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
14831484
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
14841485
let const_loc = tcx.const_caller_location((
1485-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
1486+
Symbol::intern(
1487+
&caller.file.name.prefer_remapped_codegen(self.cx.sess()).to_string_lossy(),
1488+
),
14861489
caller.line as u32,
14871490
caller.col_display as u32 + 1,
14881491
));

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, FileNameExt};
117119
(
118-
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
120+
Symbol::intern(
121+
&caller
122+
.file
123+
.name
124+
.prefer_remapped(&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
@@ -518,9 +518,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
518518
// the remapped version -- as is necessary for reproducible builds.
519519
let mut source_file = match source_file.name {
520520
FileName::Real(ref original_file_name) => {
521-
let adapted_file_name = source_map
522-
.path_mapping()
523-
.to_embeddable_absolute_path(original_file_name.clone(), working_directory);
521+
let adapted_file_name = if self.tcx.sess.should_prefer_remapped_for_codegen() {
522+
source_map.path_mapping().to_embeddable_absolute_path(
523+
original_file_name.clone(),
524+
working_directory,
525+
)
526+
} else {
527+
source_map.path_mapping().to_local_embeddable_absolute_path(
528+
original_file_name.clone(),
529+
working_directory,
530+
)
531+
};
524532

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

compiler/rustc_mir_transform/src/coverage/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,11 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
297297
let tcx = self.tcx;
298298
let source_map = tcx.sess.source_map();
299299
let body_span = self.body_span;
300-
let file_name = Symbol::intern(&self.source_file.name.prefer_remapped().to_string_lossy());
300+
301+
use rustc_session::FileNameExt;
302+
let file_name = Symbol::intern(
303+
&self.source_file.name.prefer_remapped_codegen(self.tcx.sess).to_string_lossy(),
304+
);
301305

302306
let mut bcb_counters = IndexVec::from_elem_n(None, self.basic_coverage_blocks.num_nodes());
303307
for covspan in coverage_spans {

0 commit comments

Comments
 (0)