Skip to content

Split a type from the variables declared with it in Partials - #1410

Merged
mkarlesky merged 12 commits into
masterfrom
fix/1.2.3-type-with-variable
Nov 12, 2026
Merged

mkarlesky merged 12 commits into
masterfrom
fix/1.2.3-type-with-variable

Conversation

@mkarlesky

Copy link
Copy Markdown
Member

Fixes #1401.

Problem

A Partial couldn't be generated for a module that defines a type and declares a variable in one statement, a common way to keep private state:

static struct { int count; } s_ctx;
static enum { IDLE, RUN } s_state;
struct pair { int a; } g_p1, g_p2;

The extractor read such a statement as a variable whose type held the whole body. The Partial wrote that body into both the variable's extern in _impl.h and its definition in _impl.c, and the source includes the header. Two copies of a tagged body are a redefinition, and two anonymous bodies are two different types. A function-scope static of this form failed the same way once promoted.

Probes found three more defects with the same root:

  • Member array sizes vanished from the type. struct { int buf[8]; } read as struct { int buf; }.
  • A compound statement with a member array lost a variable. struct pair { int a[2]; } g_p1, g_p2; read as one record named g_p2.
  • A promoted static's struct member could disagree with its uses. Where the member shared another promoted static's name, its declaration was renamed but s.count wasn't.

A tag-only forward declaration, struct node;, was also read as a variable, giving extern struct node;.

Fix

  • Extraction: the extractor reads such a statement as what it is, a type plus its variables (CExtractorDefinedType). The type becomes an aggregate statement ahead of its variables, and each variable names it through defined_type. The member-array and compound bugs go with the body. struct node; reads as a type statement.
  • Generation: each such type is written once, as a typedef in the types header, named ceedling_partial_<module>_<variable>_t. A typedef leaves an anonymous type anonymous, so a header's extern struct { … } g; still matches its real definition in another module.
  • Promotion: a function's statics are found by position in the code block and replaced in place. The comment-tolerant regex and the placeholder swap are gone. A promoted static's type moves with it, named ceedling_partial_<module>_<function>_<variable>_t. A local tag takes that name, so it can't collide at module scope. The shared-name check now covers the moved type's body, which fixes the member rename.
  • Test access: ceedling.h gains PARTIAL_TYPE(module, variable) and PARTIAL_LOCAL_TYPE(module, function, variable), so a test can declare another object of such a type.
  • Guidance: PartialNameSpecialist recognizes a test file's compilation failure on a name PARTIAL_LOCAL_VAR(), PARTIAL_TYPE() or PARTIAL_LOCAL_TYPE() built and nothing declares. It explains the macro's arguments and lists the names the test's Partials provide. It speaks only with Partials enabled.
  • Refactoring:
    • Statements record their kind, plus a tag and typedef name where they have them.
    • All three Partial headers go through one PartialHeaderWriter, dispatching on kind.
    • The real header's guard is filtered once, in sanitize.
    • CModule#types? replaces two copies of the types-header test.
    • CIdentifier.fold is shared with include guards.
    • Dead no-op and statement-replacement utilities are removed.

Not handled here, by decision: struct pk { … } PACKED; now reads as a type plus a variable named PACKED, so the types header loses the attribute and the build compiles unpacked with warnings. This is to be fixed with #1395's macro-expansion work before the next release, and Known Issues describes it.

Tests

  • Pure-Ruby composition: extraction of each shape, member arrays, compound statements, extensions after }, qualifiers, initializers, asm labels, forward declarations, and statement kinds and names.
  • Real compiler: each shape compiles, links, and reaches members and enum values. Also covered:
    • a header extern linking against a definition in another translation unit;
    • promoted anonymous and tagged local types;
    • shadowed and repeated local tags;
    • the member-rename case;
    • the function's line count;
    • both new macros.
  • Pending tripwires: the two pending composition specs for these shapes now pass and are regular examples.
  • Doctor: unit tables for each toolchain family's wording, plus GCC and Clang cases. It stays silent for an ordinary undeclared name.
  • System: a delta-build case tests such a module end to end, through both macros, and an unchanged rebuild regenerates nothing. A gcov case checks that a promoted three-line type keeps the lines after it aligned.
  • Local runs:
    • 6,978 unit and integration examples pass on Apple Clang.
    • The Partials integration specs pass on Homebrew GCC 14.
    • The targeted Partials, coverage and wondrous_forest system specs pass.
    • rake lint is clean. The regenerated RuboCop to-do only shrank: the generator and helper drop six entries.

