llcppsigfetch:Decl Comment

This commit is contained in:
luoliwoshang
2024-08-15 16:08:50 +08:00
parent 2c8a9d1160
commit 762ed994c1
3 changed files with 394 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"unsafe"
"github.com/goplus/llgo/c"
@@ -126,12 +127,30 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
}
func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase {
rawComment := cursor.RawCommentText()
defer rawComment.Dispose()
commentGroup := &ast.CommentGroup{}
if rawComment.CStr() != nil {
commentGroup = ct.ParseComment(c.GoString(rawComment.CStr()))
}
return ast.DeclBase{
Loc: &ct.curLoc,
Parent: ct.GetCurScope(),
Doc: commentGroup,
}
}
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})
}
return commentGroup
}
// visit top decls (struct,class,function,enum & marco,include)
func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
ct := (*Converter)(clientData)

View File

@@ -8,6 +8,7 @@ import (
func main() {
TestFuncDecl()
TestScope()
TestComment()
}
func TestFuncDecl() {
@@ -72,3 +73,48 @@ func TestScope() {
converter.Dispose()
}
}
func TestComment() {
testCases := []string{
`// not read comment 1
void foo();`,
`/* not read comment 2 */
void foo();`,
`/// comment
void foo();`,
`/** comment */
void foo();`,
`/*! comment */
void foo();`,
`/// comment 1
/// comment 2
void foo();`,
`/*! comment 1 */
/*! comment 2 */
void foo();`,
`/** comment 1 */
/** comment 1 */
void foo();`,
`/**
* comment 1
* comment 2
*/
void foo();`,
}
for i, content := range testCases {
converter, err := parse.NewConverter(content, true)
if err != nil {
panic(err)
}
_, err = converter.Convert()
if err != nil {
panic(err)
}
json := converter.GetFilesJSON()
c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
converter.Dispose()
}
}

View File

@@ -7,7 +7,9 @@ TestFuncDecl Case 1:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -35,7 +37,9 @@ TestFuncDecl Case 2:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -73,7 +77,9 @@ TestFuncDecl Case 3:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -121,7 +127,9 @@ TestFuncDecl Case 4:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -171,7 +179,9 @@ TestFuncDecl Case 5:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -223,7 +233,9 @@ TestFuncDecl Case 6:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -279,7 +291,9 @@ TestFuncDecl Case 7:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -332,7 +346,9 @@ TestFuncDecl Case 8:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -384,7 +400,9 @@ TestScope Case 1:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
@@ -412,7 +430,9 @@ TestScope Case 2:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": {
"Name": "a"
},
@@ -442,7 +462,9 @@ TestScope Case 3:
"Loc": {
"File": "temp.h"
},
"Doc": null,
"Doc": {
"List": []
},
"Parent": {
"X": {
"Name": "b"
@@ -469,6 +491,302 @@ TestScope Case 3:
}
}
TestComment Case 1:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 2:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 3:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/// comment"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 4:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/** comment */"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 5:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/*! comment */"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 6:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/// comment 1"
}, {
"Text": "/// comment 2"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 7:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/*! comment 1 */"
}, {
"Text": "/*! comment 2 */"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 8:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/** comment 1 */"
}, {
"Text": "/** comment 1 */"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
TestComment Case 9:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": [{
"Text": "/**"
}, {
"Text": " * comment 1"
}, {
"Text": " * comment 2"
}, {
"Text": " */"
}]
},
"Parent": null,
"Name": {
"Name": "foo"
},
"Type": {
"Params": {
"List": []
},
"Ret": {
"Kind": 0,
"Flags": 0
}
}
}],
"includes": [],
"macros": []
}
}
#stderr