Repository navigation
Give a C struct with no data a placeholder member [patch] - #197
Merged
Merged
Conversation
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #181
What changed
CGenerator.GenerateClassDeclarationalways wrote the struct body, even when nothing went into it. That happens for an emptyClassDeclarationand for a class whose members are all functions. Both came out astypedef 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 wroteSelf self = {0};, and gcc reportsexcess elements in struct initializerbecause there is no member for the0to 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 theType_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:char unused_;.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-onlyMathX. 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 memberstwice andexcess elements in struct initializeronce. 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