Repository navigation
refactor(printf): replace C snprintf with fl::snprintf in libFastLED.a (#2773 batch-1 item 1.1) - #2774
refactor(printf): replace C snprintf with fl::snprintf in libFastLED.a (#2773 batch-1 item 1.1)#2774zackees wants to merge 2 commits into
Conversation
#2773 item 1.1) Migrates all 12 C `snprintf` call sites inside libFastLED.a to the template-based `fl::snprintf` (`src/fl/stl/stdio.h:659`), which routes formatting through `fl::sstream` instead of newlib's `_svfprintf_r`. Per-file: - src/platforms/esp/32/ota/ota_impl.cpp.hpp (7 sites — hex encoding, auth string) - src/fl/net/http/chunked_encoding.cpp.hpp (2 sites — %zx hex) - src/fl/net/http/stream_client.cpp.hpp (1 site — %u port) - src/fl/stl/asio/ip/tcp.cpp.hpp (1 site — %u port) - src/fl/stl/asio/http/server.cpp.hpp (1 site — %d HTTP status) Verified `_svfprintf_r` no longer has a libFastLED.a reference after this change (`objdump -r .pio/build/.../platforms+.cpp.o | grep snprintf` returns nothing). esp32s3 Blink builds clean; host tests pass 309/309. ## Net `firmware.bin` impact: only ~100 B Saving is much smaller than #2773's original ~11 KB estimate. The remaining `_svfprintf_r` (11 KB) is now pulled entirely by ESP-IDF code that FastLED still depends on: - `libesp_common.a(esp_err_to_name.c.obj)` references `snprintf` for its unknown-error fallback path - `esp_err_to_name(err)` is called by FastLED's `rmt5_peripheral_esp.cpp.hpp` (and other RMT layers) inside FL_WARN/FL_LOG_RMT messages A follow-up PR that replaces `esp_err_to_name(err)` with a snprintf-free helper (or unconditionally elides the error name from FL_WARN in release builds) would close the gap and unlock the original 11 KB win. Note: this PR also includes a drive-by fix for a KBI002 lint failure on `ci/autoresearch/test_flexio_rx_objectfled.py` that the Stop hook surfaced during commit — handler now calls `handle_keyboard_interrupt(ki)` before re-raising, matching the pattern in `ci/autoresearch_loop.py`. Issue: #2773 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughCI test KeyboardInterrupt exceptions now route through a global handler. Across multiple C++ files, ChangesKeyboardInterrupt Handler Integration
snprintf to fl::snprintf Migration
C stdio usage CI checker and tests
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ci/autoresearch/test_flexio_rx_objectfled.py`:
- Around line 175-177: The import of handle_keyboard_interrupt is currently
inside an except block; move the import statement for handle_keyboard_interrupt
to the top-level imports (after the existing from typeguard import typechecked)
so all imports are grouped properly, and then remove the in-block import (the
handle_keyboard_interrupt(ki) call should remain where it's used).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ce7d6c39-3810-4362-87b9-df5c2840c499
📒 Files selected for processing (6)
ci/autoresearch/test_flexio_rx_objectfled.pysrc/fl/net/http/chunked_encoding.cpp.hppsrc/fl/net/http/stream_client.cpp.hppsrc/fl/stl/asio/http/server.cpp.hppsrc/fl/stl/asio/ip/tcp.cpp.hppsrc/platforms/esp/32/ota/ota_impl.cpp.hpp
| from ci.util.global_interrupt_handler import handle_keyboard_interrupt | ||
|
|
||
| handle_keyboard_interrupt(ki) |
There was a problem hiding this comment.
Move the import to the top of the file.
The import of handle_keyboard_interrupt should be placed at the top of the file with other imports, not inside the except block. As per coding guidelines, all system imports must be at the top of the file in proper order: standard library, third-party, local.
📦 Proposed fix to move import to top of file
Add the import after line 34 (after from typeguard import typechecked):
from typeguard import typechecked
+from ci.util.global_interrupt_handler import handle_keyboard_interrupt
Then remove the import from the except block:
except KeyboardInterrupt as ki:
# Catch Ctrl-C before the bare `Exception`/`SerialException` block so
# interactive users can abort cleanly without dumping a stack trace.
- from ci.util.global_interrupt_handler import handle_keyboard_interrupt
-
handle_keyboard_interrupt(ki)
raise📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| from ci.util.global_interrupt_handler import handle_keyboard_interrupt | |
| handle_keyboard_interrupt(ki) | |
| handle_keyboard_interrupt(ki) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@ci/autoresearch/test_flexio_rx_objectfled.py` around lines 175 - 177, The
import of handle_keyboard_interrupt is currently inside an except block; move
the import statement for handle_keyboard_interrupt to the top-level imports
(after the existing from typeguard import typechecked) so all imports are
grouped properly, and then remove the in-block import (the
handle_keyboard_interrupt(ki) call should remain where it's used).
…item 1.5) (#2777) Adds `CstdioPrintfChecker` to the unified `ci/lint_cpp/` pipeline that fails CI on direct C `snprintf` / `printf` / `sprintf` / `fprintf` / `vfprintf` / `vsnprintf` / `vprintf` / `vsprintf` / `vfiprintf` calls inside `src/`. Implements item **1.5** of the binary-size meta-issue (#2773) — the regression guard for the cleanup landed in #2774. Each such call drags newlib's `_svfprintf_r` (~11 KB on xtensa-esp-elf) into `libFastLED.a`. The repo already ships `fl::snprintf` and `fl::printf` at `src/fl/stl/stdio.h:659` which route through `fl::sstream` and don't need the libc engine. ## Allowed forms (verified by tests) - `fl::snprintf(...)`, `fl::printf(...)` — the FastLED wrappers. - `obj.printf(...)`, `ptr->printf(...)` — member access. - `SerialPort::printf(...)`, `MyClass::snprintf(...)` — any user-defined class method (qualified by an identifier other than `std`). - `void printf(const char* fmt, ...)` — function declarations / definitions inside a class body (the prefix ends with a type identifier, not `::`). - `my_snprintf(...)` — sub-word matches (defensive; `\b` already covers). ## Banned forms (verified by tests) - bare `snprintf(...)`, `printf(...)`, etc. — no prefix. - explicit `::printf(...)` — root-namespace C call. - explicit `std::snprintf(...)` — C++ std namespace (still newlib on xtensa). ## Excluded paths (don't link newlib) - `src/third_party/**` — third-party code defines its own conventions. - `src/fl/stl/stdio.{h,hpp}` + `src/fl/stl/cstdio.*` — wrapper TUs that bridge at the C-API boundary. - `src/platforms/wasm/**` — Emscripten libc, console-write. - `src/platforms/{posix,apple,win}/**` — host-only test runners. - `src/platforms/stub/**` — host-only stub. ## Opt-out Append `// ok cstdio - <reason>` on the same line for genuine platform-boundary cases. ## Verification - 26 unit tests covering every allowed/banned/opt-out/exclusion case (`ci/lint_cpp/test_cstdio_printf_checker.py`): 26/26 pass. - Full `bash lint` pipeline: clean, zero violations on `src/` after #2774 lands the snprintf → fl::snprintf migration. - Registered in `ci/lint_cpp/run_all_checkers.py` under the `src` scope alongside the existing `CtypeGlobalChecker` (same shape: ban C free-function APIs that have `fl::` template wrappers). Depends on #2774 — if rebased on master before that PR merges, the linter would fail on the 12 pre-existing `snprintf` call sites the PR migrates. Issue: #2773 Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Closing as fully obsolete after rebase against current masterRebased this branch onto current
So nothing left to merge. All 5 snprintf-side source changes are already on master via #2775 (which also includes the The corrected Closing — no action needed. |
Summary
Migrates all 12 C
snprintfcall sites insidelibFastLED.ato thetemplate-based
fl::snprintf(src/fl/stl/stdio.h:659), routing formatoutput through
fl::sstreaminstead of newlib's heavy_svfprintf_rengine.
Implements item 1.1 of the binary-size meta-issue: #2773.
Per-file changes
snprintfcall sites migratedsrc/platforms/esp/32/ota/ota_impl.cpp.hppsrc/fl/net/http/chunked_encoding.cpp.hpp%zxhex length)src/fl/net/http/stream_client.cpp.hpp%uport)src/fl/stl/asio/ip/tcp.cpp.hpp%uport)src/fl/stl/asio/http/server.cpp.hpp%dHTTP status)fl::snprintfsupports%02x,%zx,%u,%d,%s,%p,%X,%c,%f,%%with full width/zero-padding flags — verified against thespecifiers used here.
Verification
Host C++ unit tests: 309/309 passed (
bash test --cpp).ESP32-S3 Blink: builds clean (
bash compile esp32s3 --examples Blink --platformio).libFastLED.ano longer has anysnprintfrelocations:Lint:
bash lintclean (also includes a drive-by KBI002 fix onci/autoresearch/test_flexio_rx_objectfled.py:172that the Stop hooksurfaced —
except KeyboardInterrupt:handler now callshandle_keyboard_interrupt(ki)before re-raising, matching thepattern in
ci/autoresearch_loop.py).Honest size impact: only ~100 B
firmware.binshrinks from 673,504 B → 673,402 B (–102 B). The original#2773 estimate of ~11 KB assumed that removing FastLED's C
snprintfreferences would let the linker droplibc_a-snprintf.oandtherefore
_svfprintf_r(11 KB). It does not, because:esp_err_to_name(err)is called inside FastLED's RMT5 layer forFL_WARN/FL_LOG_RMTmessages. Its unknown-error fallback path usessnprintfto format\"UNKNOWN ERROR (0x%x)\", so the linker pullslibc_a-snprintf.oeven though we've eliminated FastLED's directreferences.
Follow-up that would actually unlock the 11 KB
A separate PR can replace
esp_err_to_name(err)in FastLED's RMT layerwith either:
unknown), or
FASTLED_LOG_VERBOSITY=0(Meta: ESP32-S3 binary-size reduction — Batch 1 (~26 KB) + Batch 2 (~26–50 KB) (supersedes #2771) #2773 item 2.3) is set.Either route closes the gap.
Why ship this even though savings are small
eventual 11 KB win.
libFastLED.ato newlib's printf engine — useful for any future
esp_err_to_name-replacement work.::snprintf/::printfinsrc/) — the next PR can add the rulewithout any pre-existing violations to grandfather.
Test plan
bash test --cpp→ 309/309 passbash compile esp32s3 --examples Blink --platformio→ builds cleanbash lint→ cleanobjdump -ron alllib311/FastLED/fl/build/*.cpp.oshows zerosnprintfrelocationsCloses none (item 1.1 of #2773; the meta-issue stays open for the other
seven items).
🤖 Generated with Claude Code
Summary by CodeRabbit