summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlasdair2020-08-25 17:20:00 +0100
committerAlasdair2020-08-25 17:20:00 +0100
commit14e5c79b1c6943c88ab36ccc46f073674a76e16c (patch)
treee0dbddb46ddbbba49d60081cb613737f26983b6e /lib
parent758a5b23ff0426ab6cf880d181fce1552b7f3ca7 (diff)
Add function sail_set_coverage_file to sail_coverage header
Can be set by C emulator to control where coverage information is written
Diffstat (limited to 'lib')
-rw-r--r--lib/coverage/src/lib.rs28
-rw-r--r--lib/sail_coverage.h8
2 files changed, 23 insertions, 13 deletions
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<HashSet<Span>> = Mutex::new(HashSet::new());
static ref FUNCTIONS: Mutex<HashSet<Span>> = Mutex::new(HashSet::new());
+ static ref OUTPUT_FILE: Mutex<String> = 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<HashSet<Span>>) -> 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<HashSet<Span>>) ->
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 {
@@ -69,6 +72,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,
sail_file: *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