From 95ef0357da3b8636930d405c907db4262e9c41db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Tue, 7 Feb 2023 22:25:23 +0000 Subject: [PATCH] support inline and non-spaced comments in assembly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Go contains such inline comments, like: crypto/aes/gcm_ppc64x.s:129: VPMSUMD IN, HL, XL // H.lo·H.lo We didn't notice since these comments were somewhat rare. While here, "//" does not need to be followed by a space. The code turns out to be pretty easy with strings.Cut. Fixes #672. --- main.go | 11 ++++++----- testdata/script/asm.txtar | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index ba21beb..f56cec6 100644 --- a/main.go +++ b/main.go @@ -698,14 +698,15 @@ func transformAsm(args []string) ([]string, error) { } // Leave "//" comments unchanged; they might be directives. - if strings.HasPrefix(strings.TrimSpace(line), "// ") { - buf.WriteString(line) - buf.WriteByte('\n') - continue - } + line, comment, hasComment := strings.Cut(line, "//") // Anything else is regular assembly; replace the names. replaceAsmNames(&buf, []byte(line)) + + if hasComment { + buf.WriteString("//") + buf.WriteString(comment) + } buf.WriteByte('\n') } if err := scanner.Err(); err != nil { diff --git a/testdata/script/asm.txtar b/testdata/script/asm.txtar index fd0bd7e..0f7380e 100644 --- a/testdata/script/asm.txtar +++ b/testdata/script/asm.txtar @@ -68,8 +68,9 @@ func main() { #include "extra/garble_define2_amd64.h" // A comment may include many·special∕asm·runes and it's okay. -// Or the same with leading whitespace: - // A comment may include many·special∕asm·runes and it's okay. +//No space: many·special∕asm·runes. + // Indented: many·special∕asm·runes. +DATA bar+0(SB)/8, $0 // Inline: many·special∕asm·runes. // Reference an imported package by its package path. TEXT ·addJmpPkgPath(SB),$0-16