From 14e5c79b1c6943c88ab36ccc46f073674a76e16c Mon Sep 17 00:00:00 2001 From: Alasdair Date: Tue, 25 Aug 2020 17:20:00 +0100 Subject: Add function sail_set_coverage_file to sail_coverage header Can be set by C emulator to control where coverage information is written --- lib/coverage/src/lib.rs | 28 ++++++++++++++++++---------- lib/sail_coverage.h | 8 +++++--- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/coverage/src/lib.rs b/lib/coverage/src/lib.rs index 003f8b88..34261eae 100644 --- a/lib/coverage/src/lib.rs +++ b/lib/coverage/src/lib.rs @@ -3,7 +3,7 @@ use lazy_static::lazy_static; use std::collections::HashSet; use std::ffi::{CStr, CString}; -use std::fs::{OpenOptions, File}; +use std::fs::{File, OpenOptions}; use std::io::Write; use std::os::raw::{c_char, c_int}; use std::sync::Mutex; @@ -20,6 +20,7 @@ struct Span { lazy_static! { static ref BRANCHES: Mutex> = Mutex::new(HashSet::new()); static ref FUNCTIONS: Mutex> = Mutex::new(HashSet::new()); + static ref OUTPUT_FILE: Mutex = Mutex::new("sail_coverage".to_string()); } fn function_entry(_function_name: &CStr, span: Span) { @@ -30,15 +31,13 @@ fn branch_taken(_branch_id: i32, span: Span) { BRANCHES.lock().unwrap().insert(span); } -fn branch_reached(_branch_id: i32, _span: Span) { - () -} +fn branch_reached(_branch_id: i32, _span: Span) {} fn write_locations(file: &mut File, kind: char, spans: &Mutex>) -> bool { for span in spans.lock().unwrap().iter() { - let res = write!( + let res = writeln!( file, - "{} \"{}\", {}, {}, {}, {}\n", + "{} \"{}\", {}, {}, {}, {}", kind, span.sail_file.to_string_lossy(), span.line1, @@ -49,18 +48,22 @@ fn write_locations(file: &mut File, kind: char, spans: &Mutex>) -> if res.is_err() { return false; } - }; + } true } #[no_mangle] pub extern "C" fn sail_coverage_exit() -> c_int { - if let Ok(mut file) = OpenOptions::new().create(true).append(true).open("sail_coverage") { + if let Ok(mut file) = OpenOptions::new() + .create(true) + .append(true) + .open(&*OUTPUT_FILE.lock().unwrap()) + { if !write_locations(&mut file, 'B', &BRANCHES) { - return 1 + return 1; } if !write_locations(&mut file, 'F', &FUNCTIONS) { - return 1 + return 1; } 0 } else { @@ -68,6 +71,11 @@ pub extern "C" fn sail_coverage_exit() -> c_int { } } +#[no_mangle] +pub unsafe extern "C" fn sail_set_coverage_file(output_file: *const c_char) { + *OUTPUT_FILE.lock().unwrap() = CStr::from_ptr(output_file).to_string_lossy().to_string() +} + #[no_mangle] pub unsafe extern "C" fn sail_function_entry( function_name: *const c_char, diff --git a/lib/sail_coverage.h b/lib/sail_coverage.h index 92f78f9c..487b9dce 100644 --- a/lib/sail_coverage.h +++ b/lib/sail_coverage.h @@ -3,10 +3,12 @@ int sail_coverage_exit(void); -void sail_function_entry(char *function_name, char *sail_file, int l1, int c1, int l2, int c2); +void sail_set_coverage_file(const char *output_file); -void sail_branch_taken(int branch_id, char *sail_file, int l1, int c1, int l2, int c2); +void sail_function_entry(const char *function_name, const char *sail_file, int l1, int c1, int l2, int c2); -void sail_branch_reached(int branch_id, char *sail_file, int l1, int c1, int l2, int c2); +void sail_branch_taken(int branch_id, const char *sail_file, int l1, int c1, int l2, int c2); + +void sail_branch_reached(int branch_id, const char *sail_file, int l1, int c1, int l2, int c2); #endif -- cgit v1.2.3