zig fmt: replace callconv(.Inline) with the inline keyword

This commit is contained in:
Isaac Freund 2021-05-20 01:31:37 +02:00
parent 3fd8ac092e
commit 4a582734fd
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
3 changed files with 47 additions and 19 deletions

View File

@ -459,7 +459,8 @@ pub const Tree = struct {
.keyword_extern,
.keyword_export,
.keyword_pub,
.keyword_threadlocal,
.keyword_inline,
.keyword_noinline,
.string_literal,
=> continue,

View File

@ -61,13 +61,33 @@ test "zig fmt: respect line breaks in struct field value declaration" {
);
}
// TODO Remove this after zig 0.9.0 is released.
test "zig fmt: rewrite inline functions as callconv(.Inline)" {
try testTransform(
test "zig fmt: respect line breaks before functions" {
try testCanonical(
\\const std = @import("std");
\\
\\inline fn foo() void {}
\\
,
\\noinline fn foo() void {}
\\
\\export fn foo() void {}
\\
\\extern fn foo() void;
\\
\\extern "foo" fn foo() void;
\\
);
}
test "zig fmt: rewrite callconv(.Inline) to the inline keyword" {
try testTransform(
\\fn foo() callconv(.Inline) void {}
\\const bar = .Inline;
\\fn foo() callconv(bar) void {}
\\
,
\\inline fn foo() void {}
\\const bar = .Inline;
\\fn foo() callconv(bar) void {}
\\
);
}
@ -2867,17 +2887,17 @@ test "zig fmt: functions" {
\\extern fn puts(s: *const u8) c_int;
\\extern "c" fn puts(s: *const u8) c_int;
\\export fn puts(s: *const u8) c_int;
\\fn puts(s: *const u8) callconv(.Inline) c_int;
\\inline fn puts(s: *const u8) c_int;
\\noinline fn puts(s: *const u8) c_int;
\\pub extern fn puts(s: *const u8) c_int;
\\pub extern "c" fn puts(s: *const u8) c_int;
\\pub export fn puts(s: *const u8) c_int;
\\pub fn puts(s: *const u8) callconv(.Inline) c_int;
\\pub inline fn puts(s: *const u8) c_int;
\\pub noinline fn puts(s: *const u8) c_int;
\\pub extern fn puts(s: *const u8) align(2 + 2) c_int;
\\pub extern "c" fn puts(s: *const u8) align(2 + 2) c_int;
\\pub export fn puts(s: *const u8) align(2 + 2) c_int;
\\pub fn puts(s: *const u8) align(2 + 2) callconv(.Inline) c_int;
\\pub inline fn puts(s: *const u8) align(2 + 2) c_int;
\\pub noinline fn puts(s: *const u8) align(2 + 2) c_int;
\\
);

View File

@ -83,13 +83,23 @@ fn renderMember(gpa: *Allocator, ais: *Ais, tree: ast.Tree, decl: ast.Node.Index
}
}
while (i < fn_token) : (i += 1) {
if (token_tags[i] == .keyword_inline) {
// TODO remove this special case when 0.9.0 is released.
// See the commit that introduced this comment for more details.
continue;
}
try renderToken(ais, tree, i, .space);
}
switch (tree.nodes.items(.tag)[fn_proto]) {
.fn_proto_one, .fn_proto => {
const callconv_expr = if (tree.nodes.items(.tag)[fn_proto] == .fn_proto_one)
tree.extraData(datas[fn_proto].lhs, ast.Node.FnProtoOne).callconv_expr
else
tree.extraData(datas[fn_proto].lhs, ast.Node.FnProto).callconv_expr;
if (callconv_expr != 0 and tree.nodes.items(.tag)[callconv_expr] == .enum_literal) {
if (mem.eql(u8, "Inline", tree.tokenSlice(main_tokens[callconv_expr]))) {
try ais.writer().writeAll("inline ");
}
}
},
.fn_proto_simple, .fn_proto_multi => {},
else => unreachable,
}
assert(datas[decl].rhs != 0);
try renderExpression(gpa, ais, tree, fn_proto, .space);
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.
const token_tags = tree.tokens.items(.tag);
const token_starts = tree.tokens.items(.start);
const is_inline = fn_proto.ast.fn_token > 0 and
token_tags[fn_proto.ast.fn_token - 1] == .keyword_inline;
const after_fn_token = fn_proto.ast.fn_token + 1;
const lparen = if (token_tags[after_fn_token] == .identifier) blk: {
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.
try renderToken(ais, tree, section_rparen, .space); // )
}
if (fn_proto.ast.callconv_expr != 0) {
if (fn_proto.ast.callconv_expr != 0 and
!mem.eql(u8, "Inline", tree.tokenSlice(tree.nodes.items(.main_token)[fn_proto.ast.callconv_expr])))
{
const callconv_lparen = tree.firstToken(fn_proto.ast.callconv_expr) - 1;
const callconv_rparen = tree.lastToken(fn_proto.ast.callconv_expr) + 1;
@ -1432,8 +1441,6 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
try renderToken(ais, tree, callconv_lparen, .none); // (
try renderExpression(gpa, ais, tree, fn_proto.ast.callconv_expr, .none);
try renderToken(ais, tree, callconv_rparen, .space); // )
} else if (is_inline) {
try ais.writer().writeAll("callconv(.Inline) ");
}
if (token_tags[maybe_bang] == .bang) {