From 0e5801d0b8bde30a70535eace8e43396c1318a31 Mon Sep 17 00:00:00 2001 From: Karol Sewilo Date: Sun, 25 Aug 2024 23:52:09 +0200 Subject: [PATCH] Add intermediate representation commit-id:8013bd0f --- .../src/coverage_data/function.rs | 76 +++++++++++++++++++ .../cairo-coverage/src/coverage_data/mod.rs | 1 + crates/cairo-coverage/src/input/data.rs | 1 - crates/cairo-coverage/src/input/mod.rs | 3 + crates/cairo-coverage/src/main.rs | 1 + crates/cairo-coverage/src/types.rs | 1 + 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 crates/cairo-coverage/src/coverage_data/function.rs create mode 100644 crates/cairo-coverage/src/coverage_data/mod.rs diff --git a/crates/cairo-coverage/src/coverage_data/function.rs b/crates/cairo-coverage/src/coverage_data/function.rs new file mode 100644 index 0000000..5e32bdf --- /dev/null +++ b/crates/cairo-coverage/src/coverage_data/function.rs @@ -0,0 +1,76 @@ +use crate::input::StatementMap; +use crate::input::{InputData, LineRange}; +use crate::types::{FileLocation, FunctionName, HitCount, LineNumber}; +use std::collections::HashMap; + +pub type FilesCoverageData = HashMap; + +impl From<&StatementMap> for FilesCoverageData { + fn from(statement_map: &StatementMap) -> Self { + statement_map + .values() + .cloned() + .fold(HashMap::new(), |mut acc, origin| { + acc.entry(origin.file_location) + .or_default() + .entry(origin.function_name) + .and_modify(|function_details: &mut FunctionCoverageData| { + function_details.register_line(&origin.line_range); + }) + .or_insert(origin.line_range.into()); + acc + }) + } +} + +impl From<&InputData> for FilesCoverageData { + fn from(input_data: &InputData) -> Self { + input_data + .statement_map + .iter() + .filter(|(id, _)| input_data.executed_sierra_ids.contains(id)) + .fold( + Self::from(&input_data.statement_map), + |mut files_coverage_data, (_, statement_origin)| { + if let Some(function_details) = files_coverage_data + .get_mut(&statement_origin.file_location) + .and_then(|functions| functions.get_mut(&statement_origin.function_name)) + { + function_details.register_hit(&statement_origin.line_range); + } + files_coverage_data + }, + ) + } +} + +pub type FileCoverageData = HashMap; + +pub type FunctionCoverageData = HashMap; + +impl From for FunctionCoverageData { + fn from(line_range: LineRange) -> Self { + let mut function_coverage_data = FunctionCoverageData::new(); + function_coverage_data.register_line(&line_range); + function_coverage_data + } +} + +trait Register { + fn register_line>(&mut self, lines: T); + fn register_hit>(&mut self, lines: T); +} + +impl Register for FunctionCoverageData { + fn register_line>(&mut self, lines: T) { + for line in lines { + self.entry(line).or_default(); + } + } + + fn register_hit>(&mut self, lines: T) { + for line in lines { + *self.entry(line).or_default() += 1; + } + } +} diff --git a/crates/cairo-coverage/src/coverage_data/mod.rs b/crates/cairo-coverage/src/coverage_data/mod.rs new file mode 100644 index 0000000..e43437d --- /dev/null +++ b/crates/cairo-coverage/src/coverage_data/mod.rs @@ -0,0 +1 @@ +mod function; diff --git a/crates/cairo-coverage/src/input/data.rs b/crates/cairo-coverage/src/input/data.rs index d4b45e6..b88c647 100644 --- a/crates/cairo-coverage/src/input/data.rs +++ b/crates/cairo-coverage/src/input/data.rs @@ -8,7 +8,6 @@ use camino::Utf8PathBuf; use std::borrow::Borrow; use trace_data::CallTrace; -#[allow(dead_code)] // Temporary pub struct InputData { pub executed_sierra_ids: ExecutedSierraIds, pub statement_map: StatementMap, diff --git a/crates/cairo-coverage/src/input/mod.rs b/crates/cairo-coverage/src/input/mod.rs index 988ea57..f5a63e9 100644 --- a/crates/cairo-coverage/src/input/mod.rs +++ b/crates/cairo-coverage/src/input/mod.rs @@ -1,4 +1,7 @@ mod data; mod data_loader; mod mappings; + +pub use data::InputData; +pub use data_loader::types::LineRange; pub use mappings::StatementMap; diff --git a/crates/cairo-coverage/src/main.rs b/crates/cairo-coverage/src/main.rs index a610874..85f9ceb 100644 --- a/crates/cairo-coverage/src/main.rs +++ b/crates/cairo-coverage/src/main.rs @@ -1,4 +1,5 @@ mod cli; +mod coverage_data; mod input; mod types; diff --git a/crates/cairo-coverage/src/types.rs b/crates/cairo-coverage/src/types.rs index 2d6f4c0..4762b65 100644 --- a/crates/cairo-coverage/src/types.rs +++ b/crates/cairo-coverage/src/types.rs @@ -1,3 +1,4 @@ pub type FileLocation = String; pub type FunctionName = String; pub type LineNumber = usize; +pub type HitCount = usize;