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 {...@@ -407,7 +407,9 @@ pub const Tree = struct {
407 => {407 => {
408 const main_token = main_tokens[n];408 const main_token = main_tokens[n];
409 return switch (token_tags[main_token]) {409 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]) {
411 .LBracket => main_token - 1,413 .LBracket => main_token - 1,
412 else => main_token,414 else => main_token,
413 },415 },
...@@ -1625,7 +1627,9 @@ pub const Tree = struct {...@@ -1625,7 +1627,9 @@ pub const Tree = struct {
1625 // literals in some places here1627 // literals in some places here
1626 const Kind = full.PtrType.Kind;1628 const Kind = full.PtrType.Kind;
1627 const kind: Kind = switch (token_tags[info.main_token]) {1629 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]) {
1629 .RBracket => .many,1633 .RBracket => .many,
1630 .Colon => .sentinel,1634 .Colon => .sentinel,
1631 .Identifier => if (token_tags[info.main_token - 1] == .LBracket) Kind.c else .one,1635 .Identifier => if (token_tags[info.main_token - 1] == .LBracket) Kind.c else .one,
...@@ -2393,18 +2397,26 @@ pub const Node = struct {...@@ -2393,18 +2397,26 @@ pub const Node = struct {
2393 /// `[*]align(lhs) rhs`. lhs can be omitted.2397 /// `[*]align(lhs) rhs`. lhs can be omitted.
2394 /// `*align(lhs) rhs`. lhs can be omitted.2398 /// `*align(lhs) rhs`. lhs can be omitted.
2395 /// `[]rhs`.2399 /// `[]rhs`.
2396 /// main_token is the asterisk if a pointer or the lbrace if a slice2400 /// 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.
2397 PtrTypeAligned,2403 PtrTypeAligned,
2398 /// `[*:lhs]rhs`. lhs can be omitted.2404 /// `[*:lhs]rhs`. lhs can be omitted.
2399 /// `*rhs`.2405 /// `*rhs`.
2400 /// `[:lhs]rhs`.2406 /// `[:lhs]rhs`.
2401 /// main_token is the asterisk if a pointer or the lbrace if a slice2407 /// 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.
2402 PtrTypeSentinel,2410 PtrTypeSentinel,
2403 /// lhs is index into PtrType. rhs is the element type expression.2411 /// 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 slice2412 /// 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.
2405 PtrType,2415 PtrType,
2406 /// lhs is index into PtrTypeBitRange. rhs is the element type expression.2416 /// 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 slice2417 /// 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.
2408 PtrTypeBitRange,2420 PtrTypeBitRange,
2409 /// `lhs[rhs..]`2421 /// `lhs[rhs..]`
2410 /// main_token is the lbracket.2422 /// main_token is the lbracket.
lib/std/zig/parser_test.zig+55-42
...@@ -2318,27 +2318,27 @@ test "zig fmt: alignment" {...@@ -2318,27 +2318,27 @@ test "zig fmt: alignment" {
2318 );2318 );
2319}2319}
23202320
2321//test "zig fmt: C main" {2321test "zig fmt: C main" {
2322// try testCanonical(2322 try testCanonical(
2323// \\fn main(argc: c_int, argv: **u8) c_int {2323 \\fn main(argc: c_int, argv: **u8) c_int {
2324// \\ const a = b;2324 \\ const a = b;
2325// \\}2325 \\}
2326// \\2326 \\
2327// );2327 );
2328//}2328}
2329//2329
2330//test "zig fmt: return" {2330test "zig fmt: return" {
2331// try testCanonical(2331 try testCanonical(
2332// \\fn foo(argc: c_int, argv: **u8) c_int {2332 \\fn foo(argc: c_int, argv: **u8) c_int {
2333// \\ return 0;2333 \\ return 0;
2334// \\}2334 \\}
2335// \\2335 \\
2336// \\fn bar() void {2336 \\fn bar() void {
2337// \\ return;2337 \\ return;
2338// \\}2338 \\}
2339// \\2339 \\
2340// );2340 );
2341//}2341}
23422342
2343test "zig fmt: function attributes" {2343test "zig fmt: function attributes" {
2344 try testCanonical(2344 try testCanonical(
...@@ -2356,27 +2356,40 @@ test "zig fmt: function attributes" {...@@ -2356,27 +2356,40 @@ test "zig fmt: function attributes" {
2356 );2356 );
2357}2357}
23582358
2359//test "zig fmt: pointer attributes" {2359test "zig fmt: nested pointers with ** tokens" {
2360// try testCanonical(2360 try testCanonical(
2361// \\extern fn f1(s: *align(*u8) u8) c_int;2361 \\const x: *u32 = undefined;
2362// \\extern fn f2(s: **align(1) *const *volatile u8) c_int;2362 \\const x: **u32 = undefined;
2363// \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;2363 \\const x: ***u32 = undefined;
2364// \\extern fn f4(s: *align(1) const volatile u8) c_int;2364 \\const x: ****u32 = undefined;
2365// \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;2365 \\const x: *****u32 = undefined;
2366// \\2366 \\const x: ******u32 = undefined;
2367// );2367 \\const x: *******u32 = undefined;
2368//}2368 \\
2369//2369 );
2370//test "zig fmt: slice attributes" {2370}
2371// try testCanonical(2371
2372// \\extern fn f1(s: *align(*u8) u8) c_int;2372test "zig fmt: pointer attributes" {
2373// \\extern fn f2(s: **align(1) *const *volatile u8) c_int;2373 try testCanonical(
2374// \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;2374 \\extern fn f1(s: *align(*u8) u8) c_int;
2375// \\extern fn f4(s: *align(1) const volatile u8) c_int;2375 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
2376// \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;2376 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
2377// \\2377 \\extern fn f4(s: *align(1) const volatile u8) c_int;
2378// );2378 \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
2379//}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
2381test "zig fmt: test declaration" {2394test "zig fmt: test declaration" {
2382 try testCanonical(2395 try testCanonical(
lib/std/zig/render.zig+10
...@@ -699,6 +699,16 @@ fn renderPtrType(...@@ -699,6 +699,16 @@ fn renderPtrType(
699) Error!void {699) Error!void {
700 switch (ptr_type.kind) {700 switch (ptr_type.kind) {
701 .one => {701 .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 }
702 try renderToken(ais, tree, ptr_type.ast.main_token, .None); // asterisk712 try renderToken(ais, tree, ptr_type.ast.main_token, .None); // asterisk
703 },713 },
704 .many => {714 .many => {