Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions _xtool/internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,34 @@ func createLoc(cursor clang.Cursor) *ast.Location {
//
// Note: In cases where both documentation comments and line comments conceptually exist,
// only the line comment will be preserved.
func (ct *Converter) ParseCommentGroup(cursor clang.Cursor) (comentGroup *ast.CommentGroup, isDoc bool) {
func (ct *Converter) ParseCommentGroup(cursor clang.Cursor) (*ast.CommentGroup, bool) {
rawComment := toStr(cursor.RawCommentText())
commentGroup := &ast.CommentGroup{}
if rawComment != "" {
commentRange := cursor.CommentRange()
cursorRange := cursor.Extent()
isDoc := getOffset(commentRange.RangeStart()) < getOffset(cursorRange.RangeStart())
commentGroup = ct.ParseComment(rawComment)
if len(commentGroup.List) > 0 {
return commentGroup, isDoc
}
if rawComment == "" {
return nil, false
}
commentRange := cursor.CommentRange()
cursorRange := cursor.Extent()
isDoc := getOffset(commentRange.RangeStart()) < getOffset(cursorRange.RangeStart())
commentGroup := ct.ParseComment(rawComment)
if len(commentGroup.List) > 0 {
return commentGroup, isDoc
}
return nil, false
}

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 strings.HasPrefix(rawComment, "/*") {
// Block comments (/* ... */) are kept as a single ast.Comment node.
text := strings.TrimRight(rawComment, "\n")
commentGroup.List = []*ast.Comment{{Text: text}}
} else {
// Line comments (// ...) are split into one node per line.
for _, line := range strings.Split(rawComment, "\n") {
if line != "" {
commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line})
}
}
}
return commentGroup
}
Expand Down
82 changes: 19 additions & 63 deletions _xtool/internal/parser/testdata/comment/expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Doc": {
"List": [
{
"Text": "// not read doc 1\n",
"Text": "// not read doc 1",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -47,7 +47,7 @@
"Doc": {
"List": [
{
"Text": "/* not read doc 2 */\n",
"Text": "/* not read doc 2 */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -89,7 +89,7 @@
"Doc": {
"List": [
{
"Text": "/// doc\n",
"Text": "/// doc",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -131,7 +131,7 @@
"Doc": {
"List": [
{
"Text": "/** doc */\n",
"Text": "/** doc */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -173,7 +173,7 @@
"Doc": {
"List": [
{
"Text": "/*! doc */\n",
"Text": "/*! doc */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -215,11 +215,11 @@
"Doc": {
"List": [
{
"Text": "/// doc 1\n",
"Text": "/// doc 1",
"_Type": "Comment"
},
{
"Text": "/// doc 2\n",
"Text": "/// doc 2",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -261,11 +261,7 @@
"Doc": {
"List": [
{
"Text": "/*! doc 1 */\n",
"_Type": "Comment"
},
{
"Text": "/*! doc 2 */\n",
"Text": "/*! doc 1 */\n/*! doc 2 */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -307,11 +303,7 @@
"Doc": {
"List": [
{
"Text": "/** doc 1 */\n",
"_Type": "Comment"
},
{
"Text": "/** doc 1 */\n",
"Text": "/** doc 1 */\n/** doc 1 */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -353,19 +345,7 @@
"Doc": {
"List": [
{
"Text": "/**\n",
"_Type": "Comment"
},
{
"Text": " * doc 1\n",
"_Type": "Comment"
},
{
"Text": " * doc 2\n",
"_Type": "Comment"
},
{
"Text": " */\n",
"Text": "/**\n * doc 1\n * doc 2\n */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -423,7 +403,7 @@
"Doc": {
"List": [
{
"Text": "/// doc\n",
"Text": "/// doc",
"_Type": "Comment"
}
],
Expand All @@ -448,7 +428,7 @@
"Comment": {
"List": [
{
"Text": "///\u003c comment\n",
"Text": "///\u003c comment",
"_Type": "Comment"
}
],
Expand All @@ -474,7 +454,7 @@
"Comment": {
"List": [
{
"Text": "/*!\u003c comment */\n",
"Text": "/*!\u003c comment */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -524,15 +504,7 @@
"Doc": {
"List": [
{
"Text": "/**\n",
"_Type": "Comment"
},
{
"Text": " * static field doc\n",
"_Type": "Comment"
},
{
"Text": " */\n",
"Text": "/**\n * static field doc\n */",
"_Type": "Comment"
}
],
Expand All @@ -557,7 +529,7 @@
"Comment": {
"List": [
{
"Text": "/*!\u003c static field comment */\n",
"Text": "/*!\u003c static field comment */",
"_Type": "Comment"
}
],
Expand All @@ -584,15 +556,7 @@
"Doc": {
"List": [
{
"Text": "/**\n",
"_Type": "Comment"
},
{
"Text": " * field doc\n",
"_Type": "Comment"
},
{
"Text": " */\n",
"Text": "/**\n * field doc\n */",
"_Type": "Comment"
}
],
Expand All @@ -617,7 +581,7 @@
"Comment": {
"List": [
{
"Text": "///\u003c field comment\n",
"Text": "///\u003c field comment",
"_Type": "Comment"
}
],
Expand All @@ -643,7 +607,7 @@
"Comment": {
"List": [
{
"Text": "/*!\u003c protected field comment */\n",
"Text": "/*!\u003c protected field comment */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -672,15 +636,7 @@
"Doc": {
"List": [
{
"Text": "/**\n",
"_Type": "Comment"
},
{
"Text": " * method doc\n",
"_Type": "Comment"
},
{
"Text": " */\n",
"Text": "/**\n * method doc\n */",
"_Type": "Comment"
}
],
Expand Down
8 changes: 4 additions & 4 deletions _xtool/internal/parser/testdata/forwarddecl1/expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@
"Comment": {
"List": [
{
"Text": "/* Methods for an open file */\n",
"Text": "/* Methods for an open file */",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -663,11 +663,11 @@
"Doc": {
"List": [
{
"Text": "// char in ast will got unsigned char \u0026 signed char and they are same in go\n",
"Text": "// char in ast will got unsigned char \u0026 signed char and they are same in go",
"_Type": "Comment"
},
{
"Text": " // but in ast,will have different,but with compare test,we need avoid these senario\n",
"Text": " // but in ast,will have different,but with compare test,we need avoid these senario",
"_Type": "Comment"
}
],
Expand Down Expand Up @@ -700,7 +700,7 @@
"Comment": {
"List": [
{
"Text": "/* active function */\n",
"Text": "/* active function */",
"_Type": "Comment"
}
],
Expand Down
2 changes: 1 addition & 1 deletion _xtool/internal/parser/testdata/typeof/expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
],
Expand Down
2 changes: 1 addition & 1 deletion cl/internal/convert/_testdata/gettext/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type MessageIteratorType struct {
type MessageIteratorT *MessageIteratorType

/* Create an iterator for traversing a domain of a PO file in memory.
The domain NULL denotes the default domain. */
The domain NULL denotes the default domain. */ //
//go:linkname MessageIterator C.po_message_iterator
func MessageIterator(file FileT, domain *c.Char) MessageIteratorT

Expand Down
8 changes: 4 additions & 4 deletions cl/internal/convert/_testdata/gpgerror/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ErrorT c.Uint

/* Return a pointer to a string containing a description of the error
* code in the error value ERR. This function is not thread-safe. */
* code in the error value ERR. This function is not thread-safe. */ //
// llgo:link ErrorT.Strerror C.gpg_strerror
func (recv_ ErrorT) Strerror() *c.Char {
return nil
Expand All @@ -21,14 +21,14 @@ func (recv_ ErrorT) Strerror() *c.Char {
* the system. If the function succeeds, 0 is returned and BUF
* contains the string describing the error. If the buffer was not
* large enough, ERANGE is returned and BUF contains as much of the
* beginning of the error string as fits into the buffer. */
* beginning of the error string as fits into the buffer. */ //
// llgo:link ErrorT.StrerrorR C.gpg_strerror_r
func (recv_ ErrorT) StrerrorR(buf *c.Char, buflen c.SizeT) c.Int {
return 0
}

/* Return a pointer to a string containing a description of the error
* source in the error value ERR. */
* source in the error value ERR. */ //
// llgo:link ErrorT.Strsource C.gpg_strsource
func (recv_ ErrorT) Strsource() *c.Char {
return nil
Expand All @@ -51,7 +51,7 @@ type GpgrtLockT struct {
}

/* NB: If GPGRT_LOCK_DEFINE is not used, zero out the lock variable
before passing it to gpgrt_lock_init. */
before passing it to gpgrt_lock_init. */ //
// llgo:link (*GpgrtLockT).LockInit C.gpgrt_lock_init
func (recv_ *GpgrtLockT) LockInit() CodeT {
return 0
Expand Down
20 changes: 5 additions & 15 deletions cl/internal/convert/_testdata/issue507/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,26 @@ type Ip4Addr struct {
}
type Ip4AddrT Ip4Addr

/** Args to LWIP_NSC_LINK_CHANGED callback */

type LinkChangedS struct {
/** Args to LWIP_NSC_LINK_CHANGED callback */ type LinkChangedS struct {
State c.Int
}

/** Args to LWIP_NSC_STATUS_CHANGED callback */

type StatusChangedS struct {
/** Args to LWIP_NSC_STATUS_CHANGED callback */ type StatusChangedS struct {
State c.Int
}

/** Args to LWIP_NSC_IPV4_ADDRESS_CHANGED|LWIP_NSC_IPV4_GATEWAY_CHANGED|LWIP_NSC_IPV4_NETMASK_CHANGED|LWIP_NSC_IPV4_SETTINGS_CHANGED callback */

type Ipv4ChangedS struct {
/** Args to LWIP_NSC_IPV4_ADDRESS_CHANGED|LWIP_NSC_IPV4_GATEWAY_CHANGED|LWIP_NSC_IPV4_NETMASK_CHANGED|LWIP_NSC_IPV4_SETTINGS_CHANGED callback */ type Ipv4ChangedS struct {
OldAddress *IpAddrT
OldNetmask *IpAddrT
OldGw *IpAddrT
}

/** Args to LWIP_NSC_IPV6_SET callback */

type Ipv6SetS struct {
/** Args to LWIP_NSC_IPV6_SET callback */ type Ipv6SetS struct {
AddrIndex c.Int
OldAddress *IpAddrT
}

/** Args to LWIP_NSC_IPV6_ADDR_STATE_CHANGED callback */

type Ipv6AddrStateChangedS struct {
/** Args to LWIP_NSC_IPV6_ADDR_STATE_CHANGED callback */ type Ipv6AddrStateChangedS struct {
AddrIndex c.Int
OldState c.Int
Address *IpAddrT
Expand Down
Loading
Loading