authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-02 14:50:40+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-20 02:29:04+02:00
log8672f2696f20e8989d42f69adbe09edfe5cd9332
treef889e4aabe6b78c43a791ae84a7a34289171d0d3
parent538f1bbcb35c10fb0aa633adbac64932cc89d034

Address Spaces: zig fmt + tests


2 files changed, 69 insertions(+), 12 deletions(-)

lib/std/zig/parser_test.zig+24-10
......@@ -404,6 +404,10 @@ test "zig fmt: trailing comma in fn parameter list" {
404404 \\pub fn f(
405405 \\ a: i32,
406406 \\ b: i32,
407 \\) addrspace(.generic) i32 {}
408 \\pub fn f(
409 \\ a: i32,
410 \\ b: i32,
407411 \\) linksection(".text") i32 {}
408412 \\pub fn f(
409413 \\ a: i32,
......@@ -553,8 +557,8 @@ test "zig fmt: sentinel-terminated slice type" {
553557test "zig fmt: pointer-to-one with modifiers" {
554558 try testCanonical(
555559 \\const x: *u32 = undefined;
556 \\const y: *allowzero align(8) const volatile u32 = undefined;
557 \\const z: *allowzero align(8:4:2) const volatile u32 = undefined;
560 \\const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
561 \\const z: *allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
558562 \\
559563 );
560564}
......@@ -562,8 +566,8 @@ test "zig fmt: pointer-to-one with modifiers" {
562566test "zig fmt: pointer-to-many with modifiers" {
563567 try testCanonical(
564568 \\const x: [*]u32 = undefined;
565 \\const y: [*]allowzero align(8) const volatile u32 = undefined;
566 \\const z: [*]allowzero align(8:4:2) const volatile u32 = undefined;
569 \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
570 \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
567571 \\
568572 );
569573}
......@@ -571,8 +575,8 @@ test "zig fmt: pointer-to-many with modifiers" {
571575test "zig fmt: sentinel pointer with modifiers" {
572576 try testCanonical(
573577 \\const x: [*:42]u32 = undefined;
574 \\const y: [*:42]allowzero align(8) const volatile u32 = undefined;
575 \\const y: [*:42]allowzero align(8:4:2) const volatile u32 = undefined;
578 \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
579 \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
576580 \\
577581 );
578582}
......@@ -580,8 +584,8 @@ test "zig fmt: sentinel pointer with modifiers" {
580584test "zig fmt: c pointer with modifiers" {
581585 try testCanonical(
582586 \\const x: [*c]u32 = undefined;
583 \\const y: [*c]allowzero align(8) const volatile u32 = undefined;
584 \\const z: [*c]allowzero align(8:4:2) const volatile u32 = undefined;
587 \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
588 \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
585589 \\
586590 );
587591}
......@@ -589,7 +593,7 @@ test "zig fmt: c pointer with modifiers" {
589593test "zig fmt: slice with modifiers" {
590594 try testCanonical(
591595 \\const x: []u32 = undefined;
592 \\const y: []allowzero align(8) const volatile u32 = undefined;
596 \\const y: []allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
593597 \\
594598 );
595599}
......@@ -597,7 +601,7 @@ test "zig fmt: slice with modifiers" {
597601test "zig fmt: sentinel slice with modifiers" {
598602 try testCanonical(
599603 \\const x: [:42]u32 = undefined;
600 \\const y: [:42]allowzero align(8) const volatile u32 = undefined;
604 \\const y: [:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
601605 \\
602606 );
603607}
......@@ -1129,6 +1133,16 @@ test "zig fmt: linksection" {
11291133 );
11301134}
11311135
1136test "zig fmt: addrspace" {
1137 try testCanonical(
1138 \\export var python_length: u64 align(1) addrspace(.generic);
1139 \\export var python_color: Color addrspace(.generic) = .green;
1140 \\export var python_legs: u0 align(8) addrspace(.generic) linksection(".python") = 0;
1141 \\export fn python_hiss() align(8) addrspace(.generic) linksection(".python") void;
1142 \\
1143 );
1144}
1145
11321146test "zig fmt: correctly space struct fields with doc comments" {
11331147 try testTransform(
11341148 \\pub const S = struct {
lib/std/zig/render.zig+45-2
......@@ -797,6 +797,14 @@ fn renderPtrType(
797797 }
798798 }
799799
800 if (ptr_type.ast.addrspace_node != 0) {
801 const addrspace_first = tree.firstToken(ptr_type.ast.addrspace_node);
802 try renderToken(ais, tree, addrspace_first - 2, .none); // addrspace
803 try renderToken(ais, tree, addrspace_first - 1, .none); // lparen
804 try renderExpression(gpa, ais, tree, ptr_type.ast.addrspace_node, .none);
805 try renderToken(ais, tree, tree.lastToken(ptr_type.ast.addrspace_node) + 1, .space); // rparen
806 }
807
800808 if (ptr_type.const_token) |const_token| {
801809 try renderToken(ais, tree, const_token, .space);
802810 }
......@@ -921,6 +929,7 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
921929
922930 const name_space = if (var_decl.ast.type_node == 0 and
923931 (var_decl.ast.align_node != 0 or
932 var_decl.ast.addrspace_node != 0 or
924933 var_decl.ast.section_node != 0 or
925934 var_decl.ast.init_node != 0))
926935 Space.space
......@@ -930,8 +939,8 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
930939
931940 if (var_decl.ast.type_node != 0) {
932941 try renderToken(ais, tree, var_decl.ast.mut_token + 2, Space.space); // :
933 if (var_decl.ast.align_node != 0 or var_decl.ast.section_node != 0 or
934 var_decl.ast.init_node != 0)
942 if (var_decl.ast.align_node != 0 or var_decl.ast.addrspace_node != 0 or
943 var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0)
935944 {
936945 try renderExpression(gpa, ais, tree, var_decl.ast.type_node, .space);
937946 } else {
......@@ -948,6 +957,22 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
948957 try renderToken(ais, tree, align_kw, Space.none); // align
949958 try renderToken(ais, tree, lparen, Space.none); // (
950959 try renderExpression(gpa, ais, tree, var_decl.ast.align_node, Space.none);
960 if (var_decl.ast.addrspace_node != 0 or var_decl.ast.section_node != 0 or
961 var_decl.ast.init_node != 0) {
962 try renderToken(ais, tree, rparen, .space); // )
963 } else {
964 try renderToken(ais, tree, rparen, .none); // )
965 return renderToken(ais, tree, rparen + 1, Space.newline); // ;
966 }
967 }
968
969 if (var_decl.ast.addrspace_node != 0) {
970 const lparen = tree.firstToken(var_decl.ast.addrspace_node) - 1;
971 const addrspace_kw = lparen - 1;
972 const rparen = tree.lastToken(var_decl.ast.addrspace_node) + 1;
973 try renderToken(ais, tree, addrspace_kw, Space.none); // addrspace
974 try renderToken(ais, tree, lparen, Space.none); // (
975 try renderExpression(gpa, ais, tree, var_decl.ast.addrspace_node, Space.none);
951976 if (var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0) {
952977 try renderToken(ais, tree, rparen, .space); // )
953978 } else {
......@@ -1267,6 +1292,14 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
12671292 smallest_start = start;
12681293 }
12691294 }
1295 if (fn_proto.ast.addrspace_expr != 0) {
1296 const tok = tree.firstToken(fn_proto.ast.addrspace_expr) - 3;
1297 const start = token_starts[tok];
1298 if (start < smallest_start) {
1299 rparen = tok;
1300 smallest_start = start;
1301 }
1302 }
12701303 if (fn_proto.ast.section_expr != 0) {
12711304 const tok = tree.firstToken(fn_proto.ast.section_expr) - 3;
12721305 const start = token_starts[tok];
......@@ -1407,6 +1440,16 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
14071440 try renderToken(ais, tree, align_rparen, .space); // )
14081441 }
14091442
1443 if (fn_proto.ast.addrspace_expr != 0) {
1444 const align_lparen = tree.firstToken(fn_proto.ast.addrspace_expr) - 1;
1445 const align_rparen = tree.lastToken(fn_proto.ast.addrspace_expr) + 1;
1446
1447 try renderToken(ais, tree, align_lparen - 1, .none); // addrspace
1448 try renderToken(ais, tree, align_lparen, .none); // (
1449 try renderExpression(gpa, ais, tree, fn_proto.ast.addrspace_expr, .none);
1450 try renderToken(ais, tree, align_rparen, .space); // )
1451 }
1452
14101453 if (fn_proto.ast.section_expr != 0) {
14111454 const section_lparen = tree.firstToken(fn_proto.ast.section_expr) - 1;
14121455 const section_rparen = tree.lastToken(fn_proto.ast.section_expr) + 1;