-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from ronitnallagatla/match_filename
New rule that ensures module/interface/package/program identifier matches the filename it's in
- Loading branch information
Showing
20 changed files
with
630 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/target | ||
**/*.rs.bk | ||
/Cargo.lock | ||
.svlint.toml | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
md/syntaxrules-explanation-interface_identifier_matches_filename.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Interface identifier should have the same name as the file it's in. | ||
|
||
```interface foo;``` is allowed to live in any file of naming convention ```foo <Non-Identifier> <whatever else>``` | ||
|
||
According to Clause 5.6 of IEEE 1800-2017: | ||
|
||
> A simple identifier shall consist of a sequence of letters, digits, dollar signs (`$`), and underscore (`_`) characters. | ||
Any symbol defined outside this exhaustive list is considered a non-identifier. | ||
|
||
The stopping point for string matching has to be a non-identifier character. | ||
|
||
For example, the interface declaration ```interface foo;``` is valid in filenames such as ```foo-Bar.sv```, ```foo.debug.sv```, and ```foo-final-version.sv```. Each of these filenames begins with the interface identifier ```foo``` and is immediately followed by a non-identifier character (```-```, ```.```, or another acceptable symbol), making them compliant. A filename like ```FooBar.sv``` is invalid for the ```interface Foo;``` declaration since it does not contain a non-identifier character following the interface name. | ||
|
||
Note that as a consequence, only one interface can be declared per file. |
15 changes: 15 additions & 0 deletions
15
md/syntaxrules-explanation-module_identifier_matches_filename.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Module identifier should have the same name as the file it's in. | ||
|
||
```module foo;``` is allowed to live in any file of naming convention ```foo <Non-Identifier> <whatever else>``` | ||
|
||
According to Clause 5.6 of IEEE 1800-2017: | ||
|
||
> A simple identifier shall consist of a sequence of letters, digits, dollar signs (`$`), and underscore (`_`) characters. | ||
Any symbol defined outside this exhaustive list is considered a non-identifier. | ||
|
||
The stopping point for string matching has to be a non-identifier character. | ||
|
||
For example, the module declaration ```module foo;``` is valid in filenames such as ```foo-Bar.sv```, ```foo.debug.sv```, and ```foo-final-version.sv```. Each of these filenames begins with the module identifier ```foo``` and is immediately followed by a non-identifier character (```-```, ```.```, or another acceptable symbol), making them compliant. A filename like ```FooBar.sv``` is invalid for the ```module Foo;``` declaration since it does not contain a non-identifier character following the module name. | ||
|
||
Note that as a consequence, only one module can be declared per file. |
15 changes: 15 additions & 0 deletions
15
md/syntaxrules-explanation-package_identifier_matches_filename.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Package identifier should have the same name as the file it's in. | ||
|
||
```package foo;``` is allowed to live in any file of naming convention ```foo <Non-Identifier> <whatever else>``` | ||
|
||
According to Clause 5.6 of IEEE 1800-2017: | ||
|
||
> A simple identifier shall consist of a sequence of letters, digits, dollar signs (`$`), and underscore (`_`) characters. | ||
Any symbol defined outside this exhaustive list is considered a non-identifier. | ||
|
||
The stopping point for string matching has to be a non-identifier character. | ||
|
||
For example, the package declaration ```package foo;``` is valid in filenames such as ```foo-Bar.sv```, ```foo.debug.sv```, and ```foo-final-version.sv```. Each of these filenames begins with the package identifier ```foo``` and is immediately followed by a non-identifier character (```-```, ```.```, or another acceptable symbol), making them compliant. A filename like ```FooBar.sv``` is invalid for the ```package Foo;``` declaration since it does not contain a non-identifier character following the package name. | ||
|
||
Note that as a consequence, only one package can be declared per file. |
15 changes: 15 additions & 0 deletions
15
md/syntaxrules-explanation-program_identifier_matches_filename.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Program identifier should have the same name as the file it's in. | ||
|
||
```program foo;``` is allowed to live in any file of naming convention ```foo <Non-Identifier> <whatever else>``` | ||
|
||
According to Clause 5.6 of IEEE 1800-2017: | ||
|
||
> A simple identifier shall consist of a sequence of letters, digits, dollar signs (`$`), and underscore (`_`) characters. | ||
Any symbol defined outside this exhaustive list is considered a non-identifier. | ||
|
||
The stopping point for string matching has to be a non-identifier character. | ||
|
||
For example, the program declaration ```program foo;``` is valid in filenames such as ```foo-Bar.sv```, ```foo.debug.sv```, and ```foo-final-version.sv```. Each of these filenames begins with the program identifier ```foo``` and is immediately followed by a non-identifier character (```-```, ```.```, or another acceptable symbol), making them compliant. A filename like ```FooBar.sv``` is invalid for the ```program Foo;``` declaration since it does not contain a non-identifier character following the program name. | ||
|
||
Note that as a consequence, only one program can be declared per file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::config::ConfigOption; | ||
use crate::linter::{SyntaxRule, SyntaxRuleResult}; | ||
use sv_parser::{unwrap_locate, NodeEvent, RefNode, SyntaxTree, unwrap_node, Locate}; | ||
|
||
#[derive(Default)] | ||
pub struct InterfaceIdentifierMatchesFilename; | ||
impl SyntaxRule for InterfaceIdentifierMatchesFilename { | ||
fn check( | ||
&mut self, | ||
syntax_tree: &SyntaxTree, | ||
event: &NodeEvent, | ||
_option: &ConfigOption, | ||
) -> SyntaxRuleResult { | ||
let node = match event { | ||
NodeEvent::Enter(x) => x, | ||
NodeEvent::Leave(_) => { | ||
return SyntaxRuleResult::Pass; | ||
} | ||
}; | ||
|
||
match node { | ||
RefNode::InterfaceIdentifier(x) => { | ||
let path_str = if let Some(x) = unwrap_locate!(node.clone()) { | ||
if let Some((path, _)) = syntax_tree.get_origin(&x) { | ||
path | ||
} else { | ||
return SyntaxRuleResult::Fail; | ||
} | ||
} else { | ||
return SyntaxRuleResult::Fail; | ||
}; | ||
|
||
let id: Option<&Locate> = match unwrap_node!(*x, SimpleIdentifier) { | ||
Some(RefNode::SimpleIdentifier(id_)) => { | ||
unwrap_locate!(id_) | ||
}, | ||
_ => None, | ||
}; | ||
|
||
if id.is_none() { | ||
return SyntaxRuleResult::Fail; | ||
} | ||
|
||
let interface_name = syntax_tree.get_str(id.unwrap()).unwrap(); | ||
|
||
let path = std::path::Path::new(path_str); | ||
if let Some(file_name_os_str) = path.file_name() { | ||
if let Some(file_name) = file_name_os_str.to_str() { | ||
let mut identifier_end = 0; | ||
for (i, c) in file_name.char_indices() { | ||
if c.is_alphanumeric() || c == '_' || c == '$' { | ||
identifier_end = i + c.len_utf8(); | ||
} else { | ||
// Stop at the first non-identifier character | ||
break; | ||
} | ||
} | ||
|
||
let file_ident = &file_name[..identifier_end]; | ||
|
||
// Ignoring Case | ||
if file_ident.eq_ignore_ascii_case(interface_name) { | ||
return SyntaxRuleResult::Pass; | ||
} | ||
} | ||
} | ||
|
||
SyntaxRuleResult::Fail | ||
}, | ||
|
||
_ => SyntaxRuleResult::Pass, | ||
} | ||
|
||
} | ||
|
||
fn name(&self) -> String { | ||
String::from("interface_identifier_matches_filename") | ||
} | ||
|
||
fn hint(&self, _option: &ConfigOption) -> String { | ||
String::from("Ensure that the interface name matches the file name. Interface Bar should be in some/path/to/Bar.sv") | ||
} | ||
|
||
fn reason(&self) -> String { | ||
String::from("Encourages consistent file naming standards for packages and assists in searching for interfaces.") | ||
} | ||
|
||
} |
Oops, something went wrong.