llcppsigfetch:anonymous record name

This commit is contained in:
luoliwoshang
2024-08-21 14:50:42 +08:00
parent 5e5c84ba27
commit 815fe25f2c
4 changed files with 132 additions and 24 deletions

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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",

View File

@@ -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 {