Skip to content

Commit

Permalink
llcppsigfetch:collect typedef,class,struct,method,field,enum USR
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Jan 22, 2025
1 parent 5616ea7 commit a08a1c9
Show file tree
Hide file tree
Showing 16 changed files with 711 additions and 532 deletions.
63 changes: 33 additions & 30 deletions _xtool/llcppsigfetch/parse/cvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ func (ct *Converter) GetCurFile(cursor clang.Cursor) *ast.File {
}

func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase {
usr := toStr(cursor.USR())
base := ast.DeclBase{
USR: usr,
Loc: &ast.Location{
File: ct.curLoc.File,
},
Expand Down Expand Up @@ -417,7 +415,7 @@ func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType {
func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl {
ct.incIndent()
defer ct.decIndent()
name, kind := getCursorDesc(cursor)
name, kind, usr := getCursorDesc(cursor)
ct.logln("ProcessTypeDefDecl: CursorName:", name, "CursorKind:", kind, "CursorTypeKind:", toStr(cursor.Type().Kind.String()))

typ := ct.ProcessUnderlyingType(cursor)
Expand All @@ -432,7 +430,7 @@ func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl {

decl := &ast.TypedefDecl{
DeclBase: ct.CreateDeclBase(cursor),
Name: &ast.Ident{Name: name},
Name: &ast.Ident{Name: name, USR: usr},
Type: typ,
}
return decl
Expand Down Expand Up @@ -502,7 +500,7 @@ func (ct *Converter) getActualType(t clang.Type) clang.Type {
func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl {
ct.incIndent()
defer ct.decIndent()
name, kind := getCursorDesc(cursor)
name, kind, usr := getCursorDesc(cursor)
mangledName := toStr(cursor.Mangling())
ct.logln("ProcessFuncDecl: CursorName:", name, "CursorKind:", kind, "mangledName:", mangledName)

Expand Down Expand Up @@ -532,10 +530,10 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl {
numFields := c.Int(len(funcType.Params.List))
for i := c.Int(0); i < numArgs; i++ {
arg := cursor.Argument(c.Uint(i))
name := clang.GoString(arg.DisplayName())
name := clang.GoString(arg.String())
if len(name) > 0 && i < numFields {
field := funcType.Params.List[i]
field.Names = []*ast.Ident{&ast.Ident{Name: name}}
field.Names = []*ast.Ident{{Name: name, USR: clang.GoString(arg.USR())}}
}
}
}
Expand All @@ -547,7 +545,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl {

funcDecl := &ast.FuncDecl{
DeclBase: ct.CreateDeclBase(cursor),
Name: &ast.Ident{Name: name},
Name: &ast.Ident{Name: name, USR: usr},
Type: funcType,
MangledName: mangledName,
}
Expand Down Expand Up @@ -608,15 +606,12 @@ func (ct *Converter) ProcessEnumType(cursor clang.Cursor) *ast.EnumType {

clangutils.VisitChildren(cursor, func(cursor, parent clang.Cursor) clang.ChildVisitResult {
if cursor.Kind == clang.CursorEnumConstantDecl {
name := cursor.String()
defer name.Dispose()

val := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20))
c.Sprintf(val, c.Str("%lld"), cursor.EnumConstantDeclValue())
defer c.Free(unsafe.Pointer(val))

enum := &ast.EnumItem{
Name: &ast.Ident{Name: c.GoString(name.CStr())},
Name: &ast.Ident{Name: clang.GoString(cursor.String()), USR: clang.GoString(cursor.USR())},
Value: &ast.BasicLit{
Kind: ast.IntLit,
Value: c.GoString(val),
Expand All @@ -633,7 +628,7 @@ func (ct *Converter) ProcessEnumType(cursor clang.Cursor) *ast.EnumType {
}

func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl {
cursorName, cursorKind := getCursorDesc(cursor)
cursorName, cursorKind, cursorUSR := getCursorDesc(cursor)
ct.logln("ProcessEnumDecl: CursorName:", cursorName, "CursorKind:", cursorKind)

decl := &ast.EnumTypeDecl{
Expand All @@ -643,7 +638,7 @@ func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl {

anony := cursor.IsAnonymous()
if anony == 0 {
decl.Name = &ast.Ident{Name: cursorName}
decl.Name = &ast.Ident{Name: cursorName, USR: cursorUSR}
ct.logln("ProcessEnumDecl: has name", cursorName)
} else {
ct.logln("ProcessRecordDecl: is anonymous")
Expand Down Expand Up @@ -677,8 +672,6 @@ func (ct *Converter) createBaseField(cursor clang.Cursor) *ast.Field {
ct.incIndent()
defer ct.decIndent()

fieldName := toStr(cursor.String())

typ := cursor.Type()
typeName, typeKind := getTypeDesc(typ)

Expand All @@ -696,8 +689,14 @@ func (ct *Converter) createBaseField(cursor clang.Cursor) *ast.Field {
field.Comment = commentGroup
}
}

fieldName := clang.GoString(cursor.String())

if fieldName != "" {
field.Names = []*ast.Ident{{Name: fieldName}}
field.Names = []*ast.Ident{{
Name: fieldName,
USR: clang.GoString(cursor.USR()),
}}
}
return field
}
Expand Down Expand Up @@ -754,7 +753,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl {
func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl {
ct.incIndent()
defer ct.decIndent()
cursorName, cursorKind := getCursorDesc(cursor)
cursorName, cursorKind, cursorUSR := getCursorDesc(cursor)
ct.logln("ProcessRecordDecl: CursorName:", cursorName, "CursorKind:", cursorKind)

decl := &ast.TypeDecl{
Expand All @@ -764,7 +763,7 @@ func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl {

anony := cursor.IsAnonymousRecordDecl()
if anony == 0 {
decl.Name = &ast.Ident{Name: cursorName}
decl.Name = &ast.Ident{Name: cursorName, USR: cursorUSR}
ct.logln("ProcessRecordDecl: has name", cursorName)
} else {
ct.logln("ProcessRecordDecl: is anonymous")
Expand All @@ -782,7 +781,7 @@ func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl {
}

func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl {
cursorName, cursorKind := getCursorDesc(cursor)
cursorName, cursorKind, cursorUSR := getCursorDesc(cursor)
ct.logln("ProcessClassDecl: CursorName:", cursorName, "CursorKind:", cursorKind)

// Pushing class scope before processing its type and popping after
Expand All @@ -791,7 +790,7 @@ func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl {

decl := &ast.TypeDecl{
DeclBase: base,
Name: &ast.Ident{Name: cursorName},
Name: &ast.Ident{Name: cursorName, USR: cursorUSR},
Type: typ,
}

Expand All @@ -802,7 +801,7 @@ func (ct *Converter) ProcessRecordType(cursor clang.Cursor) *ast.RecordType {
ct.incIndent()
defer ct.decIndent()

cursorName, cursorKind := getCursorDesc(cursor)
cursorName, cursorKind, _ := getCursorDesc(cursor)
ct.logln("ProcessRecordType: CursorName:", cursorName, "CursorKind:", cursorKind)

tag := toTag(cursor.Kind)
Expand Down Expand Up @@ -863,9 +862,12 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr {

func (ct *Converter) ProcessTypeDefType(t clang.Type) ast.Expr {
cursor := t.TypeDeclaration()
ct.logln("ProcessTypeDefType: Typedef TypeDeclaration", toStr(cursor.String()), toStr(t.String()))
typeName, typeKind := getTypeDesc(t)
cursorName, cursorKind, cursorUSR := getCursorDesc(cursor)

ct.logln("ProcessTypeDefType: Typedef TypeDeclaration", cursorName, cursorKind, typeName, typeKind)
if name := toStr(cursor.String()); name != "" {
return &ast.Ident{Name: name}
return &ast.Ident{Name: name, USR: cursorUSR}
}
ct.logln("ProcessTypeDefType: typedef type have no name")
return nil
Expand Down Expand Up @@ -995,16 +997,16 @@ func isMethod(cursor clang.Cursor) bool {
return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor
}

func buildScopingFromParts(parts []string) ast.Expr {
func buildScopingFromParts(parts []*clangutils.ScopingPart) ast.Expr {
if len(parts) == 0 {
return nil
}

var expr ast.Expr = &ast.Ident{Name: parts[0]}
var expr ast.Expr = &ast.Ident{Name: parts[0].Name, USR: parts[0].USR}
for _, part := range parts[1:] {
expr = &ast.ScopingExpr{
Parent: expr,
X: &ast.Ident{Name: part},
X: &ast.Ident{Name: part.Name, USR: part.USR},
}
}
return expr
Expand All @@ -1029,8 +1031,9 @@ func getTypeDesc(t clang.Type) (name string, kind string) {
return
}

func getCursorDesc(cursor clang.Cursor) (name string, kind string) {
name = toStr(cursor.String())
kind = toStr(cursor.Kind.String())
func getCursorDesc(cursor clang.Cursor) (name string, kind string, usr string) {
name = clang.GoString(cursor.String())
kind = clang.GoString(cursor.Kind.String())
usr = clang.GoString(cursor.USR())
return
}
Loading

0 comments on commit a08a1c9

Please sign in to comment.