From 7d0b47c5cb6013ea0b06b2059f84363cab8e3bed Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 28 Aug 2024 14:51:36 +0800 Subject: [PATCH] llcppsigfetch:field access & static field --- chore/_xtool/llcppsigfetch/parse/cvt.go | 46 +++++++++++++------ .../cvt_test/decl_test/class_test/class.go | 6 ++- .../cvt_test/decl_test/class_test/llgo.expect | 12 +++++ .../cvt_test/decl_test/func_test/llgo.expect | 14 ++++++ .../decl_test/struct_test/llgo.expect | 28 +++++++++++ .../decl_test/typedef_test/llgo.expect | 6 +++ .../cvt_test/decl_test/union_test/llgo.expect | 18 ++++++++ .../parse/cvt_test/type_test/llgo.expect | 10 ++++ chore/_xtool/llcppsigfetch/parse/dump.go | 2 + 9 files changed, 127 insertions(+), 15 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 9ce3af3d..4ae82c0e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -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{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index 3997016b..fd2d3847 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -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: diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index a613664f..f7435e0b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -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" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 227f446c..454819d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -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" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index f3021595..ec15acd8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -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" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index d88c973a..5e32efa0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -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": [] }] }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect index 07db788f..cc620177 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -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" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 443dab45..3afa06d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -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": [] }] }, diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 5cc02bb0..c3e0a187 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -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))