authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-10 15:19:30+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-10 11:53:53-08:00
loga524e57090f3e3412292dbe6b3e4fe4fb7bad1ea
treebee166a7f12e3859152cf046ba5d636e01ca1f59
parent80b719d967d7241182e237b42ade1cd88494c8e8

zig fmt: support bodyless function decls

extern function declarations do not have a body, so allow setting the rhs for FnDecl to 0 to indicate this is the case.

4 files changed, 52 insertions(+), 14 deletions(-)

lib/std/zig/ast.zig+12-4
...@@ -521,7 +521,8 @@ pub const Tree = struct {...@@ -521,7 +521,8 @@ pub const Tree = struct {
521 .IfSimple,521 .IfSimple,
522 .WhileSimple,522 .WhileSimple,
523 .ForSimple,523 .ForSimple,
524 .FnDecl,524 .FnProtoSimple,
525 .FnProtoMulti,
525 .PtrTypeAligned,526 .PtrTypeAligned,
526 .PtrTypeSentinel,527 .PtrTypeSentinel,
527 .PtrType,528 .PtrType,
...@@ -538,8 +539,6 @@ pub const Tree = struct {...@@ -538,8 +539,6 @@ pub const Tree = struct {
538 .AsmSimple,539 .AsmSimple,
539 .AsmOutput,540 .AsmOutput,
540 .AsmInput,541 .AsmInput,
541 .FnProtoSimple,
542 .FnProtoMulti,
543 .ErrorValue,542 .ErrorValue,
544 => return datas[n].rhs + end_offset,543 => return datas[n].rhs + end_offset,
545544
...@@ -804,6 +803,13 @@ pub const Tree = struct {...@@ -804,6 +803,13 @@ pub const Tree = struct {
804 return main_tokens[n] + end_offset;803 return main_tokens[n] + end_offset;
805 }804 }
806 },805 },
806 .FnDecl => {
807 if (datas[n].rhs != 0) {
808 n = datas[n].rhs;
809 } else {
810 n = datas[n].lhs;
811 }
812 },
807 .FnProtoOne => {813 .FnProtoOne => {
808 const extra = tree.extraData(datas[n].lhs, Node.FnProtoOne);814 const extra = tree.extraData(datas[n].lhs, Node.FnProtoOne);
809 // linksection, callconv, align can appear in any order, so we815 // linksection, callconv, align can appear in any order, so we
...@@ -2520,7 +2526,9 @@ pub const Node = struct {...@@ -2520,7 +2526,9 @@ pub const Node = struct {
2520 /// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `FnProto[lhs]`.2526 /// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `FnProto[lhs]`.
2521 /// anytype and ... parameters are omitted from the AST tree.2527 /// anytype and ... parameters are omitted from the AST tree.
2522 FnProto,2528 FnProto,
2523 /// lhs is the FnProto, rhs is the function body block.2529 /// lhs is the FnProto.
2530 /// rhs is the function body block if non-zero.
2531 /// if rhs is zero, the funtion decl has no body (e.g. an extern function)
2524 FnDecl,2532 FnDecl,
2525 /// `anyframe->rhs`. main_token is `anyframe`. `lhs` is arrow token index.2533 /// `anyframe->rhs`. main_token is `anyframe`. `lhs` is arrow token index.
2526 AnyFrameType,2534 AnyFrameType,
lib/std/zig/parse.zig+8-1
...@@ -516,7 +516,14 @@ const Parser = struct {...@@ -516,7 +516,14 @@ const Parser = struct {
516 .Semicolon => {516 .Semicolon => {
517 const semicolon_token = p.nextToken();517 const semicolon_token = p.nextToken();
518 try p.parseAppendedDocComment(semicolon_token);518 try p.parseAppendedDocComment(semicolon_token);
519 return fn_proto;519 return p.addNode(.{
520 .tag = .FnDecl,
521 .main_token = p.nodes.items(.main_token)[fn_proto],
522 .data = .{
523 .lhs = fn_proto,
524 .rhs = 0,
525 },
526 });
520 },527 },
521 .LBrace => {528 .LBrace => {
522 const body_block = try p.parseBlock();529 const body_block = try p.parseBlock();
lib/std/zig/parser_test.zig+23-7
...@@ -298,12 +298,12 @@ test "zig fmt: grouped expressions (parentheses)" {...@@ -298,12 +298,12 @@ test "zig fmt: grouped expressions (parentheses)" {
298 );298 );
299}299}
300300
301//test "zig fmt: c pointer type" {301test "zig fmt: c pointer type" {
302// try testCanonical(302 try testCanonical(
303// \\pub extern fn repro() [*c]const u8;303 \\pub extern fn repro() [*c]const u8;
304// \\304 \\
305// );305 );
306//}306}
307307
308test "zig fmt: builtin call with trailing comma" {308test "zig fmt: builtin call with trailing comma" {
309 try testCanonical(309 try testCanonical(
...@@ -2339,7 +2339,23 @@ test "zig fmt: alignment" {...@@ -2339,7 +2339,23 @@ test "zig fmt: alignment" {
2339// \\2339// \\
2340// );2340// );
2341//}2341//}
2342//2342
2343test "zig fmt: function attributes" {
2344 try testCanonical(
2345 \\export fn foo() void {}
2346 \\pub export fn foo() void {}
2347 \\extern fn foo() void;
2348 \\pub extern fn foo() void;
2349 \\extern "c" fn foo() void;
2350 \\pub extern "c" fn foo() void;
2351 \\inline fn foo() void {}
2352 \\pub inline fn foo() void {}
2353 \\noinline fn foo() void {}
2354 \\pub noinline fn foo() void {}
2355 \\
2356 );
2357}
2358
2343//test "zig fmt: pointer attributes" {2359//test "zig fmt: pointer attributes" {
2344// try testCanonical(2360// try testCanonical(
2345// \\extern fn f1(s: *align(*u8) u8) c_int;2361// \\extern fn f1(s: *align(*u8) u8) c_int;
lib/std/zig/render.zig+9-2
...@@ -121,6 +121,8 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E...@@ -121,6 +121,8 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E
121 .Keyword_export,121 .Keyword_export,
122 .Keyword_pub,122 .Keyword_pub,
123 .StringLiteral,123 .StringLiteral,
124 .Keyword_inline,
125 .Keyword_noinline,
124 => continue,126 => continue,
125127
126 else => {128 else => {
...@@ -132,8 +134,13 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E...@@ -132,8 +134,13 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E
132 while (i < fn_token) : (i += 1) {134 while (i < fn_token) : (i += 1) {
133 try renderToken(ais, tree, i, .Space);135 try renderToken(ais, tree, i, .Space);
134 }136 }
135 try renderExpression(ais, tree, fn_proto, .Space);137 if (datas[decl].rhs != 0) {
136 return renderExpression(ais, tree, datas[decl].rhs, space);138 try renderExpression(ais, tree, fn_proto, .Space);
139 return renderExpression(ais, tree, datas[decl].rhs, space);
140 } else {
141 try renderExpression(ais, tree, fn_proto, .None);
142 return renderToken(ais, tree, tree.lastToken(fn_proto) + 1, space); // semicolon
143 }
137 },144 },
138 .FnProtoSimple,145 .FnProtoSimple,
139 .FnProtoMulti,146 .FnProtoMulti,