Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linter: make constraint-name-style configurable #2017

Conversation

IEncinas10
Copy link
Collaborator

@IEncinas10 IEncinas10 commented Sep 23, 2023

Up until now, constraint-name-style rule required constraint names to end in _c. This makes it configurable, so that users can require constraint names to start with c_ instead.

I'm open to sugestions about the configuration parameter's name, it doesn't convince me too much but I can't think of a better option. We could have check_suffix and check_prefix but as it doesn't make sense for them to be on at the same time I don't like it too much either.


Edit: forgot to handle this, leaving it here so I don't forget

  • Handle invalid regex provided by user

@codecov-commenter
Copy link

codecov-commenter commented Sep 23, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (a4d61b1) 92.95% compared to head (846710b) 92.95%.

Files Patch % Lines
...og/analysis/checkers/constraint_name_style_rule.cc 94.11% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2017      +/-   ##
==========================================
- Coverage   92.95%   92.95%   -0.01%     
==========================================
  Files         358      359       +1     
  Lines       26446    26458      +12     
==========================================
+ Hits        24583    24594      +11     
- Misses       1863     1864       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hzeller
Copy link
Collaborator

hzeller commented Dec 19, 2023

(Sorry for the long delay, I was pretty busy with other things...)

Maybe we can provide a regular expression, e.g. configuration parameter name pattern, and then the user can decide. And then we can set the default to "([a-z0-9]+_)+c" or so to enforce snake-case with trailing-_c' (we can keep the regex simple and don't have to worry about the beginning starting with a digit as that already has been taken care of by the lexer/parser)

Using regular expressions is probably also a good choice to configure some of the other rules - they are
We use the re2 library successfully in other parts of the project e.g. in ./common/tools/jcxxgen.cc and ./verilog/tools/ls/autoexpand.cc.

@IEncinas10
Copy link
Collaborator Author

No worries, first things first!

I remember seeing discussion about regular expressions for some other rules in the past but thought the support wasn't there yet. I'll give this a look and definitely change it. If it ends up being as easy at it sounds I'll make an issue to eventually update every rule that could make use of this feature.

@hzeller
Copy link
Collaborator

hzeller commented Dec 21, 2023

At the time we had the disccussions about regular expressions, the choice was std::regex, but that is generally a poor choice as it is slow. Since then, RE2 had been added to the dependencies, and that is pretty performant.

@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch from 5e9b7da to bbccb82 Compare January 13, 2024 16:52
static const Matcher& ConstraintMatcher() {
absl::Status ConstraintNameStyleRule::Configure(
absl::string_view configuration) {
std::string pattern = kSuffix;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant but the rationale is the following:

If at some point we add other parameters to the rule, but pattern is not provided, the ParseNameValues wouldn't set any value to pattern, so we would then need to set a default value.

Avoid future problems by doing it already?

violations_.insert(LintViolation(*identifier_token, kMessage, context));
if (!RE2::FullMatch(constraint_name, *regex)) {
violations_.insert(
LintViolation(*identifier_token, FormatReason(), context));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't find many resources on RE2. Can it somehow help us provide a better linting message than just "follow the regex"? Perhaps provide an autofix?

@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch 2 times, most recently from 7332d1b to 846710b Compare January 14, 2024 10:24
@IEncinas10 IEncinas10 requested a review from hzeller March 29, 2024 13:25
@hzeller
Copy link
Collaborator

hzeller commented Nov 21, 2024

Looking backwards in time to see if there are active PRs. This looks like it is still relevant though probably some rebase needed due to conflicts.

@hzeller hzeller added the want-merge-needs-rebase a pull request that should not be lost, but needs rebasing first. label Nov 23, 2024
@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch from 846710b to de0dffd Compare November 23, 2024 10:45
@IEncinas10
Copy link
Collaborator Author

Looking backwards in time to see if there are active PRs. This looks like it is still relevant though probably some rebase needed due to conflicts.

Updated, thanks for the reminder!

@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch 3 times, most recently from f97ec65 to d666d5e Compare November 26, 2024 17:46
void HandleSymbol(const verible::Symbol &symbol,
const verible::SyntaxTreeContext &context) final;

verible::LintRuleStatus Report() const final;

std::string Pattern() const { return regex->pattern(); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this public method (returning a string by value) needed anywhere ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't, I used it to diagnose if the user-provided regex failed to parse but it is not needed anymore thanks to the SetRegex utility. Missed it, thanks!

// convention.
// The constraints should be named with lower_snake_case and end with _c.
// Lower snake case, ends with `_c`
#define kSuffix "([a-z0-9]+_)+c"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a macro, can this be a static constexpr absl::string_view kSuffix in the private section of ConstraintNameStyleRule instead ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch from d666d5e to de368db Compare November 26, 2024 18:47
using verible::RunLintTestCases;

// Lower snake case, starts with `c_`
#define kPrefix "c+(_[a-z0-9]+)+"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably move this into the VariousPrefixTests TEST to be close to where it is needed.
(and since it is only used in one place to assemble the pattern: configuration, maybe it doesn't need to
be a macro at all).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(other than that, looks great and ready to be merged)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I needed this because my original intent was to offer a choice between _c suffix and prefix. Thanks!

Make constraint-name-style rule regex-configurable while maintaining its
default behavior of requiring constraint names to be lower snake case
ending in `_c`
@IEncinas10 IEncinas10 force-pushed the configurable-constraint-name-style-rule branch from de368db to 2191f11 Compare November 27, 2024 16:36
Copy link
Collaborator

@hzeller hzeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.
Thanks!

@hzeller hzeller merged commit f3da2ce into chipsalliance:master Nov 27, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
want-merge-needs-rebase a pull request that should not be lost, but needs rebasing first.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants