| author | |
| committer | |
| log | 8672f2696f20e8989d42f69adbe09edfe5cd9332 |
| tree | f889e4aabe6b78c43a791ae84a7a34289171d0d3 |
| parent | 538f1bbcb35c10fb0aa633adbac64932cc89d034 |
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 | 404 | \\pub fn f( |
| 405 | 405 | \\ a: i32, |
| 406 | 406 | \\ b: i32, |
| 407 | \\) addrspace(.generic) i32 {} | |
| 408 | \\pub fn f( | |
| 409 | \\ a: i32, | |
| 410 | \\ b: i32, | |
| 407 | 411 | \\) linksection(".text") i32 {} |
| 408 | 412 | \\pub fn f( |
| 409 | 413 | \\ a: i32, |
| ... | ... | @@ -553,8 +557,8 @@ test "zig fmt: sentinel-terminated slice type" { |
| 553 | 557 | test "zig fmt: pointer-to-one with modifiers" { |
| 554 | 558 | try testCanonical( |
| 555 | 559 | \\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; | |
| 558 | 562 | \\ |
| 559 | 563 | ); |
| 560 | 564 | } |
| ... | ... | @@ -562,8 +566,8 @@ test "zig fmt: pointer-to-one with modifiers" { |
| 562 | 566 | test "zig fmt: pointer-to-many with modifiers" { |
| 563 | 567 | try testCanonical( |
| 564 | 568 | \\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; | |
| 567 | 571 | \\ |
| 568 | 572 | ); |
| 569 | 573 | } |
| ... | ... | @@ -571,8 +575,8 @@ test "zig fmt: pointer-to-many with modifiers" { |
| 571 | 575 | test "zig fmt: sentinel pointer with modifiers" { |
| 572 | 576 | try testCanonical( |
| 573 | 577 | \\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; | |
| 576 | 580 | \\ |
| 577 | 581 | ); |
| 578 | 582 | } |
| ... | ... | @@ -580,8 +584,8 @@ test "zig fmt: sentinel pointer with modifiers" { |
| 580 | 584 | test "zig fmt: c pointer with modifiers" { |
| 581 | 585 | try testCanonical( |
| 582 | 586 | \\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; | |
| 585 | 589 | \\ |
| 586 | 590 | ); |
| 587 | 591 | } |
| ... | ... | @@ -589,7 +593,7 @@ test "zig fmt: c pointer with modifiers" { |
| 589 | 593 | test "zig fmt: slice with modifiers" { |
| 590 | 594 | try testCanonical( |
| 591 | 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 | 601 | test "zig fmt: sentinel slice with modifiers" { |
| 598 | 602 | try testCanonical( |
| 599 | 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 | 1133 | ); |
| 1130 | 1134 | } |
| 1131 | 1135 | |
| 1136 | test "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 | ||
| 1132 | 1146 | test "zig fmt: correctly space struct fields with doc comments" { |
| 1133 | 1147 | try testTransform( |
| 1134 | 1148 | \\pub const S = struct { |
lib/std/zig/render.zig+45-2| ... | ... | @@ -797,6 +797,14 @@ fn renderPtrType( |
| 797 | 797 | } |
| 798 | 798 | } |
| 799 | 799 | |
| 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 | 808 | if (ptr_type.const_token) |const_token| { |
| 801 | 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 | 929 | |
| 922 | 930 | const name_space = if (var_decl.ast.type_node == 0 and |
| 923 | 931 | (var_decl.ast.align_node != 0 or |
| 932 | var_decl.ast.addrspace_node != 0 or | |
| 924 | 933 | var_decl.ast.section_node != 0 or |
| 925 | 934 | var_decl.ast.init_node != 0)) |
| 926 | 935 | Space.space |
| ... | ... | @@ -930,8 +939,8 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe |
| 930 | 939 | |
| 931 | 940 | if (var_decl.ast.type_node != 0) { |
| 932 | 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 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) | |
| 935 | 944 | { |
| 936 | 945 | try renderExpression(gpa, ais, tree, var_decl.ast.type_node, .space); |
| 937 | 946 | } else { |
| ... | ... | @@ -948,6 +957,22 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe |
| 948 | 957 | try renderToken(ais, tree, align_kw, Space.none); // align |
| 949 | 958 | try renderToken(ais, tree, lparen, Space.none); // ( |
| 950 | 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 | 976 | if (var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0) { |
| 952 | 977 | try renderToken(ais, tree, rparen, .space); // ) |
| 953 | 978 | } else { |
| ... | ... | @@ -1267,6 +1292,14 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro |
| 1267 | 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 | 1303 | if (fn_proto.ast.section_expr != 0) { |
| 1271 | 1304 | const tok = tree.firstToken(fn_proto.ast.section_expr) - 3; |
| 1272 | 1305 | const start = token_starts[tok]; |
| ... | ... | @@ -1407,6 +1440,16 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro |
| 1407 | 1440 | try renderToken(ais, tree, align_rparen, .space); // ) |
| 1408 | 1441 | } |
| 1409 | 1442 | |
| 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 | 1453 | if (fn_proto.ast.section_expr != 0) { |
| 1411 | 1454 | const section_lparen = tree.firstToken(fn_proto.ast.section_expr) - 1; |
| 1412 | 1455 | const section_rparen = tree.lastToken(fn_proto.ast.section_expr) + 1; |