@@ -547,48 +547,77 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
547
547
) -> & ' ll DIFile {
548
548
debug ! ( ?source_file. name) ;
549
549
550
+ use rustc_session:: RemapFileNameExt ;
550
551
let ( directory, file_name) = match & source_file. name {
551
552
FileName :: Real ( filename) => {
552
553
let working_directory = & cx. sess ( ) . opts . working_dir ;
553
554
debug ! ( ?working_directory) ;
554
555
555
- let filename = cx
556
- . sess ( )
557
- . source_map ( )
558
- . path_mapping ( )
559
- . to_embeddable_absolute_path ( filename. clone ( ) , working_directory) ;
560
-
561
- // Construct the absolute path of the file
562
- let abs_path = filename. remapped_path_if_available ( ) ;
563
- debug ! ( ?abs_path) ;
564
-
565
- if let Ok ( rel_path) =
566
- abs_path. strip_prefix ( working_directory. remapped_path_if_available ( ) )
567
- {
568
- // If the compiler's working directory (which also is the DW_AT_comp_dir of
569
- // the compilation unit) is a prefix of the path we are about to emit, then
570
- // only emit the part relative to the working directory.
571
- // Because of path remapping we sometimes see strange things here: `abs_path`
572
- // might actually look like a relative path
573
- // (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
574
- // taking the working directory into account, downstream tooling will
575
- // interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
576
- // which makes no sense. Usually in such cases the working directory will also
577
- // be remapped to `<crate-name-and-version>` or some other prefix of the path
578
- // we are remapping, so we end up with
579
- // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
580
- // By moving the working directory portion into the `directory` part of the
581
- // DIFile, we allow LLVM to emit just the relative path for DWARF, while
582
- // still emitting the correct absolute path for CodeView.
583
- (
584
- working_directory. to_string_lossy ( FileNameDisplayPreference :: Remapped ) ,
585
- rel_path. to_string_lossy ( ) . into_owned ( ) ,
586
- )
556
+ if cx. sess ( ) . should_prefer_remapped_for_codegen ( ) {
557
+ let filename = cx
558
+ . sess ( )
559
+ . source_map ( )
560
+ . path_mapping ( )
561
+ . to_embeddable_absolute_path ( filename. clone ( ) , working_directory) ;
562
+
563
+ // Construct the absolute path of the file
564
+ let abs_path = filename. remapped_path_if_available ( ) ;
565
+ debug ! ( ?abs_path) ;
566
+
567
+ if let Ok ( rel_path) =
568
+ abs_path. strip_prefix ( working_directory. remapped_path_if_available ( ) )
569
+ {
570
+ // If the compiler's working directory (which also is the DW_AT_comp_dir of
571
+ // the compilation unit) is a prefix of the path we are about to emit, then
572
+ // only emit the part relative to the working directory.
573
+ // Because of path remapping we sometimes see strange things here: `abs_path`
574
+ // might actually look like a relative path
575
+ // (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
576
+ // taking the working directory into account, downstream tooling will
577
+ // interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
578
+ // which makes no sense. Usually in such cases the working directory will also
579
+ // be remapped to `<crate-name-and-version>` or some other prefix of the path
580
+ // we are remapping, so we end up with
581
+ // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
582
+ // By moving the working directory portion into the `directory` part of the
583
+ // DIFile, we allow LLVM to emit just the relative path for DWARF, while
584
+ // still emitting the correct absolute path for CodeView.
585
+ (
586
+ working_directory. to_string_lossy ( FileNameDisplayPreference :: Remapped ) ,
587
+ rel_path. to_string_lossy ( ) . into_owned ( ) ,
588
+ )
589
+ } else {
590
+ ( "" . into ( ) , abs_path. to_string_lossy ( ) . into_owned ( ) )
591
+ }
587
592
} else {
588
- ( "" . into ( ) , abs_path. to_string_lossy ( ) . into_owned ( ) )
593
+ let working_directory = working_directory. local_path_if_available ( ) ;
594
+ let filename = filename. local_path_if_available ( ) ;
595
+
596
+ debug ! ( ?working_directory, ?filename) ;
597
+
598
+ let abs_path: Cow < ' _ , Path > = if filename. is_absolute ( ) {
599
+ filename. into ( )
600
+ } else {
601
+ let mut p = PathBuf :: new ( ) ;
602
+ p. push ( working_directory) ;
603
+ p. push ( filename) ;
604
+ p. into ( )
605
+ } ;
606
+
607
+ if let Ok ( rel_path) = abs_path. strip_prefix ( working_directory) {
608
+ (
609
+ working_directory. to_string_lossy ( ) . into ( ) ,
610
+ rel_path. to_string_lossy ( ) . into_owned ( ) ,
611
+ )
612
+ } else {
613
+ ( "" . into ( ) , abs_path. to_string_lossy ( ) . into_owned ( ) )
614
+ }
589
615
}
590
616
}
591
- other => ( "" . into ( ) , other. prefer_remapped ( ) . to_string_lossy ( ) . into_owned ( ) ) ,
617
+ other => {
618
+ debug ! ( ?other) ;
619
+ ( "" . into ( ) , other. for_codegen ( cx. sess ( ) ) . to_string_lossy ( ) . into_owned ( ) )
620
+ }
592
621
} ;
593
622
594
623
let hash_kind = match source_file. src_hash . kind {
@@ -822,8 +851,9 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
822
851
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
823
852
let producer = format ! ( "clang LLVM ({rustc_producer})" ) ;
824
853
854
+ use rustc_session:: RemapFileNameExt ;
825
855
let name_in_debuginfo = name_in_debuginfo. to_string_lossy ( ) ;
826
- let work_dir = tcx. sess . opts . working_dir . to_string_lossy ( FileNameDisplayPreference :: Remapped ) ;
856
+ let work_dir = tcx. sess . opts . working_dir . for_codegen ( & tcx . sess ) . to_string_lossy ( ) ;
827
857
let flags = "\0 " ;
828
858
let output_filenames = tcx. output_filenames ( ( ) ) ;
829
859
let split_name = if tcx. sess . target_can_use_split_dwarf ( ) {
@@ -834,7 +864,13 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
834
864
Some ( codegen_unit_name) ,
835
865
)
836
866
// We get a path relative to the working directory from split_dwarf_path
837
- . map ( |f| tcx. sess . source_map ( ) . path_mapping ( ) . map_prefix ( f) . 0 )
867
+ . map ( |f| {
868
+ if tcx. sess . should_prefer_remapped_for_split_debuginfo_paths ( ) {
869
+ tcx. sess . source_map ( ) . path_mapping ( ) . map_prefix ( f) . 0
870
+ } else {
871
+ f. into ( )
872
+ }
873
+ } )
838
874
} else {
839
875
None
840
876
}
0 commit comments