c/clang/symg:use llvm to demangle name

This commit is contained in:
luoliwoshang
2024-07-29 17:21:32 +08:00
parent f427c0532d
commit 01d0338851

View File

@@ -32,6 +32,7 @@ import (
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/chore/llcppg/types" "github.com/goplus/llgo/chore/llcppg/types"
"github.com/goplus/llgo/cpp/llvm"
) )
func main() { func main() {
@@ -53,8 +54,8 @@ func main() {
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile) fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile)
} }
symbols, err := parseDylibSymbols(config.Libs) symbols, err := parseDylibSymbols(config.Libs)
check(err) check(err)
files, err := parseHeaderFile(config) files, err := parseHeaderFile(config)
@@ -109,7 +110,7 @@ func getConf(data []byte) (config types.Config, err error) {
func parseDylibSymbols(lib string) ([]types.CPPSymbol, error) { func parseDylibSymbols(lib string) ([]types.CPPSymbol, error) {
dylibPath, _ := generateDylibPath(lib) dylibPath, _ := generateDylibPath(lib)
nmCmd := exec.Command("nm", "-gU", dylibPath) nmCmd := exec.Command("nm", "-gU", dylibPath)
nmOutput, err := nmCmd.Output() nmOutput, err := nmCmd.Output() // maybe lock
if err != nil { if err != nil {
return nil, errors.New("failed to execute nm command") return nil, errors.New("failed to execute nm command")
} }
@@ -171,13 +172,9 @@ func parseNmOutput(output []byte) []types.CPPSymbol {
} }
func decodeSymbolName(symbolName string) (string, error) { func decodeSymbolName(symbolName string) (string, error) {
cppfiltCmd := exec.Command("c++filt", symbolName) llvm.ItaniumDemangle(symbolName, true)
cppfiltOutput, err := cppfiltCmd.Output() demangleName := c.GoString(llvm.ItaniumDemangle(symbolName, true))
if err != nil { decodedName := strings.TrimSpace(string(demangleName))
return "", errors.New("failed to execute c++filt command")
}
decodedName := strings.TrimSpace(string(cppfiltOutput))
decodedName = strings.ReplaceAll(decodedName, "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const", "std::string") decodedName = strings.ReplaceAll(decodedName, "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const", "std::string")
return decodedName, nil return decodedName, nil
} }