llcppsigfetch:marco tokens & refine test

This commit is contained in:
luoliwoshang
2024-08-20 10:08:25 +08:00
parent f0e92343cb
commit bf8aa502f9
6 changed files with 104 additions and 210 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/c/clang"
"github.com/goplus/llgo/chore/llcppg/ast" "github.com/goplus/llgo/chore/llcppg/ast"
"github.com/goplus/llgo/chore/llcppg/token"
) )
type Converter struct { type Converter struct {
@@ -322,23 +323,17 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
tokensSlice := unsafe.Slice(tokens, int(numTokens)) tokensSlice := unsafe.Slice(tokens, int(numTokens))
macro := &ast.Macro{ macro := &ast.Macro{
Name: &ast.TokenInfo{ Name: c.GoString(name.CStr()),
Token: ast.Token(tokensSlice[0].Kind()), Tokens: make([]*ast.Token, 0),
Lit: c.GoString(ct.unit.Token(tokensSlice[0]).CStr()),
},
Body: make([]*ast.TokenInfo, 0),
} }
if numTokens > 1 { //have body for _, tok := range tokensSlice {
for i := 1; i < int(numTokens); i++ { tokStr := ct.unit.Token(tok)
tok := tokensSlice[i] macro.Tokens = append(macro.Tokens, &ast.Token{
tokStr := ct.unit.Token(tok) Token: toToken(tok),
macro.Body = append(macro.Body, &ast.TokenInfo{ Lit: c.GoString(tokStr.CStr()),
Token: ast.Token(tok.Kind()), })
Lit: c.GoString(tokStr.CStr()), tokStr.Dispose()
})
tokStr.Dispose()
}
} }
ct.curFile.Macros = append(ct.curFile.Macros, macro) 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.TypeULong || t.Kind == clang.TypeULongLong ||
t.Kind == clang.TypeUInt128 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)
}
}

View 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()
}
}

View File

