Skip to content

Commit

Permalink
Replace RemapFileNameExt::for_codegen with explicit calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Mar 15, 2024
1 parent ce62d57 commit 84e54cd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 40 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Expand Up @@ -62,7 +62,9 @@ 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 should_remap_filepaths =
tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);

let producer = producer(tcx.sess);
let comp_dir = tcx
Expand Down
6 changes: 4 additions & 2 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,7 +258,8 @@ pub fn target_machine_factory(
};
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);

let should_prefer_remapped_paths = sess.should_prefer_remapped_for_codegen();
let should_prefer_remapped_paths =
sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);

Arc::new(move |config: TargetMachineFactoryConfig| {
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Expand Up @@ -174,8 +174,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::DEBUGINFO)
.to_string_lossy();

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

use rustc_session::RemapFileNameExt;
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
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 cx.sess().should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
let filename = cx
.sess()
.source_map()
Expand Down Expand Up @@ -623,7 +623,13 @@ 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
.for_scope(cx.sess(), RemapPathScopeComponents::DEBUGINFO)
.to_string_lossy()
.into_owned(),
)
}
};

Expand Down Expand Up @@ -862,9 +868,14 @@ 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;
use rustc_session::{config::RemapPathScopeComponents, 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
Expand All @@ -875,7 +886,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
)
// We get a path relative to the working directory from split_dwarf_path
.map(|f| {
if tcx.sess.should_prefer_remapped_for_codegen() {
if tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
tcx.sess.source_map().path_mapping().map_prefix(f).0
} else {
f.into()
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -21,7 +21,7 @@ use rustc_middle::ty::fast_reject::{self, TreatParams};
use rustc_middle::ty::{AssocItemContainer, SymbolName};
use rustc_middle::util::common::to_readable_str;
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
use rustc_session::config::{CrateType, OptLevel};
use rustc_session::config::{CrateType, OptLevel, RemapPathScopeComponents};
use rustc_span::hygiene::HygieneEncodeContext;
use rustc_span::symbol::sym;
use rustc_span::{
Expand Down Expand Up @@ -549,7 +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() {
let adapted_file_name = if self
.tcx
.sess
.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO)
{
source_map.path_mapping().to_embeddable_absolute_path(
original_file_name.clone(),
working_directory,
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;
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::DEBUGINFO)
.to_string_lossy(),
),
caller.line as u32,
caller.col_display as u32 + 1,
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Expand Up @@ -125,8 +125,14 @@ 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::DEBUGINFO)
.to_string_lossy(),
);

let term_for_bcb = |bcb| {
coverage_counters
Expand Down
29 changes: 5 additions & 24 deletions compiler/rustc_session/src/session.rs
Expand Up @@ -252,7 +252,8 @@ impl Session {

pub fn local_crate_source_file(&self) -> Option<PathBuf> {
let path = self.io.input.opt_path()?;
if self.should_prefer_remapped_for_codegen() {
// FIXME: The remap path scope should probably not be hardcoded.
if self.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned())
} else {
Some(path.to_path_buf())
Expand Down Expand Up @@ -886,8 +887,8 @@ impl Session {
self.opts.cg.link_dead_code.unwrap_or(false)
}

pub fn should_prefer_remapped_for_codegen(&self) -> bool {
self.opts.unstable_opts.remap_path_scope.contains(RemapPathScopeComponents::DEBUGINFO)
pub fn should_prefer_remapped(&self, scope: RemapPathScopeComponents) -> bool {
self.opts.unstable_opts.remap_path_scope.contains(scope)
}
}

Expand Down Expand Up @@ -1440,12 +1441,8 @@ pub trait RemapFileNameExt {

/// Returns a possibly remapped filename based on the passed scope and remap cli options.
///
/// One and only one scope should be passed to this method. For anything related to
/// "codegen" see the [`RemapFileNameExt::for_codegen`] method.
/// One and only one scope should be passed to this method, it will panic otherwise.
fn for_scope(&self, sess: &Session, scope: RemapPathScopeComponents) -> Self::Output<'_>;

/// Return a possibly remapped filename, to be used in "codegen" related parts.
fn for_codegen(&self, sess: &Session) -> Self::Output<'_>;
}

impl RemapFileNameExt for rustc_span::FileName {
Expand All @@ -1462,14 +1459,6 @@ impl RemapFileNameExt for rustc_span::FileName {
self.prefer_local()
}
}

fn for_codegen(&self, sess: &Session) -> Self::Output<'_> {
if sess.should_prefer_remapped_for_codegen() {
self.prefer_remapped_unconditionaly()
} else {
self.prefer_local()
}
}
}

impl RemapFileNameExt for rustc_span::RealFileName {
Expand All @@ -1486,12 +1475,4 @@ impl RemapFileNameExt for rustc_span::RealFileName {
self.local_path_if_available()
}
}

fn for_codegen(&self, sess: &Session) -> Self::Output<'_> {
if sess.should_prefer_remapped_for_codegen() {
self.remapped_path_if_available()
} else {
self.local_path_if_available()
}
}
}

0 comments on commit 84e54cd

Please sign in to comment.