-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add nxstyle directive support for selective style checking #18257
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
base: master
Are you sure you want to change the base?
Conversation
Add support for three nxstyle directives to allow fine-grained control over style checking, similar to clang-format directives: 1. /* nxstyle off */ - Disable style checks starting from the next line 2. /* nxstyle on */ - Re-enable style checks starting from the next line 3. /* nxstyle ignore */ - Ignore style checks for the current line only Key implementation details: - Directives are detected in the check_nxstyle_directive() function by searching for 'nxstyle ' pattern in the line - 'off' and 'on' directives take effect from the line AFTER the directive, allowing the directive line itself to be checked for format issues - 'ignore' applies only to the current line and does NOT suppress line length checks (line length is always verified) - The directive state is tracked across the file and properly handles nested off/on pairs - Line length validation is enhanced to check even lines with right-hand comments when they contain 'nxstyle ignore' This allows developers to wrap protobuf-generated types and other non-conforming code blocks with minimal style directive overhead. Signed-off-by: wangjianyu3 <[email protected]>
michallenc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document the feature on this page https://nuttx.apache.org/docs/latest/contributing/coding_style.html.
Btw, I think we need to come up with some rules regarding the usage. I think it's good to use it in generated code like headers and register maps as you mentioned in PR description, but imho it shouldn't occur in a manually written code.
Exactly, otherwise the coding style never will be enforced :-) |
raiden00pl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nuttx has never allowed meta language in the source code. Introducing such tags violates this principle and should be discussed within the community. I'm not opposed to this change, I think it's necessary at this point (too many external dependencies), but it should be better communicated within the community and clear rules when to use this should be decided.
Yes, I think Greg always tried to keep NuttX self-contained, avoiding dependencies on many external tools, even python scripts were avoided in favor of shell script (it bring other benefit: python is a moving target, shell script is not, but of course, it is very limited). But I think what you call "meta language" is very limited too and will not impact too much, unless used to avoid valid rules that should be followed. So it should be used with care |
|
why not ignore the style warning from the foregin source file direct by simply mentioning the reason why the style issue can't be fixed. |
|
@xiaoxiang781216 @acassis @michallenc @raiden00pl Adding a whitelist is a viable approach, but it is more appropriate to include the generated source files in the whitelist as a whole. For the code that references these generated types/symbols, we only need to skip the format check for the parts related to the generated types/symbols, while the vast majority of the remaining code still needs to undergo a full format check. For example
|
|
you can just asking the maintainer ignore the style error in the generated file. |
But that would cause CI to fail on that file. It's not a big problem for PR where generated file was introduced, but every subsequent PR that changes the file with non nxstyle code would fail as well and other maintainers and contributors would have to figure out if the code doesn't follow nxstyle on purpose or just accidentally. It's much clearer to avoid this by allowing nxcheck tool to ignore parts of code or entire files and thus CI will still remain green. The only thing the maintainers would then be responsible is to judge if the code should be allowed to not follow nxstyle. |
The auto generated code shouldn't be edited manually, so the code is changed only when maintainer/contributor run protoc to regenerate the code again. The context is very clear for anybody.
the solution in this pr require you add the note into the generated code, which isn't good too. |
@JianyuWang0623 The right approach isn't checking in the pregenerated code, but call protoc in Makefile/cmake to generate the code during the build process. |
|
My explanation may not have been clear enough. Code generated by tools (e.g., protoc-c) — such as The reason for adding this feature is that some components may reference structs in the generated code that do not comply with NuttX naming conventions, and for which specifying a custom naming style is not supported (e.g., |
Note: Please adhere to Contributing Guidelines.
Summary
This PR adds support for three nxstyle directives to allow fine-grained control over style checking, similar to clang-format directives. This addresses the challenge of enforcing code style consistency while accommodating generated code (e.g., protobuf-c types) that inherently violates NuttX coding standards.
New directives:
/* nxstyle off */- Disable style checks starting from the next line/* nxstyle on */- Re-enable style checks starting from the next line/* nxstyle ignore */- Ignore style checks for the current line only (except line length)Key implementation details:
check_nxstyle_directive()function that searches for "nxstyle " pattern in commentsoffandontake effect from the line AFTER the directive, allowing the directive line itself to be checkedignoreapplies only to the current line and does NOT suppress line length validationnxstyle ignoreExample usage in consumer_protocol.c:
This eliminates the need to scatter
/* nxstyle ignore */comments throughout the code.Impact
Testing
Test Environment:
Testing performed:
Directive Parsing Test:
Ignore Directive Test:
Line Length Validation:
Real-world Application (nsh):
Regression Testing:
Test Results Summary:
offdirective disables checks from next lineondirective re-enables checks from next lineignoreskips checks for current line onlyignore)ignoreare validated