| author | |
| committer | |
| log | e863204c47eb89bfefd762739943f6b2f99f2c16 |
| tree | f8f4cbac0e2687f2e8dca5e22d9d53309f59e1d1 |
| parent | 70a9a3a562582173c140a064762b674e3c761a41 |
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 { | ... | @@ -1979,20 +1979,26 @@ pub const Tree = struct { |
| 1979 | // asm ("foo" :: [_] "" (y) : "a", "b"); | 1979 | // asm ("foo" :: [_] "" (y) : "a", "b"); |
| 1980 | const last_input = result.inputs[result.inputs.len - 1]; | 1980 | const last_input = result.inputs[result.inputs.len - 1]; |
| 1981 | const rparen = tree.lastToken(last_input); | 1981 | const rparen = tree.lastToken(last_input); |
| 1982 | if (token_tags[rparen + 1] == .colon and | 1982 | var i = rparen + 1; |
| 1983 | token_tags[rparen + 2] == .string_literal) | 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) | ||
| 1984 | { | 1987 | { |
| 1985 | result.first_clobber = rparen + 2; | 1988 | result.first_clobber = i + 1; |
| 1986 | } | 1989 | } |
| 1987 | } else { | 1990 | } else { |
| 1988 | // asm ("foo" : [_] "" (x) :: "a", "b"); | 1991 | // asm ("foo" : [_] "" (x) :: "a", "b"); |
| 1989 | const last_output = result.outputs[result.outputs.len - 1]; | 1992 | const last_output = result.outputs[result.outputs.len - 1]; |
| 1990 | const rparen = tree.lastToken(last_output); | 1993 | const rparen = tree.lastToken(last_output); |
| 1991 | if (token_tags[rparen + 1] == .colon and | 1994 | var i = rparen + 1; |
| 1992 | token_tags[rparen + 2] == .colon and | 1995 | // Allow a (useless) comma right after the closing parenthesis. |
| 1993 | token_tags[rparen + 3] == .string_literal) | 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) | ||
| 1994 | { | 2000 | { |
| 1995 | result.first_clobber = rparen + 3; | 2001 | result.first_clobber = i + 2; |
| 1996 | } | 2002 | } |
| 1997 | } | 2003 | } |
| 1998 | 2004 |
lib/std/zig/parser_test.zig+32| ... | @@ -4,6 +4,38 @@ | ... | @@ -4,6 +4,38 @@ |
| 4 | // The MIT license requires this copyright notice to be included in all copies | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | // and substantial portions of the software. | 5 | // and substantial portions of the software. |
| 6 | 6 | ||
| 7 | test "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 | |||
| 7 | test "zig fmt: respect line breaks in struct field value declaration" { | 39 | test "zig fmt: respect line breaks in struct field value declaration" { |
| 8 | try testCanonical( | 40 | try testCanonical( |
| 9 | \\const Foo = struct { | 41 | \\const Foo = struct { |