Skip to content

Commit

Permalink
Move unused code to report
Browse files Browse the repository at this point in the history
  • Loading branch information
adpaco-aws committed Aug 29, 2024
1 parent 9e9575a commit dd53e38
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 143 deletions.
52 changes: 26 additions & 26 deletions tools/kani-cov/src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,32 @@ pub struct CombinedCoverageResults {
pub data: BTreeMap<String, Vec<CovResult>>,
}

pub fn fmt_coverage_results(coverage_results: &CoverageResults) -> Result<String> {
let mut fmt_string = String::new();
for (file, checks) in coverage_results.data.iter() {
let mut checks_by_function: BTreeMap<String, Vec<CoverageCheck>> = 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<CoverageCheck> = 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<String> {
// let mut fmt_string = String::new();
// for (file, checks) in coverage_results.data.iter() {
// let mut checks_by_function: BTreeMap<String, Vec<CoverageCheck>> = 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<CoverageCheck> = 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 {
Expand Down
121 changes: 4 additions & 117 deletions tools/kani-cov/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<String> {
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<PathBuf> {
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(())
}
101 changes: 101 additions & 0 deletions tools/kani-cov/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// 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<PathBuf> {
// 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()
// }
// }

0 comments on commit dd53e38

Please sign in to comment.