Skip to content

Commit

Permalink
zig fmt: fix extra whitespace with multiline strings
Browse files Browse the repository at this point in the history
Fixes #13937
  • Loading branch information
yujiri8 committed Dec 16, 2022
1 parent 8da9cc8 commit 68d2f68
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 72 deletions.
29 changes: 29 additions & 0 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5500,6 +5500,35 @@ test "zig fmt: canonicalize symbols (keywords)" {
);
}

test "zig fmt: no space before newline before multiline string" {
try testCanonical(
\\const S = struct {
\\ text: []const u8,
\\ comment: []const u8,
\\};
\\
\\test {
\\ const s1 = .{
\\ .text =
\\ \\hello
\\ \\world
\\ ,
\\ .comment = "test",
\\ };
\\ _ = s1;
\\ const s2 = .{
\\ .comment = "test",
\\ .text =
\\ \\hello
\\ \\world
\\ ,
\\ };
\\ _ = s2;
\\}
\\
);
}

// Normalize \xNN and \u{NN} escapes and unicode inside @"" escapes.
test "zig fmt: canonicalize symbols (character escapes)" {
try testTransform(
Expand Down
10 changes: 8 additions & 2 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1675,15 +1675,21 @@ fn renderStructInit(

try renderToken(ais, tree, struct_init.ast.lbrace + 1, .none); // .
try renderIdentifier(ais, tree, struct_init.ast.lbrace + 2, .space, .eagerly_unquote); // name
try renderToken(ais, tree, struct_init.ast.lbrace + 3, .space); // =
// Don't output a space after the = if expression is a multiline string,
// since then it will start on the next line.
const nodes = tree.nodes.items(.tag);
const expr = nodes[struct_init.ast.fields[0]];
var space_after_equal: Space = if (expr == .multiline_string_literal) .none else .space;
try renderToken(ais, tree, struct_init.ast.lbrace + 3, space_after_equal); // =
try renderExpression(gpa, ais, tree, struct_init.ast.fields[0], .comma);

for (struct_init.ast.fields[1..]) |field_init| {
const init_token = tree.firstToken(field_init);
try renderExtraNewlineToken(ais, tree, init_token - 3);
try renderToken(ais, tree, init_token - 3, .none); // .
try renderIdentifier(ais, tree, init_token - 2, .space, .eagerly_unquote); // name
try renderToken(ais, tree, init_token - 1, .space); // =
space_after_equal = if (nodes[field_init] == .multiline_string_literal) .none else .space;
try renderToken(ais, tree, init_token - 1, space_after_equal); // =
try renderExpression(gpa, ais, tree, field_init, .comma);
}

Expand Down
6 changes: 3 additions & 3 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9948,23 +9948,23 @@ pub const FuncGen = struct {
constraints: [:0]const u8,
} = switch (target.cpu.arch) {
.x86 => .{
.template =
.template =
\\roll $$3, %edi ; roll $$13, %edi
\\roll $$61, %edi ; roll $$51, %edi
\\xchgl %ebx,%ebx
,
.constraints = "={edx},{eax},0,~{cc},~{memory}",
},
.x86_64 => .{
.template =
.template =
\\rolq $$3, %rdi ; rolq $$13, %rdi
\\rolq $$61, %rdi ; rolq $$51, %rdi
\\xchgq %rbx,%rbx
,
.constraints = "={rdx},{rax},0,~{cc},~{memory}",
},
.aarch64, .aarch64_32, .aarch64_be => .{
.template =
.template =
\\ror x12, x12, #3 ; ror x12, x12, #13
\\ror x12, x12, #51 ; ror x12, x12, #61
\\orr x10, x10, x10
Expand Down
Loading

0 comments on commit 68d2f68

Please sign in to comment.