Skip to content

Give a C struct with no data a placeholder member [patch] - #197

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/181-empty-c-struct
Oct 9, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/181-empty-c-struct

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #181

What changed

CGenerator.GenerateClassDeclaration always wrote the struct body, even when nothing went into it. That happens for an empty ClassDeclaration and for a class whose members are all functions. Both came out as typedef struct X { } X;. ISO C forbids a struct with no members, so this fails under -pedantic-errors, and MSVC rejects it outright with C2016. A constructor on such a type also wrote Self self = {0};, and gcc reports excess elements in struct initializer because there is no member for the 0 to initialise.

The issue offered two options. This takes the first one: a type with no fields, no base and no interfaces now gets one char unused_; member, with a note before it. It keeps a single rule for every type. The constructor's {0} and the Type_create() call sites stay valid without any changes. A type with a base or interfaces is not affected, because its embedded member is already the first member.

Tests

  • CGeneratorTests.Struct_WithNoData_GetsAPlaceholderMember:
    • An empty class gets the note and char unused_;.
    • A class with only a static function gets the same.
    • A class with only a base gets no placeholder.
  • CGeneratedSourceCompilesTests.TypesWithNoData_CompileAsStrictC: this is the case the acceptance criteria ask for. It generates a header with an empty class that has a constructor, and a static-only MathX. It compiles the header and a driver that calls both under -std=c11 -pedantic-errors.

With the generator change reverted, both tests fail. gcc reports struct has no members twice and excess elements in struct initializer once. With the change, the full suite passes (1102/1102).

The existing exemplar compile still uses -pedantic. I left it alone so the scope matches the issue.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR


Generated by Claude Code

ISO C forbids a struct with no members and MSVC rejects one, so an
empty class, or one holding only functions, came out as
typedef struct X { } X; and failed under -pedantic-errors. A
constructor on such a type also wrote {0} into a struct with no member
to zero. The C generator now writes one char member, with a note, when
a type has no fields, no base and no interfaces.

Fixes #181

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X8BPy6trRZiUxiwJcbUGzR
@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 5031668 into main Oct 9, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/181-empty-c-struct branch October 9, 2026 08:08
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.

C generator emits typedef struct X { } X; for a class with no instance data, which ISO C forbids and MSVC rejects

2 participants