Skip to content

refactor(stdio): route libFastLED.a printf through fl::snprintf + lint guard (#2773 item 1.1 + 1.5) - #2775

Merged
zackees merged 1 commit into
masterfrom
feat/bloat-1.1-fl-snprintf
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/bloat-1.1-fl-snprintf

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Items 1.1 and 1.5 from the ESP32-S3 binary-size meta tracker (#2773). Replaces 12 bare ::snprintf call sites in libFastLED.a sources with fl::snprintf (which already exists in src/fl/stl/stdio.h) and adds a lint check that prevents regressions.

Source changes

File Sites
src/platforms/esp/32/ota/ota_impl.cpp.hpp 7 — hex hashing + auth string formatting
src/fl/net/http/chunked_encoding.cpp.hpp 2
src/fl/net/http/stream_client.cpp.hpp 1
src/fl/stl/asio/ip/tcp.cpp.hpp 1
src/fl/stl/asio/http/server.cpp.hpp 1

After this change, xtensa-esp32s3-elf-nm -A libFastLED.a | grep ' U ' no longer shows any printf-family undefined symbol — only log_printf (ESP-IDF logger) remains.

⚠️ Honest measurement: ~0 B firmware.bin delta on Blink

Build firmware.bin
master baseline (per #2773) 673,504 B
this branch 673,840 B

_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.a is 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

  1. Sets up 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.
  2. fl::snprintf is faster than newlib's _svfprintf_r for the format specifiers FastLED actually uses (%d/%u/%s/%02x/%zx) — no va_list walking, no per-%-token state machine.
  3. Hygiene: gives us one consistent printf API across libFastLED.a.

New lint check

ci/lint_cpp/bare_snprintf_checker.py:

  • Bans snprintf|sprintf|vsnprintf|vsprintf|fprintf|vfprintf inside src/ unless prefixed with fl:: / :: / 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.

Tests

  • bash test fastled_core --cpp — passes (host tests use fl::stl/string not fl::stl/stdio shim, unaffected)
  • bash compile esp32s3 --examples Blink --platformio — succeeds, 673,840 B
  • uv run python ci/lint_cpp/bare_snprintf_checker.py — clean on src/

Test plan

  • CI runs the new checker as part of bash lint --cpp
  • CodeRabbit / human reviewer agrees the 0 B delta is OK given the regression-guard value

Refs: #2773 #2473 #2421

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores

    • Added new C++ code quality linting tool to the development infrastructure.
  • Refactor

    • Updated internal string formatting implementations across network and platform modules for improved consistency and safety.

…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]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR introduces a C++ linting rule that enforces use of fl::snprintf over bare C printf-family calls, creates the checker that enforces it, and applies the migration across HTTP networking and OTA firmware modules.

Changes

Bare snprintf to fl::snprintf migration

Layer / File(s) Summary
Lint checker creation and integration
ci/lint_cpp/bare_snprintf_checker.py, ci/lint_cpp/run_all_checkers.py
BareSnprintfChecker scans src/ files for bare snprintf, sprintf, fprintf and related printf-family calls. It exempts qualified calls (fl::, ::), ignores comment blocks, and supports // ok snprintf suppression per line. The checker is wired into the global linter suite.
HTTP and networking module snprintf migration
src/fl/net/http/chunked_encoding.cpp.hpp, src/fl/net/http/stream_client.cpp.hpp, src/fl/stl/asio/http/server.cpp.hpp, src/fl/stl/asio/ip/tcp.cpp.hpp
HTTP chunked encoding, stream client port formatting, ESP32 HTTP server status formatting, and TCP port string construction all switch from bare snprintf to fl::snprintf. Includes are added to make fl::snprintf available in each file.
OTA firmware module snprintf migration
src/platforms/esp/32/ota/ota_impl.cpp.hpp
OTA authentication nonce generation, password hash hex encoding, derived key hex formatting, expected response hex conversion, MD5 hex encoding, and UDP server auth challenge message all replace snprintf with fl::snprintf.

🎯 2 (Simple) | ⏱️ ~12 minutes

🐰 A linter hops to guard the code,
Bare snprintf must go the fl:: way,
HTTP and OTA now obey,
With namespaced calls, bugs decay. 🚀✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: replacing bare printf calls with fl::snprintf and adding a lint checker to prevent future regressions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/bloat-1.1-fl-snprintf

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Jun 5, 2026
@zackees
zackees merged commit 99ddad4 into master Jun 5, 2026
7 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 5, 2026
zackees added a commit that referenced this pull request Jun 5, 2026
…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]>
zackees added a commit that referenced this pull request Jun 7, 2026
…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.
@zackees
zackees deleted the feat/bloat-1.1-fl-snprintf branch August 21, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant