Skip to content

Commit

Permalink
Init. version for terminal reports
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaco-aws committed Sep 16, 2024
1 parent 0df18b9 commit 50bb553
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 55 deletions.
8 changes: 6 additions & 2 deletions tools/kani-cov/src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use console::style;
use serde_derive::{Deserialize, Serialize};
use std::{fmt, fs};
use std::path::PathBuf;
use std::{collections::BTreeMap, fmt::Display};
use std::{fmt, fs};
use tree_sitter::{Node, Parser};

#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -190,7 +190,11 @@ pub struct FunctionInfo {
pub num_lines: usize,
}

pub fn function_coverage_results(info: &FunctionInfo, file: &PathBuf, results: &CombinedCoverageResults) -> Option<(String, Vec<CovResult>)> {
pub fn function_coverage_results(
info: &FunctionInfo,
file: &PathBuf,
results: &CombinedCoverageResults,
) -> Option<(String, Vec<CovResult>)> {
// `info` does not include file so how do we match?
// use function just for now...
let filename = file.clone().into_os_string().into_string().unwrap();
Expand Down
9 changes: 8 additions & 1 deletion tools/kani-cov/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ fn combine_raw_results(results: &Vec<CoverageResults>) -> CombinedCoverageResult

for result in results {
if result.data.contains_key(&file_name) {
this_fun_checks.extend(result.data.get(&file_name).unwrap().iter().filter(|check| check.function == fun_name))
this_fun_checks.extend(
result
.data
.get(&file_name)
.unwrap()
.iter()
.filter(|check| check.function == fun_name),
)
}
}

Expand Down
86 changes: 74 additions & 12 deletions tools/kani-cov/src/report.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::io::{BufRead, IsTerminal};
use std::{fs::File, io::BufReader, path::PathBuf};

use anyhow::Result;

use crate::coverage::{
function_coverage_results, function_info_from_file, CovResult, FileCoverageInfo, FunctionInfo,
};
use crate::summary::{line_coverage_info, line_coverage_results};
use crate::{args::ReportArgs, coverage::CombinedCoverageResults};
use crate::coverage::{function_coverage_results, function_info_from_file, FileCoverageInfo};
// use crate::coverage::CoverageResults;
// use args::Args;

Expand All @@ -22,14 +26,21 @@ pub fn report_main(args: &ReportArgs) -> Result<()> {
let source_files: Vec<PathBuf> =
serde_json::from_reader(reader).expect("could not parse coverage metadata");

let mut all_cov_info: Vec<FileCoverageInfo> = Vec::new();

for file in source_files {
let fun_info = function_info_from_file(&file);
// let mut file_cov_info = Vec::new();
let mut file_cov_info = Vec::new();
for info in fun_info {
let cov_results = function_coverage_results(&info, &file, &results);
let line_coverage = line_coverage_results(&info, &cov_results);
let line_coverage_matched: Vec<(usize, Option<(u32, Vec<CovResult>)>)> =
(info.start.0..=info.end.0).zip(line_coverage.clone()).collect();
// println!("REG: {line_coverage:?}");
// println!("MATCHED: {line_coverage_matched:?}");
// let new_res = line_coverage_matched.into_iter().filter(|(num, data)| data.is_some()).collect();
file_cov_info.push(line_coverage_matched);
}
// let file_coverage_results = match_line_coverage_file(file_cov_info);
print_coverage_results(file, file_cov_info)?;
}

Ok(())
Expand All @@ -39,6 +50,57 @@ pub fn validate_report_args(_args: &ReportArgs) -> Result<()> {
Ok(())
}

pub fn print_coverage_results(
filepath: PathBuf,
results: Vec<Vec<(usize, Option<(u32, Vec<CovResult>)>)>>,
) -> Result<()> {
let flattened_results: Vec<(usize, Option<(u32, Vec<CovResult>)>)> =
results.into_iter().flatten().collect();
println!("{}", filepath.to_string_lossy().to_string());

let file = File::open(filepath)?;
let reader = BufReader::new(file);

println!("{flattened_results:?}");
for (i, line) in reader.lines().enumerate() {
let idx = i + 1;
let line = line?;
let cur_line_result = flattened_results.iter().find(|(num, _)| *num == idx);
let max = if let Some((num, data)) = cur_line_result {
if data.is_some() {
format!("{:4}", data.clone().unwrap().0)
} else {
format!("{:4}", " ".to_string())
}
} else {
format!("{:4}", " ".to_string())
};
let line_fmt = if max == " 0" { format!("{}{line}{}", "\x1b[42m", "\x1b[0m") } else {line};
println!("{idx:4}| {max}| {line_fmt}");
let differing_results: Vec<CovResult> =
if let Some((num, data)) = cur_line_result { if data.is_some() {data.clone().unwrap().1} else {vec![]} } else { vec![] };
let zero_differing_results: Vec<&CovResult> =
differing_results.iter().filter(|x| x.times_covered == 0).collect();
let mut str = std::iter::repeat(' ').take(11_usize).collect::<String>();
let mut cur_shift = 0;
let mut print_differing = false;
for res in zero_differing_results {
let start: usize = res.region.start.1.try_into().unwrap();
let spaces_next = std::iter::repeat(' ').take(start - cur_shift).collect::<String>();
str.push_str(&format!("{spaces_next}^0"));
cur_shift += start + 2;
print_differing = true;
// res.region.start
}
if print_differing {
println!("{str}");
}
}

Ok(())
}
// fn match_line_coverage_file(line_cov_info: Vec<(FunctionInfo, Vec<Option<(u32, Vec<crate::coverage::CovResult>)>) -> ?? {
// }
// fn visualize_coverage_results(cov_results: &CoverageResults, root_path: PathBuf) -> Result<String> {
// let mut formatted_output = String::new();
// let cov_data = &cov_results.data;
Expand Down Expand Up @@ -128,11 +190,11 @@ pub fn validate_report_args(_args: &ReportArgs) -> Result<()> {
// new_str
// }

// fn open_marker() -> String {
// let support_color = std::io::stdout().is_terminal();
// if support_color {
// "\x1b[42m".to_string()
// } else {
// "```".to_string()
// }
// }
fn red() -> String {
let support_color = std::io::stdout().is_terminal();
if support_color {
"\x1b[42m".to_string()
} else {
"```".to_string()
}
}
Loading

0 comments on commit 50bb553

Please sign in to comment.