llcppsigfetch:refine file handling, remove curFile field
This commit is contained in:
@@ -17,7 +17,6 @@ import (
|
|||||||
type Converter struct {
|
type Converter struct {
|
||||||
Files map[string]*ast.File
|
Files map[string]*ast.File
|
||||||
curLoc ast.Location
|
curLoc ast.Location
|
||||||
curFile *ast.File
|
|
||||||
index *clang.Index
|
index *clang.Index
|
||||||
unit *clang.TranslationUnit
|
unit *clang.TranslationUnit
|
||||||
scopeStack []ast.Expr //namespace & class
|
scopeStack []ast.Expr //namespace & class
|
||||||
@@ -120,7 +119,7 @@ func (ct *Converter) GetCurScope() ast.Expr {
|
|||||||
return ct.scopeStack[len(ct.scopeStack)-1]
|
return ct.scopeStack[len(ct.scopeStack)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
|
func (ct *Converter) UpdateLoc(cursor clang.Cursor) {
|
||||||
loc := cursor.Location()
|
loc := cursor.Location()
|
||||||
var file clang.File
|
var file clang.File
|
||||||
loc.SpellingLocation(&file, nil, nil, nil)
|
loc.SpellingLocation(&file, nil, nil, nil)
|
||||||
@@ -129,25 +128,30 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
|
|||||||
|
|
||||||
if filename.CStr() == nil {
|
if filename.CStr() == nil {
|
||||||
//todo(zzy): For some built-in macros, there is no file.
|
//todo(zzy): For some built-in macros, there is no file.
|
||||||
|
ct.curLoc = ast.Location{File: ""}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath := c.GoString(filename.CStr())
|
filePath := c.GoString(filename.CStr())
|
||||||
ct.curLoc = ast.Location{File: filePath}
|
ct.curLoc = ast.Location{File: filePath}
|
||||||
|
|
||||||
if ct.curFile == nil || ct.curFile.Path != filePath {
|
}
|
||||||
if f, ok := ct.Files[filePath]; ok {
|
|
||||||
ct.curFile = f
|
func (ct *Converter) GetCurFile() *ast.File {
|
||||||
} else {
|
if ct.curLoc.File == "" {
|
||||||
ct.curFile = &ast.File{
|
return nil
|
||||||
Path: filePath,
|
|
||||||
Decls: make([]ast.Decl, 0),
|
|
||||||
Includes: make([]*ast.Include, 0),
|
|
||||||
Macros: make([]*ast.Macro, 0),
|
|
||||||
}
|
|
||||||
ct.Files[filePath] = ct.curFile
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
file, ok := ct.Files[ct.curLoc.File]
|
||||||
|
if !ok {
|
||||||
|
file = &ast.File{
|
||||||
|
Path: ct.curLoc.File,
|
||||||
|
Decls: make([]ast.Decl, 0),
|
||||||
|
Includes: make([]*ast.Include, 0),
|
||||||
|
Macros: make([]*ast.Macro, 0),
|
||||||
|
}
|
||||||
|
ct.Files[ct.curLoc.File] = file
|
||||||
|
}
|
||||||
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase {
|
func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase {
|
||||||
@@ -179,25 +183,36 @@ func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup {
|
|||||||
// 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 {
|
||||||
ct := (*Converter)(clientData)
|
ct := (*Converter)(clientData)
|
||||||
ct.UpdateCurFile(cursor)
|
ct.UpdateLoc(cursor)
|
||||||
|
|
||||||
|
curFile := ct.GetCurFile()
|
||||||
|
if curFile == nil {
|
||||||
|
return clang.ChildVisit_Continue
|
||||||
|
}
|
||||||
|
|
||||||
switch cursor.Kind {
|
switch cursor.Kind {
|
||||||
case clang.CursorInclusionDirective:
|
case clang.CursorInclusionDirective:
|
||||||
ct.ProcessInclude(cursor)
|
include := ct.ProcessInclude(cursor)
|
||||||
|
curFile.Includes = append(curFile.Includes, include)
|
||||||
case clang.CursorMacroDefinition:
|
case clang.CursorMacroDefinition:
|
||||||
ct.ProcessMarco(cursor)
|
marco := ct.ProcessMarco(cursor)
|
||||||
|
curFile.Macros = append(curFile.Macros, marco)
|
||||||
case clang.CursorEnumDecl:
|
case clang.CursorEnumDecl:
|
||||||
ct.ProcessEnum(cursor)
|
enum := ct.ProcessEnum(cursor)
|
||||||
|
curFile.Decls = append(curFile.Decls, enum)
|
||||||
case clang.CursorClassDecl:
|
case clang.CursorClassDecl:
|
||||||
ct.PushScope(cursor)
|
ct.PushScope(cursor)
|
||||||
ct.ProcessClass(cursor)
|
classDecl := ct.ProcessClass(cursor)
|
||||||
|
curFile.Decls = append(curFile.Decls, classDecl)
|
||||||
ct.PopScope()
|
ct.PopScope()
|
||||||
case clang.CursorStructDecl:
|
case clang.CursorStructDecl:
|
||||||
ct.ProcessStruct(cursor)
|
structDecl := ct.ProcessStruct(cursor)
|
||||||
|
curFile.Decls = append(curFile.Decls, structDecl)
|
||||||
case clang.CursorUnionDecl:
|
case clang.CursorUnionDecl:
|
||||||
ct.ProcessUnion(cursor)
|
unionDecl := ct.ProcessUnion(cursor)
|
||||||
|
curFile.Decls = append(curFile.Decls, unionDecl)
|
||||||
case clang.CursorFunctionDecl:
|
case clang.CursorFunctionDecl:
|
||||||
ct.curFile.Decls = append(ct.curFile.Decls, ct.ProcessFunc(cursor))
|
curFile.Decls = append(curFile.Decls, ct.ProcessFunc(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))
|
||||||
@@ -291,7 +306,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) {
|
func (ct *Converter) ProcessEnum(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)
|
||||||
@@ -301,20 +316,15 @@ func (ct *Converter) ProcessEnum(cursor clang.Cursor) {
|
|||||||
}
|
}
|
||||||
clang.VisitChildren(cursor, visitEnum, c.Pointer(ctx))
|
clang.VisitChildren(cursor, visitEnum, c.Pointer(ctx))
|
||||||
|
|
||||||
enum := &ast.EnumTypeDecl{
|
return &ast.EnumTypeDecl{
|
||||||
DeclBase: ct.CreateDeclBase(cursor),
|
DeclBase: ct.CreateDeclBase(cursor),
|
||||||
Name: &ast.Ident{Name: c.GoString(name.CStr())},
|
Name: &ast.Ident{Name: c.GoString(name.CStr())},
|
||||||
Items: items,
|
Items: items,
|
||||||
}
|
}
|
||||||
|
|
||||||
ct.curFile.Decls = append(ct.curFile.Decls, enum)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// current only collect marco which defined in file
|
// current only collect marco which defined in file
|
||||||
func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
|
func (ct *Converter) ProcessMarco(cursor clang.Cursor) *ast.Macro {
|
||||||
if ct.curFile == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
name := cursor.String()
|
name := cursor.String()
|
||||||
defer name.Dispose()
|
defer name.Dispose()
|
||||||
|
|
||||||
@@ -339,13 +349,13 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
|
|||||||
})
|
})
|
||||||
tokStr.Dispose()
|
tokStr.Dispose()
|
||||||
}
|
}
|
||||||
ct.curFile.Macros = append(ct.curFile.Macros, macro)
|
return macro
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessInclude(cursor clang.Cursor) {
|
func (ct *Converter) ProcessInclude(cursor clang.Cursor) *ast.Include {
|
||||||
name := cursor.String()
|
name := cursor.String()
|
||||||
defer name.Dispose()
|
defer name.Dispose()
|
||||||
ct.curFile.Includes = append(ct.curFile.Includes, &ast.Include{Path: c.GoString(name.CStr())})
|
return &ast.Include{Path: c.GoString(name.CStr())}
|
||||||
}
|
}
|
||||||
|
|
||||||
type visitFieldContext struct {
|
type visitFieldContext struct {
|
||||||
@@ -418,6 +428,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl {
|
func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl {
|
||||||
|
|
||||||
name := cursor.String()
|
name := cursor.String()
|
||||||
defer name.Dispose()
|
defer name.Dispose()
|
||||||
|
|
||||||
@@ -435,20 +446,16 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast
|
|||||||
return decl
|
return decl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessStruct(cursor clang.Cursor) {
|
func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl {
|
||||||
structDecl := ct.ProcessStructOrClass(cursor, ast.Struct)
|
return ct.ProcessStructOrClass(cursor, ast.Struct)
|
||||||
ct.curFile.Decls = append(ct.curFile.Decls, structDecl)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessUnion(cursor clang.Cursor) {
|
func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl {
|
||||||
structDecl := ct.ProcessStructOrClass(cursor, ast.Union)
|
return ct.ProcessStructOrClass(cursor, ast.Union)
|
||||||
ct.curFile.Decls = append(ct.curFile.Decls, structDecl)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessClass(cursor clang.Cursor) {
|
func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl {
|
||||||
classDecl := ct.ProcessStructOrClass(cursor, ast.Class)
|
return ct.ProcessStructOrClass(cursor, ast.Class)
|
||||||
// other logic for class
|
|
||||||
ct.curFile.Decls = append(ct.curFile.Decls, classDecl)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType {
|
func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType {
|
||||||
|
|||||||
Reference in New Issue
Block a user