-
Notifications
You must be signed in to change notification settings - Fork 49
Add C translation support for anonymous structs and unions #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bahnwaerter
wants to merge
5
commits into
ultimate-pa:dev
Choose a base branch
from
bahnwaerter:wip/mb/fix-c-anonymous-structs
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8cfde1f
Return TypesResult instead of SkipResult when dispatching IASTSimpleD…
schuessf 85f165b
Flatten unnamed (anonymous) structs or unions by adding fields to par…
bahnwaerter d63b811
Add anonymous (unnamed) struct regression tests
bahnwaerter a088159
Add anonymous (unnamed) union regression tests
bahnwaerter a12d341
Add more anonymous (unnamed) struct regression tests
bahnwaerter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
trunk/examples/programs/regression/c/struct-anonymous-01.c
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous struct in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| struct { | ||
| int a; | ||
| }; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| int a = f.a; | ||
| //@ assert (a == 10); | ||
| return f.a; | ||
| } |
28 changes: 28 additions & 0 deletions
28
trunk/examples/programs/regression/c/struct-anonymous-02.c
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous struct and variables in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| int a; | ||
| struct { | ||
| int b; | ||
| }; | ||
| int c; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| //@ assert (a == 10 && b == 20 && c == 30); | ||
| return f.a + f.b + f.c; | ||
| } |
27 changes: 27 additions & 0 deletions
27
trunk/examples/programs/regression/c/struct-anonymous-03.c
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with nested anonymous structs in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| struct { | ||
| int a; | ||
| struct { | ||
| int b; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| //@ assert (a == 10 && b == 20); | ||
| return f.a + f.b; | ||
| } |
28 changes: 28 additions & 0 deletions
28
trunk/examples/programs/regression/c/struct-anonymous-04.c
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous struct in an outer struct as part of a typedef | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| typedef struct { | ||
| int a; | ||
| struct { | ||
| int b; | ||
| }; | ||
| int c; | ||
| } foo_t; | ||
|
|
||
| int main() | ||
| { | ||
| foo_t f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| //@ assert (a == 10 && b == 20 && c == 30); | ||
| return f.a + f.b + f.c; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous union in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| union { | ||
| int a; | ||
| int b; | ||
| }; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| //@ assert (a == 20 && b == 20); | ||
| return f.a + f.b; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous union and variables in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| int a; | ||
| union { | ||
| int b; | ||
| int c; | ||
| }; | ||
| int d; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| f.d = 40; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| int d = f.d; | ||
| //@ assert (a == 10 && b == 30 && c == 30 && d == 40); | ||
| return f.a + f.b + f.c + f.d; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with nested anonymous unions in a named (outer) struct | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| struct foo { | ||
| union { | ||
| int a; | ||
| union { | ||
| int b; | ||
| int c; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| int main() | ||
| { | ||
| struct foo f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| //@ assert (a == 30 && b == 30 && c == 30); | ||
| return f.a + f.b + f.c; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous union in an outer union as part of a typedef | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| typedef union { | ||
| int a; | ||
| union { | ||
| int b; | ||
| int c; | ||
| }; | ||
| } foo_t; | ||
|
|
||
| int main() | ||
| { | ||
| foo_t f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| //@ assert (a == 30 && b == 30 && c == 30); | ||
| return f.a + f.b + f.c; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* #Safe | ||
| *----------------------------------------------------------------------------- | ||
| * Example with an anonymous struct in an outer union as part of a typedef | ||
| *----------------------------------------------------------------------------- | ||
| * Author: Manuel Bentele | ||
| * Date: 13.11.2025 | ||
| *---------------------------------------------------------------------------*/ | ||
|
|
||
| typedef union { | ||
| struct { | ||
| unsigned int a : 8; | ||
| unsigned int b : 8; | ||
| unsigned int c : 8; | ||
| unsigned int d : 8; | ||
| }; | ||
| unsigned int all; | ||
| } foo_t; | ||
|
|
||
| int main() | ||
| { | ||
| foo_t f; | ||
| f.a = 10; | ||
| f.b = 20; | ||
| f.c = 30; | ||
| f.d = 40; | ||
| int a = f.a; | ||
| int b = f.b; | ||
| int c = f.c; | ||
| int d = f.d; | ||
| int all = f.all; | ||
| assert (a == 10 && b == 20 && c == 30 && d == 40 && all == 673059850); | ||
| return f.all; | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the idea to flatten anonymous structs into a parent struct; and I guess, similarly, to flatten anonymous unions into a parent union.
But does it also make sense to flatten an anonymous struct into a parent union, or vice versa? A struct is a kind of product type (basically a tuple, but the fields are named), whereas a union is a sum type (only one of the values is present, not all). It seems to me that this information is lost by flattening one into the other, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, after I thought more about this, I think that the should not be flattened for this reason. At least the
sizeofcomputation could be fixed for that reason. I guess the better solution would be to just have a recursive lookup inCStructOrUnioninstead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I was thinking in a similar direction: structs and unions could have special handling for anonymous fields (let's be careful to avoid "name clashes" because they all have the field name
""), and then the field lookup should recurse into these anonymous fields.Relatedly, is it also possible in C to have an anonymous field but where the struct type is named? (and perhaps declared separately?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, @maul-esel. So far, anonymous unions are not supported correctly.
That would be indeed a great conceptual idea. Unfortunately, our representation of anonymous structs and unions is not created exactly like you described it. Declaration nodes with empty names are not stored as valid declarations. This means that anonymous structs and unions cannot be members of a parent type. We could add more hacks but I wanted to keep the changes as simple as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just suggest to have a separate member in
CStructOrUnionfor anonymous fields and to adapt the methods (recursive lookup in anonymous fields, somehow return the fields in getters).