Skip to content

Commit

Permalink
Adding back the original configuration to avoid breaking users setups…
Browse files Browse the repository at this point in the history
…. The rule now forms a single regex using the original configuration values and a user regex.
  • Loading branch information
sconwayaus committed Aug 10, 2024
1 parent f9359f8 commit 23d76be
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 47 deletions.
3 changes: 3 additions & 0 deletions verilog/analysis/checkers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,9 @@ cc_test(
"//common/analysis:syntax-tree-linter-test-utils",
"//verilog/analysis:verilog-analyzer",
"//verilog/parser:verilog-token-enum",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
Expand Down
130 changes: 111 additions & 19 deletions verilog/analysis/checkers/parameter_name_style_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

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

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

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -45,40 +47,41 @@ using verible::LintViolation;
using verible::SyntaxTreeContext;
using Matcher = verible::matcher::Matcher;

// PascalCase, may end in _[0-9]+
static constexpr absl::string_view localparam_default_regex =
// Upper Camel Case (may end in _[0-9]+)
static constexpr absl::string_view kUpperCamelCaseRegex =
"([A-Z0-9]+[a-z0-9]*)+(_[0-9]+)?";
// ALL_CAPS
static constexpr absl::string_view kAllCapsRegex = "[A-Z_0-9]+";

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

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)) {}
absl::StrCat("(", kUpperCamelCaseRegex, ")"), re2::RE2::Quiet)),
parameter_style_regex_(std::make_unique<re2::RE2>(
absl::StrCat("(", kUpperCamelCaseRegex, ")|(", kAllCapsRegex, ")"),
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 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 "
"convention based on a choice of 'CamelCase', 'ALL_CAPS' and a user "
"defined regex ORed together. Empty configurtaion: no style "
"enforcement. 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),
.param = {{"localparam_style", "CamelCase", "Style of localparam names"},
{"parameter_style", "CamelCase|ALL_CAPS",
"Style of parameter names."},
{"localparam_style_regex", std::string(kLocalparamDefaultRegex),
"A regex used to check localparam name style."},
{"parameter_style_regex", std::string(parameter_default_regex),
{"parameter_style_regex", std::string(kParameterDefaultRegex),
"A regex used to check parameter name style."}},
};

return d;
}

Expand Down Expand Up @@ -133,14 +136,103 @@ void ParameterNameStyleRule::HandleSymbol(const verible::Symbol &symbol,
}
}

absl::Status ParameterNameStyleRule::AppendRegex(
std::unique_ptr<re2::RE2> *rule_regex, absl::string_view regex_str) {
if (*rule_regex == nullptr) {
*rule_regex = std::make_unique<re2::RE2>(absl::StrCat("(", regex_str, ")"),
re2::RE2::Quiet);
} else {
*rule_regex = std::make_unique<re2::RE2>(
absl::StrCat((*rule_regex)->pattern(), "|(", regex_str, ")"),
re2::RE2::Quiet);
}

if ((*rule_regex)->ok()) {
return absl::OkStatus();
}

std::string error_msg = absl::StrCat("Failed to parse regular expression: ",
(*rule_regex)->error());
return absl::InvalidArgumentError(error_msg);
}

absl::Status ParameterNameStyleRule::ConfigureRegex(
std::unique_ptr<re2::RE2> *rule_regex, uint32_t config_style,
std::unique_ptr<re2::RE2> *config_style_regex) {
absl::Status s;

int config_style_regex_length = (*config_style_regex)->pattern().length();

// If no rule is set, no style enforcement
if (config_style == 0 && config_style_regex_length == 0) {
// Set the regex pattern to match everything
*rule_regex = std::make_unique<re2::RE2>(".*", re2::RE2::Quiet);
return absl::OkStatus();
}

// Clear rule_regex
*rule_regex = nullptr;

// Append UpperCamelCase regex to rule_regex (if enabled)
if (config_style & kUpperCamelCase) {
s = AppendRegex(rule_regex, kUpperCamelCaseRegex);
if (!s.ok()) {
return s;
}
}

// Append ALL_CAPS regex to rule_regex (if enabled)
if (config_style & kAllCaps) {
s = AppendRegex(rule_regex, kAllCapsRegex);
if (!s.ok()) {
return s;
}
}

// Append regex from config_style_regex (if provided by the user)
if (config_style_regex_length > 0) {
s = AppendRegex(rule_regex, (*config_style_regex)->pattern());
}

return s;
}

absl::Status ParameterNameStyleRule::Configure(
absl::string_view configuration) {
// same sequence as enum StyleChoicesBits
static const std::vector<absl::string_view> choices = {"CamelCase",
"ALL_CAPS"};

uint32_t localparam_style = kUpperCamelCase;
uint32_t parameter_style = kUpperCamelCase | kAllCaps;
std::unique_ptr<re2::RE2> localparam_style_regex =
std::make_unique<re2::RE2>(kLocalparamDefaultRegex, re2::RE2::Quiet);
std::unique_ptr<re2::RE2> parameter_style_regex =
std::make_unique<re2::RE2>(kParameterDefaultRegex, re2::RE2::Quiet);

using verible::config::SetNamedBits;
using verible::config::SetRegex;

absl::Status s = verible::ParseNameValues(
configuration,
{{"localparam_style_regex", SetRegex(&localparam_style_regex_)},
{"parameter_style_regex", SetRegex(&parameter_style_regex_)}});
{{"localparam_style", SetNamedBits(&localparam_style, choices)},
{"parameter_style", SetNamedBits(&parameter_style, choices)},
{"localparam_style_regex", SetRegex(&localparam_style_regex)},
{"parameter_style_regex", SetRegex(&parameter_style_regex)}});

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

// Form a regex to use based on *_style, and *_style_regex
s = ConfigureRegex(&localparam_style_regex_, localparam_style,
&localparam_style_regex);
if (!s.ok()) {
return s;
}

s = ConfigureRegex(&parameter_style_regex_, parameter_style,
&parameter_style_regex);
return s;
}

Expand Down
21 changes: 20 additions & 1 deletion verilog/analysis/checkers/parameter_name_style_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#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 Down Expand Up @@ -51,10 +52,28 @@ class ParameterNameStyleRule : public verible::SyntaxTreeLintRule {

absl::Status Configure(absl::string_view configuration) final;

const RE2 *localparam_style_regex() const {
return localparam_style_regex_.get();
}
const RE2 *parameter_style_regex() const {
return parameter_style_regex_.get();
}

private:
absl::Status AppendRegex(std::unique_ptr<re2::RE2> *rule_regex,
absl::string_view regex_str);
absl::Status ConfigureRegex(std::unique_ptr<re2::RE2> *rule_regex,
uint32_t config_style,
std::unique_ptr<re2::RE2> *config_style_regex);

enum StyleChoicesBits {
kUpperCamelCase = (1 << 0),
kAllCaps = (1 << 1),
};

std::set<verible::LintViolation> violations_;

// A regex to check the style against
// Regex's to check the style against
std::unique_ptr<re2::RE2> localparam_style_regex_;
std::unique_ptr<re2::RE2> parameter_style_regex_;
};
Expand Down
Loading

0 comments on commit 23d76be

Please sign in to comment.