Repository navigation
refactor(stdio): route libFastLED.a printf through fl::snprintf + lint guard (#2773 item 1.1 + 1.5) - #2775
Conversation
…t guard Item 1.1 + 1.5 from the ESP32-S3 binary-size meta tracker (#2773). Replaces 12 bare C `::snprintf` call sites in `libFastLED.a` sources with the existing `fl::snprintf` template: - src/platforms/esp/32/ota/ota_impl.cpp.hpp (7 sites — hex hashing + auth string formatting on the OTA path) - src/fl/net/http/chunked_encoding.cpp.hpp (2 sites) - src/fl/net/http/stream_client.cpp.hpp - src/fl/stl/asio/ip/tcp.cpp.hpp - src/fl/stl/asio/http/server.cpp.hpp After this change, `xtensa-esp32s3-elf-nm -A libFastLED.a | grep ' U '` shows the printf-family pull-in chain is gone — only `log_printf` (ESP-IDF logger) remains as an undefined symbol. Empirical Blink size delta on ESP32-S3 + PlatformIO + IDF 5.4: before: 673,504 B (#2773 baseline) after: 673,840 B (this branch) i.e. essentially no firmware.bin change. Honest measurement: `_svfprintf_r` (11,118 B) survives because 24+ ESP-IDF archives independently pull the C printf engine (`libbt`, `libconsole`, `libesp_http_client`, `libesp_http_server`, `libespressif__esp_matter`, `libmbedcrypto`, …). `libFastLED.a` is no longer one of them, but eliminating one pull source out of 25 doesn't move the linker's decision. Will append corrected numbers to #2773. The change is still load-bearing for two reasons: 1. Sets up `BareSnprintfChecker` (new ci/lint_cpp checker, item 1.5) so no future FastLED code adds back the C printf reference. 2. `fl::snprintf` is faster than newlib's `_svfprintf_r` for the actual format specifiers FastLED uses (just `%d`/`%u`/`%s`/`%02x`/`%zx`). `BareSnprintfChecker`: - Bans `snprintf|sprintf|vsnprintf|vsprintf|fprintf|vfprintf` inside `src/` unless prefixed with `fl::` or `::` or member-accessed. - Skips the `fl::stl/stdio` / `fl::stl/cstdio` shim itself. - Skips `src/third_party/` (we don't control upstream). - Skips `src/platforms/{apple,posix,win}/run_unit_test.hpp` — host-only watchdog scaffolding that never links into ESP32 firmware. - Suppression `// ok snprintf` for the one-off cases that ever need it. Refs: #2773 #2473 #2421 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR introduces a C++ linting rule that enforces use of ChangesBare snprintf to fl::snprintf migration
🎯 2 (Simple) | ⏱️ ~12 minutes
🚥 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)
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 |
…RD (#2773 item 1.2) (#2778) Replaces the unconditional `FL_HAS_INCLUDE(<SD.h>) && FL_HAS_INCLUDE(<fs.h>)` auto-detect at `src/fl/system/file_system.cpp.hpp:18` with an explicit opt-in: #elif defined(FASTLED_USE_SDCARD) && FL_HAS_INCLUDE(<SD.h>) && FL_HAS_INCLUDE(<fs.h>) Implements item **1.2** of the binary-size meta-issue: #2773. ## Why this matters Auto-detecting `<SD.h>` on the include path caused PlatformIO's LDF to unconditionally link `libSD.a` + `libFS.a` + Arduino's `VFSImpl` into every ESP32 sketch — even Blink-style sketches that never call `FileSystem::beginSd()`. Once those archives were in the link, the vtable for `VFSImpl` kept all 12 of its virtual methods alive, including `VFSFileImpl::VFSFileImpl` which calls C `snprintf` and so dragged newlib's `_svfprintf_r` (~11 KB) along for the ride. The `-ffunction-sections` + `--gc-sections` pair can't break this chain: a vtable is one rodata section that points at every virtual method; the linker can't drop `rename()` out of the vtable just because nobody calls it, since the layout must stay intact for `~VFSImpl` to fire. One static reference to `fs::File`'s vtable from `libFastLED.a(fl.system+.cpp.o)` was pulling **~14 KB** of code + **~11 KB** of printf engine that no Blink-style sketch can avoid. ## Migration Sketches that need SD support add **one line** before `#include <FastLED.h>`: #define FASTLED_USE_SDCARD Without it, `FileSystem::beginSd()` returns `false` and falls back to the weak `NullFileSystem` stub (already present at `src/fl/system/file_system.cpp.hpp:208`). Sketches that don't call `beginSd()` see no behavior change — only the freed ~15 KB of flash. ## Measured (ESP32-S3, Blink, PIO + IDF 5.4 + xtensa-esp-elf-gcc 14.2.0) | Build | `firmware.bin` | Δ | |---------------|---------------:|-----------:| | master baseline (`99ddad4487`) | 673,402 B | — | | this PR | **657,680 B** | **−15,722 B** | Verification: xtensa-esp32s3-elf-nm firmware.elf | grep VFSImpl # (empty after this PR — was 12 symbols totaling ~3 KB before) `_svfprintf_r` itself is still in the binary (~11 KB) because ESP-IDF's `esp_err_to_name` keeps it alive via FastLED's RMT5 logging — same caveat documented in PR #2775. The 15.7 KB delta is the VFS code itself plus the rodata strings and Arduino FS scaffolding that GC dropped alongside it. ## Verification - Host C++ unit tests: 309/309 pass (`bash test --cpp`). - ESP32-S3 Blink: builds clean (`bash compile esp32s3 --examples Blink --platformio`). - `firmware.bin` size: 673,402 → 657,680 (−15,722 B). - `nm firmware.elf | grep VFSImpl`: zero matches. - Lint: `bash lint` clean. Issue: #2773 Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
…fenders with fl::printf (#2888) ## What Closes the long tail of the FastLED #2773 item 1.1 / 1.5 work. 1. **Extend `ci/lint_cpp/bare_snprintf_checker.py`** to also ban bare `printf` and `vprintf` — previously only the snprintf/sprintf/ fprintf branches were on the list, leaving raw `printf(...)` calls uncaught. 2. **Replace 13 bare `printf(...)` call sites** under `src/platforms/wasm/` with `fl::printf(...)`: - `src/platforms/wasm/audio_input_wasm.cpp.hpp` — 5 sites (lines 101, 113, 163, 166, 221) - `src/platforms/wasm/fs_wasm.cpp.hpp` — 1 site (line 344) + `#include "fl/stl/stdio.h"` - `src/platforms/wasm/js_bindings.cpp.hpp` — 7 sites (lines 186, 226, 327, 333, 352, 387, 392; already had the include) 3. **Suppress 2 false-positive method declarations** with `// ok snprintf`: - `src/fl/system/serial.h:180` — `SerialPort::printf(...)` declaration; body at line 284 already routes through `fl::snprintf`. - `src/platforms/stub/Arduino.h:191` — stub `printf` method that forwards to `fl::printf`. ## Why The FastLED #2773 audit landed `fl::snprintf` (#2775) and the existing `bare_snprintf_checker.py` lint guard, but the lint banned `snprintf/sprintf/vsnprintf/vsprintf/fprintf/vfprintf` and missed bare `printf`. WASM code accumulated 13 bare `printf(...)` calls; now fixed and lint-enforced going forward. `fl::printf` (from `src/fl/stl/stdio.h:37`) is a template that formats via `fl::sstream` and avoids pulling newlib's `_vfprintf_r` engine. ## Verification - `uv run python ci/lint_cpp/bare_snprintf_checker.py` → 0 violations. - `bash lint --cpp` → all checks pass. - `bash bloat esp32s3` → 388,380 B flash unchanged (WASM files are `#ifdef FL_IS_WASM`-gated; ESP32-S3 doesn't link them, so the bloat metric is identical pre/post change, as expected). - `bash test --cpp` could not validate from inside the worktree due to the meson-encoded-source-path bug filed at #2887; the same test passes on the main checkout against this exact diff at HEAD.
Summary
Items 1.1 and 1.5 from the ESP32-S3 binary-size meta tracker (#2773). Replaces 12 bare
::snprintfcall sites inlibFastLED.asources withfl::snprintf(which already exists insrc/fl/stl/stdio.h) and adds a lint check that prevents regressions.Source changes
src/platforms/esp/32/ota/ota_impl.cpp.hppsrc/fl/net/http/chunked_encoding.cpp.hppsrc/fl/net/http/stream_client.cpp.hppsrc/fl/stl/asio/ip/tcp.cpp.hppsrc/fl/stl/asio/http/server.cpp.hppAfter this change,
xtensa-esp32s3-elf-nm -A libFastLED.a | grep ' U 'no longer shows any printf-family undefined symbol — onlylog_printf(ESP-IDF logger) remains.firmware.bin_svfprintf_r(11,118 B) survives because 24+ ESP-IDF archives independently pull the C printf engine —libbt,libconsole,libesp_http_client,libesp_http_server,libespressif__esp_matter(36 refs),libmbedcrypto,libmbedx509, etc.libFastLED.ais no longer one of them, but eliminating one pull source out of ~25 doesn't move the linker's decision on this sketch.#2773 item 1.1's claim of ~11 KB savings was incomplete — it accounted only for the FastLED reference. Will append corrected analysis to #2773 in a comment.
Why merge anyway
BareSnprintfChecker(item 1.5) — prevents any future FastLED code from re-adding the C printf reference. When the ESP-IDF side eventually catches up (or when running on builds where some of those archives aren't pulled), the savings will land automatically.fl::snprintfis faster than newlib's_svfprintf_rfor the format specifiers FastLED actually uses (%d/%u/%s/%02x/%zx) — nova_listwalking, no per-%-token state machine.libFastLED.a.New lint check
ci/lint_cpp/bare_snprintf_checker.py:snprintf|sprintf|vsnprintf|vsprintf|fprintf|vfprintfinsidesrc/unless prefixed withfl::/::/ member-accessed.fl/stl/stdio/fl/stl/cstdioshim itself.src/third_party/(we don't control upstream).src/platforms/{apple,posix,win}/run_unit_test.hpp— host-only watchdog scaffolding that never links into ESP32 firmware.// ok snprintffor the one-off cases that ever need it.Tests
bash test fastled_core --cpp— passes (host tests usefl::stl/stringnotfl::stl/stdioshim, unaffected)bash compile esp32s3 --examples Blink --platformio— succeeds, 673,840 Buv run python ci/lint_cpp/bare_snprintf_checker.py— clean on src/Test plan
bash lint --cppRefs: #2773 #2473 #2421
🤖 Generated with Claude Code
Summary by CodeRabbit
Chores
Refactor