diff --git a/format/expansion_test.go b/format/expansion_test.go index 6c6ffe3..5b4ee12 100644 --- a/format/expansion_test.go +++ b/format/expansion_test.go @@ -117,6 +117,16 @@ func TestFormatPreservesExpansionSemantics(t *testing.T) { "\tA: M\n" + "\tRET\n", }, + { + // A stacked label naming a macro: its expansion decides what + // the line becomes (an empty body makes it vanish), so it is + // never peeled onto a line of its own. + name: "stacked label naming a macro", + src: "#define M\n" + + "TEXT ·f(SB), $0\n" + + "\tA: M:\n" + + "\tRET\n", + }, { // A use above its define: the name is not a macro at that point // of the file, so splitting would be safe; the whole-file set diff --git a/format/format.go b/format/format.go index 4db8579..c6abfe2 100644 --- a/format/format.go +++ b/format/format.go @@ -70,10 +70,11 @@ func Source(src string) string { // instruction after the last one is rendered at the // function's alignment width, so its mnemonic counts here, // unless it names a macro and never reaches a line of its - // own. + // own. A stacked label naming a macro is never peeled + // either: its expansion decides what the line becomes. rest := line[2:] for len(rest) >= 2 && rest[0].Kind == token.Ident && rest[1].Kind == token.Colon && - !isDirective(rest[0].Text) { + !isDirective(rest[0].Text) && !macros[rest[0].Text] { rest = rest[2:] } if len(rest) > 0 && rest[0].Kind == token.Ident && !isDirective(rest[0].Text) && @@ -146,8 +147,10 @@ func Source(src string) string { // honest about what it is looking at. outs = append(outs, outLine{kind: kLabel, text: line[0].Text + ":"}) rest := line[2:] + // A stacked label naming a macro is never peeled: its expansion + // decides what the line becomes, exactly as the first label's. for len(rest) >= 2 && rest[0].Kind == token.Ident && rest[1].Kind == token.Colon && - !isDirective(rest[0].Text) { + !isDirective(rest[0].Text) && !macros[rest[0].Text] { outs = append(outs, outLine{kind: kLabel, text: rest[0].Text + ":"}) rest = rest[2:] } diff --git a/format/testdata/fuzz/FuzzFormatExpansionRoundTrip/4173fd7be1b3f1a3 b/format/testdata/fuzz/FuzzFormatExpansionRoundTrip/4173fd7be1b3f1a3 new file mode 100644 index 0000000..04c66d6 --- /dev/null +++ b/format/testdata/fuzz/FuzzFormatExpansionRoundTrip/4173fd7be1b3f1a3 @@ -0,0 +1,2 @@ +go test fuzz v1 +string("#define M \nA:M:")