llcppsigfetch:order output
This commit is contained in:
@@ -14,11 +14,17 @@ import (
|
|||||||
"github.com/goplus/llgo/chore/llcppg/token"
|
"github.com/goplus/llgo/chore/llcppg/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type FileEntry struct {
|
||||||
|
Path string
|
||||||
|
Doc *ast.File
|
||||||
|
}
|
||||||
|
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
Files map[string]*ast.File
|
Files []*FileEntry
|
||||||
curLoc ast.Location
|
FileOrder []string // todo(zzy): more efficient struct
|
||||||
index *clang.Index
|
curLoc ast.Location
|
||||||
unit *clang.TranslationUnit
|
index *clang.Index
|
||||||
|
unit *clang.TranslationUnit
|
||||||
|
|
||||||
typeDecls map[string]ast.Decl // cursorUsr -> ast.Decl
|
typeDecls map[string]ast.Decl // cursorUsr -> ast.Decl
|
||||||
|
|
||||||
@@ -64,7 +70,7 @@ func NewConverter(config *Config) (*Converter, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Converter{
|
return &Converter{
|
||||||
Files: make(map[string]*ast.File),
|
Files: make([]*FileEntry, 0),
|
||||||
index: index,
|
index: index,
|
||||||
unit: unit,
|
unit: unit,
|
||||||
anonyTypeMap: make(map[string]bool),
|
anonyTypeMap: make(map[string]bool),
|
||||||
@@ -169,16 +175,19 @@ func (ct *Converter) GetCurFile() *ast.File {
|
|||||||
if ct.curLoc.File == "" {
|
if ct.curLoc.File == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
file, ok := ct.Files[ct.curLoc.File]
|
// todo(zzy): more efficient
|
||||||
if !ok {
|
for i, entry := range ct.Files {
|
||||||
file = &ast.File{
|
if entry.Path == ct.curLoc.File {
|
||||||
Decls: make([]ast.Decl, 0),
|
return ct.Files[i].Doc
|
||||||
Includes: make([]*ast.Include, 0),
|
|
||||||
Macros: make([]*ast.Macro, 0),
|
|
||||||
}
|
}
|
||||||
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) {
|
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
|
return clang.ChildVisit_Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) Convert() (map[string]*ast.File, error) {
|
func (ct *Converter) Convert() ([]*FileEntry, error) {
|
||||||
cursor := ct.unit.Cursor()
|
cursor := ct.unit.Cursor()
|
||||||
// visit top decls (struct,class,function & macro,include)
|
// visit top decls (struct,class,function & macro,include)
|
||||||
clang.VisitChildren(cursor, visitTop, c.Pointer(ct))
|
clang.VisitChildren(cursor, visitTop, c.Pointer(ct))
|
||||||
|
|||||||
@@ -6,22 +6,22 @@ import (
|
|||||||
"github.com/goplus/llgo/chore/llcppg/ast"
|
"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()
|
root := cjson.Array()
|
||||||
for path, file := range files {
|
for _, entry := range files {
|
||||||
f := cjson.Object()
|
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("path"), path)
|
||||||
f.SetItem(c.Str("doc"), MarshalASTFile(file))
|
f.SetItem(c.Str("doc"), MarshalASTFile(entry.Doc))
|
||||||
root.AddItem(f)
|
root.AddItem(f)
|
||||||
}
|
}
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON {
|
func MarshalASTFiles(files []*FileEntry) *cjson.JSON {
|
||||||
root := cjson.Object()
|
root := cjson.Object()
|
||||||
for path, file := range files {
|
for _, entry := range files {
|
||||||
root.SetItem(c.AllocaCStr(path), MarshalASTFile(file))
|
root.SetItem(c.AllocaCStr(entry.Path), MarshalASTFile(entry.Doc))
|
||||||
}
|
}
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,17 +4,16 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
"github.com/goplus/llgo/chore/llcppg/ast"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Files map[string]*ast.File
|
Files []*FileEntry
|
||||||
IsCpp bool
|
IsCpp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(isCpp bool) *Context {
|
func NewContext(isCpp bool) *Context {
|
||||||
return &Context{
|
return &Context{
|
||||||
Files: make(map[string]*ast.File),
|
Files: make([]*FileEntry, 0),
|
||||||
IsCpp: isCpp,
|
IsCpp: isCpp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,23 +34,21 @@ func (p *Context) ProcessFiles(files []string) error {
|
|||||||
|
|
||||||
// parse file and add it to the context,avoid duplicate parsing
|
// parse file and add it to the context,avoid duplicate parsing
|
||||||
func (p *Context) processFile(path string) error {
|
func (p *Context) processFile(path string) error {
|
||||||
if _, exists := p.Files[path]; exists {
|
for _, entry := range p.Files {
|
||||||
return nil
|
if entry.Path == path {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
parsedfiles, err := p.parseFile(path)
|
parsedFiles, err := p.parseFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to parse file: " + path)
|
return errors.New("failed to parse file: " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
for path, file := range parsedfiles {
|
p.Files = append(p.Files, parsedFiles...)
|
||||||
if _, exist := p.Files[path]; !exist {
|
|
||||||
p.Files[path] = file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
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{
|
converter, err := NewConverter(&Config{
|
||||||
File: path,
|
File: path,
|
||||||
Temp: false,
|
Temp: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user