11use std:: ffi:: OsStr ;
2+ use std:: time:: SystemTime ;
23use std:: path:: { Component , Path } ;
34
45use crate :: prelude:: * ;
56
6- use rustc_span:: { FileName , SourceFileAndLine , Pos } ;
7+ use rustc_span:: { FileName , SourceFile , SourceFileAndLine , Pos , SourceFileHash , SourceFileHashAlgorithm } ;
78
89use cranelift_codegen:: binemit:: CodeOffset ;
910
1011use gimli:: write:: {
11- Address , AttributeValue , FileId , LineProgram , LineString , LineStringTable , UnitEntryId ,
12+ Address , AttributeValue , FileId , LineProgram , LineString , FileInfo , LineStringTable , UnitEntryId ,
1213} ;
1314
1415// OPTIMIZATION: It is cheaper to do this in one pass than using `.parent()` and `.file_name()`.
@@ -38,9 +39,9 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
3839fn line_program_add_file (
3940 line_program : & mut LineProgram ,
4041 line_strings : & mut LineStringTable ,
41- file : & FileName ,
42+ file : & SourceFile ,
4243) -> FileId {
43- match file {
44+ match & file. name {
4445 FileName :: Real ( path) => {
4546 let ( dir_path, file_name) = split_path_dir_and_file ( path) ;
4647 let dir_name = osstr_as_utf8_bytes ( dir_path. as_os_str ( ) ) ;
@@ -57,13 +58,32 @@ fn line_program_add_file(
5758 line_program. encoding ( ) ,
5859 line_strings,
5960 ) ;
60- line_program. add_file ( file_name, dir_id, None )
61+
62+ let md5 = Some ( file. src_hash )
63+ . filter ( |h| matches ! ( h, SourceFileHash { kind: SourceFileHashAlgorithm :: Md5 , .. } ) )
64+ . map ( |h| {
65+ let mut buf = [ 0u8 ; super :: MD5_LEN ] ;
66+ buf. copy_from_slice ( h. hash_bytes ( ) ) ;
67+ buf
68+ } ) ;
69+
70+ line_program. file_has_timestamp = true ;
71+ line_program. file_has_md5 = md5. is_some ( ) ;
72+
73+ line_program. add_file ( file_name, dir_id, Some ( FileInfo {
74+ timestamp : SystemTime :: now ( )
75+ . duration_since ( SystemTime :: UNIX_EPOCH )
76+ . map ( |t| t. as_secs ( ) )
77+ . unwrap_or ( 0 ) ,
78+ size : 0 ,
79+ md5 : md5. unwrap_or_default ( ) ,
80+ } ) )
6181 }
6282 // FIXME give more appropriate file names
63- _ => {
83+ filename => {
6484 let dir_id = line_program. default_directory ( ) ;
6585 let dummy_file_name = LineString :: new (
66- file . to_string ( ) . into_bytes ( ) ,
86+ filename . to_string ( ) . into_bytes ( ) ,
6787 line_program. encoding ( ) ,
6888 line_strings,
6989 ) ;
@@ -79,7 +99,7 @@ impl<'tcx> DebugContext<'tcx> {
7999 let file_id = line_program_add_file (
80100 & mut self . dwarf . unit . line_program ,
81101 & mut self . dwarf . line_strings ,
82- & loc. file . name ,
102+ & loc. file ,
83103 ) ;
84104
85105 let entry = self . dwarf . unit . get_mut ( entry_id) ;
@@ -167,7 +187,7 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
167187 true
168188 } ;
169189 if current_file_changed {
170- let file_id = line_program_add_file ( line_program, line_strings, & file. name ) ;
190+ let file_id = line_program_add_file ( line_program, line_strings, & file) ;
171191 line_program. row ( ) . file = file_id;
172192 last_file = Some ( file. clone ( ) ) ;
173193 }
0 commit comments