llcppsigfetch:basic marco process
This commit is contained in:
@@ -163,6 +163,7 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
|
|||||||
// todo(zzy)
|
// todo(zzy)
|
||||||
case clang.CursorMacroDefinition:
|
case clang.CursorMacroDefinition:
|
||||||
// todo(zzy)
|
// todo(zzy)
|
||||||
|
ct.ProcessMarco(cursor)
|
||||||
case clang.CursorEnumDecl:
|
case clang.CursorEnumDecl:
|
||||||
// todo(zzy)
|
// todo(zzy)
|
||||||
case clang.CursorClassDecl:
|
case clang.CursorClassDecl:
|
||||||
@@ -240,6 +241,42 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl {
|
|||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// current only collect marco which defined in file
|
||||||
|
func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
|
||||||
|
if ct.curFile == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name := cursor.String()
|
||||||
|
defer name.Dispose()
|
||||||
|
|
||||||
|
ran := cursor.Extent()
|
||||||
|
var numTokens c.Uint
|
||||||
|
var tokens *clang.Token
|
||||||
|
ct.unit.Tokenize(ran, &tokens, &numTokens)
|
||||||
|
tokensSlice := unsafe.Slice(tokens, int(numTokens))
|
||||||
|
|
||||||
|
macro := &ast.Macro{
|
||||||
|
Name: &ast.TokenInfo{
|
||||||
|
Token: ast.Token(tokensSlice[0].Kind()),
|
||||||
|
Lit: c.GoString(ct.unit.Token(tokensSlice[0]).CStr()),
|
||||||
|
},
|
||||||
|
Body: make([]*ast.TokenInfo, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
if numTokens > 1 { //have body
|
||||||
|
for i := 1; i < int(numTokens); i++ {
|
||||||
|
tok := tokensSlice[i]
|
||||||
|
tokStr := ct.unit.Token(tok)
|
||||||
|
macro.Body = append(macro.Body, &ast.TokenInfo{
|
||||||
|
Token: ast.Token(tok.Kind()),
|
||||||
|
Lit: c.GoString(tokStr.CStr()),
|
||||||
|
})
|
||||||
|
tokStr.Dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ct.curFile.Macros = append(ct.curFile.Macros, macro)
|
||||||
|
}
|
||||||
|
|
||||||
type visitFieldContext struct {
|
type visitFieldContext struct {
|
||||||
params *ast.FieldList
|
params *ast.FieldList
|
||||||
converter *Converter
|
converter *Converter
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#stdout
|
||||||
|
TestDefine Case 1:
|
||||||
|
{
|
||||||
|
"temp.h": {
|
||||||
|
"path": "temp.h",
|
||||||
|
"decls": [],
|
||||||
|
"includes": [],
|
||||||
|
"macros": [{
|
||||||
|
"Name": {
|
||||||
|
"Token": 2,
|
||||||
|
"Lit": "foo"
|
||||||
|
},
|
||||||
|
"Body": [{
|
||||||
|
"Token": 3,
|
||||||
|
"Lit": "1"
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#stderr
|
||||||
|
|
||||||
|
#exit 0
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
TestDefine()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefine() {
|
||||||
|
testCases := []string{
|
||||||
|
`#define foo 1`,
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, content := range testCases {
|
||||||
|
converter, err := parse.NewConverter(content, true)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = converter.Convert()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
json := converter.MarshalASTFiles()
|
||||||
|
c.Printf(c.Str("TestDefine Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||||
|
|
||||||
|
converter.Dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,9 +34,24 @@ func MarshalASTFile(file *ast.File) *cjson.JSON {
|
|||||||
// json:macros,omitempty
|
// json:macros,omitempty
|
||||||
if file.Macros != nil {
|
if file.Macros != nil {
|
||||||
macros := cjson.Array()
|
macros := cjson.Array()
|
||||||
|
for _, m := range file.Macros {
|
||||||
|
marco := cjson.Object()
|
||||||
|
marco.SetItem(c.Str("Name"), TokenInfo(m.Name))
|
||||||
|
body := cjson.Array()
|
||||||
|
for _, b := range m.Body {
|
||||||
|
body.AddItem(TokenInfo(b))
|
||||||
|
}
|
||||||
|
marco.SetItem(c.Str("Body"), body)
|
||||||
|
macros.AddItem(marco)
|
||||||
|
}
|
||||||
root.SetItem(c.Str("macros"), macros)
|
root.SetItem(c.Str("macros"), macros)
|
||||||
}
|
}
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
func TokenInfo(t *ast.TokenInfo) *cjson.JSON {
|
||||||
|
root := cjson.Object()
|
||||||
|
root.SetItem(c.Str("Token"), cjson.Number(float64(t.Token)))
|
||||||
|
root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(t.Lit)))
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user