diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index df10cac8..ee3a2f1b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -427,10 +427,15 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { return methods } -func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { +func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { + anony := cursor.IsAnonymousRecordDecl() - name := cursor.String() - defer name.Dispose() + var name *ast.Ident + if anony == 0 { + cursorName := cursor.String() + defer cursorName.Dispose() + name = &ast.Ident{Name: c.GoString(cursorName.CStr())} + } fields := ct.ProcessFieldList(cursor) methods := ct.ProcessMethods(cursor) @@ -438,7 +443,7 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast decl := &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Tag: tag, - Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Name: name, Fields: fields, Methods: methods, } @@ -447,15 +452,15 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast } func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Struct) + return ct.ProcessRecord(cursor, ast.Struct) } func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Union) + return ct.ProcessRecord(cursor, ast.Union) } func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Class) + return ct.ProcessRecord(cursor, ast.Class) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index ec00ba19..7b5126f7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -85,6 +85,9 @@ void foo();`, func TestStructDecl() { testCases := []string{ + `struct { + int a; + };`, `struct A { int a; int b; @@ -103,6 +106,10 @@ func TestStructDecl() { func TestUnionDecl() { testCases := []string{ + `union { + int a; + int b; + };`, `union A { int a; int b; diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index b97682c7..425ba1a6 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -958,9 +958,7 @@ TestStructDecl Case 1: }, "Parent": null, "Tag": 0, - "Name": { - "Name": "A" - }, + "Name": null, "Fields": { "List": [{ "Type": { @@ -976,20 +974,6 @@ TestStructDecl Case 1: "Names": [{ "Name": "a" }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] }] }, "Methods": [] @@ -1054,6 +1038,60 @@ TestStructDecl Case 2: } TestStructDecl Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Name": { + "Name": "A" + }, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 4: { "temp.h": { "path": "temp.h", @@ -1318,6 +1356,58 @@ TestClassDecl Case 2: } TestUnionDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 1, + "Name": null, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestUnionDecl Case 2: { "temp.h": { "path": "temp.h", diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c43951da..ce20623b 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -131,6 +131,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("Names"), names) case *ast.Ident: + if d == nil { + return cjson.Null() + } root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) case *ast.EnumItem: root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) @@ -147,6 +150,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) case *ast.Comment: + if d == nil { + return cjson.Null() + } root.SetItem(c.Str("Text"), cjson.String(c.AllocaCStr(d.Text))) case *ast.CommentGroup: if d == nil {