authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-05-04 19:29:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-05 03:16:07-04:00
loge863204c47eb89bfefd762739943f6b2f99f2c16
treef8f4cbac0e2687f2e8dca5e22d9d53309f59e1d1
parent70a9a3a562582173c140a064762b674e3c761a41

zig fmt: Fix edge case in inline asm parsing

The presence of a trailing comma in the single and only input/output declaration confused the parser and made zig fmt discard any element placed after the comma.

2 files changed, 45 insertions(+), 7 deletions(-)

lib/std/zig/ast.zig+13-7
......@@ -1979,20 +1979,26 @@ pub const Tree = struct {
19791979 // asm ("foo" :: [_] "" (y) : "a", "b");
19801980 const last_input = result.inputs[result.inputs.len - 1];
19811981 const rparen = tree.lastToken(last_input);
1982 if (token_tags[rparen + 1] == .colon and
1983 token_tags[rparen + 2] == .string_literal)
1982 var i = rparen + 1;
1983 // Allow a (useless) comma right after the closing parenthesis.
1984 if (token_tags[i] == .comma) i += 1;
1985 if (token_tags[i] == .colon and
1986 token_tags[i + 1] == .string_literal)
19841987 {
1985 result.first_clobber = rparen + 2;
1988 result.first_clobber = i + 1;
19861989 }
19871990 } else {
19881991 // asm ("foo" : [_] "" (x) :: "a", "b");
19891992 const last_output = result.outputs[result.outputs.len - 1];
19901993 const rparen = tree.lastToken(last_output);
1991 if (token_tags[rparen + 1] == .colon and
1992 token_tags[rparen + 2] == .colon and
1993 token_tags[rparen + 3] == .string_literal)
1994 var i = rparen + 1;
1995 // Allow a (useless) comma right after the closing parenthesis.
1996 if (token_tags[i] == .comma) i += 1;
1997 if (token_tags[i] == .colon and
1998 token_tags[i + 1] == .colon and
1999 token_tags[i + 2] == .string_literal)
19942000 {
1995 result.first_clobber = rparen + 3;
2001 result.first_clobber = i + 2;
19962002 }
19972003 }
19982004
lib/std/zig/parser_test.zig+32
......@@ -4,6 +4,38 @@
44// The MIT license requires this copyright notice to be included in all copies
55// and substantial portions of the software.
66
7test "zig fmt: preserves clobbers in inline asm with stray comma" {
8 try testTransform(
9 \\fn foo() void {
10 \\ asm volatile (""
11 \\ : [_] "" (-> type),
12 \\ :
13 \\ : "clobber"
14 \\ );
15 \\ asm volatile (""
16 \\ :
17 \\ : [_] "" (type),
18 \\ : "clobber"
19 \\ );
20 \\}
21 \\
22 ,
23 \\fn foo() void {
24 \\ asm volatile (""
25 \\ : [_] "" (-> type)
26 \\ :
27 \\ : "clobber"
28 \\ );
29 \\ asm volatile (""
30 \\ :
31 \\ : [_] "" (type)
32 \\ : "clobber"
33 \\ );
34 \\}
35 \\
36 );
37}
38
739test "zig fmt: respect line breaks in struct field value declaration" {
840 try testCanonical(
941 \\const Foo = struct {