feat(llgo/xtools): add TrimPrefix

This commit is contained in:
morpingsss
2024-07-26 15:00:06 +08:00
committed by luoliwoshang
parent c53484f92e
commit ce36a25ba3
2 changed files with 29 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"github.com/goplus/llgo/chore/_xtool/llcppsymg/common"
"os"
"strconv"
"unsafe"
@@ -11,31 +12,15 @@ import (
"github.com/goplus/llgo/c/clang"
)
type ASTInformation struct {
Namespace string `json:"namespace"`
Class string `json:"class"`
Name string `json:"name"`
// BaseClasses []string `json:"baseClasses"`
ReturnType string `json:"returnType"`
Location string `json:"location"`
Parameters []Parameter `json:"parameters"`
Symbol string `json:"symbol"`
}
type Parameter struct {
Name string `json:"name"`
Type string `json:"type"`
}
type Context struct {
namespaceName string
className string
astInfo []ASTInformation
astInfo []common.ASTInformation
}
func newContext() *Context {
return &Context{
astInfo: make([]ASTInformation, 0),
astInfo: make([]common.ASTInformation, 0),
}
}
@@ -49,8 +34,8 @@ func (c *Context) setClassName(name string) {
var context = newContext()
func collectFuncInfo(cursor clang.Cursor) ASTInformation {
info := ASTInformation{
func collectFuncInfo(cursor clang.Cursor) common.ASTInformation {
info := common.ASTInformation{
Namespace: context.namespaceName,
Class: context.className,
}
@@ -97,12 +82,12 @@ func collectFuncInfo(cursor clang.Cursor) ASTInformation {
info.ReturnType = c.GoString(typeStr.CStr())
// c.Printf(c.Str("Parameters(%d): ( "), cursor.NumArguments())
info.Parameters = make([]Parameter, cursor.NumArguments())
info.Parameters = make([]common.Parameter, cursor.NumArguments())
for i := 0; i < int(cursor.NumArguments()); i++ {
argCurSor := cursor.Argument(c.Uint(i))
argType := argCurSor.Type().String()
argName := argCurSor.String()
info.Parameters[i] = Parameter{
info.Parameters[i] = common.Parameter{
Name: c.GoString(argName.CStr()),
Type: c.GoString(argType.CStr()),
}
@@ -154,7 +139,7 @@ func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitRe
return clang.ChildVisit_Continue
}
func parse(filename *c.Char) []ASTInformation {
func parse(filename *c.Char) []common.ASTInformation {
index := clang.CreateIndex(0, 0)
args := make([]*c.Char, 3)
args[0] = c.Str("-x")
@@ -181,7 +166,7 @@ func parse(filename *c.Char) []ASTInformation {
return context.astInfo
}
func printJson(infos []ASTInformation) {
func printJson(infos []common.ASTInformation) {
root := cjson.Array()
for _, info := range infos {

View File

@@ -35,10 +35,10 @@ import (
)
func main() {
cfgFile := "llcppg.cfg"
if len(os.Args) > 1 {
cfgFile = os.Args[1]
}
cfgFile := "/Users/lixianyu/go/src/llgo/chore/_xtool/llcppsymg/llcppg.cfg"
//if len(os.Args) > 1 {
// cfgFile = os.Args[1]
//}
var data []byte
var err error
@@ -59,7 +59,7 @@ func main() {
files, err := parseHeaderFile(config)
check(err)
symbolInfo := getCommonSymbols(symbols, files)
symbolInfo := getCommonSymbols(symbols, files, config.TrimPrefixes)
jsonData, err := json.MarshalIndent(symbolInfo, "", " ")
check(err)
@@ -195,7 +195,7 @@ func generateHeaderFilePath(cflags string, files []string) []string {
return includePaths
}
func getCommonSymbols(dylibSymbols []common.CPPSymbol, astInfoList []common.ASTInformation) []common.SymbolInfo {
func getCommonSymbols(dylibSymbols []common.CPPSymbol, astInfoList []common.ASTInformation, prefix []string) []common.SymbolInfo {
var commonSymbols []common.SymbolInfo
functionNameMap := make(map[string]int)
@@ -207,7 +207,7 @@ func getCommonSymbols(dylibSymbols []common.CPPSymbol, astInfoList []common.ASTI
symbolInfo := common.SymbolInfo{
Mangle: dylibSym.Symbol,
CPP: cppName,
Go: generateMangle(astInfo, functionNameMap[cppName]),
Go: generateMangle(astInfo, functionNameMap[cppName], prefix),
}
commonSymbols = append(commonSymbols, symbolInfo)
break
@@ -226,7 +226,10 @@ func generateCPPName(astInfo common.ASTInformation) string {
return cppName
}
func generateMangle(astInfo common.ASTInformation, count int) string {
func generateMangle(astInfo common.ASTInformation, count int, prefixes []string) string {
// 去除前缀
astInfo.Class = removePrefix(astInfo.Class, prefixes)
astInfo.Name = removePrefix(astInfo.Name, prefixes)
res := ""
if astInfo.Class != "" {
if astInfo.Class == astInfo.Name {
@@ -254,6 +257,15 @@ func generateMangle(astInfo common.ASTInformation, count int) string {
return res
}
func removePrefix(str string, prefixes []string) string {
for _, prefix := range prefixes {
if strings.HasPrefix(str, prefix) {
return strings.TrimPrefix(str, prefix)
}
}
return str
}
var (
reSubcmd = regexp.MustCompile(`\$\([^)]+\)`)
reFlag = regexp.MustCompile(`[^ \t\n]+`)