Skip to content

feat(cc): multi-declarator per-decl pointers + preprocessor stringify#494

Merged
bboe merged 2 commits into
mainfrom
bboe/cc-multi-decl-pointer-and-stringify
May 24, 2026
Merged

feat(cc): multi-declarator per-decl pointers + preprocessor stringify#494
bboe merged 2 commits into
mainfrom
bboe/cc-multi-decl-pointer-and-stringify

Conversation

@bboe
Copy link
Copy Markdown
Owner

@bboe bboe commented May 24, 2026

Summary

Two small cc.py features, each unblocking a Phase 6 libbboeos file.

  • Commit 1 — per-declarator pointer stars on multi-declarator locals. parse_variable_declaration now consumes optional * tokens between each , and IDENT (capped at parse_type's two-star combined limit), so const unsigned char *p = a, *q = b; and similar shapes parse at function scope. The file-scope path already supported this; the local path now mirrors it. Unblocks user/libbboeos/string.c (memcmp's exact declaration shape) — parsing now advances past line 138.
  • Commit 2 — preprocessor stringify operator (#x). Function-like macro bodies may now contain #<parameter>; _parse_function_define lifts each match into a STRINGIFY sentinel token (raising at define time when # does not precede a parameter), and _expand_function_macro materialises those into STRING literals per C99 §6.10.3.2. Non-stringify parameter substitution now also fully macro-expands the argument tokens first, so the standard STR(x) STR2(x) / STR2(x) #x double-indirection trick exposes the expanded value (e.g. SYSNUM_STR(SYS_RTC_ALARM) becomes "0x40"). Unblocks user/libbboeos/signal.c — parsing now advances past line 1.

Both probes still fail at later, expected, unrelated points (*++p in string.c line 166; volatile in signal.c line 5).

Test plan

  • python3 -m pytest tests/unit/ tests/test_ccobj.py tests/test_ccld.py tests/test_ccar.py -x -q — 567 passed
  • python3 tests/test_cc_local_structs.py — 9 / 9
  • python3 tests/test_cc_bits.py — 112 / 112
  • timeout 240 python3 tests/test_asm.py — 42 / 42 byte-equivalent
  • ./make_os.sh — clean build
  • string.c probe parses past line 138 (now fails at 166 on *++p)
  • signal.c probe parses past line 1 (now fails at 5 on volatile)

bboe added 2 commits May 24, 2026 03:46
Extend parse_variable_declaration so additional declarators after the
first may grow their own pointer depth on top of the shared base type
(``const unsigned char *p = a, *q = b;``).  The file-scope path
already supported this shape; the local path now mirrors it by
consuming optional ``*`` tokens between COMMA and IDENT, capped at
parse_type's combined two-star limit.

Unblocks ``user/libbboeos/string.c`` (memcmp opens with that exact
declaration); parsing now advances past line 138.
Implement the C99 §6.10.3.2 stringify operator inside function-like
macro replacement-lists.  A ``#`` preceding a parameter name in the
body now expands at the call site to a C string literal whose content
is the unexpanded argument tokens joined with single spaces, with
every ``"`` and ``\`` escaped.

Two surface-area changes in cc/preprocessor.py:

- ``_parse_function_define`` scans the body text for ``#<param>``
  sequences before lexing; each becomes a ``STRINGIFY`` sentinel
  token, and a ``#`` not followed by a parameter raises CompileError
  at define time (rather than the previous ``unexpected character``
  lexer error).
- ``_expand_function_macro`` materialises ``STRINGIFY`` tokens into
  STRING literals via the new ``_stringify_argument`` helper, and
  fully macro-expands argument tokens before non-stringify
  substitution so the standard ``STR(x) STR2(x)`` /
  ``STR2(x) #x`` double-indirection trick exposes the expanded value.

Unblocks ``user/libbboeos/signal.c`` (transitively pulls
``_SYSNUM_STR2(x) #x`` via ``<signal.h>``); parsing now advances
past line 1.
@bboe bboe merged commit 261c58d into main May 24, 2026
27 checks passed
@bboe bboe deleted the bboe/cc-multi-decl-pointer-and-stringify branch May 24, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant