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 27, 2024
1 parent 652c940 commit 0bebff3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
74 changes: 74 additions & 0 deletions crates/cairo-coverage/src/coverage_data/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::input::StatementsMap;
use crate::input::{InputData, LineRange};
use crate::types::{FileLocation, FunctionName, HitCount, LineNumber};
use std::collections::HashMap;

pub type FilesCoverageData = HashMap<FileLocation, FileCoverageData>;

impl From<&InputData> for FilesCoverageData {
fn from(input_data: &InputData) -> Self {
input_data
.statements_map
.iter()
.filter(|(id, _)| input_data.executed_sierra_ids.contains(id))
.fold(
create_files_coverage_data(&input_data.statements_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
},
)
}
}

fn create_files_coverage_data(statements_map: &StatementsMap) -> FilesCoverageData {
statements_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
})
}

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 serde::de::DeserializeOwned;
use std::fs;
use trace_data::CallTrace;

#[allow(dead_code)] // Temporary
pub struct InputData {
pub executed_sierra_ids: ExecutedSierraIds,
pub statements_map: StatementsMap,
Expand Down
2 changes: 2 additions & 0 deletions crates/cairo-coverage/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ mod data_loader;
mod mappings;
mod statements_map;

pub use data::InputData;
pub use data_loader::types::LineRange;
pub use mappings::executed_sierra_ids::ExecutedSierraIds;
pub use statements_map::{create_statements_map, StatementsMap};
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 0bebff3

Please sign in to comment.