Docs

  • Partials guide (variables.md): the known-limit section becomes how the split works, the naming rule and the macros.
  • Macros reference and configuration.md: the two new macros.
  • Changelog: the fixes and the macros.
  • Known Issues: drops the resolved items, rewords the trailing-macro item, adds the local anonymous enum, and extends the "Resolved in 1.2.3" lines back to 1.1.x.
  • Design notes: lib/ceedling/partials/README.md and diagnostics/DOCTOR.md.

🤖 Generated with Claude Code

mkarlesky and others added 10 commits November 12, 2026 13:27
Generated include guards and the type names Partials are about to invent
both build identifiers from names C does not allow. CIdentifier.fold
replaces each such character with an underscore, one for one, so a test
author can predict a folded name. The include guard builds on it with its
own squeezing and trimming. FFF keeps Unity's rule, which its runner names
must match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A declaration such as `static struct { int count; } s_ctx;` defines a type
and declares a variable at once. Extraction read it as a variable whose
type held the whole body, so a Partial wrote the body into both the extern
and the definition. Two copies of a tagged body are a redefinition, and two
anonymous bodies are two different types.

The extractor now records the type as an aggregate statement ahead of its
variables, and each variable names it through defined_type. Reading the
variables without the body also keeps a member's array size and the first
variable of a compound declaration, which the body used to lose.

A Partial names each such type after its first variable and writes it once,
as a typedef in the types header. A typedef leaves an anonymous type
anonymous, so a header's extern still matches the real definition in
another module.

Statements also record their kind, and type statements their tag and the
name a typedef gives them. A struct or union forward declaration now reads
as a type statement instead of as a variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each generated header kept its own blank-line state and recovered each
statement's category by searching the module's lists. A PartialHeaderWriter
now writes all three headers, as PartialSourceWriter writes the source, and
the generator dispatches on a statement's kind. A CModule gives each
statement the kind of the list that holds it, so the two always agree.

The real header's include guard is removed from the extracted macros once,
along with the generated guards, instead of being filtered at each place a
header writes macros. Whether a module has types is one CModule#types?
that the generator and PartialsManager share. A variable's definition text
and its defining-declaration index each have one builder.

Generated output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Promotion found each static's statement again in the function by its text,
tolerating comments through a regex, and swapped placeholders in and out
around the rename. It now records where each statement sits in the
function's code block as it reads it, splices the no-ops in at those
places, and renames only the text around them. The comment holding a
statement keeps its line breaks, so a function keeps its line count.

A static that defines its type, as `static struct { int n; } st;` does,
promotes its type too. The type is named for the function and the
variable, and a tagged one takes that name as its tag, since a tag local
to the function can collide at module scope. The function's other uses of
the tag follow. Promoted items now join the module just ahead of their
function.

A struct member sharing a promoted static's name is renamed alike in its
uses. The member's own declaration sat in a statement the rename never
reached, so `s.count` and the member it named disagreed.

