llcppsigfetch:TypedefDecl & Elaborated Type Refer

This commit is contained in:
luoliwoshang
2024-08-21 18:25:51 +08:00
parent 815fe25f2c
commit e09c5fcb3c
4 changed files with 237 additions and 2 deletions

View File

@@ -213,6 +213,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
curFile.Decls = append(curFile.Decls, unionDecl)
case clang.CursorFunctionDecl:
curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor))
case clang.CursorTypedefDecl:
curFile.Decls = append(curFile.Decls, ct.ProcessTypeDef(cursor))
case clang.CursorNamespace:
ct.PushScope(cursor)
clang.VisitChildren(cursor, visit, c.Pointer(ct))
@@ -240,8 +242,6 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
// function type will only collect return type, params will be collected in ProcessFunc
ret := ct.ProcessType(t.ResultType())
expr = &ast.FuncType{Ret: ret}
case clang.TypeTypedef:
expr = ct.ProcessType(t.CanonicalType())
case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray:
if t.Kind == clang.TypeConstantArray {
len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20))
@@ -261,6 +261,27 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
return expr
}
func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl {
name := cursor.String()
defer name.Dispose()
return &ast.TypedefDecl{
DeclBase: ct.CreateDeclBase(cursor),
Name: &ast.Ident{Name: c.GoString(name.CStr())},
Type: ct.ProcessUnderLyingType(cursor),
}
}
func (ct *Converter) ProcessUnderLyingType(cursor clang.Cursor) ast.Expr {
underlying := cursor.TypedefDeclUnderlyingType()
// enum,union,class,struct,typedef -> elaborated type
if underlying.Kind == clang.TypeElaborated {
return &ast.Ident{
Name: c.GoString(underlying.String().CStr()),
}
}
return ct.ProcessType(underlying)
}
func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl {
name := cursor.String()
defer name.Dispose()

View File

@@ -12,6 +12,7 @@ func main() {
TestClassDecl()
TestUnionDecl()
TestEnumDecl()
TestTypeDefDecl()
}
func TestFuncDecl() {
@@ -153,3 +154,20 @@ func TestEnumDecl() {
}
test.RunTest("TestEnumDecl", testCases)
}
func TestTypeDefDecl() {
testCases := []string{
`typedef int INT;`,
`typedef int INT;
typedef INT STANDARD_INT;`,
`struct StructFoo {};
union UnionFoo {};
class ClassFoo {};
enum EnumFoo {};
typedef StructFoo STRUCT_FOO;
typedef UnionFoo UNION_FOO;
typedef ClassFoo CLASS_FOO;
typedef EnumFoo ENUM_FOO;`,
}
test.RunTest("TestTypeDefDecl", testCases)
}

View File

@@ -1599,6 +1599,198 @@ TestEnumDecl Case 3:
}
}
TestTypeDefDecl Case 1:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "INT"
},
"Type": {
"Kind": 6,
"Flags": 0
}
}],
"includes": [],
"macros": []
}
}
TestTypeDefDecl Case 2:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "INT"
},
"Type": {
"Kind": 6,
"Flags": 0
}
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "STANDARD_INT"
},
"Type": {
"Name": "INT"
}
}],
"includes": [],
"macros": []
}
}
TestTypeDefDecl Case 3:
{
"temp.h": {
"path": "temp.h",
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Tag": 0,
"Name": {
"Name": "StructFoo"
},
"Fields": {
"List": []
},
"Methods": []
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Tag": 1,
"Name": {
"Name": "UnionFoo"
},
"Fields": {
"List": []
},
"Methods": []
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": {
"Name": "ClassFoo"
},
"Tag": 3,
"Name": {
"Name": "ClassFoo"
},
"Fields": {
"List": []
},
"Methods": []
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "EnumFoo"
},
"Items": []
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "STRUCT_FOO"
},
"Type": {
"Name": "StructFoo"
}
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "UNION_FOO"
},
"Type": {
"Name": "UnionFoo"
}
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "CLASS_FOO"
},
"Type": {
"Name": "ClassFoo"
}
}, {
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "ENUM_FOO"
},
"Type": {
"Name": "EnumFoo"
}
}],
"includes": [],
"macros": []
}
}
#stderr

View File

@@ -74,6 +74,10 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON {
items.AddItem(MarshalASTExpr(i))
}
root.SetItem(c.Str("Items"), items)
case *ast.TypedefDecl:
MarshalASTDeclBase(d.DeclBase, root)
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
case *ast.FuncDecl:
MarshalASTDeclBase(d.DeclBase, root)
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))