Skip to content

Commit f26d750

Browse files
fix: correct comment parsing for block comments and update gogen to v1.20.2
This fixes issue #618 by properly handling multi-line C block comments according to Go's ast.Comment specification. Changes: - Fixed ParseComment() in _xtool/internal/parser/parser.go to keep block comments (/* ... */) as single ast.Comment nodes instead of splitting them by line - Line comments (//) continue to be split per line as expected - Updated gogen dependency from v1.19.7 to v1.20.2 The bug was exposed when gogen v1.20.2 became more spec-compliant with Go's standard ast printer behavior. The fix ensures llcppg generates valid Go code that compiles correctly with the updated gogen version. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <[email protected]>
1 parent 46992ed commit f26d750

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

_xtool/internal/parser/parser.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +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{}
198-
for _, line := range lines {
199-
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line + "\n"})
197+
198+
// Block comments (/* ... */ or /** ... */) should be kept as a single Comment node.
199+
// Go's ast.Comment documentation states: "A Comment node represents a single //-style or /*-style comment."
200+
// This means the entire block comment must be ONE ast.Comment node, not split across multiple nodes.
201+
if strings.HasPrefix(rawComment, "/*") {
202+
// Ensure the comment ends with newline for proper formatting
203+
text := rawComment
204+
if !strings.HasSuffix(text, "\n") {
205+
text += "\n"
206+
}
207+
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: text})
208+
} else {
209+
// Line comments (// or ///) - each line is a separate Comment node
210+
lines := strings.Split(rawComment, "\n")
211+
for _, line := range lines {
212+
if line != "" {
213+
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line + "\n"})
214+
}
215+
}
200216
}
217+
201218
return commentGroup
202219
}
203220

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/goplus/llcppg
33
go 1.23.0
44

55
require (
6-
github.com/goplus/gogen v1.19.7
6+
github.com/goplus/gogen v1.20.2
77
github.com/goplus/lib v0.3.1
88
github.com/goplus/llgo v0.12.0
99
github.com/goplus/mod v0.19.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/goplus/gogen v1.19.7 h1:0i30on0GwtYIJ+D9/I5QujswBU+mnnmNewoRk/uRVkw=
2-
github.com/goplus/gogen v1.19.7/go.mod h1:owX2e1EyU5WD+Nm6oH2m/GXjLdlBYcwkLO4wN8HHXZI=
1+
github.com/goplus/gogen v1.20.2 h1:c9wm7NzjWSrncbtH+lz4jM2j31p+6JTji8cjF1K79qg=
2+
github.com/goplus/gogen v1.20.2/go.mod h1:87ZJD1mdljXx2pkvBrMGynGUz6m08brj9p6xhb5aq2Y=
33
github.com/goplus/lib v0.3.1 h1:Xws4DBVvgOMu58awqB972wtvTacDbk3nqcbHjdx9KSg=
44
github.com/goplus/lib v0.3.1/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
55
github.com/goplus/llgo v0.12.0 h1:UhbmwR+9fSy1y944rp6fPkSP39n4YhH4TpAN2QJ15ns=

0 commit comments

Comments
 (0)