The no-op and text-replacement utilities promotion no longer needs are
gone, and the rename specs moved beside Partials::Rename.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PARTIAL_TYPE(module, variable) and PARTIAL_LOCAL_TYPE(module, function,
variable) give the name a Partial writes for the type of a variable
declared together with it. A test can then declare another object of that
type, such as a copy of a module's state. Each argument pastes before
anything expands, as PARTIAL_LOCAL_VAR's do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PARTIAL_LOCAL_VAR(), PARTIAL_TYPE() and PARTIAL_LOCAL_TYPE() paste their
arguments into one name as a test compiles. An argument that doesn't match
the module builds a name nothing declares, and the compiler reports only
that name. PartialNameSpecialist recognizes such a name in a test file's
compilation failure, in a build with Partials, across the common
compilers' wordings. Its hedged guidance explains how the macro builds the
name, what each argument must match, and the names this test's Partials
actually provide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A system build tests a module holding an anonymous struct and enum at
file scope and a tagged struct inside a function, through the variables,
their members and values, and ceedling.h's names for their types. An
unchanged rebuild regenerates nothing. A coverage case confirms that a
promoted three-line type keeps the lines after it aligned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Promotion read a function's body from the code block's first brace, so an
attribute string in the signature holding one, as in
`__attribute__((section("t{x}")))`, sent it reading from inside the string
and nothing was promoted. The body now opens at the first brace outside a
literal or comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Partials guide describes how a Partial splits a type from its
variables, the names it gives such types, and ceedling.h's PARTIAL_TYPE()
and PARTIAL_LOCAL_TYPE(). The macros reference lists both. The Partials
and doctor design notes describe the extraction model, promotion, the
header writer and PartialNameSpecialist.

The Changelog records the fixes and the macros. Known Issues drops the
items this resolves, describes the trailing-macro case as builds now meet
it, and adds the local anonymous enum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Partials generator no longer exceeds the ABC, cyclomatic, perceived
complexity or method length limits, and the Partializer helper no longer
exceeds the complexity limits. Nothing was added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

CI summary · 8d2844a · ✅ 25/25 jobs passed · nearest baseline bf3683b (master)

Tests (Linux 3.3) Examples Failures Pending
units 6176 (+33) 0 0
integration 803 (+34) 0 17
system 425 (+4) 0 37
Coverage Lines Δ
units 91.47% ▼0.01
integration 55.91% ▲0.48
system 84.70% ▲0.11
combined 98.61% ▲0.02

Diff coverage: 100.00% (345/345 changed lines).

RuboCop to-do Count Δ
offenses 678 ▼15
cops 10 ±0
All legs (examples / failures / pending)
Leg units integration system
Linux 3.0 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Linux 3.1 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Linux 3.2 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Linux 3.3 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Linux 3.4 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Linux 3.5 6176 / 0 / 0 803 / 0 / 17 425 / 0 / 37
Windows 3.0 6174 / 0 / 2 806 / 0 / 17 425 / 0 / 64
Windows 3.1 6174 / 0 / 2 806 / 0 / 17 425 / 0 / 64
Windows 3.2 6174 / 0 / 2 806 / 0 / 17 425 / 0 / 64
Windows 3.3 6174 / 0 / 2 806 / 0 / 17 425 / 0 / 64
Windows 3.4 6174 / 0 / 2 806 / 0 / 17 425 / 0 / 64
macOS 3.1 6176 / 0 / 0 803 / 0 / 18 425 / 0 / 53
macOS 3.2 6176 / 0 / 0 803 / 0 / 18 425 / 0 / 53
macOS 3.3 6176 / 0 / 0 803 / 0 / 18 425 / 0 / 53
macOS 3.4 6176 / 0 / 0 803 / 0 / 18 425 / 0 / 53
macOS 3.5 6176 / 0 / 0 803 / 0 / 18 425 / 0 / 53
Jobs

mkarlesky and others added 2 commits November 12, 2026 16:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A promoted statement's comment blanked the comments inside it to spaces,
keeping only their line feeds, so a CRLF inside a multi-line comment lost
its carriage return. Each comment now keeps both.

The function line-count check moves from the real-compiler spec to the
helper's own, with LF and CRLF endings. On Windows, the function text a
real preprocessor hands promotion already doubles each line break, so the
compiled check measured the toolchain rather than promotion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mkarlesky
mkarlesky merged commit cf29ced into master Nov 12, 2026
26 checks passed
@mkarlesky
mkarlesky deleted the fix/1.2.3-type-with-variable branch November 12, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partials fail on a variable declared together with its type

1 participant