llcppsigfetch:correct class scoping

This commit is contained in:
luoliwoshang
2024-08-22 18:49:00 +08:00
parent 319e746a55
commit 14b335a51e
5 changed files with 49 additions and 60 deletions

View File

@@ -197,23 +197,21 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
marco := ct.ProcessMarco(cursor) marco := ct.ProcessMarco(cursor)
curFile.Macros = append(curFile.Macros, marco) curFile.Macros = append(curFile.Macros, marco)
case clang.CursorEnumDecl: case clang.CursorEnumDecl:
enum := ct.ProcessEnum(cursor) enum := ct.ProcessEnumDecl(cursor)
curFile.Decls = append(curFile.Decls, enum) curFile.Decls = append(curFile.Decls, enum)
case clang.CursorClassDecl: case clang.CursorClassDecl:
ct.PushScope(cursor) classDecl := ct.ProcessClassDecl(cursor)
classDecl := ct.ProcessClass(cursor)
curFile.Decls = append(curFile.Decls, classDecl) curFile.Decls = append(curFile.Decls, classDecl)
ct.PopScope()
case clang.CursorStructDecl: case clang.CursorStructDecl:
structDecl := ct.ProcessStruct(cursor) structDecl := ct.ProcessStructDecl(cursor)
curFile.Decls = append(curFile.Decls, structDecl) curFile.Decls = append(curFile.Decls, structDecl)
case clang.CursorUnionDecl: case clang.CursorUnionDecl:
unionDecl := ct.ProcessUnion(cursor) unionDecl := ct.ProcessUnionDecl(cursor)
curFile.Decls = append(curFile.Decls, unionDecl) curFile.Decls = append(curFile.Decls, unionDecl)
case clang.CursorFunctionDecl: case clang.CursorFunctionDecl:
curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor)) curFile.Decls = append(curFile.Decls, ct.ProcessFuncDecl(cursor))
case clang.CursorTypedefDecl: case clang.CursorTypedefDecl:
curFile.Decls = append(curFile.Decls, ct.ProcessTypeDef(cursor)) curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor))
case clang.CursorNamespace: case clang.CursorNamespace:
ct.PushScope(cursor) ct.PushScope(cursor)
clang.VisitChildren(cursor, visit, c.Pointer(ct)) 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: case clang.TypeLValueReference, clang.TypeRValueReference:
expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())}
case clang.TypeFunctionProto: 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()) ret := ct.ProcessType(t.ResultType())
expr = &ast.FuncType{Ret: ret} expr = &ast.FuncType{Ret: ret}
case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray:
@@ -268,7 +266,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
return expr return expr
} }
func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl {
name := cursor.String() name := cursor.String()
defer name.Dispose() defer name.Dispose()
return &ast.TypedefDecl{ 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() name := cursor.String()
defer name.Dispose() defer name.Dispose()
// function type will only collect return type // 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 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() name := cursor.String()
defer name.Dispose() defer name.Dispose()
items := make([]*ast.EnumItem, 0) items := make([]*ast.EnumItem, 0)
@@ -426,7 +424,7 @@ type visitMethodsContext struct {
func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
ctx := (*visitMethodsContext)(clientData) ctx := (*visitMethodsContext)(clientData)
if cursor.Kind == clang.CursorCXXMethod { if cursor.Kind == clang.CursorCXXMethod {
method := ctx.converter.ProcessFunc(cursor) method := ctx.converter.ProcessFuncDecl(cursor)
if method != nil { if method != nil {
*ctx.methods = append(*ctx.methods, method) *ctx.methods = append(*ctx.methods, method)
} }
@@ -444,7 +442,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl {
return methods 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() anony := cursor.IsAnonymousRecordDecl()
var name *ast.Ident 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())} name = &ast.Ident{Name: c.GoString(cursorName.CStr())}
} }
fields := ct.ProcessFieldList(cursor) return &ast.TypeDecl{
methods := ct.ProcessMethods(cursor)
decl := &ast.TypeDecl{
DeclBase: ct.CreateDeclBase(cursor), DeclBase: ct.CreateDeclBase(cursor),
Name: name, Name: name,
Type: &ast.RecordType{ Type: ct.ProcessRecordType(cursor, tag),
Tag: tag,
Fields: fields,
Methods: methods,
},
} }
return decl
} }
func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) *ast.TypeDecl {
return ct.ProcessRecord(cursor, ast.Struct) return ct.ProcessRecordDecl(cursor, ast.Struct)
} }
func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl {
return ct.ProcessRecord(cursor, ast.Union) return ct.ProcessRecordDecl(cursor, ast.Union)
} }
func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl {
return ct.ProcessRecord(cursor, ast.Class) // 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 { func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr {

View File

@@ -9,9 +9,7 @@ TestClassDecl Case 1:
"Doc": { "Doc": {
"List": [] "List": []
}, },
"Parent": { "Parent": null,
"Name": "A"
},
"Name": { "Name": {
"Name": "A" "Name": "A"
}, },
@@ -66,9 +64,7 @@ TestClassDecl Case 2:
"Doc": { "Doc": {
"List": [] "List": []
}, },
"Parent": { "Parent": null,
"Name": "A"
},
"Name": { "Name": {
"Name": "A" "Name": "A"
}, },

View File

@@ -105,9 +105,7 @@ TestScope Case 4:
"Doc": { "Doc": {
"List": [] "List": []
}, },
"Parent": { "Parent": null,
"Name": "a"
},
"Name": { "Name": {
"Name": "a" "Name": "a"
}, },
@@ -157,12 +155,7 @@ TestScope Case 5:
"List": [] "List": []
}, },
"Parent": { "Parent": {
"X": { "Name": "a"
"Name": "b"
},
"Parent": {
"Name": "a"
}
}, },
"Name": { "Name": {
"Name": "b" "Name": "b"
@@ -219,15 +212,10 @@ TestScope Case 6:
}, },
"Parent": { "Parent": {
"X": { "X": {
"Name": "c" "Name": "b"
}, },
"Parent": { "Parent": {
"X": { "Name": "a"
"Name": "b"
},
"Parent": {
"Name": "a"
}
} }
}, },
"Name": { "Name": {

View File

@@ -107,9 +107,7 @@ TestTypeDefDecl Case 3:
"Doc": { "Doc": {
"List": [] "List": []
}, },
"Parent": { "Parent": null,
"Name": "ClassFoo"
},
"Name": { "Name": {
"Name": "ClassFoo" "Name": "ClassFoo"
}, },
@@ -240,9 +238,7 @@ TestTypeDefDecl Case 4:
"Doc": { "Doc": {
"List": [] "List": []
}, },
"Parent": { "Parent": null,
"Name": "ClassFoo"
},
"Name": { "Name": {
"Name": "ClassFoo" "Name": "ClassFoo"
}, },

View File

@@ -9,9 +9,10 @@ func main() {
func TestTypeDefDecl() { func TestTypeDefDecl() {
testCases := []string{ testCases := []string{
`typedef int INT;`, `typedef int INT;`,
`typedef int INT;
`typedef int INT;
typedef INT STANDARD_INT;`, typedef INT STANDARD_INT;`,
`struct StructFoo {}; `struct StructFoo {};
union UnionFoo {}; union UnionFoo {};
class ClassFoo {}; class ClassFoo {};