| 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 | 459 | .keyword_extern, |
| 460 | 460 | .keyword_export, |
| 461 | 461 | .keyword_pub, |
| 462 | .keyword_threadlocal, | |
| 462 | .keyword_inline, | |
| 463 | .keyword_noinline, | |
| 463 | 464 | .string_literal, |
| 464 | 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 | 61 | ); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | // TODO Remove this after zig 0.9.0 is released. | |
| 65 | test "zig fmt: rewrite inline functions as callconv(.Inline)" { | |
| 66 | try testTransform( | |
| 64 | test "zig fmt: respect line breaks before functions" { | |
| 65 | try testCanonical( | |
| 66 | \\const std = @import("std"); | |
| 67 | \\ | |
| 67 | 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 | 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 | 2887 | \\extern fn puts(s: *const u8) c_int; |
| 2868 | 2888 | \\extern "c" fn puts(s: *const u8) c_int; |
| 2869 | 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 | 2891 | \\noinline fn puts(s: *const u8) c_int; |
| 2872 | 2892 | \\pub extern fn puts(s: *const u8) c_int; |
| 2873 | 2893 | \\pub extern "c" fn puts(s: *const u8) c_int; |
| 2874 | 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 | 2896 | \\pub noinline fn puts(s: *const u8) c_int; |
| 2877 | 2897 | \\pub extern fn puts(s: *const u8) align(2 + 2) c_int; |
| 2878 | 2898 | \\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int; |
| 2879 | 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 | 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 | 83 | } |
| 84 | 84 | } |
| 85 | 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 | 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 | 103 | assert(datas[decl].rhs != 0); |
| 94 | 104 | try renderExpression(gpa, ais, tree, fn_proto, .space); |
| 95 | 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 | 1256 | const token_tags = tree.tokens.items(.tag); |
| 1247 | 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 | 1259 | const after_fn_token = fn_proto.ast.fn_token + 1; |
| 1253 | 1260 | const lparen = if (token_tags[after_fn_token] == .identifier) blk: { |
| 1254 | 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 | 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 | 1437 | const callconv_lparen = tree.firstToken(fn_proto.ast.callconv_expr) - 1; |
| 1429 | 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 | 1441 | try renderToken(ais, tree, callconv_lparen, .none); // ( |
| 1433 | 1442 | try renderExpression(gpa, ais, tree, fn_proto.ast.callconv_expr, .none); |
| 1434 | 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 | 1446 | if (token_tags[maybe_bang] == .bang) { |