@@ -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 ,
0 commit comments