Add include line check

This commit is contained in:
dalance 2019-12-12 18:49:01 +09:00
parent 1225d3e972
commit 6fdc341ed7
5 changed files with 68 additions and 0 deletions

View File

@ -2,6 +2,8 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.4.18...Unreleased) - ReleaseDate
* [Added] include line check
## [v0.4.18](https://github.com/dalance/sv-parser/compare/v0.4.17...v0.4.18) - 2019-12-12
## [v0.4.17](https://github.com/dalance/sv-parser/compare/v0.4.16...v0.4.17) - 2019-12-12

View File

@ -26,6 +26,8 @@ pub enum ErrorKind {
DefineNoArgs,
#[fail(display = "Exceed recursive limit")]
ExceedRecursiveLimit,
#[fail(display = "Include line can't have other items")]
IncludeLine,
}
// -----------------------------------------------------------------------------

View File

@ -140,6 +140,9 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
let mut skip_nodes = vec![];
let mut defines = HashMap::new();
let mut last_item_line = None;
let mut last_include_line = None;
for (k, v) in pre_defines {
defines.insert(k.clone(), (*v).clone());
}
@ -178,6 +181,33 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
}
}
}
match n.clone() {
NodeEvent::Enter(RefNode::SourceDescriptionNotDirective(x)) => {
let locate: Locate = x.try_into().unwrap();
if let Some(last_include_line) = last_include_line {
if last_include_line == locate.line {
return Err(ErrorKind::IncludeLine.into());
}
}
}
NodeEvent::Enter(RefNode::CompilerDirective(x)) => {
let locate: Locate = x.try_into().unwrap();
if let Some(last_include_line) = last_include_line {
if last_include_line == locate.line {
return Err(ErrorKind::IncludeLine.into());
}
}
}
NodeEvent::Leave(RefNode::SourceDescriptionNotDirective(x)) => {
let locate: Locate = x.try_into().unwrap();
last_item_line = Some(locate.line);
}
NodeEvent::Leave(RefNode::CompilerDirective(x)) => {
let locate: Locate = x.try_into().unwrap();
last_item_line = Some(locate.line);
}
_ => (),
}
match n {
NodeEvent::Enter(RefNode::ResetallCompilerDirective(_)) if !skip => {}
NodeEvent::Enter(RefNode::UndefineCompilerDirective(x)) if !skip => {
@ -315,6 +345,15 @@ pub fn preprocess_str<T: AsRef<Path>, U: AsRef<Path>>(
defines.insert(id, Some(define));
}
NodeEvent::Enter(RefNode::IncludeCompilerDirective(x)) if !skip => {
let locate: Locate = x.try_into().unwrap();
last_include_line = Some(locate.line);
if let Some(last_item_line) = last_item_line {
if last_item_line == locate.line {
return Err(ErrorKind::IncludeLine.into());
}
}
let mut path = match x {
IncludeCompilerDirective::DoubleQuote(x) => {
let (_, _, ref literal) = x.nodes;
@ -722,4 +761,24 @@ endmodule
"Err(Error { inner: \n\nExceed recursive limit })"
);
}
#[test]
fn test9() {
let include_paths = [get_testcase("")];
let ret = preprocess(get_testcase("test9.sv"), &HashMap::new(), &include_paths);
assert_eq!(
format!("{:?}", ret),
"Err(Error { inner: \n\nInclude line can\'t have other items })"
);
}
#[test]
fn test10() {
let include_paths = [get_testcase("")];
let ret = preprocess(get_testcase("test10.sv"), &HashMap::new(), &include_paths);
assert_eq!(
format!("{:?}", ret),
"Err(Error { inner: \n\nInclude line can\'t have other items })"
);
}
}

View File

@ -0,0 +1,2 @@
module and_op (a, b, c);
`include "test2.svh" endmodule

View File

@ -0,0 +1,3 @@
module and_op (a, b, c);
`include "test2.svh" `include "test2.svh"
endmodule