From 14b335a51e9b8c459e811ed92146ace949b46d17 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 18:49:00 +0800 Subject: [PATCH] llcppsigfetch:correct class scoping --- chore/_xtool/llcppsigfetch/parse/cvt.go | 70 +++++++++++-------- .../cvt_test/decl_test/class_test/llgo.expect | 8 +-- .../cvt_test/decl_test/scope_test/llgo.expect | 20 ++---- .../decl_test/typedef_test/llgo.expect | 8 +-- .../decl_test/typedef_test/typedef.go | 3 +- 5 files changed, 49 insertions(+), 60 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 003df96b..60664aef 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -197,23 +197,21 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi marco := ct.ProcessMarco(cursor) curFile.Macros = append(curFile.Macros, marco) case clang.CursorEnumDecl: - enum := ct.ProcessEnum(cursor) + enum := ct.ProcessEnumDecl(cursor) curFile.Decls = append(curFile.Decls, enum) case clang.CursorClassDecl: - ct.PushScope(cursor) - classDecl := ct.ProcessClass(cursor) + classDecl := ct.ProcessClassDecl(cursor) curFile.Decls = append(curFile.Decls, classDecl) - ct.PopScope() case clang.CursorStructDecl: - structDecl := ct.ProcessStruct(cursor) + structDecl := ct.ProcessStructDecl(cursor) curFile.Decls = append(curFile.Decls, structDecl) case clang.CursorUnionDecl: - unionDecl := ct.ProcessUnion(cursor) + unionDecl := ct.ProcessUnionDecl(cursor) curFile.Decls = append(curFile.Decls, unionDecl) case clang.CursorFunctionDecl: - curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor)) + curFile.Decls = append(curFile.Decls, ct.ProcessFuncDecl(cursor)) case clang.CursorTypedefDecl: - curFile.Decls = append(curFile.Decls, ct.ProcessTypeDef(cursor)) + curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) clang.VisitChildren(cursor, visit, c.Pointer(ct)) @@ -246,7 +244,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { case clang.TypeLValueReference, clang.TypeRValueReference: expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: - // function type will only collect return type, params will be collected in ProcessFunc + // function type will only collect return type, params will be collected in ProcessFuncDecl ret := ct.ProcessType(t.ResultType()) expr = &ast.FuncType{Ret: ret} case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: @@ -268,7 +266,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } -func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { +func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() return &ast.TypedefDecl{ @@ -278,7 +276,7 @@ func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { } } -func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { +func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() // function type will only collect return type @@ -323,7 +321,7 @@ func visitEnum(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chi return clang.ChildVisit_Continue } -func (ct *Converter) ProcessEnum(cursor clang.Cursor) *ast.EnumTypeDecl { +func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { name := cursor.String() defer name.Dispose() items := make([]*ast.EnumItem, 0) @@ -426,7 +424,7 @@ type visitMethodsContext struct { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitMethodsContext)(clientData) if cursor.Kind == clang.CursorCXXMethod { - method := ctx.converter.ProcessFunc(cursor) + method := ctx.converter.ProcessFuncDecl(cursor) if method != nil { *ctx.methods = append(*ctx.methods, method) } @@ -444,7 +442,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { return methods } -func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { +func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { anony := cursor.IsAnonymousRecordDecl() var name *ast.Ident @@ -454,32 +452,42 @@ func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDe name = &ast.Ident{Name: c.GoString(cursorName.CStr())} } - fields := ct.ProcessFieldList(cursor) - methods := ct.ProcessMethods(cursor) - - decl := &ast.TypeDecl{ + return &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: name, - Type: &ast.RecordType{ - Tag: tag, - Fields: fields, - Methods: methods, - }, + Type: ct.ProcessRecordType(cursor, tag), } - - return decl } -func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Struct) +func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessRecordDecl(cursor, ast.Struct) } -func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Union) +func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessRecordDecl(cursor, ast.Union) } -func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Class) +func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { + // Pushing class scope before processing its type and popping after + base := ct.CreateDeclBase(cursor) + + ct.PushScope(cursor) + typ := ct.ProcessRecordType(cursor, ast.Class) + ct.PopScope() + + return &ast.TypeDecl{ + DeclBase: base, + Name: &ast.Ident{Name: c.GoString(cursor.String().CStr())}, + Type: typ, + } +} + +func (ct *Converter) ProcessRecordType(cursor clang.Cursor, tag ast.Tag) *ast.RecordType { + return &ast.RecordType{ + Tag: tag, + Fields: ct.ProcessFieldList(cursor), + Methods: ct.ProcessMethods(cursor), + } } func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { 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 b8542bd2..9b66edd8 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 @@ -9,9 +9,7 @@ TestClassDecl Case 1: "Doc": { "List": [] }, - "Parent": { - "Name": "A" - }, + "Parent": null, "Name": { "Name": "A" }, @@ -66,9 +64,7 @@ TestClassDecl Case 2: "Doc": { "List": [] }, - "Parent": { - "Name": "A" - }, + "Parent": null, "Name": { "Name": "A" }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index bba9d9b9..58b035dd 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -105,9 +105,7 @@ TestScope Case 4: "Doc": { "List": [] }, - "Parent": { - "Name": "a" - }, + "Parent": null, "Name": { "Name": "a" }, @@ -157,12 +155,7 @@ TestScope Case 5: "List": [] }, "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } + "Name": "a" }, "Name": { "Name": "b" @@ -219,15 +212,10 @@ TestScope Case 6: }, "Parent": { "X": { - "Name": "c" + "Name": "b" }, "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } + "Name": "a" } }, "Name": { 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 fbaed500..63084752 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 @@ -107,9 +107,7 @@ TestTypeDefDecl Case 3: "Doc": { "List": [] }, - "Parent": { - "Name": "ClassFoo" - }, + "Parent": null, "Name": { "Name": "ClassFoo" }, @@ -240,9 +238,7 @@ TestTypeDefDecl Case 4: "Doc": { "List": [] }, - "Parent": { - "Name": "ClassFoo" - }, + "Parent": null, "Name": { "Name": "ClassFoo" }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 9c90c6c7..9487e245 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -9,9 +9,10 @@ func main() { func TestTypeDefDecl() { testCases := []string{ `typedef int INT;`, - `typedef int INT; + `typedef int INT; typedef INT STANDARD_INT;`, + `struct StructFoo {}; union UnionFoo {}; class ClassFoo {};