Skip to content

Commit 95e5642

Browse files
fix: preserve block comments as single nodes for gogen v1.20.2 compatibility
The gogen v1.20.2 update exposed an issue where multi-line C block comments (/* ... */) were being incorrectly split into multiple Go AST Comment nodes. This created invalid Go syntax because each Comment node in Go's AST must be self-contained (either a complete // line or a complete /* */ block). Fixed ParseComment() to: - Keep /* */ block comments as a single Comment node - Preserve existing behavior for // line comments Fixes test failures in: - TestFromTestdata/gettext - TestFromTestdata/gpgerror - TestFromTestdata/issue507 - TestFromTestdata/keepcomment Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
1 parent 5bf65f1 commit 95e5642

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

_xtool/internal/parser/parser.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,28 @@ func (ct *Converter) ParseCommentGroup(cursor clang.Cursor) (comentGroup *ast.Co
193193
}
194194

195195
func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup {
196-
lines := strings.Split(rawComment, "\n")
197196
commentGroup := &ast.CommentGroup{}
197+
198+
// Check if this is a block comment (/* ... */)
199+
trimmed := strings.TrimSpace(rawComment)
200+
if strings.HasPrefix(trimmed, "/*") && strings.HasSuffix(trimmed, "*/") {
201+
// For block comments, keep the entire comment as a single node
202+
// Remove the trailing newline if present to match Go's Comment convention
203+
text := rawComment
204+
if strings.HasSuffix(text, "\n") {
205+
text = text[:len(text)-1]
206+
}
207+
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: text})
208+
return commentGroup
209+
}
210+
211+
// For line comments (// ...) or other formats, split by line
212+
lines := strings.Split(rawComment, "\n")
198213
for _, line := range lines {
199-
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line + "\n"})
214+
if line != "" || len(lines) == 1 {
215+
// Preserve empty lines only if it's a single line
216+
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line + "\n"})
217+
}
200218
}
201219
return commentGroup
202220
}

0 commit comments

Comments
 (0)