fix(format): preserve flag separators and normalise CRLF input

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-19 23:48:47 +02:00
parent 7aefe6a42d
commit 94e09e8070
2 changed files with 34 additions and 3 deletions
+2 -2
View File
@@ -312,7 +312,7 @@ func spaceBetween(prev, cur token.Token) bool {
return false
case token.Comma:
return false
case token.Star, token.Plus, token.Minus, token.Slash:
case token.Star, token.Plus, token.Minus, token.Slash, token.Pipe:
return false
case token.LShift, token.RShift, token.Arrow, token.At:
return false
@@ -328,7 +328,7 @@ func spaceBetween(prev, cur token.Token) bool {
}
}
switch prev.Kind {
case token.LParen, token.Star, token.Plus, token.Minus, token.Slash:
case token.LParen, token.Star, token.Plus, token.Minus, token.Slash, token.Pipe:
return false
case token.Dollar:
return false
+32 -1
View File
@@ -85,7 +85,7 @@ func TestBlankLines(t *testing.T) {
"TEXT ·f(SB), NOSPLIT, $0\n" +
"first:\n" + // first label: no blank after TEXT
"XORQ AX, AX\n" +
"JMP next\n" + // unlabeled glue: fmt inserts a blank before next:
"JMP next\n" + // unlabelled glue: fmt inserts a blank before next:
"next:\n" +
"stacked:\n" + // stacked labels share an address: no blank between
"INCQ AX\n" +
@@ -143,6 +143,7 @@ func TestOperandSpacing(t *testing.T) {
"swin_base+0(FP)": "swin_base+0(FP)",
"mask24<>(SB)": "mask24<>(SB)",
"·idx16+0(SB)/4": "·idx16+0(SB)/4",
"NOSPLIT|DUPOK": "NOSPLIT|DUPOK",
}
for in, want := range cases {
toks := lexOperands(in)
@@ -152,6 +153,36 @@ func TestOperandSpacing(t *testing.T) {
}
}
// TestFlagListRoundTrip pins the '|' flag separator and the <ABIInternal>
// marker through a full format pass: the bars the Go toolchain requires and
// the ABI bracket must survive byte for byte, on TEXT and GLOBL alike.
func TestFlagListRoundTrip(t *testing.T) {
for _, in := range []string{
"TEXT ·f(SB), NOSPLIT|NOFRAME|DUPOK, $0\n\tRET\n",
"TEXT ·foo<ABIInternal>(SB), NOSPLIT, $-0-24\n\tRET\n",
"GLOBL ·mask(SB), RODATA|NOPTR, $8\n",
} {
if got := Source(in); got != in {
t.Fatalf("flag list did not round-trip:\n--- got ---\n%q\n--- want ---\n%q", got, in)
}
}
}
// TestCRLFInputIsNormalisedToLF checks that a CRLF file comes out with
// uniform LF endings: a // comment must not carry its line's trailing \r
// into the output.
func TestCRLFInputIsNormalisedToLF(t *testing.T) {
in := "// func f()\r\nTEXT ·f(SB), NOSPLIT, $0\r\nRET\r\n"
want := "// func f()\nTEXT ·f(SB), NOSPLIT, $0\n\tRET\n"
got := Source(in)
if got != want {
t.Fatalf("CRLF formatting mismatch:\n--- got ---\n%q\n--- want ---\n%q", got, want)
}
if strings.Contains(got, "\r") {
t.Fatalf("output still contains CR: %q", got)
}
}
// lexOperands lexes a single operand string and drops the EOF token.
func lexOperands(s string) []token.Token {
toks := lexer.Tokenize(s)