Make local_crate_source_file return a RealFileName

so it can be remapped (or not) by callers
This commit is contained in:
Urgau 2024-03-19 13:51:22 +01:00
parent 106146fd95
commit ee2898d3f1
5 changed files with 23 additions and 14 deletions

View File

@ -99,9 +99,16 @@ impl DebugContext {
FileNameDisplayPreference::Local
})
.into_owned();
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(if should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.into_owned();
(name, None)
}
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),

View File

@ -838,9 +838,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
@ -868,7 +870,6 @@ 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::{config::RemapPathScopeComponents, RemapFileNameExt};
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx
.sess

View File

@ -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};
@ -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);

View File

@ -29,6 +29,7 @@ use rustc_macros::HashStable_Generic;
pub use rustc_span::def_id::StableCrateId;
use rustc_span::edition::Edition;
use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap};
use rustc_span::RealFileName;
use rustc_span::{SourceFileHashAlgorithm, Span, Symbol};
use rustc_target::asm::InlineAsmArch;
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@ -250,14 +251,9 @@ impl Session {
self.miri_unleashed_features.lock().push((span, feature_gate));
}
pub fn local_crate_source_file(&self) -> Option<PathBuf> {
pub fn local_crate_source_file(&self) -> Option<RealFileName> {
let path = self.io.input.opt_path()?;
// 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())
}
Some(RealFileName::LocalPath(path.to_path_buf()))
}
fn check_miri_unleashed_features(&self) -> Option<ErrorGuaranteed> {

View File

@ -2506,7 +2506,7 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
// Look for the example file in the source map if it exists, otherwise return a dummy span
let file_span = (|| {
let source_map = tcx.sess.source_map();
let crate_src = tcx.sess.local_crate_source_file()?;
let crate_src = tcx.sess.local_crate_source_file()?.into_local_path()?;
let abs_crate_src = crate_src.canonicalize().ok()?;
let crate_root = abs_crate_src.parent()?.parent()?;
let rel_path = path.strip_prefix(crate_root).ok()?;