Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/parameter_name_style_regex' into…
Browse files Browse the repository at this point in the history
… my_master
  • Loading branch information
sconwayaus committed Jul 23, 2024
2 parents b82576d + f9359f8 commit 8727f04
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 55 deletions.
1 change: 0 additions & 1 deletion verilog/analysis/checkers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,6 @@ cc_library(
"//common/analysis:syntax-tree-lint-rule",
"//common/analysis/matcher",
"//common/analysis/matcher:bound-symbol-manager",
"//common/strings:naming-utils",
"//common/text:config-utils",
"//common/text:symbol",
"//common/text:syntax-tree-context",
Expand Down
83 changes: 33 additions & 50 deletions verilog/analysis/checkers/parameter_name_style_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@

#include "verilog/analysis/checkers/parameter_name_style_rule.h"

#include <cstdint>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -49,52 +46,36 @@ using verible::SyntaxTreeContext;
using Matcher = verible::matcher::Matcher;

// PascalCase, may end in _[0-9]+
#define LOCALPARAM_DEFAULT_REGEX "([A-Z0-9]+[a-z0-9]*)+(_[0-9]+)?"
static constexpr absl::string_view localparam_default_regex =
LOCALPARAM_DEFAULT_REGEX;
"([A-Z0-9]+[a-z0-9]*)+(_[0-9]+)?";

// PascalCase (may end in _[0-9]+) or UPPER_SNAKE_CASE
#define PARAMETER_DEFAULT_REGEX "(([A-Z0-9]+[a-z0-9]*)+(_[0-9]+)?)|([A-Z_0-9]+)"
static constexpr absl::string_view parameter_default_regex =
PARAMETER_DEFAULT_REGEX;
"(([A-Z0-9]+[a-z0-9]*)+(_[0-9]+)?)|([A-Z_0-9]+)";

ParameterNameStyleRule::ParameterNameStyleRule() {
localparam_style_regex_ =
std::make_unique<re2::RE2>(localparam_default_regex, re2::RE2::Quiet);

parameter_style_regex_ =
std::make_unique<re2::RE2>(parameter_default_regex, re2::RE2::Quiet);

kLocalparamErrorMessage = absl::StrCat(
"Localparam name does not match the naming convention ",
"defined by regex pattern: ", localparam_style_regex_->pattern());

kParameterErrorMessage = absl::StrCat(
"Parameter name does not match the naming convention ",
"defined by regex pattern: ", parameter_style_regex_->pattern());
}
ParameterNameStyleRule::ParameterNameStyleRule()
: localparam_style_regex_(std::make_unique<re2::RE2>(
localparam_default_regex, re2::RE2::Quiet)),
parameter_style_regex_(std::make_unique<re2::RE2>(parameter_default_regex,
re2::RE2::Quiet)) {}

const LintRuleDescriptor &ParameterNameStyleRule::GetDescriptor() {
static const LintRuleDescriptor d{
.name = "parameter-name-style",
.topic = "constants",
.desc =
"Checks that parameter and localparm names conform to 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"
" No style enforcement: \".*\"\n"
"RE2 regular expression syntax documentation can be found at "
"https://github.com/google/re2/wiki/syntax\n",
.param = {{"localparam_style_regex", LOCALPARAM_DEFAULT_REGEX,
"convention defined by RE2 regular expressions. The default regex "
"pattern for boht localparam and parameter names is PascalCase with "
"an optional _digit suffix. Parameters may also be UPPER_SNAKE_CASE. "
"Refer "
"to "
"https://github.com/chipsalliance/verible/tree/master/verilog/tools/"
"lint#readme for more detail on verible regex patterns.",
.param = {{"localparam_style_regex",
std::string(localparam_default_regex),
"A regex used to check localparam name style."},
{"parameter_style_regex", PARAMETER_DEFAULT_REGEX,
{"parameter_style_regex", std::string(parameter_default_regex),
"A regex used to check parameter name style."}},
};

Expand All @@ -106,6 +87,18 @@ static const Matcher &ParamDeclMatcher() {
return matcher;
}

std::string ParameterNameStyleRule::CreateLocalparamViolationMessage() {
return absl::StrCat(
"Localparam name does not match the naming convention ",
"defined by regex pattern: ", localparam_style_regex_->pattern());
}

std::string ParameterNameStyleRule::CreateParameterViolationMessage() {
return absl::StrCat(
"Parameter name does not match the naming convention ",
"defined by regex pattern: ", parameter_style_regex_->pattern());
}

void ParameterNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
const SyntaxTreeContext &context) {
verible::matcher::BoundSymbolManager manager;
Expand All @@ -121,15 +114,15 @@ void ParameterNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
switch (param_decl_token) {
case TK_localparam:
if (!RE2::FullMatch(name, *localparam_style_regex_)) {
violations_.insert(
LintViolation(*id, kLocalparamErrorMessage, context));
violations_.insert(LintViolation(
*id, CreateLocalparamViolationMessage(), context));
}
break;

case TK_parameter:
if (!RE2::FullMatch(name, *parameter_style_regex_)) {
violations_.insert(
LintViolation(*id, kParameterErrorMessage, context));
LintViolation(*id, CreateParameterViolationMessage(), context));
}
break;

Expand All @@ -148,17 +141,7 @@ absl::Status ParameterNameStyleRule::Configure(
{{"localparam_style_regex", SetRegex(&localparam_style_regex_)},
{"parameter_style_regex", SetRegex(&parameter_style_regex_)}});

if (!s.ok()) return s;

kLocalparamErrorMessage = absl::StrCat(
"Localparam name does not match the naming convention ",
"defined by regex pattern: ", localparam_style_regex_->pattern());

kParameterErrorMessage = absl::StrCat(
"Parameter name does not match the naming convention ",
"defined by regex pattern: ", parameter_style_regex_->pattern());

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

LintRuleStatus ParameterNameStyleRule::Report() const {
Expand Down
7 changes: 3 additions & 4 deletions verilog/analysis/checkers/parameter_name_style_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#ifndef VERIBLE_VERILOG_ANALYSIS_CHECKERS_PARAMETER_NAME_STYLE_RULE_H_
#define VERIBLE_VERILOG_ANALYSIS_CHECKERS_PARAMETER_NAME_STYLE_RULE_H_

#include <cstdint>
#include <memory>
#include <set>
#include <string>
Expand All @@ -42,6 +41,9 @@ class ParameterNameStyleRule : public verible::SyntaxTreeLintRule {

static const LintRuleDescriptor &GetDescriptor();

std::string CreateLocalparamViolationMessage();
std::string CreateParameterViolationMessage();

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

Expand All @@ -55,9 +57,6 @@ class ParameterNameStyleRule : public verible::SyntaxTreeLintRule {
// A regex to check the style against
std::unique_ptr<re2::RE2> localparam_style_regex_;
std::unique_ptr<re2::RE2> parameter_style_regex_;

std::string kLocalparamErrorMessage;
std::string kParameterErrorMessage;
};

} // namespace analysis
Expand Down

0 comments on commit 8727f04

Please sign in to comment.