JianyuWang0623 opened a new pull request, #18257: URL: https://github.com/apache/nuttx/pull/18257
*Note: Please adhere to [Contributing Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).* ## 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:** 1. Directives are parsed in a new `check_nxstyle_directive()` function that searches for "nxstyle " pattern in comments 2. `off` and `on` take effect from the line AFTER the directive, allowing the directive line itself to be checked 3. `ignore` applies only to the current line and does NOT suppress line length validation 4. Directive state is properly tracked and handles nested off/on pairs 5. Line length checks are enhanced to validate lines with right-hand comments even when they contain `nxstyle ignore` **Example usage in consumer_protocol.c:** ```c /* nxstyle off */ typedef Perfetto__ReadBuffersResponse perfetto_response_t; typedef Perfetto__ReadBuffersResponse__Slice perfetto_slice_t; /* nxstyle on */ ``` This eliminates the need to scatter `/* nxstyle ignore */` comments throughout the code. ## Impact - **Users:** Developers can now selectively disable style checking for specific code blocks or lines, improving code maintainability when integrating generated code or third-party libraries - **Code Quality:** Centralized directive placement (e.g., in typedef sections) reduces code clutter and maintains consistent formatting elsewhere - **Compatibility:** Fully backward compatible - existing code continues to work without changes. Directives are only processed if explicitly used - **Build Process:** Minimal impact; nxstyle compilation adds 107 lines of code ## Testing **Test Environment:** - Host: Linux (Ubuntu 20.04+) - Tested on: NuttX code checker (nxstyle tool) **Testing performed:** 1. **Directive Parsing Test:** ```bash # Test off/on directives cat > test_off_on.c << 'EOF' int GoodCode = 0; /* nxstyle off */ int BadName1 = 1; int BadName2 = 2; /* nxstyle on */ int BadName3 = 3; EOF ./nxstyle test_off_on.c # Result: Only line 5 (BadName3) reported as error ✓ ``` 2. **Ignore Directive Test:** ```bash # Test inline ignore cat > test_ignore.c << 'EOF' int BadName = 0; /* nxstyle ignore */ int AnotherBad = 1; EOF ./nxstyle test_ignore.c # Result: BadName ignored, AnotherBad reported ✓ ``` 3. **Line Length Validation:** ```bash # Test that line length is still checked with ignore echo 'typedef Perfetto__ReadBuffersResponse perfetto_response_t; /* nxstyle ignore */' > test_length.c ./nxstyle test_length.c # Result: Line length error reported (84 chars > 78 limit) ✓ ``` 4. **Real-world Application (nsh):** ```bash cd apps/system/nsh ls *.c *.h | xargs ../../../nuttx/tools/checkpatch.sh -f # Result: All files PASS ✓ ``` 5. **Regression Testing:** - Ran nxstyle on entire NuttX tree with test samples - Verified no false negatives on code that should fail - Confirmed proper error reporting on mixed case identifiers, spacing, etc. **Test Results Summary:** - ✅ `off` directive disables checks from next line - ✅ `on` directive re-enables checks from next line - ✅ `ignore` skips checks for current line only - ✅ Line length always checked (even with `ignore`) - ✅ Right-hand comments with `ignore` are validated - ✅ nsh files pass full validation - ✅ No regressions in existing style checking -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
