hujun260 opened a new pull request, #18221: URL: https://github.com/apache/nuttx/pull/18221
## Summary Refactor the scheduler state information function to improve code quality by moving the static `g_statenames` array from file scope to block scope within the `nxsched_get_stateinfo()` function. This change addresses a MISRA C 2012 Rule 8.9 violation and enhances code maintainability. ## Motivation and Problem MISRA C 2012 Rule 8.9 states that "An object should be defined at block scope if its identifier only appears in a single function." The static `g_statenames` array was defined at file scope but is only used within the `nxsched_get_stateinfo()` function, creating an unnecessary global scope binding that violates this safety standard. ### Coding Standard Compliance - **MISRA C 2012 Rule 8.9**: Ensures proper variable scope discipline for embedded safety-critical systems - **Purpose**: Reduces the scope of static data to the minimum necessary level, improving code modularity - **Impact**: Enhances code clarity and reduces potential for unintended variable interactions ## Changes Made 1. **Removed**: Static `g_statenames` array declaration from file scope (lines 40-68 in original) 2. **Added**: Static `g_statenames` array declaration moved to block scope within `nxsched_get_stateinfo()` function body 3. **Preserved**: All functionality, array content, and conditional compilation directives remain identical 4. **Result**: Code structure and behavior unchanged; compliance improved ### File Statistics - **File Modified**: `sched/sched/sched_get_stateinfo.c` - **Lines Changed**: 55 (26 insertions, 29 deletions) - **Type**: Code reorganization (no functional changes) ## Impact Analysis ✅ **Backward Compatibility**: MAINTAINED - Function behavior and output identical ✅ **Code Quality**: IMPROVED - Enhanced MISRA C 2012 compliance ✅ **Performance**: NO IMPACT - Static array still initialized once on first function call ✅ **Memory**: NO CHANGE - Array location optimized but size identical ✅ **Portability**: IMPROVED - Better conforms to safety standards ## Verification Checklist - [x] Compilation successful with `-Werror=implicit-function-declaration` flag - [x] No functional changes to `nxsched_get_stateinfo()` behavior - [x] All conditional compilation sections preserved (`CONFIG_SMP`, `CONFIG_SCHED_EVENTS`, `CONFIG_DISABLE_MQUEUE`, `CONFIG_DISABLE_MQUEUE_SYSV`, `CONFIG_LEGACY_PAGING`, `CONFIG_SIG_SIGSTOP_ACTION`) - [x] Static array initialization semantics unchanged - [x] MISRA C 2012 Rule 8.9 compliance verified - [x] Code review and testing completed ## Testing Scenarios 1. **State Information Query**: Test `nxsched_get_stateinfo()` with various task states to confirm correct state name retrieval 2. **Array Initialization**: Verify static array is properly initialized on first function call across multiple architectures 3. **Conditional Compilation**: Validate all CONFIG_* dependent state name entries compile correctly in different configurations 4. **Performance Baseline**: Confirm no performance regression compared to previous implementation 5. **Architecture Coverage**: Test on ARM (ARMv7-A, ARMv7-R) and x86_64 architectures ## Technical Notes - The static array is still initialized only once due to static storage duration - Block-scope static variables have the same lifetime as file-scope statics - This change aligns with MISRA C standards for embedded systems - The array contains task state names used by the scheduler debugging infrastructure ## Related Issues - **Category**: Code Quality & Safety Standard Compliance - **Standard**: MISRA C 2012 Rule 8.9 (Variable Scope) - **Similar Fixes**: Part of ongoing effort to improve MISRA C 2012 compliance across kernel subsystems ## Build Information - **Compiler**: ARM GCC 10.x (primary test environment) - **Architectures**: ARMv7-A, ARMv7-R, x86_64 - **Target**: NuttX kernel - scheduler subsystem (sched/) - **Build Flags**: `-Wall -Wextra -Werror` ## Files Changed -- 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]
