authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-10 16:00:54+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-10 11:53:53-08:00
log8c4f3e5a319b2c700a57d833e1aaf01e769f8d4a
treeff997f4cf39dc256acb38ae68ebcc1d2f0530303
parenta524e57090f3e3412292dbe6b3e4fe4fb7bad1ea

zig fmt: fix render of pointers with ** tokens


3 files changed, 83 insertions(+), 48 deletions(-)

lib/std/zig/ast.zig+18-6
......@@ -407,7 +407,9 @@ pub const Tree = struct {
407407 => {
408408 const main_token = main_tokens[n];
409409 return switch (token_tags[main_token]) {
410 .Asterisk => switch (token_tags[main_token - 1]) {
410 .Asterisk,
411 .AsteriskAsterisk,
412 => switch (token_tags[main_token - 1]) {
411413 .LBracket => main_token - 1,
412414 else => main_token,
413415 },
......@@ -1625,7 +1627,9 @@ pub const Tree = struct {
16251627 // literals in some places here
16261628 const Kind = full.PtrType.Kind;
16271629 const kind: Kind = switch (token_tags[info.main_token]) {
1628 .Asterisk => switch (token_tags[info.main_token + 1]) {
1630 .Asterisk,
1631 .AsteriskAsterisk,
1632 => switch (token_tags[info.main_token + 1]) {
16291633 .RBracket => .many,
16301634 .Colon => .sentinel,
16311635 .Identifier => if (token_tags[info.main_token - 1] == .LBracket) Kind.c else .one,
......@@ -2393,18 +2397,26 @@ pub const Node = struct {
23932397 /// `[*]align(lhs) rhs`. lhs can be omitted.
23942398 /// `*align(lhs) rhs`. lhs can be omitted.
23952399 /// `[]rhs`.
2396 /// main_token is the asterisk if a pointer or the lbrace if a slice
2400 /// main_token is the asterisk if a pointer or the lbracket if a slice
2401 /// main_token might be a ** token, which is shared with a parent/child
2402 /// pointer type and may require special handling.
23972403 PtrTypeAligned,
23982404 /// `[*:lhs]rhs`. lhs can be omitted.
23992405 /// `*rhs`.
24002406 /// `[:lhs]rhs`.
2401 /// main_token is the asterisk if a pointer or the lbrace if a slice
2407 /// main_token is the asterisk if a pointer or the lbracket if a slice
2408 /// main_token might be a ** token, which is shared with a parent/child
2409 /// pointer type and may require special handling.
24022410 PtrTypeSentinel,
24032411 /// lhs is index into PtrType. rhs is the element type expression.
2404 /// main_token is the asterisk if a pointer or the lbrace if a slice
2412 /// main_token is the asterisk if a pointer or the lbracket if a slice
2413 /// main_token might be a ** token, which is shared with a parent/child
2414 /// pointer type and may require special handling.
24052415 PtrType,
24062416 /// lhs is index into PtrTypeBitRange. rhs is the element type expression.
2407 /// main_token is the asterisk if a pointer or the lbrace if a slice
2417 /// main_token is the asterisk if a pointer or the lbracket if a slice
2418 /// main_token might be a ** token, which is shared with a parent/child
2419 /// pointer type and may require special handling.
24082420 PtrTypeBitRange,
24092421 /// `lhs[rhs..]`
24102422 /// main_token is the lbracket.
lib/std/zig/parser_test.zig+55-42
......@@ -2318,27 +2318,27 @@ test "zig fmt: alignment" {
23182318 );
23192319}
23202320
2321//test "zig fmt: C main" {
2322// try testCanonical(
2323// \\fn main(argc: c_int, argv: **u8) c_int {
2324// \\ const a = b;
2325// \\}
2326// \\
2327// );
2328//}
2329//
2330//test "zig fmt: return" {
2331// try testCanonical(
2332// \\fn foo(argc: c_int, argv: **u8) c_int {
2333// \\ return 0;
2334// \\}
2335// \\
2336// \\fn bar() void {
2337// \\ return;
2338// \\}
2339// \\
2340// );
2341//}
2321test "zig fmt: C main" {
2322 try testCanonical(
2323 \\fn main(argc: c_int, argv: **u8) c_int {
2324 \\ const a = b;
2325 \\}
2326 \\
2327 );
2328}
2329
2330test "zig fmt: return" {
2331 try testCanonical(
2332 \\fn foo(argc: c_int, argv: **u8) c_int {
2333 \\ return 0;
2334 \\}
2335 \\
2336 \\fn bar() void {
2337 \\ return;
2338 \\}
2339 \\
2340 );
2341}
23422342
23432343test "zig fmt: function attributes" {
23442344 try testCanonical(
......@@ -2356,27 +2356,40 @@ test "zig fmt: function attributes" {
23562356 );
23572357}
23582358
2359//test "zig fmt: pointer attributes" {
2360// try testCanonical(
2361// \\extern fn f1(s: *align(*u8) u8) c_int;
2362// \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
2363// \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
2364// \\extern fn f4(s: *align(1) const volatile u8) c_int;
2365// \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
2366// \\
2367// );
2368//}
2369//
2370//test "zig fmt: slice attributes" {
2371// try testCanonical(
2372// \\extern fn f1(s: *align(*u8) u8) c_int;
2373// \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
2374// \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
2375// \\extern fn f4(s: *align(1) const volatile u8) c_int;
2376// \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
2377// \\
2378// );
2379//}
2359test "zig fmt: nested pointers with ** tokens" {
2360 try testCanonical(
2361 \\const x: *u32 = undefined;
2362 \\const x: **u32 = undefined;
2363 \\const x: ***u32 = undefined;
2364 \\const x: ****u32 = undefined;
2365 \\const x: *****u32 = undefined;
2366 \\const x: ******u32 = undefined;
2367 \\const x: *******u32 = undefined;
2368 \\
2369 );
2370}
2371
2372test "zig fmt: pointer attributes" {
2373 try testCanonical(
2374 \\extern fn f1(s: *align(*u8) u8) c_int;
2375 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
2376 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
2377 \\extern fn f4(s: *align(1) const volatile u8) c_int;
2378 \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
2379 \\
2380 );
2381}
2382
2383test "zig fmt: slice attributes" {
2384 try testCanonical(
2385 \\extern fn f1(s: []align(*u8) u8) c_int;
2386 \\extern fn f2(s: []align(1) []const []volatile u8) c_int;
2387 \\extern fn f3(s: []align(1) const [:0]align(1) volatile []const volatile u8) c_int;
2388 \\extern fn f4(s: []align(1) const volatile u8) c_int;
2389 \\extern fn f5(s: [:0]align(1) const volatile u8) c_int;
2390 \\
2391 );
2392}
23802393
23812394test "zig fmt: test declaration" {
23822395 try testCanonical(
lib/std/zig/render.zig+10
......@@ -699,6 +699,16 @@ fn renderPtrType(
699699) Error!void {
700700 switch (ptr_type.kind) {
701701 .one => {
702 // Since ** tokens exist and the same token is shared by two
703 // nested pointer types, we check to see if we are the parent
704 // in such a relationship. If so, skip rendering anything for
705 // this pointer type and rely on the child to render our asterisk
706 // as well when it renders the ** token.
707 if (tree.tokens.items(.tag)[ptr_type.ast.main_token] == .AsteriskAsterisk and
708 ptr_type.ast.main_token == tree.nodes.items(.main_token)[ptr_type.ast.child_type])
709 {
710 return renderExpression(ais, tree, ptr_type.ast.child_type, space);
711 }
702712 try renderToken(ais, tree, ptr_type.ast.main_token, .None); // asterisk
703713 },
704714 .many => {