| ... | ... | @@ -1391,6 +1391,74 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1391 | 1391 | \\pub const Foo = union_Foo; |
| 1392 | 1392 | }); |
| 1393 | 1393 | |
| 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 | |
| 1394 | 1462 | cases.add("string literal", |
| 1395 | 1463 | \\const char *foo(void) { |
| 1396 | 1464 | \\ return "bar"; |