Repository navigation
fix(lint): port the real literal tokenizer to SingletonElisionChecker (#3486) - #3754
Merged
Merged
Conversation
…#3486) Investigating #3486 (migrate fl/audio/detector state to fl::Singleton) showed all 11 flagged sites in that directory were false positives, so this fixes the checker instead of performing the migration. Why the migration was not done: - Builds already pass -ffunction-sections -fdata-sections (ci/boards.py :795). A file-scope `static int` therefore lands in its own .bss section and --gc-sections already drops it when the detector is not linked. Elision gained by wrapping it: 0 bytes. - The Singleton form is strictly larger when the detector IS used: storage grows from one 4-byte object to aligned storage PLUS a `T*`, and every access gains a null-check and a cold placement-new tail. - Singleton<T>::instance() hand-rolls `if (!ptr) { ptr = new(...) T(); }`, which bypasses the C++11 magic-static init guard. The old constant-initialized static had no init race; the wrapped form lets an ESP32 audio task and loop() both first-touch, both placement-new, and the second construction silently re-zero the counter. So the two diagnostic counters stay plain statics, now carrying FL_LINT_ALLOW_GLOBAL with that rationale. The real defect was in the checker. Its constexpr-triviality test used `str::parse`, but Rust's numeric parsers reject both the `0x`/`0b` prefixes and the C++ `u`/`l`/`f` suffixes, so ordinary constants were misreported as non-literal. `ci/scan_singleton_elision.py` already had a correct tokenizer (TRIVIAL_RHS_RE) and its docstring says it mirrors this checker, so that regex is ported over rather than re-derived. Tree-wide violations drop 76 -> 39, entirely by removing false positives: src/platforms/arm/lpc/rx_sct_capture.cpp.hpp alone had 41 hex register masks flagged and is now clean. Also: - const -> constexpr for the nine compile-time constants in equalizer.cpp.hpp / frequency_bands.cpp.hpp (no size change; they were already folded, this just states the intent). - FL_NO_EXCEPT on the four diagnostic accessors, fixing a decl/def mismatch in vibe.h and satisfying NoexceptAstChecker. FL_NO_EXCEPT is a documented no-op, so this is cosmetic. - First tests for this checker: 4 cases in lint_core/tests.rs (the home linter-architecture.md prescribes), covering the literal grammar, the allow-marker, and the third_party/header exclusions. - First coverage for the getPrivateFFTCount/resetPrivateFFTCount API. Delta form, since the counter is process-wide and these headers link into a shared DLL. Co-Authored-By: Claude <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (9)
📝 WalkthroughWalkthroughThe PR expands singleton-elision constexpr detection and opt-out handling, updates the accompanying Python scanner, converts audio detector constants to ChangesSingleton elision checker
Audio detector constants and counters
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant SourceFile
participant SingletonElisionChecker
participant LiteralMatcher
SourceFile->>SingletonElisionChecker: check constexpr declaration
SingletonElisionChecker->>LiteralMatcher: match RHS grammar
LiteralMatcher-->>SingletonElisionChecker: trivial or non-trivial result
SingletonElisionChecker-->>SourceFile: skip or report violation
Possibly related issues
Possibly related PRs
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
… the opt-out markers Follow-up to review on #3754. 1. The comment on trivial_rhs_regex() cited `test_singleton_elision_rust_python_parity` as pinning the two implementations together. No such test exists -- the only match in the tree was that comment. Replaced with the real test names and an explicit statement that the grammars are kept in sync by hand. 2. ci/scan_singleton_elision.py recognized only [[gnu::used]] / __attribute__((used)) / FL_KEEP, so it re-reported the two counters this branch had just annotated with FL_LINT_ALLOW_GLOBAL. It also strips comments before testing for markers, which made the preferred preceding-comment form invisible. Added all three opt-out forms the Rust checker honors, matched off the raw line: inline, comment-carry-over, and the first-15-lines FL_LINT_ALLOW_GLOBAL_FILE. Python total 92 -> 62; fl/audio/detector now clean in both tools. 3. The float alternative missed exponent, trailing-dot, and long-double spellings, so `1e-6f`, `2.5e3f`, `10.f`, `1.5L` stayed false-positive in both. Widened in both regexes and pinned in the Rust test. The two tools still disagree on the total (62 vs 39) because the Rust checker carries skip heuristics this scanner lacks. Rather than claim a parity that does not hold, the scanner docstring now states it is not authoritative and points at the Rust checker as the enforcement path. Co-Authored-By: Claude <[email protected]>
This was referenced Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Investigating #3486 (migrate
fl/audio/detectorstate tofl::Singleton) showed that all 11 flagged sites in that directory were false positives. So this fixes the checker rather than performing the migration.Why the migration was not performed
I implemented it first, then measured it. It is a regression on every axis it claims to improve:
-ffunction-sections -fdata-sections(ci/boards.py:795). A file-scopestatic inttherefore lands in its own.bsssection, and--gc-sectionsalready drops it in sketches that never link the detector.T*(8-12 B), and each access site gains a null-check plus a cold placement-new tail — roughly +100-140 B flash on Thumb-2-Osacross the six sites.Singleton<T>::instance()hand-rollsif (!ptr) { ptr = new (&storage) T(); }, bypassing the C++11 magic-static guard. The old constant-initialized static had no init window at all. On ESP32 the audio task callingVibe::update()andloop()callinggetPrivateFFTCount()can both first-touch, both placement-new, and the second construction silently re-zeroes the counter.The two diagnostic counters therefore stay plain statics, now carrying
FL_LINT_ALLOW_GLOBALwith that rationale inline.The actual defect
The checker's
constexpr-triviality test usedstr::parse. Rust's numeric parsers reject both the0x/0bprefixes and the C++u/l/fsuffixes, so ordinary constants were misreported as non-literal and flagged.ci/scan_singleton_elision.pyalready contained a correct tokenizer (TRIVIAL_RHS_RE), and its docstring states it mirrors this checker — so this ports that regex over instead of re-deriving one.Tree-wide violations drop 76 -> 39, entirely by removing false positives.
src/platforms/arm/lpc/rx_sct_capture.cpp.hppalone had 41 hex register masks flagged and is now clean — those are exactly the driver files the checker's own header comment says it was written for.Also in this PR
const->constexprfor the nine compile-time constants inequalizer.cpp.hpp/frequency_bands.cpp.hpp. No size change (they were already folded); this just states the intent.FL_NO_EXCEPTon the four diagnostic accessors — fixes a decl/def mismatch invibe.hand satisfiesNoexceptAstChecker.FL_NO_EXCEPTis a documented no-op, so this is cosmetic.lint_core/tests.rs, the homeagents/docs/linter-architecture.md:109prescribes, using itsfile()helper so paths getnormalize_path. Covers the literal grammar, the allow-marker, and thethird_party/header exclusions.getPrivateFFTCount/resetPrivateFFTCount. Delta form on purpose — the counter is process-wide and these headers link into a shared DLL alongside the other detector suites.Verification
bash test --cpp --clean— 357/357 passbash lint— exit 0bash test --cpp audiobuildsfl_audiowhile these compile intofl_audio_detector_vibe/fl_audio_detectors.Notes for the reviewer
.init_arrayentries resist gc), not for constant-initialized PODs. Several sibling issues under Meta: migrate namespace-scope globals to fl::Singleton<T> for --gc-sections elision #3481 target trivial scalars and likely warrant the same re-scoping. I did not change the checker's policy on those unilaterally.rust_lint_source_files_stay_under_one_thousand_linesbecausecheckers/unity_build.rsis 1205 lines. It is 1205 onmastertoo.FrequencyBands::update()dereferencescontextwith no null check, while the siblingVibe::update()guards withif (!context) return;.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Refactor
Tests