| author | |
| committer | |
| log | 4a582734fd84f2096ca9bb559387ddd04c70ed57 |
| tree | fa4a8353e8eaa9aa901e1f7641631905df6f3368 |
| parent | 3fd8ac092e88ac4bc604afcdd3fcb33249dba967 |
| signature |
3 files changed, 47 insertions(+), 19 deletions(-)
lib/std/zig/ast.zig+2-1| ... | @@ -459,7 +459,8 @@ pub const Tree = struct { | ... | @@ -459,7 +459,8 @@ pub const Tree = struct { |
| 459 | .keyword_extern, | 459 | .keyword_extern, |
| 460 | .keyword_export, | 460 | .keyword_export, |
| 461 | .keyword_pub, | 461 | .keyword_pub, |
| 462 | .keyword_threadlocal, | 462 | .keyword_inline, |
| 463 | .keyword_noinline, | ||
| 463 | .string_literal, | 464 | .string_literal, |
| 464 | => continue, | 465 | => continue, |
| 465 | 466 |
lib/std/zig/parser_test.zig+27-7| ... | @@ -61,13 +61,33 @@ test "zig fmt: respect line breaks in struct field value declaration" { | ... | @@ -61,13 +61,33 @@ test "zig fmt: respect line breaks in struct field value declaration" { |
| 61 | ); | 61 | ); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | // TODO Remove this after zig 0.9.0 is released. | 64 | test "zig fmt: respect line breaks before functions" { |
| 65 | test "zig fmt: rewrite inline functions as callconv(.Inline)" { | 65 | try testCanonical( |
| 66 | try testTransform( | 66 | \\const std = @import("std"); |
| 67 | \\ | ||
| 67 | \\inline fn foo() void {} | 68 | \\inline fn foo() void {} |
| 68 | \\ | 69 | \\ |
| 69 | , | 70 | \\noinline fn foo() void {} |
| 71 | \\ | ||
| 72 | \\export fn foo() void {} | ||
| 73 | \\ | ||
| 74 | \\extern fn foo() void; | ||
| 75 | \\ | ||
| 76 | \\extern "foo" fn foo() void; | ||
| 77 | \\ | ||
| 78 | ); | ||
| 79 | } | ||
| 80 | |||
| 81 | test "zig fmt: rewrite callconv(.Inline) to the inline keyword" { | ||
| 82 | try testTransform( | ||
| 70 | \\fn foo() callconv(.Inline) void {} | 83 | \\fn foo() callconv(.Inline) void {} |
| 84 | \\const bar = .Inline; | ||
| 85 | \\fn foo() callconv(bar) void {} | ||
| 86 | \\ | ||
| 87 | , | ||
| 88 | \\inline fn foo() void {} | ||
| 89 | \\const bar = .Inline; | ||
| 90 | \\fn foo() callconv(bar) void {} | ||
| 71 | \\ | 91 | \\ |
| 72 | ); | 92 | ); |
| 73 | } | 93 | } |
| ... | @@ -2867,17 +2887,17 @@ test "zig fmt: functions" { | ... | @@ -2867,17 +2887,17 @@ test "zig fmt: functions" { |
| 2867 | \\extern fn puts(s: *const u8) c_int; | 2887 | \\extern fn puts(s: *const u8) c_int; |
| 2868 | \\extern "c" fn puts(s: *const u8) c_int; | 2888 | \\extern "c" fn puts(s: *const u8) c_int; |
| 2869 | \\export fn puts(s: *const u8) c_int; | 2889 | \\export fn puts(s: *const u8) c_int; |
| 2870 | \\fn puts(s: *const u8) callconv(.Inline) c_int; | 2890 | \\inline fn puts(s: *const u8) c_int; |
| 2871 | \\noinline fn puts(s: *const u8) c_int; | 2891 | \\noinline fn puts(s: *const u8) c_int; |
| 2872 | \\pub extern fn puts(s: *const u8) c_int; | 2892 | \\pub extern fn puts(s: *const u8) c_int; |
| 2873 | \\pub extern "c" fn puts(s: *const u8) c_int; | 2893 | \\pub extern "c" fn puts(s: *const u8) c_int; |
| 2874 | \\pub export fn puts(s: *const u8) c_int; | 2894 | \\pub export fn puts(s: *const u8) c_int; |
| 2875 | \\pub fn puts(s: *const u8) callconv(.Inline) c_int; | 2895 | \\pub inline fn puts(s: *const u8) c_int; |
| 2876 | \\pub noinline fn puts(s: *const u8) c_int; | 2896 | \\pub noinline fn puts(s: *const u8) c_int; |
| 2877 | \\pub extern fn puts(s: *const u8) align(2 + 2) c_int; | 2897 | \\pub extern fn puts(s: *const u8) align(2 + 2) c_int; |
| 2878 | \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int; | 2898 | \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int; |
| 2879 | \\pub export fn puts(s: *const u8) align(2 + 2) c_int; | 2899 | \\pub export fn puts(s: *const u8) align(2 + 2) c_int; |
| 2880 | \\pub fn puts(s: *const u8) align(2 + 2) callconv(.Inline) c_int; | 2900 | \\pub inline fn puts(s: *const u8) align(2 + 2) c_int; |
| 2881 | \\pub noinline fn puts(s: *const u8) align(2 + 2) c_int; | 2901 | \\pub noinline fn puts(s: *const u8) align(2 + 2) c_int; |
| 2882 | \\ | 2902 | \\ |
| 2883 | ); | 2903 | ); |
lib/std/zig/render.zig+18-11| ... | @@ -83,13 +83,23 @@ fn renderMember(gpa: *Allocator, ais: *Ais, tree: ast.Tree, decl: ast.Node.Index | ... | @@ -83,13 +83,23 @@ fn renderMember(gpa: *Allocator, ais: *Ais, tree: ast.Tree, decl: ast.Node.Index |
| 83 | } | 83 | } |
| 84 | } | 84 | } |
| 85 | while (i < fn_token) : (i += 1) { | 85 | while (i < fn_token) : (i += 1) { |
| 86 | if (token_tags[i] == .keyword_inline) { | ||
| 87 | // TODO remove this special case when 0.9.0 is released. | ||
| 88 | // See the commit that introduced this comment for more details. | ||
| 89 | continue; | ||
| 90 | } | ||
| 91 | try renderToken(ais, tree, i, .space); | 86 | try renderToken(ais, tree, i, .space); |
| 92 | } | 87 | } |
| 88 | switch (tree.nodes.items(.tag)[fn_proto]) { | ||
| 89 | .fn_proto_one, .fn_proto => { | ||
| 90 | const callconv_expr = if (tree.nodes.items(.tag)[fn_proto] == .fn_proto_one) | ||
| 91 | tree.extraData(datas[fn_proto].lhs, ast.Node.FnProtoOne).callconv_expr | ||
| 92 | else | ||
| 93 | tree.extraData(datas[fn_proto].lhs, ast.Node.FnProto).callconv_expr; | ||
| 94 | if (callconv_expr != 0 and tree.nodes.items(.tag)[callconv_expr] == .enum_literal) { | ||
| 95 | if (mem.eql(u8, "Inline", tree.tokenSlice(main_tokens[callconv_expr]))) { | ||
| 96 | try ais.writer().writeAll("inline "); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | }, | ||
| 100 | .fn_proto_simple, .fn_proto_multi => {}, | ||
| 101 | else => unreachable, | ||
| 102 | } | ||
| 93 | assert(datas[decl].rhs != 0); | 103 | assert(datas[decl].rhs != 0); |
| 94 | try renderExpression(gpa, ais, tree, fn_proto, .space); | 104 | try renderExpression(gpa, ais, tree, fn_proto, .space); |
| 95 | return renderExpression(gpa, ais, tree, datas[decl].rhs, space); | 105 | return renderExpression(gpa, ais, tree, datas[decl].rhs, space); |
| ... | @@ -1246,9 +1256,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. | ... | @@ -1246,9 +1256,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. |
| 1246 | const token_tags = tree.tokens.items(.tag); | 1256 | const token_tags = tree.tokens.items(.tag); |
| 1247 | const token_starts = tree.tokens.items(.start); | 1257 | const token_starts = tree.tokens.items(.start); |
| 1248 | 1258 | ||
| 1249 | const is_inline = fn_proto.ast.fn_token > 0 and | ||
| 1250 | token_tags[fn_proto.ast.fn_token - 1] == .keyword_inline; | ||
| 1251 | |||
| 1252 | const after_fn_token = fn_proto.ast.fn_token + 1; | 1259 | const after_fn_token = fn_proto.ast.fn_token + 1; |
| 1253 | const lparen = if (token_tags[after_fn_token] == .identifier) blk: { | 1260 | const lparen = if (token_tags[after_fn_token] == .identifier) blk: { |
| 1254 | try renderToken(ais, tree, fn_proto.ast.fn_token, .space); // fn | 1261 | try renderToken(ais, tree, fn_proto.ast.fn_token, .space); // fn |
| ... | @@ -1424,7 +1431,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. | ... | @@ -1424,7 +1431,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. |
| 1424 | try renderToken(ais, tree, section_rparen, .space); // ) | 1431 | try renderToken(ais, tree, section_rparen, .space); // ) |
| 1425 | } | 1432 | } |
| 1426 | 1433 | ||
| 1427 | if (fn_proto.ast.callconv_expr != 0) { | 1434 | if (fn_proto.ast.callconv_expr != 0 and |
| 1435 | !mem.eql(u8, "Inline", tree.tokenSlice(tree.nodes.items(.main_token)[fn_proto.ast.callconv_expr]))) | ||
| 1436 | { | ||
| 1428 | const callconv_lparen = tree.firstToken(fn_proto.ast.callconv_expr) - 1; | 1437 | const callconv_lparen = tree.firstToken(fn_proto.ast.callconv_expr) - 1; |
| 1429 | const callconv_rparen = tree.lastToken(fn_proto.ast.callconv_expr) + 1; | 1438 | const callconv_rparen = tree.lastToken(fn_proto.ast.callconv_expr) + 1; |
| 1430 | 1439 | ||
| ... | @@ -1432,8 +1441,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. | ... | @@ -1432,8 +1441,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full. |
| 1432 | try renderToken(ais, tree, callconv_lparen, .none); // ( | 1441 | try renderToken(ais, tree, callconv_lparen, .none); // ( |
| 1433 | try renderExpression(gpa, ais, tree, fn_proto.ast.callconv_expr, .none); | 1442 | try renderExpression(gpa, ais, tree, fn_proto.ast.callconv_expr, .none); |
| 1434 | try renderToken(ais, tree, callconv_rparen, .space); // ) | 1443 | try renderToken(ais, tree, callconv_rparen, .space); // ) |
| 1435 | } else if (is_inline) { | ||
| 1436 | try ais.writer().writeAll("callconv(.Inline) "); | ||
| 1437 | } | 1444 | } |
| 1438 | 1445 | ||
| 1439 | if (token_tags[maybe_bang] == .bang) { | 1446 | if (token_tags[maybe_bang] == .bang) { |