llcppsigfetch:field access & static field

This commit is contained in:
luoliwoshang
2024-08-28 14:51:36 +08:00
parent 9351a1f900
commit 7d0b47c5cb
9 changed files with 127 additions and 15 deletions

View File

@@ -437,11 +437,11 @@ type visitFieldContext struct {
func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
ctx := (*visitFieldContext)(clientData)
if cursor.Kind == clang.CursorParmDecl || cursor.Kind == clang.CursorFieldDecl {
switch cursor.Kind {
case clang.CursorParmDecl, clang.CursorFieldDecl:
paramName := cursor.String()
defer paramName.Dispose()
argType := ctx.converter.ProcessType(cursor.Type())
// In C language, parameter lists do not have similar parameter grouping in Go.
// func foo(a, b int)
@@ -449,17 +449,36 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan
// struct A {
// int a, b;
// };
ctx.params.List = append(ctx.params.List,
&ast.Field{
//todo(zzy): comment & doc
Doc: &ast.CommentGroup{},
Comment: &ast.CommentGroup{},
Type: argType,
Names: []*ast.Ident{
{Name: c.GoString(paramName.CStr())},
},
field := &ast.Field{
Doc: &ast.CommentGroup{},
Comment: &ast.CommentGroup{},
Type: ctx.converter.ProcessType(cursor.Type()),
Names: []*ast.Ident{{Name: c.GoString(paramName.CStr())}},
}
if cursor.Kind == clang.CursorFieldDecl {
field.Access = ast.AccessSpecifier(cursor.CXXAccessSpecifier())
}
ctx.params.List = append(ctx.params.List, field)
case clang.CursorVarDecl:
if cursor.StorageClass() == clang.SCStatic {
// static member variable
fieldname := cursor.String()
defer fieldname.Dispose()
//todo(zzy): comment & doc
ctx.params.List = append(ctx.params.List, &ast.Field{
Doc: &ast.CommentGroup{},
Comment: &ast.CommentGroup{},
Type: ctx.converter.ProcessType(cursor.Type()),
Access: ast.AccessSpecifier(cursor.CXXAccessSpecifier()),
IsStatic: true,
Names: []*ast.Ident{{Name: c.GoString(fieldname.CStr())}},
})
}
}
return clang.ChildVisit_Continue
}
@@ -486,7 +505,7 @@ type visitMethodsContext struct {
func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
ctx := (*visitMethodsContext)(clientData)
if isMethod(cursor) && cursor.CXXAccessSpecifier() != clang.CXXPrivate {
if isMethod(cursor) && cursor.CXXAccessSpecifier() == clang.CXXPublic {
method := ctx.converter.ProcessFuncDecl(cursor)
if method != nil {
*ctx.methods = append(*ctx.methods, method)
@@ -495,6 +514,7 @@ func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.
return clang.ChildVisit_Continue
}
// Note:Public Method is considered
func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl {
methods := make([]*ast.FuncDecl, 0)
ctx := &visitMethodsContext{

View File

@@ -15,11 +15,13 @@ func TestClassDecl() {
};`,
`class A {
public:
int a;
static int a;
int b;
float foo(int a,double b);
private:
void bar();
static void bar();
protected:
void bar2();
};`,
`class A {
public:

View File

@@ -27,6 +27,8 @@ TestClassDecl Case 1:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -41,6 +43,8 @@ TestClassDecl Case 1:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -82,6 +86,8 @@ TestClassDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": true,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -96,6 +102,8 @@ TestClassDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -127,6 +135,8 @@ TestClassDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "a"
}]
@@ -141,6 +151,8 @@ TestClassDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "b"
}]

View File

@@ -63,6 +63,8 @@ TestFuncDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "a"
}]
@@ -108,6 +110,8 @@ TestFuncDecl Case 3:
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}, {
"Type": {
@@ -120,6 +124,8 @@ TestFuncDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "a"
}]
@@ -171,6 +177,8 @@ TestFuncDecl Case 4:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "a"
}]
@@ -185,6 +193,8 @@ TestFuncDecl Case 4:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "b"
}]
@@ -238,6 +248,8 @@ TestFuncDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "a"
}]
@@ -252,6 +264,8 @@ TestFuncDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 0,
"Names": [{
"Name": "b"
}]

View File

@@ -25,6 +25,8 @@ TestStructDecl Case 1:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -66,6 +68,8 @@ TestStructDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -80,6 +84,8 @@ TestStructDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -121,6 +127,8 @@ TestStructDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -135,6 +143,8 @@ TestStructDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -176,6 +186,8 @@ TestStructDecl Case 4:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -190,6 +202,8 @@ TestStructDecl Case 4:
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}, {
"Type": {
@@ -198,6 +212,8 @@ TestStructDecl Case 4:
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}]
},
@@ -213,6 +229,8 @@ TestStructDecl Case 4:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "Foo"
}]
@@ -254,6 +272,8 @@ TestStructDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "age"
}]
@@ -272,6 +292,8 @@ TestStructDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "year"
}]
@@ -286,6 +308,8 @@ TestStructDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "day"
}]
@@ -300,6 +324,8 @@ TestStructDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "month"
}]
@@ -313,6 +339,8 @@ TestStructDecl Case 5:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "birthday"
}]

View File

@@ -85,6 +85,8 @@ TestTypeDefDecl Case 3:
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}, {
"Type": {
@@ -93,12 +95,16 @@ TestTypeDefDecl Case 3:
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}, {
"Type": {
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}]
},

View File

@@ -25,6 +25,8 @@ TestUnionDecl Case 1:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -39,6 +41,8 @@ TestUnionDecl Case 1:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -80,6 +84,8 @@ TestUnionDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "a"
}]
@@ -94,6 +100,8 @@ TestUnionDecl Case 2:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "b"
}]
@@ -135,6 +143,8 @@ TestUnionDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "i"
}]
@@ -149,6 +159,8 @@ TestUnionDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "f"
}]
@@ -167,6 +179,8 @@ TestUnionDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "c"
}]
@@ -181,6 +195,8 @@ TestUnionDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "s"
}]
@@ -194,6 +210,8 @@ TestUnionDecl Case 3:
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "inner"
}]

View File

@@ -120,6 +120,8 @@ Type: struct (unnamed struct at temp.h:1:1):
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "x"
}]
@@ -153,6 +155,8 @@ Type: union (unnamed union at temp.h:1:1):
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 1,
"Names": [{
"Name": "x"
}]
@@ -209,6 +213,8 @@ Type: class (unnamed class at temp.h:1:1):
"Comment": {
"List": []
},
"IsStatic": false,
"Access": 3,
"Names": [{
"Name": "x"
}]
@@ -258,6 +264,8 @@ Type: int (*)(int, char):
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}, {
"Type": {
@@ -266,6 +274,8 @@ Type: int (*)(int, char):
},
"Doc": null,
"Comment": null,
"IsStatic": false,
"Access": 0,
"Names": []
}]
},

View File

@@ -143,6 +143,8 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON {
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
root.SetItem(c.Str("Doc"), MarshalASTExpr(d.Doc))
root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment))
root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic))
root.SetItem(c.Str("Access"), numberField(uint(d.Access)))
names := cjson.Array()
for _, n := range d.Names {
names.AddItem(MarshalASTExpr(n))