Skip to content

Commit

Permalink
Moving some documentation into the linter README.md.
Browse files Browse the repository at this point in the history
Cleaned up the violation message (remove code duplication).
  • Loading branch information
sconwayaus committed Jul 10, 2024
1 parent 6f6c4a1 commit fa735dd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion verilog/analysis/checkers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ User documentation for the lint rules is generated dynamically, and can be found
at https://chipsalliance.github.io/verible/verilog_lint.html, or by running
`verible-verilog-lint --help_rules` for text or `--generate_markdown`.

[Linter user documentation can be found here](../../tool/lint).
[Linter user documentation can be found here](../../tools/lint).
39 changes: 15 additions & 24 deletions verilog/analysis/checkers/enum_name_style_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,19 @@ static constexpr absl::string_view style_default_regex = "[a-z_0-9]+(_t|_e)";

EnumNameStyleRule::EnumNameStyleRule()
: style_regex_(
std::make_unique<re2::RE2>(style_default_regex, re2::RE2::Quiet)),
violation_message_(absl::StrCat(
"Enum type name does not match the naming convention ",
"defined by regex pattern: ", style_regex_->pattern())) {}
std::make_unique<re2::RE2>(style_default_regex, re2::RE2::Quiet)) {}

const LintRuleDescriptor &EnumNameStyleRule::GetDescriptor() {
static const LintRuleDescriptor d{
.name = "enum-name-style",
.topic = "enumerations",
.desc =
"Checks that enum type names follow a naming convention defined by "
"a RE2 regular expression.\n"
"Example common regex patterns:\n"
" lower_snake_case: \"[a-z_0-9]+\"\n"
" UPPER_SNAKE_CASE: \"[A-Z_0-9]+\"\n"
" Title_Snake_Case: \"[A-Z]+[a-z0-9]*(_[A-Z0-9]+[a-z0-9]*)*\"\n"
" Sentence_snake_case: \"([A-Z0-9]+[a-z0-9]*_?)([a-z0-9]*_*)*\"\n"
" camelCase: \"([a-z0-9]+[A-Z0-9]*)+\"\n"
" PascalCaseRegexPattern: \"([A-Z0-9]+[a-z0-9]*)+\"\n"
"RE2 regular expression syntax documentation can be found at "
"https://github.com/google/re2/wiki/syntax\n",
"Checks that enum type names follow a naming convention defined by a "
"RE2 regular expression. The default regex pattern expects "
"\"lower_snake_case\" with either a \"_t\" or \"_e\" suffix. Refer "
"to "
"https://github.com/chipsalliance/verible/tree/master/verilog/tools/"
"lint#readme for more detail on verible regex patterns.",
.param = {{"style_regex", std::string(style_default_regex),
"A regex used to check enum type name style."}},
};
Expand All @@ -80,6 +72,11 @@ static const Matcher &TypedefMatcher() {
return matcher;
}

std::string EnumNameStyleRule::CreateViolationMessage() {
return absl::StrCat("Enum type name does not match the naming convention ",
"defined by regex pattern: ", style_regex_->pattern());
}

void EnumNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
const SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
Expand All @@ -90,8 +87,8 @@ void EnumNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
const auto *identifier_leaf = GetIdentifierFromTypeDeclaration(symbol);
const auto name = ABSL_DIE_IF_NULL(identifier_leaf)->get().text();
if (!RE2::FullMatch(name, *style_regex_)) {
violations_.insert(
LintViolation(identifier_leaf->get(), violation_message_, context));
violations_.insert(LintViolation(identifier_leaf->get(),
CreateViolationMessage(), context));
}
} else {
// Not an enum definition
Expand All @@ -104,13 +101,7 @@ absl::Status EnumNameStyleRule::Configure(absl::string_view configuration) {
using verible::config::SetRegex;
absl::Status s = verible::ParseNameValues(
configuration, {{"style_regex", SetRegex(&style_regex_)}});
if (!s.ok()) return s;

violation_message_ =
absl::StrCat("Enum type name does not match the naming convention ",
"defined by regex pattern: ", style_regex_->pattern());

return absl::OkStatus();
return s;
}

LintRuleStatus EnumNameStyleRule::Report() const {
Expand Down
4 changes: 2 additions & 2 deletions verilog/analysis/checkers/enum_name_style_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class EnumNameStyleRule : public verible::SyntaxTreeLintRule {

static const LintRuleDescriptor &GetDescriptor();

std::string CreateViolationMessage();

void HandleSymbol(const verible::Symbol &symbol,
const verible::SyntaxTreeContext &context) final;

Expand All @@ -53,8 +55,6 @@ class EnumNameStyleRule : public verible::SyntaxTreeLintRule {

// A regex to check the style against
std::unique_ptr<re2::RE2> style_regex_;

std::string violation_message_;
};

} // namespace analysis
Expand Down
10 changes: 5 additions & 5 deletions verilog/analysis/checkers/interface_name_style_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static constexpr absl::string_view kMessage =
"Interface names must use lower_snake_case naming convention "
"and end with _if.";

const LintRuleDescriptor& InterfaceNameStyleRule::GetDescriptor() {
const LintRuleDescriptor &InterfaceNameStyleRule::GetDescriptor() {
static const LintRuleDescriptor d{
.name = "interface-name-style",
.topic = "interface-conventions",
Expand All @@ -56,16 +56,16 @@ const LintRuleDescriptor& InterfaceNameStyleRule::GetDescriptor() {
return d;
}

static const Matcher& InterfaceMatcher() {
static const Matcher &InterfaceMatcher() {
static const Matcher matcher(NodekInterfaceDeclaration());
return matcher;
}

void InterfaceNameStyleRule::HandleSymbol(const verible::Symbol& symbol,
const SyntaxTreeContext& context) {
void InterfaceNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
const SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
absl::string_view name;
const verible::TokenInfo* identifier_token;
const verible::TokenInfo *identifier_token;
if (InterfaceMatcher().Matches(symbol, &manager)) {
identifier_token = GetInterfaceNameToken(symbol);
name = identifier_token->text();
Expand Down
20 changes: 20 additions & 0 deletions verilog/tools/lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ Additionally, the `--rules_config` flag can be used to read configuration stored
in a file. The syntax is the same as above, except the rules can be also
separated with the newline character.

### Regular Expressions
Some lint rules allow users to use a regular expression to configure the rule. Verible utilises the RE2 regular expression library and full syntax documentation can be found at https://github.com/google/re2/wiki/syntax.

```bash
verible-verilog-lint --rules="enum-name-style=style_regex:[a-z_0-9]+(_t|_e)" ...
```

For naming style rules, the regex can be kept simple and not have to worry about
the beginning starting with a digit as this is already taken care of by the
lexer/parser. Below are some common naming style regex patterns that can be used.

| Naming Style | Regex Expression |
|------------------------|--------------------------------------------|
| lower\_snake\_case | [a-z\_0-9]+ |
| UPPER\_SNAKE\_CASE | [A-Z\_0-9]+ |
| Title\_Snake\_Case | [A-Z]+[a-z0-9]\*(\_[A-Z0-9]+[a-z0-9]\*)\* |
| Sentence\_snake\_case | ([A-Z0-9]+[a-z0-9]\*\_?)([a-z0-9]\*\_\*)\* |
| camelCase | ([a-z0-9]+[A-Z0-9]\*)+ |
| PascalCase | ([A-Z0-9]+[a-z0-9]\*)+ |

## Waiving Lint Violations {#lint-waiver}

### In-file waiver comments
Expand Down

0 comments on commit fa735dd

Please sign in to comment.