Skip to content

Commit fb709ed

Browse files
committed
Simplify with make_file_info
1 parent 0e837e3 commit fb709ed

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

src/debuginfo/line_info.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,17 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
3737

3838
pub(crate) const MD5_LEN: usize = 16;
3939

40-
#[derive(Default, Clone, Copy)]
41-
pub struct FileHash([u8; MD5_LEN]);
42-
43-
impl FileHash {
44-
pub fn from_source_hash(hash: SourceFileHash) -> Option<Self> {
45-
if hash.kind == SourceFileHashAlgorithm::Md5 {
46-
let mut buf = [0u8; MD5_LEN];
47-
buf.copy_from_slice(hash.hash_bytes());
48-
Some(Self(buf))
49-
} else {
50-
None
51-
}
52-
}
53-
54-
pub fn inner(self) -> [u8; MD5_LEN] {
55-
self.0
40+
pub fn make_file_info(hash: SourceFileHash) -> Option<FileInfo> {
41+
if hash.kind == SourceFileHashAlgorithm::Md5 {
42+
let mut buf = [0u8; MD5_LEN];
43+
buf.copy_from_slice(hash.hash_bytes());
44+
Some(FileInfo {
45+
timestamp: 0,
46+
size: 0,
47+
md5: buf,
48+
})
49+
} else {
50+
None
5651
}
5752
}
5853

@@ -79,14 +74,10 @@ fn line_program_add_file(
7974
line_strings,
8075
);
8176

82-
let file_hash = FileHash::from_source_hash(file.src_hash);
77+
let info = make_file_info(file.src_hash);
8378

84-
line_program.file_has_md5 = file_hash.is_some();
85-
line_program.add_file(file_name, dir_id, Some(FileInfo {
86-
timestamp: 0,
87-
size: 0,
88-
md5: file_hash.unwrap_or_default().inner(),
89-
}))
79+
line_program.file_has_md5 = info.is_some();
80+
line_program.add_file(file_name, dir_id, info)
9081
}
9182
// FIXME give more appropriate file names
9283
filename => {

src/debuginfo/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cranelift_codegen::ValueLocRange;
1111

1212
use gimli::write::{
1313
self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString, Location,
14-
LocationList, Range, RangeList, UnitEntryId, Writer, FileInfo,
14+
LocationList, Range, RangeList, UnitEntryId, Writer,
1515
};
1616
use gimli::{Encoding, Format, LineEncoding, RunTimeEndian, X86_64};
1717

@@ -59,15 +59,15 @@ impl<'tcx> DebugContext<'tcx> {
5959
// Normally this would use option_env!("CFG_VERSION").
6060
let producer = format!("cg_clif (rustc {})", "unknown version");
6161
let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
62-
let (name, file_hash) = match tcx.sess.local_crate_source_file.clone() {
62+
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
6363
Some(path) => {
6464
let name = path.to_string_lossy().into_owned();
65-
let hash = tcx.sess
65+
let info = tcx.sess
6666
.source_map()
6767
.get_source_file(&FileName::Real(path))
6868
.map(|f| f.src_hash)
69-
.and_then(line_info::FileHash::from_source_hash);
70-
(name, hash)
69+
.and_then(line_info::make_file_info);
70+
(name, info)
7171
},
7272
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
7373
};
@@ -77,13 +77,9 @@ impl<'tcx> DebugContext<'tcx> {
7777
LineEncoding::default(),
7878
LineString::new(comp_dir.as_bytes(), encoding, &mut dwarf.line_strings),
7979
LineString::new(name.as_bytes(), encoding, &mut dwarf.line_strings),
80-
Some(FileInfo {
81-
timestamp: 0,
82-
size: 0,
83-
md5: file_hash.unwrap_or_default().inner(),
84-
}),
80+
file_info,
8581
);
86-
line_program.file_has_md5 = file_hash.is_some();
82+
line_program.file_has_md5 = file_info.is_some();
8783

8884
dwarf.unit.line_program = line_program;
8985

0 commit comments

Comments
 (0)