authorgravatar for Techcable@techcable.netTechcable <Techcable@techcable.net> 2022-10-01 01:31:17-07:00
committergravatar for Techcable@techcable.netTechcable <Techcable@techcable.net> 2022-10-01 15:22:26-07:00
loga560af96568a668bf0c01b3c58ad4dda647ea1b4
tree6e4a49b52e31fd14def6d51d0d4c175654c2bafa
parent5b689d389fa50070c08520cdb6296dda3ad78a62
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: Add tests for packed unions

Was missing test coverage before this (although it was suppored)

1 files changed, 68 insertions(+), 0 deletions(-)

test/translate_c.zig+68
......@@ -1391,6 +1391,74 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13911391 \\pub const Foo = union_Foo;
13921392 });
13931393
1394 cases.add("packed union - simple",
1395 \\union Foo {
1396 \\ char x;
1397 \\ double y;
1398 \\} __attribute__((packed));
1399 , &[_][]const u8{
1400 \\pub const union_Foo = extern union {
1401 \\ x: u8 align(1),
1402 \\ y: f64 align(1),
1403 \\};
1404 ,
1405 \\pub const Foo = union_Foo;
1406 });
1407
1408 cases.add("packed union - nested unpacked",
1409 \\union Foo{
1410 \\ char x;
1411 \\ double y;
1412 \\ struct {
1413 \\ char a;
1414 \\ int b;
1415 \\ } z;
1416 \\} __attribute__((packed));
1417 , &[_][]const u8{
1418 // NOTE: The nested struct is *not* packed/aligned,
1419 // even though the parent struct is
1420 // this is consistent with GCC docs
1421 \\const struct_unnamed_1 = extern struct {
1422 \\ a: u8,
1423 \\ b: c_int,
1424 \\};
1425 ,
1426 \\pub const union_Foo = extern union {
1427 \\ x: u8 align(1),
1428 \\ y: f64 align(1),
1429 \\ z: struct_unnamed_1 align(1),
1430 \\};
1431 ,
1432 \\pub const Foo = union_Foo;
1433 });
1434
1435 cases.add("packed union - nested packed",
1436 \\union Foo{
1437 \\ char x;
1438 \\ double y;
1439 \\ struct {
1440 \\ char a;
1441 \\ int b;
1442 \\ } __attribute__((packed)) z;
1443 \\} __attribute__((packed));
1444 , &[_][]const u8{
1445 // in order for the nested struct to be packed, it must
1446 // have an independent packed declaration on
1447 // the nested type (see GCC docs for details)
1448 \\const struct_unnamed_1 = extern struct {
1449 \\ a: u8 align(1),
1450 \\ b: c_int align(1),
1451 \\};
1452 ,
1453 \\pub const union_Foo = extern union {
1454 \\ x: u8 align(1),
1455 \\ y: f64 align(1),
1456 \\ z: struct_unnamed_1 align(1),
1457 \\};
1458 ,
1459 \\pub const Foo = union_Foo;
1460 });
1461
13941462 cases.add("string literal",
13951463 \\const char *foo(void) {
13961464 \\ return "bar";