From dd53e384099389e77ed10999e7178a084f97f029 Mon Sep 17 00:00:00 2001 From: Adrian Palacios Date: Thu, 29 Aug 2024 19:26:04 +0000 Subject: [PATCH] Move unused code to report --- tools/kani-cov/src/coverage.rs | 52 +++++++------- tools/kani-cov/src/main.rs | 121 ++------------------------------- tools/kani-cov/src/report.rs | 101 +++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 143 deletions(-) diff --git a/tools/kani-cov/src/coverage.rs b/tools/kani-cov/src/coverage.rs index b53070c1af8e..181f21663e5e 100644 --- a/tools/kani-cov/src/coverage.rs +++ b/tools/kani-cov/src/coverage.rs @@ -36,32 +36,32 @@ pub struct CombinedCoverageResults { pub data: BTreeMap>, } -pub fn fmt_coverage_results(coverage_results: &CoverageResults) -> Result { - let mut fmt_string = String::new(); - for (file, checks) in coverage_results.data.iter() { - let mut checks_by_function: BTreeMap> = BTreeMap::new(); - - // // Group checks by function - for check in checks { - // Insert the check into the vector corresponding to its function - checks_by_function - .entry(check.function.clone()) - .or_insert_with(Vec::new) - .push(check.clone()); - } - - for (function, checks) in checks_by_function { - writeln!(fmt_string, "{file} ({function})")?; - let mut sorted_checks: Vec = checks.to_vec(); - sorted_checks.sort_by(|a, b| a.region.start.cmp(&b.region.start)); - for check in sorted_checks.iter() { - writeln!(fmt_string, " * {} {}", check.region, check.status)?; - } - writeln!(fmt_string, "")?; - } - } - Ok(fmt_string) -} +// pub fn fmt_coverage_results(coverage_results: &CoverageResults) -> Result { +// let mut fmt_string = String::new(); +// for (file, checks) in coverage_results.data.iter() { +// let mut checks_by_function: BTreeMap> = BTreeMap::new(); + +// // // Group checks by function +// for check in checks { +// // Insert the check into the vector corresponding to its function +// checks_by_function +// .entry(check.function.clone()) +// .or_insert_with(Vec::new) +// .push(check.clone()); +// } + +// for (function, checks) in checks_by_function { +// writeln!(fmt_string, "{file} ({function})")?; +// let mut sorted_checks: Vec = checks.to_vec(); +// sorted_checks.sort_by(|a, b| a.region.start.cmp(&b.region.start)); +// for check in sorted_checks.iter() { +// writeln!(fmt_string, " * {} {}", check.region, check.status)?; +// } +// writeln!(fmt_string, "")?; +// } +// } +// Ok(fmt_string) +// } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct CoverageCheck { diff --git a/tools/kani-cov/src/main.rs b/tools/kani-cov/src/main.rs index ee68c137a0a5..d7e9ff295aef 100644 --- a/tools/kani-cov/src/main.rs +++ b/tools/kani-cov/src/main.rs @@ -6,17 +6,10 @@ mod args; mod merge; mod summary; mod report; -use std::fs::File; -use std::io::{IsTerminal, Read}; -use std::io::{BufRead, BufReader}; -use std::path::PathBuf; -use crate::coverage::CoverageResults; -use args::{Args, validate_args, Subcommand}; +use args::{validate_args, Subcommand}; use clap::Parser; -use anyhow::{Context, Result}; -use coverage::CoverageCheck; - +use anyhow::Result; fn main() -> Result<()> { let args = args::Args::parse(); @@ -25,115 +18,9 @@ fn main() -> Result<()> { match args.command.unwrap() { Subcommand::Merge(merge_args) => merge::merge_main(&merge_args)?, - // Subcommand::Summary => summary::summary_main()?, + Subcommand::Summary(summary_args) => summary::summary_main(&summary_args)?, // Subcommand::Report => report::report_main()?, }; - // let file_path = args.get(1).context("kanicov file not specified")?; - // let mut file = File::open(file_path)?; - // let mut contents = String::new(); - // file.read_to_string(&mut contents)?; - // let path_to_root = cargo_root_dir(file_path.into()); - // let cov_results: CoverageResults = serde_json::from_str(&contents)?; - // let visual_results = visualize_coverage_results(&cov_results, path_to_root.unwrap()) - // .expect("error: couldn't format coverage results"); - // println!("{visual_results}"); - Ok(()) -} - -fn visualize_coverage_results(cov_results: &CoverageResults, root_path: PathBuf) -> Result { - let mut formatted_output = String::new(); - let cov_data = &cov_results.data; - - for file in cov_data.keys() { - let file_path = root_path.join(file); - let file_handle = File::open(file_path)?; - let reader = BufReader::new(file_handle); - - let checks = cov_data.get(file).unwrap().to_vec(); - let mut must_highlight = false; - - for (idx, line) in reader.lines().enumerate() { - let line = format!("{}\n", line.unwrap()); - - let cur_idx = idx + 1; - let line_checks: Vec<&CoverageCheck> = checks - .iter() - .filter(|c| { - c.is_covered() - && (cur_idx == c.region.start.0 as usize - || cur_idx == c.region.end.0 as usize) - }) - .collect(); - let new_line = if line_checks.is_empty() { - if must_highlight { - insert_escapes(&line, vec![(0, true), (line.len() - 1, false)]) - } else { - line - } - } else { - let mut markers = vec![]; - if must_highlight { - markers.push((0, true)) - }; - - for check in line_checks { - let start_line = check.region.start.0 as usize; - let start_column = (check.region.start.1 - 1u32) as usize; - let end_line = check.region.end.0 as usize; - let end_column = (check.region.end.1 - 1u32) as usize; - if start_line == cur_idx { - markers.push((start_column, true)) - } - if end_line == cur_idx { - markers.push((end_column, false)) - } - } - if markers.last().unwrap().1 { - must_highlight = true; - markers.push((line.len() - 1, false)) - } else { - must_highlight = false; - } - println!("{:?}", markers); - insert_escapes(&line, markers) - }; - formatted_output.push_str(&new_line); - } - } - Ok(formatted_output) -} - -fn cargo_root_dir(filepath: PathBuf) -> Option { - let mut cargo_root_path = filepath.clone(); - while !cargo_root_path.join("Cargo.toml").exists() { - let pop_result = cargo_root_path.pop(); - if !pop_result { - return None; - } - } - Some(cargo_root_path) -} - -fn insert_escapes(str: &String, markers: Vec<(usize, bool)>) -> String { - let mut new_str = str.clone(); - let mut offset = 0; - - let sym_markers = markers.iter().map(|(i, b)| (i, if *b { "\x1b[42m" } else { "\x1b[0m" })); - // let sym_markers = markers.iter().map(|(i, b)| (i, if *b { "```" } else { "'''" })); - for (i, b) in sym_markers { - println!("{}", i + offset); - new_str.insert_str(i + offset, b); - offset = offset + b.bytes().len(); - } - new_str -} - -fn open_marker() -> String { - let support_color = std::io::stdout().is_terminal(); - if support_color { - "\x1b[42m".to_string() - } else { - "```".to_string() - } + Ok(()) } diff --git a/tools/kani-cov/src/report.rs b/tools/kani-cov/src/report.rs index c8e6196b948d..11932f614349 100644 --- a/tools/kani-cov/src/report.rs +++ b/tools/kani-cov/src/report.rs @@ -2,7 +2,108 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use anyhow::Result; +// use coverage::CoverageCheck; +// use crate::coverage::CoverageResults; +// use args::Args; pub fn report_main() -> Result<()> { Ok(()) } + +// fn visualize_coverage_results(cov_results: &CoverageResults, root_path: PathBuf) -> Result { +// let mut formatted_output = String::new(); +// let cov_data = &cov_results.data; + +// for file in cov_data.keys() { +// let file_path = root_path.join(file); +// let file_handle = File::open(file_path)?; +// let reader = BufReader::new(file_handle); + +// let checks = cov_data.get(file).unwrap().to_vec(); +// let mut must_highlight = false; + +// for (idx, line) in reader.lines().enumerate() { +// let line = format!("{}\n", line.unwrap()); + +// let cur_idx = idx + 1; +// let line_checks: Vec<&CoverageCheck> = checks +// .iter() +// .filter(|c| { +// c.is_covered() +// && (cur_idx == c.region.start.0 as usize +// || cur_idx == c.region.end.0 as usize) +// }) +// .collect(); +// let new_line = if line_checks.is_empty() { +// if must_highlight { +// insert_escapes(&line, vec![(0, true), (line.len() - 1, false)]) +// } else { +// line +// } +// } else { +// let mut markers = vec![]; +// if must_highlight { +// markers.push((0, true)) +// }; + +// for check in line_checks { +// let start_line = check.region.start.0 as usize; +// let start_column = (check.region.start.1 - 1u32) as usize; +// let end_line = check.region.end.0 as usize; +// let end_column = (check.region.end.1 - 1u32) as usize; +// if start_line == cur_idx { +// markers.push((start_column, true)) +// } +// if end_line == cur_idx { +// markers.push((end_column, false)) +// } +// } + +// if markers.last().unwrap().1 { +// must_highlight = true; +// markers.push((line.len() - 1, false)) +// } else { +// must_highlight = false; +// } +// println!("{:?}", markers); +// insert_escapes(&line, markers) +// }; +// formatted_output.push_str(&new_line); +// } +// } +// Ok(formatted_output) +// } + +// fn cargo_root_dir(filepath: PathBuf) -> Option { +// let mut cargo_root_path = filepath.clone(); +// while !cargo_root_path.join("Cargo.toml").exists() { +// let pop_result = cargo_root_path.pop(); +// if !pop_result { +// return None; +// } +// } +// Some(cargo_root_path) +// } + +// fn insert_escapes(str: &String, markers: Vec<(usize, bool)>) -> String { +// let mut new_str = str.clone(); +// let mut offset = 0; + +// let sym_markers = markers.iter().map(|(i, b)| (i, if *b { "\x1b[42m" } else { "\x1b[0m" })); +// // let sym_markers = markers.iter().map(|(i, b)| (i, if *b { "```" } else { "'''" })); +// for (i, b) in sym_markers { +// println!("{}", i + offset); +// new_str.insert_str(i + offset, b); +// offset = offset + b.bytes().len(); +// } +// new_str +// } + +// fn open_marker() -> String { +// let support_color = std::io::stdout().is_terminal(); +// if support_color { +// "\x1b[42m".to_string() +// } else { +// "```".to_string() +// } +// }