llcppsigfetch:basic fileset

This commit is contained in:
luoliwoshang
2024-08-11 19:39:33 +08:00
parent 364d3996f4
commit 95dc01cdcb
2 changed files with 54 additions and 36 deletions

View File

@@ -11,9 +11,9 @@ import (
) )
type Converter struct { type Converter struct {
files map[string]*ast.File files map[string]*ast.File
typeMap map[clang.Type]ast.Expr // todo(zzy):maybe a other map key for typemap is better // typeMap map[clang.Type]ast.Expr // todo(zzy):cache type
declMap map[clang.Cursor]ast.Decl // declMap map[clang.Cursor]ast.Decl
curLoc ast.Location curLoc ast.Location
curFile *ast.File curFile *ast.File
index *clang.Index index *clang.Index
@@ -39,11 +39,11 @@ func NewConverter(filepath string) (*Converter, error) {
} }
return &Converter{ return &Converter{
typeMap: make(map[clang.Type]ast.Expr), // typeMap: make(map[clang.Type]ast.Expr),
declMap: make(map[clang.Cursor]ast.Decl), // declMap: make(map[clang.Cursor]ast.Decl),
files: make(map[string]*ast.File), files: make(map[string]*ast.File),
index: index, index: index,
unit: unit, unit: unit,
}, nil }, nil
} }
@@ -54,8 +54,9 @@ func (ct *Converter) Dispose() {
// visit top decls (struct,class,function,enum & marco,include) // visit top decls (struct,class,function,enum & marco,include)
func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
// todo(zzy): set current file
ct := (*Converter)(clientData) ct := (*Converter)(clientData)
ct.UpdateCurFile(cursor)
switch cursor.Kind { switch cursor.Kind {
case clang.CursorInclusionDirective: case clang.CursorInclusionDirective:
fmt.Println("todo: Process include") fmt.Println("todo: Process include")
@@ -81,11 +82,47 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
} }
} }
func (ct *Converter) ProcessType(t clang.Type) ast.Expr { func (ct *Converter) Convert() (map[string]*ast.File, error) {
// todo(zzy):a other map key for typemap cursor := ct.unit.Cursor()
if cache, ok := ct.typeMap[t]; ok { // visit top decls (struct,class,function & marco,include)
return cache clang.VisitChildren(cursor, visit, c.Pointer(ct))
return nil, nil
}
func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
loc := cursor.Location()
var file clang.File
loc.SpellingLocation(&file, nil, nil, nil)
filename := file.FileName()
defer filename.Dispose()
if filename.CStr() == nil {
// For some built-in macros, there is no file.
println("todo: filename is empty")
return
} }
filePath := c.GoString(filename.CStr())
if ct.curFile == nil || ct.curFile.Path != filePath {
if f, ok := ct.files[filePath]; ok {
ct.curFile = f
} else {
ct.curFile = &ast.File{
Path: filePath,
Decls: make([]ast.Decl, 0),
Includes: make([]*ast.Include, 0),
Macros: make([]*ast.Macro, 0),
}
}
ct.files[filePath] = ct.curFile
}
}
func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
// todo(zzy):cache type
// if cache, ok := ct.typeMap[t]; ok {
// return cache
// }
var expr ast.Expr var expr ast.Expr
switch t.Kind { switch t.Kind {
case clang.TypePointer: case clang.TypePointer:
@@ -107,47 +144,27 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
} }
// todo(zzy):array length // todo(zzy):array length
} }
ct.typeMap[t] = expr // ct.typeMap[t] = expr
return expr return expr
} }
func (ct *Converter) ProcessFunc(cursor clang.Cursor) { func (ct *Converter) ProcessFunc(cursor clang.Cursor) {
name := cursor.String() name := cursor.String()
defer name.Dispose() defer name.Dispose()
funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType)
if !ok { if !ok {
fmt.Println("failed to process function type") fmt.Println("failed to process function type")
return return
} }
fn := &ast.FuncDecl{ fn := &ast.FuncDecl{
Name: &ast.Ident{Name: c.GoString(name.CStr())}, Name: &ast.Ident{Name: c.GoString(name.CStr())},
Type: funcType, Type: funcType,
// todo(zzy):DeclBase use the converter's current namespace expr // todo(zzy):DeclBase use the converter's current namespace expr
} }
ct.curFile.Decls = append(ct.curFile.Decls, fn)
decls := ct.GetCurFile() // ct.declMap[cursor] = fn
decls.Decls = append(decls.Decls, fn)
ct.declMap[cursor] = fn
} }
func (ct *Converter) ProcessClass(cursor clang.Cursor) { func (ct *Converter) ProcessClass(cursor clang.Cursor) {
println("todo: Process class") println("todo: Process class")
} }
func (ct *Converter) Convert() (map[string]*ast.File, error) {
cursor := ct.unit.Cursor()
// visit top decls (struct,class,function & marco,include)
clang.VisitChildren(cursor, visit, c.Pointer(ct))
return nil, nil
}
func (ct *Converter) UpdateLocation(loc ast.Location) {
ct.curLoc = loc
}
func (ct *Converter) GetCurFile() *ast.File {
return ct.curFile
}

View File

@@ -370,6 +370,7 @@ func (*Macro) ppdNode() {}
// ------------------------------------------------ // ------------------------------------------------
type File struct { type File struct {
Path string `json:"path"`
Decls []Decl `json:"decls"` Decls []Decl `json:"decls"`
Includes []*Include `json:"includes,omitempty"` Includes []*Include `json:"includes,omitempty"`
Macros []*Macro `json:"macros,omitempty"` Macros []*Macro `json:"macros,omitempty"`