mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 15:54:15 +00:00
Dedup file hashing logic with type
This commit is contained in:
parent
1e472531c6
commit
710da05af7
@ -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<SourceFileHash> for FileHash {
|
||||
type Error = UnsupportedHashType;
|
||||
|
||||
fn try_from(hash: SourceFileHash) -> Result<Self, Self::Error> {
|
||||
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
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user