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 ( import (
"fmt" "fmt"
"github.com/goplus/llgo/chore/_xtool/llcppsymg/common"
"os" "os"
"strconv" "strconv"
"unsafe" "unsafe"
@@ -11,31 +12,15 @@ import (
"github.com/goplus/llgo/c/clang" "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 { type Context struct {
namespaceName string namespaceName string
className string className string
astInfo []ASTInformation astInfo []common.ASTInformation
} }
func newContext() *Context { func newContext() *Context {
return &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() var context = newContext()
func collectFuncInfo(cursor clang.Cursor) ASTInformation { func collectFuncInfo(cursor clang.Cursor) common.ASTInformation {
info := ASTInformation{ info := common.ASTInformation{
Namespace: context.namespaceName, Namespace: context.namespaceName,
Class: context.className, Class: context.className,
} }
@@ -97,12 +82,12 @@ func collectFuncInfo(cursor clang.Cursor) ASTInformation {
info.ReturnType = c.GoString(typeStr.CStr()) info.ReturnType = c.GoString(typeStr.CStr())
// c.Printf(c.Str("Parameters(%d): ( "), cursor.NumArguments()) // 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++ { for i := 0; i < int(cursor.NumArguments()); i++ {
argCurSor := cursor.Argument(c.Uint(i)) argCurSor := cursor.Argument(c.Uint(i))
argType := argCurSor.Type().String() argType := argCurSor.Type().String()
argName := argCurSor.String() argName := argCurSor.String()
info.Parameters[i] = Parameter{ info.Parameters[i] = common.Parameter{
Name: c.GoString(argName.CStr()), Name: c.GoString(argName.CStr()),
Type: c.GoString(argType.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 return clang.ChildVisit_Continue
} }
func parse(filename *c.Char) []ASTInformation { func parse(filename *c.Char) []common.ASTInformation {
index := clang.CreateIndex(0, 0) index := clang.CreateIndex(0, 0)
args := make([]*c.Char, 3) args := make([]*c.Char, 3)
args[0] = c.Str("-x") args[0] = c.Str("-x")
@@ -181,7 +166,7 @@ func parse(filename *c.Char) []ASTInformation {
return context.astInfo return context.astInfo
} }
func printJson(infos []ASTInformation) { func printJson(infos []common.ASTInformation) {
root := cjson.Array() root := cjson.Array()
for _, info := range infos { for _, info := range infos {

View File

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