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" {...@@ -404,6 +404,10 @@ test "zig fmt: trailing comma in fn parameter list" {
404 \\pub fn f(404 \\pub fn f(
405 \\ a: i32,405 \\ a: i32,
406 \\ b: i32,406 \\ b: i32,
407 \\) addrspace(.generic) i32 {}
408 \\pub fn f(
409 \\ a: i32,
410 \\ b: i32,
407 \\) linksection(".text") i32 {}411 \\) linksection(".text") i32 {}
408 \\pub fn f(412 \\pub fn f(
409 \\ a: i32,413 \\ a: i32,
...@@ -553,8 +557,8 @@ test "zig fmt: sentinel-terminated slice type" {...@@ -553,8 +557,8 @@ test "zig fmt: sentinel-terminated slice type" {
553test "zig fmt: pointer-to-one with modifiers" {557test "zig fmt: pointer-to-one with modifiers" {
554 try testCanonical(558 try testCanonical(
555 \\const x: *u32 = undefined;559 \\const x: *u32 = undefined;
556 \\const y: *allowzero align(8) const volatile u32 = undefined;560 \\const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
557 \\const z: *allowzero align(8:4:2) const volatile u32 = undefined;561 \\const z: *allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
558 \\562 \\
559 );563 );
560}564}
...@@ -562,8 +566,8 @@ test "zig fmt: pointer-to-one with modifiers" {...@@ -562,8 +566,8 @@ test "zig fmt: pointer-to-one with modifiers" {
562test "zig fmt: pointer-to-many with modifiers" {566test "zig fmt: pointer-to-many with modifiers" {
563 try testCanonical(567 try testCanonical(
564 \\const x: [*]u32 = undefined;568 \\const x: [*]u32 = undefined;
565 \\const y: [*]allowzero align(8) const volatile u32 = undefined;569 \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
566 \\const z: [*]allowzero align(8:4:2) const volatile u32 = undefined;570 \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
567 \\571 \\
568 );572 );
569}573}
...@@ -571,8 +575,8 @@ test "zig fmt: pointer-to-many with modifiers" {...@@ -571,8 +575,8 @@ test "zig fmt: pointer-to-many with modifiers" {
571test "zig fmt: sentinel pointer with modifiers" {575test "zig fmt: sentinel pointer with modifiers" {
572 try testCanonical(576 try testCanonical(
573 \\const x: [*:42]u32 = undefined;577 \\const x: [*:42]u32 = undefined;
574 \\const y: [*:42]allowzero align(8) const volatile u32 = undefined;578 \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
575 \\const y: [*:42]allowzero align(8:4:2) const volatile u32 = undefined;579 \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
576 \\580 \\
577 );581 );
578}582}
...@@ -580,8 +584,8 @@ test "zig fmt: sentinel pointer with modifiers" {...@@ -580,8 +584,8 @@ test "zig fmt: sentinel pointer with modifiers" {
580test "zig fmt: c pointer with modifiers" {584test "zig fmt: c pointer with modifiers" {
581 try testCanonical(585 try testCanonical(
582 \\const x: [*c]u32 = undefined;586 \\const x: [*c]u32 = undefined;
583 \\const y: [*c]allowzero align(8) const volatile u32 = undefined;587 \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
584 \\const z: [*c]allowzero align(8:4:2) const volatile u32 = undefined;588 \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
585 \\589 \\
586 );590 );
587}591}
...@@ -589,7 +593,7 @@ test "zig fmt: c pointer with modifiers" {...@@ -589,7 +593,7 @@ test "zig fmt: c pointer with modifiers" {
589test "zig fmt: slice with modifiers" {593test "zig fmt: slice with modifiers" {
590 try testCanonical(594 try testCanonical(
591 \\const x: []u32 = undefined;595 \\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;
593 \\597 \\
594 );598 );
595}599}
...@@ -597,7 +601,7 @@ test "zig fmt: slice with modifiers" {...@@ -597,7 +601,7 @@ test "zig fmt: slice with modifiers" {
597test "zig fmt: sentinel slice with modifiers" {601test "zig fmt: sentinel slice with modifiers" {
598 try testCanonical(602 try testCanonical(
599 \\const x: [:42]u32 = undefined;603 \\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;
601 \\605 \\
602 );606 );
603}607}
...@@ -1129,6 +1133,16 @@ test "zig fmt: linksection" {...@@ -1129,6 +1133,16 @@ test "zig fmt: linksection" {
1129 );1133 );
1130}1134}
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
1132test "zig fmt: correctly space struct fields with doc comments" {1146test "zig fmt: correctly space struct fields with doc comments" {
1133 try testTransform(1147 try testTransform(
1134 \\pub const S = struct {1148 \\pub const S = struct {
lib/std/zig/render.zig+45-2
...@@ -797,6 +797,14 @@ fn renderPtrType(...@@ -797,6 +797,14 @@ fn renderPtrType(
797 }797 }
798 }798 }
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
800 if (ptr_type.const_token) |const_token| {808 if (ptr_type.const_token) |const_token| {
801 try renderToken(ais, tree, const_token, .space);809 try renderToken(ais, tree, const_token, .space);
802 }810 }
...@@ -921,6 +929,7 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe...@@ -921,6 +929,7 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
921929
922 const name_space = if (var_decl.ast.type_node == 0 and930 const name_space = if (var_decl.ast.type_node == 0 and
923 (var_decl.ast.align_node != 0 or931 (var_decl.ast.align_node != 0 or
932 var_decl.ast.addrspace_node != 0 or
924 var_decl.ast.section_node != 0 or933 var_decl.ast.section_node != 0 or
925 var_decl.ast.init_node != 0))934 var_decl.ast.init_node != 0))
926 Space.space935 Space.space
...@@ -930,8 +939,8 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe...@@ -930,8 +939,8 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
930939
931 if (var_decl.ast.type_node != 0) {940 if (var_decl.ast.type_node != 0) {
932 try renderToken(ais, tree, var_decl.ast.mut_token + 2, Space.space); // :941 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 or942 if (var_decl.ast.align_node != 0 or var_decl.ast.addrspace_node != 0 or
934 var_decl.ast.init_node != 0)943 var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0)
935 {944 {
936 try renderExpression(gpa, ais, tree, var_decl.ast.type_node, .space);945 try renderExpression(gpa, ais, tree, var_decl.ast.type_node, .space);
937 } else {946 } else {
...@@ -948,6 +957,22 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe...@@ -948,6 +957,22 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
948 try renderToken(ais, tree, align_kw, Space.none); // align957 try renderToken(ais, tree, align_kw, Space.none); // align
949 try renderToken(ais, tree, lparen, Space.none); // (958 try renderToken(ais, tree, lparen, Space.none); // (
950 try renderExpression(gpa, ais, tree, var_decl.ast.align_node, Space.none);959 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);
951 if (var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0) {976 if (var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0) {
952 try renderToken(ais, tree, rparen, .space); // )977 try renderToken(ais, tree, rparen, .space); // )
953 } else {978 } else {
...@@ -1267,6 +1292,14 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro...@@ -1267,6 +1292,14 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
1267 smallest_start = start;1292 smallest_start = start;
1268 }1293 }
1269 }1294 }
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 }
1270 if (fn_proto.ast.section_expr != 0) {1303 if (fn_proto.ast.section_expr != 0) {
1271 const tok = tree.firstToken(fn_proto.ast.section_expr) - 3;1304 const tok = tree.firstToken(fn_proto.ast.section_expr) - 3;
1272 const start = token_starts[tok];1305 const start = token_starts[tok];
...@@ -1407,6 +1440,16 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro...@@ -1407,6 +1440,16 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
1407 try renderToken(ais, tree, align_rparen, .space); // )1440 try renderToken(ais, tree, align_rparen, .space); // )
1408 }1441 }
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
1410 if (fn_proto.ast.section_expr != 0) {1453 if (fn_proto.ast.section_expr != 0) {
1411 const section_lparen = tree.firstToken(fn_proto.ast.section_expr) - 1;1454 const section_lparen = tree.firstToken(fn_proto.ast.section_expr) - 1;
1412 const section_rparen = tree.lastToken(fn_proto.ast.section_expr) + 1;1455 const section_rparen = tree.lastToken(fn_proto.ast.section_expr) + 1;