From ec2424e0d1bfed86ac350281fe7c517c28aed8bc Mon Sep 17 00:00:00 2001 From: xgopilot Date: Wed, 11 Feb 2026 11:25:41 +0000 Subject: [PATCH 1/3] fix: remove trailing \n from ast.Comment.Text to fix go/printer compatibility Each ast.Comment node represents a single line of comment text and should not contain a trailing newline character. The go/printer in newer versions of gogen strictly follows the ast.Comment specification, causing invalid Go code when comments have trailing \n. Changes: - Remove `+ "\n"` from ParseComment in parser.go, skip empty lines - Add defensive strings.TrimRight in NewCommentGroupFromC - Update test expectation file Fixes #635 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com> --- _xtool/internal/parser/parser.go | 5 +- .../parser/testdata/comment/expect.json | 60 +++++++++---------- cl/internal/convert/comments.go | 2 +- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/_xtool/internal/parser/parser.go b/_xtool/internal/parser/parser.go index 86bdc60b3..a5a5436ae 100644 --- a/_xtool/internal/parser/parser.go +++ b/_xtool/internal/parser/parser.go @@ -196,7 +196,10 @@ func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { lines := strings.Split(rawComment, "\n") commentGroup := &ast.CommentGroup{} for _, line := range lines { - commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line + "\n"}) + if line == "" { + continue + } + commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line}) } return commentGroup } diff --git a/_xtool/internal/parser/testdata/comment/expect.json b/_xtool/internal/parser/testdata/comment/expect.json index f30194afd..75323f563 100755 --- a/_xtool/internal/parser/testdata/comment/expect.json +++ b/_xtool/internal/parser/testdata/comment/expect.json @@ -5,7 +5,7 @@ "Doc": { "List": [ { - "Text": "// not read doc 1\n", + "Text": "// not read doc 1", "_Type": "Comment" } ], @@ -47,7 +47,7 @@ "Doc": { "List": [ { - "Text": "/* not read doc 2 */\n", + "Text": "/* not read doc 2 */", "_Type": "Comment" } ], @@ -89,7 +89,7 @@ "Doc": { "List": [ { - "Text": "/// doc\n", + "Text": "/// doc", "_Type": "Comment" } ], @@ -131,7 +131,7 @@ "Doc": { "List": [ { - "Text": "/** doc */\n", + "Text": "/** doc */", "_Type": "Comment" } ], @@ -173,7 +173,7 @@ "Doc": { "List": [ { - "Text": "/*! doc */\n", + "Text": "/*! doc */", "_Type": "Comment" } ], @@ -215,11 +215,11 @@ "Doc": { "List": [ { - "Text": "/// doc 1\n", + "Text": "/// doc 1", "_Type": "Comment" }, { - "Text": "/// doc 2\n", + "Text": "/// doc 2", "_Type": "Comment" } ], @@ -261,11 +261,11 @@ "Doc": { "List": [ { - "Text": "/*! doc 1 */\n", + "Text": "/*! doc 1 */", "_Type": "Comment" }, { - "Text": "/*! doc 2 */\n", + "Text": "/*! doc 2 */", "_Type": "Comment" } ], @@ -307,11 +307,11 @@ "Doc": { "List": [ { - "Text": "/** doc 1 */\n", + "Text": "/** doc 1 */", "_Type": "Comment" }, { - "Text": "/** doc 1 */\n", + "Text": "/** doc 1 */", "_Type": "Comment" } ], @@ -353,19 +353,19 @@ "Doc": { "List": [ { - "Text": "/**\n", + "Text": "/**", "_Type": "Comment" }, { - "Text": " * doc 1\n", + "Text": " * doc 1", "_Type": "Comment" }, { - "Text": " * doc 2\n", + "Text": " * doc 2", "_Type": "Comment" }, { - "Text": " */\n", + "Text": " */", "_Type": "Comment" } ], @@ -423,7 +423,7 @@ "Doc": { "List": [ { - "Text": "/// doc\n", + "Text": "/// doc", "_Type": "Comment" } ], @@ -448,7 +448,7 @@ "Comment": { "List": [ { - "Text": "///\u003c comment\n", + "Text": "///\u003c comment", "_Type": "Comment" } ], @@ -474,7 +474,7 @@ "Comment": { "List": [ { - "Text": "/*!\u003c comment */\n", + "Text": "/*!\u003c comment */", "_Type": "Comment" } ], @@ -524,15 +524,15 @@ "Doc": { "List": [ { - "Text": "/**\n", + "Text": "/**", "_Type": "Comment" }, { - "Text": " * static field doc\n", + "Text": " * static field doc", "_Type": "Comment" }, { - "Text": " */\n", + "Text": " */", "_Type": "Comment" } ], @@ -557,7 +557,7 @@ "Comment": { "List": [ { - "Text": "/*!\u003c static field comment */\n", + "Text": "/*!\u003c static field comment */", "_Type": "Comment" } ], @@ -584,15 +584,15 @@ "Doc": { "List": [ { - "Text": "/**\n", + "Text": "/**", "_Type": "Comment" }, { - "Text": " * field doc\n", + "Text": " * field doc", "_Type": "Comment" }, { - "Text": " */\n", + "Text": " */", "_Type": "Comment" } ], @@ -617,7 +617,7 @@ "Comment": { "List": [ { - "Text": "///\u003c field comment\n", + "Text": "///\u003c field comment", "_Type": "Comment" } ], @@ -643,7 +643,7 @@ "Comment": { "List": [ { - "Text": "/*!\u003c protected field comment */\n", + "Text": "/*!\u003c protected field comment */", "_Type": "Comment" } ], @@ -672,15 +672,15 @@ "Doc": { "List": [ { - "Text": "/**\n", + "Text": "/**", "_Type": "Comment" }, { - "Text": " * method doc\n", + "Text": " * method doc", "_Type": "Comment" }, { - "Text": " */\n", + "Text": " */", "_Type": "Comment" } ], diff --git a/cl/internal/convert/comments.go b/cl/internal/convert/comments.go index 8a634bf7c..82a77fa2d 100644 --- a/cl/internal/convert/comments.go +++ b/cl/internal/convert/comments.go @@ -37,7 +37,7 @@ func NewCommentGroupFromC(doc *ast.CommentGroup) *goast.CommentGroup { for _, comment := range doc.List { goDoc.List = append(goDoc.List, &goast.Comment{ - Slash: token.NoPos, Text: comment.Text, + Slash: token.NoPos, Text: strings.TrimRight(comment.Text, "\n"), }, ) } From 5f8b74d5fcfece4d2804814360f57eabc3ead755 Mon Sep 17 00:00:00 2001 From: xgopilot Date: Wed, 11 Feb 2026 12:20:20 +0000 Subject: [PATCH 2/3] fix: remove redundant strings.TrimRight from NewCommentGroupFromC The trailing \n is already removed at the source in ParseComment, so the defensive TrimRight in comments.go is unnecessary. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com> --- cl/internal/convert/comments.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cl/internal/convert/comments.go b/cl/internal/convert/comments.go index 82a77fa2d..8a634bf7c 100644 --- a/cl/internal/convert/comments.go +++ b/cl/internal/convert/comments.go @@ -37,7 +37,7 @@ func NewCommentGroupFromC(doc *ast.CommentGroup) *goast.CommentGroup { for _, comment := range doc.List { goDoc.List = append(goDoc.List, &goast.Comment{ - Slash: token.NoPos, Text: strings.TrimRight(comment.Text, "\n"), + Slash: token.NoPos, Text: comment.Text, }, ) } From e2db36df7495c17adc8ce25c05e997db74d2eb0b Mon Sep 17 00:00:00 2001 From: xgopilot Date: Wed, 11 Feb 2026 12:34:41 +0000 Subject: [PATCH 3/3] fix: remove empty-line skip from ParseComment and update typeof test Remove the redundant `if line == "" { continue }` logic from ParseComment that is not currently needed, and update the typeof test expectation to remove trailing \n from ast.Comment.Text. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com> --- _xtool/internal/parser/parser.go | 3 --- _xtool/internal/parser/testdata/typeof/expect.json | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/_xtool/internal/parser/parser.go b/_xtool/internal/parser/parser.go index a5a5436ae..9ef9fb3a4 100644 --- a/_xtool/internal/parser/parser.go +++ b/_xtool/internal/parser/parser.go @@ -196,9 +196,6 @@ func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { lines := strings.Split(rawComment, "\n") commentGroup := &ast.CommentGroup{} for _, line := range lines { - if line == "" { - continue - } commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line}) } return commentGroup diff --git a/_xtool/internal/parser/testdata/typeof/expect.json b/_xtool/internal/parser/testdata/typeof/expect.json index 542ca7d97..00053e908 100755 --- a/_xtool/internal/parser/testdata/typeof/expect.json +++ b/_xtool/internal/parser/testdata/typeof/expect.json @@ -5,7 +5,7 @@ "Doc": { "List": [ { - "Text": "// https://github.com/goplus/llcppg/issues/497\n", + "Text": "// https://github.com/goplus/llcppg/issues/497", "_Type": "Comment" } ],