Skip to content

Commit 710da05

Browse files
committed
Dedup file hashing logic with type
1 parent 1e47253 commit 710da05

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

src/debuginfo/line_info.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ffi::OsStr;
2+
use std::convert::TryFrom;
23
use std::path::{Component, Path};
34

45
use crate::prelude::*;
@@ -35,6 +36,33 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] {
3536
}
3637
}
3738

39+
pub(crate) const MD5_LEN: usize = 16;
40+
41+
#[derive(Default, Clone, Copy)]
42+
pub struct FileHash([u8; MD5_LEN]);
43+
44+
impl FileHash {
45+
pub fn inner(self) -> [u8; MD5_LEN] {
46+
self.0
47+
}
48+
}
49+
50+
pub struct UnsupportedHashType;
51+
52+
impl TryFrom<SourceFileHash> for FileHash {
53+
type Error = UnsupportedHashType;
54+
55+
fn try_from(hash: SourceFileHash) -> Result<Self, Self::Error> {
56+
if hash.kind == SourceFileHashAlgorithm::Md5 {
57+
let mut buf = [0u8; MD5_LEN];
58+
buf.copy_from_slice(hash.hash_bytes());
59+
Ok(Self(buf))
60+
} else {
61+
Err(UnsupportedHashType)
62+
}
63+
}
64+
}
65+
3866
fn line_program_add_file(
3967
line_program: &mut LineProgram,
4068
line_strings: &mut LineStringTable,
@@ -58,20 +86,13 @@ fn line_program_add_file(
5886
line_strings,
5987
);
6088

61-
let md5 = Some(file.src_hash)
62-
.filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. }))
63-
.map(|h| {
64-
let mut buf = [0u8; super::MD5_LEN];
65-
buf.copy_from_slice(h.hash_bytes());
66-
buf
67-
});
68-
69-
line_program.file_has_md5 = md5.is_some();
89+
let file_hash = FileHash::try_from(file.src_hash);
7090

91+
line_program.file_has_md5 = file_hash.is_ok();
7192
line_program.add_file(file_name, dir_id, Some(FileInfo {
7293
timestamp: 0,
7394
size: 0,
74-
md5: md5.unwrap_or_default(),
95+
md5: file_hash.unwrap_or_default().inner(),
7596
}))
7697
}
7798
// FIXME give more appropriate file names

src/debuginfo/mod.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod emit;
22
mod line_info;
33

4+
use std::convert::TryFrom;
5+
46
use crate::prelude::*;
57

6-
use rustc_span::{FileName, SourceFileHash, SourceFileHashAlgorithm};
8+
use rustc_span::FileName;
79

810
use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc};
911
use cranelift_codegen::isa::TargetIsa;
@@ -26,8 +28,6 @@ fn target_endian(tcx: TyCtxt<'_>) -> RunTimeEndian {
2628
}
2729
}
2830

29-
const MD5_LEN: usize = 16;
30-
3131
pub(crate) struct DebugContext<'tcx> {
3232
tcx: TyCtxt<'tcx>,
3333

@@ -61,19 +61,13 @@ impl<'tcx> DebugContext<'tcx> {
6161
// Normally this would use option_env!("CFG_VERSION").
6262
let producer = format!("cg_clif (rustc {})", "unknown version");
6363
let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned();
64-
let (name, md5) = match tcx.sess.local_crate_source_file.clone() {
64+
let (name, file_hash) = match tcx.sess.local_crate_source_file.clone() {
6565
Some(path) => {
6666
let name = path.to_string_lossy().into_owned();
6767
let hash = tcx.sess
6868
.source_map()
6969
.get_source_file(&FileName::Real(path))
70-
.map(|f| f.src_hash)
71-
.filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. }))
72-
.map(|h| {
73-
let mut buf = [0u8; MD5_LEN];
74-
buf.copy_from_slice(h.hash_bytes());
75-
buf
76-
});
70+
.and_then(|f| line_info::FileHash::try_from(f.src_hash).ok());
7771
(name, hash)
7872
},
7973
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@@ -87,10 +81,10 @@ impl<'tcx> DebugContext<'tcx> {
8781
Some(FileInfo {
8882
timestamp: 0,
8983
size: 0,
90-
md5: md5.unwrap_or_default(),
84+
md5: file_hash.unwrap_or_default().inner(),
9185
}),
9286
);
93-
line_program.file_has_md5 = md5.is_some();
87+
line_program.file_has_md5 = file_hash.is_some();
9488

9589
dwarf.unit.line_program = line_program;
9690

0 commit comments

Comments
 (0)