diff --git a/sv-parser-error/src/lib.rs b/sv-parser-error/src/lib.rs index f6a7a1a..3895cc3 100644 --- a/sv-parser-error/src/lib.rs +++ b/sv-parser-error/src/lib.rs @@ -15,6 +15,9 @@ pub enum Error { path: PathBuf, }, + #[error("File could not be read as UTF8: {0:?}")] + ReadUtf8(Option), + #[error("Include error")] Include { #[from] diff --git a/sv-parser-pp/src/preprocess.rs b/sv-parser-pp/src/preprocess.rs index 91013b1..0bf18b6 100644 --- a/sv-parser-pp/src/preprocess.rs +++ b/sv-parser-pp/src/preprocess.rs @@ -149,18 +149,21 @@ fn preprocess_inner, U: AsRef, V: BuildHasher>( })?; let mut reader = BufReader::new(f); let mut s = String::new(); - reader.read_to_string(&mut s)?; - preprocess_str( - &s, - path, - pre_defines, - include_paths, - ignore_include, - strip_comments, - 0, // resolve_depth - include_depth, - ) + if let Err(_) = reader.read_to_string(&mut s) { + Err(Error::ReadUtf8(Some(PathBuf::from(path.as_ref())))) + } else { + preprocess_str( + &s, + path, + pre_defines, + include_paths, + ignore_include, + strip_comments, + 0, // resolve_depth + include_depth, + ) + } } struct SkipNodes<'a> { @@ -1113,6 +1116,25 @@ mod tests { }; } // }}} + #[test] + #[allow(non_snake_case)] + fn err_ReadUtf8() { // {{{ + match preprocess_usualargs("err_ReadUtf8.sv").unwrap_err() { + Error::ReadUtf8(path) => { + assert_eq!( + path, + Some(PathBuf::from(format!( + "{}/testcases/err_ReadUtf8.sv", + env::var("CARGO_MANIFEST_DIR").unwrap(), + ))) + ); + } + _ => { + panic!("Error::ReadUtf8 not raised."); + } + }; + } // }}} + #[test] #[allow(non_snake_case)] fn IEEE18002017_keywords_if2_13642005() { // {{{ diff --git a/sv-parser-pp/testcases/err_ReadUtf8.sv b/sv-parser-pp/testcases/err_ReadUtf8.sv new file mode 100644 index 0000000..f47cf3c --- /dev/null +++ b/sv-parser-pp/testcases/err_ReadUtf8.sv @@ -0,0 +1,4 @@ +// In the next comment, there are non-UTF8 bytes. +module M; +// Non-UTF8:XñòóôõöX +endmodule