11use std:: borrow:: Cow ;
22use std:: fmt:: { self , Write } ;
33use std:: hash:: { Hash , Hasher } ;
4- use std:: path:: { Path , PathBuf } ;
4+ use std:: path:: PathBuf ;
55use std:: sync:: Arc ;
66use std:: { iter, ptr} ;
77
@@ -19,9 +19,7 @@ use rustc_middle::ty::{
1919 self , AdtKind , CoroutineArgsExt , ExistentialTraitRef , Instance , Ty , TyCtxt , Visibility ,
2020} ;
2121use rustc_session:: config:: { self , DebugInfo , Lto } ;
22- use rustc_span:: {
23- DUMMY_SP , FileName , FileNameDisplayPreference , SourceFile , Span , Symbol , hygiene,
24- } ;
22+ use rustc_span:: { DUMMY_SP , FileName , RemapPathScopeComponents , SourceFile , Span , Symbol , hygiene} ;
2523use rustc_symbol_mangling:: typeid_for_trait_ref;
2624use rustc_target:: spec:: DebuginfoKind ;
2725use smallvec:: smallvec;
@@ -555,79 +553,38 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
555553 ) -> & ' ll DIFile {
556554 debug ! ( ?source_file. name) ;
557555
558- let filename_display_preference =
559- cx. sess ( ) . filename_display_preference ( RemapPathScopeComponents :: DEBUGINFO ) ;
560-
561- use rustc_session:: config:: RemapPathScopeComponents ;
562556 let ( directory, file_name) = match & source_file. name {
563557 FileName :: Real ( filename) => {
564- let working_directory = & cx. sess ( ) . opts . working_dir ;
565- debug ! ( ?working_directory) ;
566-
567- if filename_display_preference == FileNameDisplayPreference :: Remapped {
568- let filename = cx
569- . sess ( )
570- . source_map ( )
571- . path_mapping ( )
572- . to_embeddable_absolute_path ( filename. clone ( ) , working_directory) ;
573-
574- // Construct the absolute path of the file
575- let abs_path = filename. remapped_path_if_available ( ) ;
576- debug ! ( ?abs_path) ;
577-
578- if let Ok ( rel_path) =
579- abs_path. strip_prefix ( working_directory. remapped_path_if_available ( ) )
580- {
581- // If the compiler's working directory (which also is the DW_AT_comp_dir of
582- // the compilation unit) is a prefix of the path we are about to emit, then
583- // only emit the part relative to the working directory. Because of path
584- // remapping we sometimes see strange things here: `abs_path` might
585- // actually look like a relative path (e.g.
586- // `<crate-name-and-version>/src/lib.rs`), so if we emit it without taking
587- // the working directory into account, downstream tooling will interpret it
588- // as `<working-directory>/<crate-name-and-version>/src/lib.rs`, which
589- // makes no sense. Usually in such cases the working directory will also be
590- // remapped to `<crate-name-and-version>` or some other prefix of the path
591- // we are remapping, so we end up with
592- // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
593- // By moving the working directory portion into the `directory` part of the
594- // DIFile, we allow LLVM to emit just the relative path for DWARF, while
595- // still emitting the correct absolute path for CodeView.
596- (
597- working_directory. to_string_lossy ( FileNameDisplayPreference :: Remapped ) ,
598- rel_path. to_string_lossy ( ) . into_owned ( ) ,
599- )
600- } else {
601- ( "" . into ( ) , abs_path. to_string_lossy ( ) . into_owned ( ) )
602- }
558+ let ( working_directory, embeddable_name) =
559+ filename. embeddable_name ( RemapPathScopeComponents :: DEBUGINFO ) ;
560+
561+ debug ! ( ?working_directory, ?embeddable_name) ;
562+
563+ if let Ok ( rel_path) = embeddable_name. strip_prefix ( working_directory) {
564+ // If the compiler's working directory (which also is the DW_AT_comp_dir of
565+ // the compilation unit) is a prefix of the path we are about to emit, then
566+ // only emit the part relative to the working directory. Because of path
567+ // remapping we sometimes see strange things here: `abs_path` might
568+ // actually look like a relative path (e.g.
569+ // `<crate-name-and-version>/src/lib.rs`), so if we emit it without taking
570+ // the working directory into account, downstream tooling will interpret it
571+ // as `<working-directory>/<crate-name-and-version>/src/lib.rs`, which
572+ // makes no sense. Usually in such cases the working directory will also be
573+ // remapped to `<crate-name-and-version>` or some other prefix of the path
574+ // we are remapping, so we end up with
575+ // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
576+ //
577+ // By moving the working directory portion into the `directory` part of the
578+ // DIFile, we allow LLVM to emit just the relative path for DWARF, while
579+ // still emitting the correct absolute path for CodeView.
580+ ( working_directory. to_string_lossy ( ) , rel_path. to_string_lossy ( ) . into_owned ( ) )
603581 } else {
604- let working_directory = working_directory. local_path_if_available ( ) ;
605- let filename = filename. local_path_if_available ( ) ;
606-
607- debug ! ( ?working_directory, ?filename) ;
608-
609- let abs_path: Cow < ' _ , Path > = if filename. is_absolute ( ) {
610- filename. into ( )
611- } else {
612- let mut p = PathBuf :: new ( ) ;
613- p. push ( working_directory) ;
614- p. push ( filename) ;
615- p. into ( )
616- } ;
617-
618- if let Ok ( rel_path) = abs_path. strip_prefix ( working_directory) {
619- (
620- working_directory. to_string_lossy ( ) ,
621- rel_path. to_string_lossy ( ) . into_owned ( ) ,
622- )
623- } else {
624- ( "" . into ( ) , abs_path. to_string_lossy ( ) . into_owned ( ) )
625- }
582+ ( "" . into ( ) , embeddable_name. to_string_lossy ( ) . into_owned ( ) )
626583 }
627584 }
628585 other => {
629586 debug ! ( ?other) ;
630- ( "" . into ( ) , other. display ( filename_display_preference ) . to_string ( ) )
587+ ( "" . into ( ) , other. display ( RemapPathScopeComponents :: DEBUGINFO ) . to_string ( ) )
631588 }
632589 } ;
633590
@@ -889,12 +846,10 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
889846 codegen_unit_name : & str ,
890847 debug_context : & CodegenUnitDebugContext < ' ll , ' tcx > ,
891848) -> & ' ll DIDescriptor {
892- use rustc_session:: RemapFileNameExt ;
893- use rustc_session:: config:: RemapPathScopeComponents ;
894849 let mut name_in_debuginfo = tcx
895850 . sess
896851 . local_crate_source_file ( )
897- . map ( |src| src. for_scope ( & tcx . sess , RemapPathScopeComponents :: DEBUGINFO ) . to_path_buf ( ) )
852+ . map ( |src| src. path ( RemapPathScopeComponents :: DEBUGINFO ) . to_path_buf ( ) )
898853 . unwrap_or_else ( || PathBuf :: from ( tcx. crate_name ( LOCAL_CRATE ) . as_str ( ) ) ) ;
899854
900855 // To avoid breaking split DWARF, we need to ensure that each codegen unit
@@ -923,12 +878,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
923878 let producer = format ! ( "clang LLVM ({rustc_producer})" ) ;
924879
925880 let name_in_debuginfo = name_in_debuginfo. to_string_lossy ( ) ;
926- let work_dir = tcx
927- . sess
928- . opts
929- . working_dir
930- . for_scope ( tcx. sess , RemapPathScopeComponents :: DEBUGINFO )
931- . to_string_lossy ( ) ;
881+ let work_dir = tcx. sess . psess . source_map ( ) . working_dir ( ) ;
932882 let output_filenames = tcx. output_filenames ( ( ) ) ;
933883 let split_name = if tcx. sess . target_can_use_split_dwarf ( )
934884 && let Some ( f) = output_filenames. split_dwarf_path (
@@ -938,14 +888,15 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
938888 tcx. sess . invocation_temp . as_deref ( ) ,
939889 ) {
940890 // We get a path relative to the working directory from split_dwarf_path
941- Some ( tcx. sess . source_map ( ) . path_mapping ( ) . to_real_filename ( f) )
891+ Some ( tcx. sess . source_map ( ) . path_mapping ( ) . to_real_filename ( work_dir , f) )
942892 } else {
943893 None
944894 } ;
945895 let split_name = split_name
946896 . as_ref ( )
947- . map ( |f| f. for_scope ( tcx . sess , RemapPathScopeComponents :: DEBUGINFO ) . to_string_lossy ( ) )
897+ . map ( |f| f. path ( RemapPathScopeComponents :: DEBUGINFO ) . to_string_lossy ( ) )
948898 . unwrap_or_default ( ) ;
899+ let work_dir = work_dir. path ( RemapPathScopeComponents :: DEBUGINFO ) . to_string_lossy ( ) ;
949900 let kind = DebugEmissionKind :: from_generic ( tcx. sess . opts . debuginfo ) ;
950901
951902 let dwarf_version = tcx. sess . dwarf_version ( ) ;
0 commit comments