@@ -6,9 +6,7 @@ use std::path::{Component, Path};
6
6
use cranelift_codegen:: MachSrcLoc ;
7
7
use cranelift_codegen:: binemit:: CodeOffset ;
8
8
use gimli:: write:: { AttributeValue , FileId , FileInfo , LineProgram , LineString , LineStringTable } ;
9
- use rustc_span:: {
10
- FileName , Pos , SourceFile , SourceFileAndLine , SourceFileHash , SourceFileHashAlgorithm , hygiene,
11
- } ;
9
+ use rustc_span:: { FileName , Pos , SourceFile , SourceFileAndLine , SourceFileHashAlgorithm , hygiene} ;
12
10
13
11
use crate :: debuginfo:: FunctionDebugContext ;
14
12
use crate :: debuginfo:: emit:: address_for_func;
@@ -44,21 +42,27 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
44
42
}
45
43
}
46
44
47
- const MD5_LEN : usize = 16 ;
48
-
49
- fn make_file_info ( hash : SourceFileHash ) -> Option < FileInfo > {
50
- if hash. kind == SourceFileHashAlgorithm :: Md5 {
51
- let mut buf = [ 0u8 ; MD5_LEN ] ;
52
- buf. copy_from_slice ( hash. hash_bytes ( ) ) ;
53
- Some ( FileInfo {
54
- timestamp : 0 ,
55
- size : 0 ,
56
- md5 : buf,
57
- source : None , // FIXME implement -Zembed-source
58
- } )
59
- } else {
60
- None
45
+ fn make_file_info ( source_file : & SourceFile , embed_source : bool ) -> Option < FileInfo > {
46
+ let has_md5 = source_file. src_hash . kind == SourceFileHashAlgorithm :: Md5 ;
47
+ let has_source = embed_source && source_file. src . is_some ( ) ;
48
+
49
+ if !has_md5 && !has_source {
50
+ return None ;
51
+ }
52
+
53
+ let mut info = FileInfo :: default ( ) ;
54
+
55
+ if has_md5 {
56
+ info. md5 . copy_from_slice ( source_file. src_hash . hash_bytes ( ) ) ;
61
57
}
58
+
59
+ if embed_source {
60
+ if let Some ( src) = & source_file. src {
61
+ info. source = Some ( LineString :: String ( src. as_bytes ( ) . to_vec ( ) ) ) ;
62
+ }
63
+ }
64
+
65
+ Some ( info)
62
66
}
63
67
64
68
impl DebugContext {
@@ -105,15 +109,19 @@ impl DebugContext {
105
109
let file_name =
106
110
LineString :: new ( file_name, line_program. encoding ( ) , line_strings) ;
107
111
108
- let info = make_file_info ( source_file. src_hash ) ;
112
+ let info = make_file_info ( source_file, self . embed_source ) ;
109
113
110
- line_program. file_has_md5 &= info. is_some ( ) ;
114
+ let has_md5 = source_file. src_hash . kind == SourceFileHashAlgorithm :: Md5 ;
115
+ line_program. file_has_md5 &= has_md5;
111
116
line_program. add_file ( file_name, dir_id, info)
112
117
}
113
118
filename => {
114
- let dir_id = line_program. default_directory ( ) ;
119
+ // For anonymous sources, create an empty directory instead of using the default
120
+ let empty_dir = LineString :: new ( b"" , line_program. encoding ( ) , line_strings) ;
121
+ let dir_id = line_program. add_directory ( empty_dir) ;
122
+
115
123
let dummy_file_name = LineString :: new (
116
- filename. display ( self . filename_display_preference ) . to_string ( ) . into_bytes ( ) ,
124
+ filename. prefer_remapped_unconditionally ( ) . to_string ( ) . into_bytes ( ) ,
117
125
line_program. encoding ( ) ,
118
126
line_strings,
119
127
) ;
0 commit comments