From e09c5fcb3c019d258a14e6f59d37f481b88fd546 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 21 Aug 2024 18:25:51 +0800 Subject: [PATCH] llcppsigfetch:TypedefDecl & Elaborated Type Refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 25 ++- .../parse/cvt_test/decl_test/decl.go | 18 ++ .../parse/cvt_test/decl_test/llgo.expect | 192 ++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 4 + 4 files changed, 237 insertions(+), 2 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index ee3a2f1b..bc9ace67 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -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() 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 7b5126f7..577afd85 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -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) +} 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 425ba1a6..cfc8fc73 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -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 diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index ce20623b..2e44514f 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -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))