llcppsigfetch:basic cjson dump for ast.File
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
"github.com/goplus/llgo/chore/llcppg/ast"
|
||||
)
|
||||
@@ -79,19 +80,19 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
|
||||
|
||||
switch cursor.Kind {
|
||||
case clang.CursorInclusionDirective:
|
||||
fmt.Println("todo: Process include")
|
||||
// todo(zzy)
|
||||
return clang.ChildVisit_Continue
|
||||
case clang.CursorMacroDefinition:
|
||||
fmt.Println("todo: Process macro")
|
||||
// todo(zzy)
|
||||
return clang.ChildVisit_Continue
|
||||
case clang.CursorEnumDecl:
|
||||
fmt.Println("todo: Process enum")
|
||||
// todo(zzy)
|
||||
return clang.ChildVisit_Continue
|
||||
case clang.CursorClassDecl:
|
||||
ct.ProcessClass(cursor)
|
||||
return clang.ChildVisit_Continue
|
||||
case clang.CursorStructDecl:
|
||||
fmt.Println("todo: Process struct")
|
||||
// todo(zzy)
|
||||
return clang.ChildVisit_Continue
|
||||
case clang.CursorFunctionDecl:
|
||||
ct.ProcessFunc(cursor)
|
||||
@@ -117,8 +118,7 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
|
||||
defer filename.Dispose()
|
||||
|
||||
if filename.CStr() == nil {
|
||||
// For some built-in macros, there is no file.
|
||||
println("todo: filename is empty")
|
||||
//todo(zzy): For some built-in macros, there is no file.
|
||||
return
|
||||
}
|
||||
|
||||
@@ -260,3 +260,65 @@ func IsExplicitUnsigned(t clang.Type) bool {
|
||||
t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong ||
|
||||
t.Kind == clang.TypeUInt128
|
||||
}
|
||||
|
||||
func (ct *Converter) GetFilesJSON() *cjson.JSON {
|
||||
root := cjson.Object()
|
||||
|
||||
Files := cjson.Object()
|
||||
root.SetItem(c.Str("Files"), Files)
|
||||
|
||||
for _, file := range ct.files {
|
||||
f := cjson.Object()
|
||||
f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path)))
|
||||
ct.FileJSON(file, f)
|
||||
Files.SetItem(c.AllocaCStr(file.Path), f)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) {
|
||||
decls := cjson.Array()
|
||||
includes := cjson.Array()
|
||||
macros := cjson.Array()
|
||||
|
||||
for _, decl := range file.Decls {
|
||||
ct.DeclJSON(decl, decls)
|
||||
}
|
||||
|
||||
root.SetItem(c.Str("decls"), decls)
|
||||
root.SetItem(c.Str("includes"), includes)
|
||||
root.SetItem(c.Str("macros"), macros)
|
||||
}
|
||||
|
||||
func (ct *Converter) DeclJSON(decl ast.Decl, root *cjson.JSON) {
|
||||
switch d := decl.(type) {
|
||||
case *ast.FuncDecl:
|
||||
fn := cjson.Object()
|
||||
fntype := cjson.Object()
|
||||
fn.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name.Name)))
|
||||
ct.TypeJSON(d.Type, fntype)
|
||||
fn.SetItem(c.Str("Type"), fntype)
|
||||
root.AddItem(fn)
|
||||
}
|
||||
}
|
||||
func (ct *Converter) TypeJSON(t ast.Expr, root *cjson.JSON) {
|
||||
|
||||
switch d := t.(type) {
|
||||
case *ast.FuncType:
|
||||
params := cjson.Array()
|
||||
|
||||
for _, p := range d.Params.List {
|
||||
param := cjson.Object()
|
||||
ct.TypeJSON(p.Type, param)
|
||||
params.AddItem(param)
|
||||
}
|
||||
|
||||
root.SetItem(c.Str("Params"), params)
|
||||
ret := cjson.Object()
|
||||
ct.TypeJSON(d.Ret, ret)
|
||||
root.SetItem(c.Str("Ret"), ret)
|
||||
case *ast.BuiltinType:
|
||||
root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind)))
|
||||
root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags)))
|
||||
}
|
||||
}
|
||||
|
||||
26
chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go
Normal file
26
chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||
)
|
||||
|
||||
func main() {
|
||||
TestFuncDecl()
|
||||
}
|
||||
|
||||
func TestFuncDecl() {
|
||||
content := `int foo(int a, int b);`
|
||||
converter, err := parse.NewConverter(content, true)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer converter.Dispose()
|
||||
converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
json := converter.GetFilesJSON()
|
||||
c.Printf(c.Str("%s\n"), json.Print())
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#stdout
|
||||
{
|
||||
"Files": {
|
||||
"temp.h": {
|
||||
"Path": "temp.h",
|
||||
"decls": [{
|
||||
"Name": "foo",
|
||||
"Type": {
|
||||
"Params": [{
|
||||
"Kind": 6,
|
||||
"Flags": 0
|
||||
}, {
|
||||
"Kind": 6,
|
||||
"Flags": 0
|
||||
}],
|
||||
"Ret": {
|
||||
"Kind": 6,
|
||||
"Flags": 0
|
||||
}
|
||||
}
|
||||
}],
|
||||
"includes": [],
|
||||
"macros": []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#stderr
|
||||
|
||||
#exit 0
|
||||
Reference in New Issue
Block a user