translate-c: Group field access LHS if necessary

This commit is contained in:
LemonBoy
2021-04-20 11:07:21 +02:00
committed by Veikka Tuominen
parent 63304a871e
commit be551d85b7
2 changed files with 18 additions and 2 deletions

View File

@@ -1728,7 +1728,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
},
.field_access => {
const payload = node.castTag(.field_access).?.data;
const lhs = try renderNode(c, payload.lhs);
const lhs = try renderNodeGrouped(c, payload.lhs);
return renderFieldAccess(c, lhs, payload.field_name);
},
.@"struct", .@"union" => return renderRecord(c, node),
@@ -2073,7 +2073,7 @@ fn renderNullSentinelArrayType(c: *Context, len: usize, elem_type: Node) !NodeIn
.main_token = l_bracket,
.data = .{
.lhs = len_expr,
.rhs = try c.addExtra(std.zig.ast.Node.ArrayTypeSentinel {
.rhs = try c.addExtra(std.zig.ast.Node.ArrayTypeSentinel{
.sentinel = sentinel_expr,
.elem_type = elem_type_expr,
}),

View File

@@ -3,6 +3,22 @@ const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
pub fn addCases(cases: *tests.TranslateCContext) void {
cases.add("field access is grouped if necessary",
\\unsigned long foo(unsigned long x) {
\\ return ((union{unsigned long _x}){x})._x;
\\}
, &[_][]const u8{
\\pub export fn foo(arg_x: c_ulong) c_ulong {
\\ var x = arg_x;
\\ const union_unnamed_1 = extern union {
\\ _x: c_ulong,
\\ };
\\ return (union_unnamed_1{
\\ ._x = x,
\\ })._x;
\\}
});
cases.add("unnamed child types of typedef receive typedef's name",
\\typedef enum {
\\ FooA,