Skip to content

Commit

Permalink
Add intermediate representation
Browse files Browse the repository at this point in the history
commit-id:8013bd0f
  • Loading branch information
ksew1 committed Aug 26, 2024
1 parent 770b936 commit 0e5801d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
76 changes: 76 additions & 0 deletions crates/cairo-coverage/src/coverage_data/function.rs
Original file line number Diff line number Diff line change
@@ -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<FileLocation, FileCoverageData>;

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<FunctionName, FunctionCoverageData>;

pub type FunctionCoverageData = HashMap<LineNumber, HitCount>;

impl From<LineRange> 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<T: IntoIterator<Item = LineNumber>>(&mut self, lines: T);
fn register_hit<T: IntoIterator<Item = LineNumber>>(&mut self, lines: T);
}

impl Register for FunctionCoverageData {
fn register_line<T: IntoIterator<Item = LineNumber>>(&mut self, lines: T) {
for line in lines {
self.entry(line).or_default();
}
}

fn register_hit<T: IntoIterator<Item = LineNumber>>(&mut self, lines: T) {
for line in lines {
*self.entry(line).or_default() += 1;
}
}
}
1 change: 1 addition & 0 deletions crates/cairo-coverage/src/coverage_data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod function;
1 change: 0 additions & 1 deletion crates/cairo-coverage/src/input/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/cairo-coverage/src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions crates/cairo-coverage/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cli;
mod coverage_data;
mod input;
mod types;

Expand Down
1 change: 1 addition & 0 deletions crates/cairo-coverage/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub type FileLocation = String;
pub type FunctionName = String;
pub type LineNumber = usize;
pub type HitCount = usize;

0 comments on commit 0e5801d

Please sign in to comment.