From 710da05af7e4ee13c00b9019752b6076ae6b71b4 Mon Sep 17 00:00:00 2001 From: Kitsu Date: Sat, 18 Apr 2020 16:16:17 +0300 Subject: [PATCH] Dedup file hashing logic with type --- src/debuginfo/line_info.rs | 41 ++++++++++++++++++++++++++++---------- src/debuginfo/mod.rs | 20 +++++++------------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index 98257e89f1e..afef0b8ec2f 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -1,4 +1,5 @@ use std::ffi::OsStr; +use std::convert::TryFrom; use std::path::{Component, Path}; use crate::prelude::*; @@ -35,6 +36,33 @@ fn osstr_as_utf8_bytes(path: &OsStr) -> &[u8] { } } +pub(crate) const MD5_LEN: usize = 16; + +#[derive(Default, Clone, Copy)] +pub struct FileHash([u8; MD5_LEN]); + +impl FileHash { + pub fn inner(self) -> [u8; MD5_LEN] { + self.0 + } +} + +pub struct UnsupportedHashType; + +impl TryFrom for FileHash { + type Error = UnsupportedHashType; + + fn try_from(hash: SourceFileHash) -> Result { + if hash.kind == SourceFileHashAlgorithm::Md5 { + let mut buf = [0u8; MD5_LEN]; + buf.copy_from_slice(hash.hash_bytes()); + Ok(Self(buf)) + } else { + Err(UnsupportedHashType) + } + } +} + fn line_program_add_file( line_program: &mut LineProgram, line_strings: &mut LineStringTable, @@ -58,20 +86,13 @@ fn line_program_add_file( line_strings, ); - let md5 = Some(file.src_hash) - .filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. })) - .map(|h| { - let mut buf = [0u8; super::MD5_LEN]; - buf.copy_from_slice(h.hash_bytes()); - buf - }); - - line_program.file_has_md5 = md5.is_some(); + let file_hash = FileHash::try_from(file.src_hash); + line_program.file_has_md5 = file_hash.is_ok(); line_program.add_file(file_name, dir_id, Some(FileInfo { timestamp: 0, size: 0, - md5: md5.unwrap_or_default(), + md5: file_hash.unwrap_or_default().inner(), })) } // FIXME give more appropriate file names diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 17bb53aa347..fbf5fa05236 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -1,9 +1,11 @@ mod emit; mod line_info; +use std::convert::TryFrom; + use crate::prelude::*; -use rustc_span::{FileName, SourceFileHash, SourceFileHashAlgorithm}; +use rustc_span::FileName; use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc}; use cranelift_codegen::isa::TargetIsa; @@ -26,8 +28,6 @@ fn target_endian(tcx: TyCtxt<'_>) -> RunTimeEndian { } } -const MD5_LEN: usize = 16; - pub(crate) struct DebugContext<'tcx> { tcx: TyCtxt<'tcx>, @@ -61,19 +61,13 @@ impl<'tcx> DebugContext<'tcx> { // Normally this would use option_env!("CFG_VERSION"). let producer = format!("cg_clif (rustc {})", "unknown version"); let comp_dir = tcx.sess.working_dir.0.to_string_lossy().into_owned(); - let (name, md5) = match tcx.sess.local_crate_source_file.clone() { + let (name, file_hash) = match tcx.sess.local_crate_source_file.clone() { Some(path) => { let name = path.to_string_lossy().into_owned(); let hash = tcx.sess .source_map() .get_source_file(&FileName::Real(path)) - .map(|f| f.src_hash) - .filter(|h| matches!(h, SourceFileHash { kind: SourceFileHashAlgorithm::Md5, .. })) - .map(|h| { - let mut buf = [0u8; MD5_LEN]; - buf.copy_from_slice(h.hash_bytes()); - buf - }); + .and_then(|f| line_info::FileHash::try_from(f.src_hash).ok()); (name, hash) }, None => (tcx.crate_name(LOCAL_CRATE).to_string(), None), @@ -87,10 +81,10 @@ impl<'tcx> DebugContext<'tcx> { Some(FileInfo { timestamp: 0, size: 0, - md5: md5.unwrap_or_default(), + md5: file_hash.unwrap_or_default().inner(), }), ); - line_program.file_has_md5 = md5.is_some(); + line_program.file_has_md5 = file_hash.is_some(); dwarf.unit.line_program = line_program;