diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c77863e3..26c66fab 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -14,11 +14,17 @@ import ( "github.com/goplus/llgo/chore/llcppg/token" ) +type FileEntry struct { + Path string + Doc *ast.File +} + type Converter struct { - Files map[string]*ast.File - curLoc ast.Location - index *clang.Index - unit *clang.TranslationUnit + Files []*FileEntry + FileOrder []string // todo(zzy): more efficient struct + curLoc ast.Location + index *clang.Index + unit *clang.TranslationUnit typeDecls map[string]ast.Decl // cursorUsr -> ast.Decl @@ -64,7 +70,7 @@ func NewConverter(config *Config) (*Converter, error) { } return &Converter{ - Files: make(map[string]*ast.File), + Files: make([]*FileEntry, 0), index: index, unit: unit, anonyTypeMap: make(map[string]bool), @@ -169,16 +175,19 @@ func (ct *Converter) GetCurFile() *ast.File { if ct.curLoc.File == "" { return nil } - file, ok := ct.Files[ct.curLoc.File] - if !ok { - file = &ast.File{ - Decls: make([]ast.Decl, 0), - Includes: make([]*ast.Include, 0), - Macros: make([]*ast.Macro, 0), + // todo(zzy): more efficient + for i, entry := range ct.Files { + if entry.Path == ct.curLoc.File { + return ct.Files[i].Doc } - ct.Files[ct.curLoc.File] = file } - return file + newDoc := &ast.File{ + Decls: make([]ast.Decl, 0), + Includes: make([]*ast.Include, 0), + Macros: make([]*ast.Macro, 0), + } + ct.Files = append(ct.Files, &FileEntry{Path: ct.curLoc.File, Doc: newDoc}) + return newDoc } func (ct *Converter) SetAnonyType(cursor clang.Cursor) { @@ -300,7 +309,7 @@ func visitTop(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chil return clang.ChildVisit_Continue } -func (ct *Converter) Convert() (map[string]*ast.File, error) { +func (ct *Converter) Convert() ([]*FileEntry, error) { cursor := ct.unit.Cursor() // visit top decls (struct,class,function & macro,include) clang.VisitChildren(cursor, visitTop, c.Pointer(ct)) diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 553de95c..bb58a02c 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -6,22 +6,22 @@ import ( "github.com/goplus/llgo/chore/llcppg/ast" ) -func MarshalOutputASTFiles(files map[string]*ast.File) *cjson.JSON { +func MarshalOutputASTFiles(files []*FileEntry) *cjson.JSON { root := cjson.Array() - for path, file := range files { + for _, entry := range files { f := cjson.Object() - path := cjson.String(c.AllocaCStr(path)) + path := cjson.String(c.AllocaCStr(entry.Path)) f.SetItem(c.Str("path"), path) - f.SetItem(c.Str("doc"), MarshalASTFile(file)) + f.SetItem(c.Str("doc"), MarshalASTFile(entry.Doc)) root.AddItem(f) } return root } -func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { +func MarshalASTFiles(files []*FileEntry) *cjson.JSON { root := cjson.Object() - for path, file := range files { - root.SetItem(c.AllocaCStr(path), MarshalASTFile(file)) + for _, entry := range files { + root.SetItem(c.AllocaCStr(entry.Path), MarshalASTFile(entry.Doc)) } return root } diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index 90684c80..9a1c3978 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -4,17 +4,16 @@ import ( "errors" "github.com/goplus/llgo/c/cjson" - "github.com/goplus/llgo/chore/llcppg/ast" ) type Context struct { - Files map[string]*ast.File + Files []*FileEntry IsCpp bool } func NewContext(isCpp bool) *Context { return &Context{ - Files: make(map[string]*ast.File), + Files: make([]*FileEntry, 0), IsCpp: isCpp, } } @@ -35,23 +34,21 @@ func (p *Context) ProcessFiles(files []string) error { // parse file and add it to the context,avoid duplicate parsing func (p *Context) processFile(path string) error { - if _, exists := p.Files[path]; exists { - return nil + for _, entry := range p.Files { + if entry.Path == path { + return nil + } } - parsedfiles, err := p.parseFile(path) + parsedFiles, err := p.parseFile(path) if err != nil { return errors.New("failed to parse file: " + path) } - for path, file := range parsedfiles { - if _, exist := p.Files[path]; !exist { - p.Files[path] = file - } - } + p.Files = append(p.Files, parsedFiles...) return nil } -func (p *Context) parseFile(path string) (map[string]*ast.File, error) { +func (p *Context) parseFile(path string) ([]*FileEntry, error) { converter, err := NewConverter(&Config{ File: path, Temp: false,