llcppsigfetch:marco tokens & refine test
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/goplus/llgo/c/cjson"
|
||||
"github.com/goplus/llgo/c/clang"
|
||||
"github.com/goplus/llgo/chore/llcppg/ast"
|
||||
"github.com/goplus/llgo/chore/llcppg/token"
|
||||
)
|
||||
|
||||
type Converter struct {
|
||||
@@ -322,23 +323,17 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
|
||||
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),
|
||||
Name: c.GoString(name.CStr()),
|
||||
Tokens: make([]*ast.Token, 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()
|
||||
}
|
||||
for _, tok := range tokensSlice {
|
||||
tokStr := ct.unit.Token(tok)
|
||||
macro.Tokens = append(macro.Tokens, &ast.Token{
|
||||
Token: toToken(tok),
|
||||
Lit: c.GoString(tokStr.CStr()),
|
||||
})
|
||||
tokStr.Dispose()
|
||||
}
|
||||
ct.curFile.Macros = append(ct.curFile.Macros, macro)
|
||||
}
|
||||
@@ -530,3 +525,11 @@ func IsExplicitUnsigned(t clang.Type) bool {
|
||||
t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong ||
|
||||
t.Kind == clang.TypeUInt128
|
||||
}
|
||||
|
||||
func toToken(tok clang.Token) token.Token {
|
||||
if tok.Kind() < clang.Punctuation || tok.Kind() > clang.Comment {
|
||||
return token.ILLEGAL
|
||||
} else {
|
||||
return token.Token(tok.Kind() + 1)
|
||||
}
|
||||
}
|
||||
|
||||
28
chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go
Normal file
28
chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package cvttest
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||
)
|
||||
|
||||
func RunTest(testName string, testCases []string) {
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("%s Case %d:\n%s\n\n"), c.AllocaCStr(testName), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||
test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -27,26 +26,7 @@ func TestFuncDecl() {
|
||||
`float* foo(char str[], double x);`,
|
||||
`float* foo(int arr[3][4]);`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestFuncDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestFuncDecl", testCases)
|
||||
}
|
||||
|
||||
func TestScope() {
|
||||
@@ -69,26 +49,7 @@ func TestScope() {
|
||||
};
|
||||
}`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestScope Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestScope", testCases)
|
||||
}
|
||||
|
||||
func TestComment() {
|
||||
@@ -118,26 +79,7 @@ void foo();`,
|
||||
*/
|
||||
void foo();`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestComment", testCases)
|
||||
}
|
||||
|
||||
func TestStructDecl() {
|
||||
@@ -155,26 +97,7 @@ func TestStructDecl() {
|
||||
float foo(int a,double b);;
|
||||
};`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestStructDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestStructDecl", testCases)
|
||||
}
|
||||
|
||||
func TestClassDecl() {
|
||||
@@ -189,26 +112,7 @@ func TestClassDecl() {
|
||||
float foo(int a,double b);;
|
||||
};`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestClassDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestClassDecl", testCases)
|
||||
}
|
||||
|
||||
func TestEnumDecl() {
|
||||
@@ -229,24 +133,5 @@ func TestEnumDecl() {
|
||||
c,
|
||||
};`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestEnumDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestEnumDecl", testCases)
|
||||
}
|
||||
|
||||
@@ -6,13 +6,10 @@ TestDefine Case 1:
|
||||
"decls": [],
|
||||
"includes": [],
|
||||
"macros": [{
|
||||
"Name": {
|
||||
"Token": 2,
|
||||
"Lit": "OK"
|
||||
},
|
||||
"Body": [{
|
||||
"Name": "DEBUG",
|
||||
"Tokens": [{
|
||||
"Token": 3,
|
||||
"Lit": "1"
|
||||
"Lit": "DEBUG"
|
||||
}]
|
||||
}]
|
||||
}
|
||||
@@ -25,45 +22,64 @@ TestDefine Case 2:
|
||||
"decls": [],
|
||||
"includes": [],
|
||||
"macros": [{
|
||||
"Name": {
|
||||
"Token": 2,
|
||||
"Lit": "SQUARE"
|
||||
},
|
||||
"Body": [{
|
||||
"Token": 0,
|
||||
"Name": "OK",
|
||||
"Tokens": [{
|
||||
"Token": 3,
|
||||
"Lit": "OK"
|
||||
}, {
|
||||
"Token": 4,
|
||||
"Lit": "1"
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
TestDefine Case 3:
|
||||
{
|
||||
"temp.h": {
|
||||
"path": "temp.h",
|
||||
"decls": [],
|
||||
"includes": [],
|
||||
"macros": [{
|
||||
"Name": "SQUARE",
|
||||
"Tokens": [{
|
||||
"Token": 3,
|
||||
"Lit": "SQUARE"
|
||||
}, {
|
||||
"Token": 1,
|
||||
"Lit": "("
|
||||
}, {
|
||||
"Token": 2,
|
||||
"Token": 3,
|
||||
"Lit": "x"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": ")"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": "("
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": "("
|
||||
}, {
|
||||
"Token": 2,
|
||||
"Token": 3,
|
||||
"Lit": "x"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": ")"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": "*"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": "("
|
||||
}, {
|
||||
"Token": 2,
|
||||
"Token": 3,
|
||||
"Lit": "x"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": ")"
|
||||
}, {
|
||||
"Token": 0,
|
||||
"Token": 1,
|
||||
"Lit": ")"
|
||||
}]
|
||||
}]
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||
test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -12,29 +11,11 @@ func main() {
|
||||
|
||||
func TestDefine() {
|
||||
testCases := []string{
|
||||
`#define DEBUG`,
|
||||
`#define OK 1`,
|
||||
`#define SQUARE(x) ((x) * (x))`,
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: 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()
|
||||
}
|
||||
test.RunTest("TestDefine", testCases)
|
||||
}
|
||||
|
||||
func TestInclude() {
|
||||
@@ -42,24 +23,5 @@ func TestInclude() {
|
||||
`#include "foo.h"`,
|
||||
// `#include <limits.h>`, // Standard libraries are mostly platform-dependent
|
||||
}
|
||||
|
||||
for i, content := range testCases {
|
||||
converter, err := parse.NewConverter(&parse.Config{
|
||||
File: content,
|
||||
Temp: true,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = converter.Convert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
json := converter.MarshalASTFiles()
|
||||
c.Printf(c.Str("TestInclude Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
|
||||
|
||||
converter.Dispose()
|
||||
}
|
||||
test.RunTest("TestInclude", testCases)
|
||||
}
|
||||
|
||||
@@ -41,22 +41,22 @@ func MarshalASTFile(file *ast.File) *cjson.JSON {
|
||||
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("Name"), cjson.String(c.AllocaCStr(m.Name)))
|
||||
tokens := cjson.Array()
|
||||
for _, tok := range m.Tokens {
|
||||
tokens.AddItem(Token(tok))
|
||||
}
|
||||
marco.SetItem(c.Str("Body"), body)
|
||||
marco.SetItem(c.Str("Tokens"), tokens)
|
||||
macros.AddItem(marco)
|
||||
}
|
||||
root.SetItem(c.Str("macros"), macros)
|
||||
}
|
||||
return root
|
||||
}
|
||||
func TokenInfo(t *ast.TokenInfo) *cjson.JSON {
|
||||
func Token(tok *ast.Token) *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)))
|
||||
root.SetItem(c.Str("Token"), cjson.Number(float64(tok.Token)))
|
||||
root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(tok.Lit)))
|
||||
return root
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user