@@ -1,8 +1,7 @@
package main package main
import ( import (
"github.com/goplus/llgo/c" test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test"
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
) )
func main() { func main() {
@@ -27,26 +26,7 @@ func TestFuncDecl() {
`float* foo(char str[], double x);`, `float* foo(char str[], double x);`,
`float* foo(int arr[3][4]);`, `float* foo(int arr[3][4]);`,
} }
test.RunTest("TestFuncDecl", testCases)
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()
}
} }
func TestScope() { func TestScope() {
@@ -69,26 +49,7 @@ func TestScope() {
}; };
}`, }`,
} }
test.RunTest("TestScope", testCases)
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()
}
} }
func TestComment() { func TestComment() {
@@ -118,26 +79,7 @@ void foo();`,
*/ */
void foo();`, void foo();`,
} }
test.RunTest("TestComment", testCases)
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()
}
} }
func TestStructDecl() { func TestStructDecl() {
@@ -155,26 +97,7 @@ func TestStructDecl() {
float foo(int a,double b);; float foo(int a,double b);;
};`, };`,
} }
test.RunTest("TestStructDecl", testCases)
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()
}
} }
func TestClassDecl() { func TestClassDecl() {
@@ -189,26 +112,7 @@ func TestClassDecl() {
float foo(int a,double b);; float foo(int a,double b);;
};`, };`,
} }
test.RunTest("TestClassDecl", testCases)
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()
}
} }
func TestEnumDecl() { func TestEnumDecl() {
@@ -229,24 +133,5 @@ func TestEnumDecl() {
c, c,
};`, };`,
} }
test.RunTest("TestEnumDecl", testCases)
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()
}
} }

View File

@@ -6,13 +6,10 @@ TestDefine Case 1:
"decls": [], "decls": [],
"includes": [], "includes": [],
"macros": [{ "macros": [{
"Name": { "Name": "DEBUG",
"Token": 2, "Tokens": [{
"Lit": "OK"
},
"Body": [{
"Token": 3, "Token": 3,
"Lit": "1" "Lit": "DEBUG"
}] }]
}] }]
} }
@@ -25,45 +22,64 @@ TestDefine Case 2:
"decls": [], "decls": [],
"includes": [], "includes": [],
"macros": [{ "macros": [{
"Name": { "Name": "OK",
"Token": 2, "Tokens": [{
"Lit": "SQUARE" "Token": 3,
}, "Lit": "OK"
"Body": [{ }, {
"Token": 0, "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": "(" "Lit": "("
}, { }, {
"Token": 2, "Token": 3,
"Lit": "x" "Lit": "x"
}, { }, {
"Token": 0, "Token": 1,
"Lit": ")" "Lit": ")"
}, { }, {
"Token": 0, "Token": 1,
"Lit": "(" "Lit": "("
}, { }, {
"Token": 0, "Token": 1,
"Lit": "(" "Lit": "("
}, { }, {
"Token": 2, "Token": 3,
"Lit": "x" "Lit": "x"
}, { }, {
"Token": 0, "Token": 1,
"Lit": ")" "Lit": ")"
}, { }, {
"Token": 0, "Token": 1,
"Lit": "*" "Lit": "*"
}, { }, {
"Token": 0, "Token": 1,
"Lit": "(" "Lit": "("
}, { }, {
"Token": 2, "Token": 3,
"Lit": "x" "Lit": "x"
}, { }, {
"Token": 0, "Token": 1,
"Lit": ")" "Lit": ")"
}, { }, {
"Token": 0, "Token": 1,
"Lit": ")" "Lit": ")"
}] }]
}] }]

View File

@@ -1,8 +1,7 @@
package main package main
import ( import (
"github.com/goplus/llgo/c" test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test"
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
) )
func main() { func main() {
@@ -12,29 +11,11 @@ func main() {
func TestDefine() { func TestDefine() {
testCases := []string{ testCases := []string{
`#define DEBUG`,
`#define OK 1`, `#define OK 1`,
`#define SQUARE(x) ((x) * (x))`, `#define SQUARE(x) ((x) * (x))`,
} }
test.RunTest("TestDefine", testCases)
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()
}
} }
func TestInclude() { func TestInclude() {
@@ -42,24 +23,5 @@ func TestInclude() {
`#include "foo.h"`, `#include "foo.h"`,
// `#include <limits.h>`, // Standard libraries are mostly platform-dependent // `#include <limits.h>`, // Standard libraries are mostly platform-dependent
} }
test.RunTest("TestInclude", testCases)
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()
}
} }

View File

@@ -41,22 +41,22 @@ func MarshalASTFile(file *ast.File) *cjson.JSON {
macros := cjson.Array() macros := cjson.Array()
for _, m := range file.Macros { for _, m := range file.Macros {
marco := cjson.Object() marco := cjson.Object()
marco.SetItem(c.Str("Name"), TokenInfo(m.Name)) marco.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(m.Name)))
body := cjson.Array() tokens := cjson.Array()
for _, b := range m.Body { for _, tok := range m.Tokens {
body.AddItem(TokenInfo(b)) tokens.AddItem(Token(tok))
} }
marco.SetItem(c.Str("Body"), body) marco.SetItem(c.Str("Tokens"), tokens)
macros.AddItem(marco) macros.AddItem(marco)
} }
root.SetItem(c.Str("macros"), macros) root.SetItem(c.Str("macros"), macros)
} }
return root return root
} }
func TokenInfo(t *ast.TokenInfo) *cjson.JSON { func Token(tok *ast.Token) *cjson.JSON {
root := cjson.Object() root := cjson.Object()
root.SetItem(c.Str("Token"), cjson.Number(float64(t.Token))) root.SetItem(c.Str("Token"), cjson.Number(float64(tok.Token)))
root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(t.Lit))) root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(tok.Lit)))
return root return root
} }