@@ -6,9 +6,7 @@ use std::path::{Component, Path};
66use cranelift_codegen:: MachSrcLoc ;
77use cranelift_codegen:: binemit:: CodeOffset ;
88use 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} ;
1210
1311use crate :: debuginfo:: FunctionDebugContext ;
1412use crate :: debuginfo:: emit:: address_for_func;
@@ -44,21 +42,27 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
4442 }
4543}
4644
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 ( ) ) ;
6157 }
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)
6266}
6367
6468impl DebugContext {
@@ -105,15 +109,19 @@ impl DebugContext {
105109 let file_name =
106110 LineString :: new ( file_name, line_program. encoding ( ) , line_strings) ;
107111
108- let info = make_file_info ( source_file. src_hash ) ;
112+ let info = make_file_info ( source_file, self . embed_source ) ;
109113
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;
111116 line_program. add_file ( file_name, dir_id, info)
112117 }
113118 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+
115123 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 ( ) ,
117125 line_program. encoding ( ) ,
118126 line_strings,
119127 ) ;
0 commit comments