Merge pull request #124 from luoliwoshang/llcppsymg/underprefix

llcppsymg:underprefix
This commit is contained in:
张之阳
2024-10-26 18:56:56 +08:00
committed by GitHub
7 changed files with 56 additions and 39 deletions

View File

@@ -28,10 +28,10 @@ Before: Class: INIReader, Method: HasValue After: (*Reader).HasValue
=== Test Case: C++ Class with Methods ===
Parsed Symbols:
Symbol Map GoName: (*Reader).Init__1, ProtoName In HeaderFile: INIReader::INIReader(const char *, int), MangledName: ZN9INIReaderC1EPKci
Symbol Map GoName: (*Reader).Init, ProtoName In HeaderFile: INIReader::INIReader(const int &), MangledName: ZN9INIReaderC1ERKi
Symbol Map GoName: (*Reader).Dispose, ProtoName In HeaderFile: INIReader::~INIReader(), MangledName: ZN9INIReaderD1Ev
Symbol Map GoName: (*Reader).ParseError, ProtoName In HeaderFile: INIReader::ParseError(), MangledName: ZNK9INIReader10ParseErrorEv
Symbol Map GoName: (*Reader).Init__1, ProtoName In HeaderFile: INIReader::INIReader(const char *, int), MangledName: _ZN9INIReaderC1EPKci
Symbol Map GoName: (*Reader).Init, ProtoName In HeaderFile: INIReader::INIReader(const int &), MangledName: _ZN9INIReaderC1ERKi
Symbol Map GoName: (*Reader).Dispose, ProtoName In HeaderFile: INIReader::~INIReader(), MangledName: _ZN9INIReaderD1Ev
Symbol Map GoName: (*Reader).ParseError, ProtoName In HeaderFile: INIReader::ParseError(), MangledName: _ZNK9INIReader10ParseErrorEv
=== Test Case: C Functions ===
Parsed Symbols:

View File

@@ -10,9 +10,9 @@ Mangle: lua_callk, CPP: lua_callk(lua_State *, int, int, lua_KContext, lua_KFunc
Test Case: INIReader and Std library symbols
Common Symbols (3):
Mangle: ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x, CPP: INIReader::GetInteger64(const std::string &, const std::string &, int64_t), Go: (*Reader).GetInteger64
Mangle: ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d, CPP: INIReader::GetReal(const std::string &, const std::string &, double), Go: (*Reader).GetReal
Mangle: ZNK9INIReader10ParseErrorEv, CPP: INIReader::ParseError(), Go: (*Reader).ParseError
Mangle: _ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x, CPP: INIReader::GetInteger64(const std::string &, const std::string &, int64_t), Go: (*Reader).GetInteger64
Mangle: _ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d, CPP: INIReader::GetReal(const std::string &, const std::string &, double), Go: (*Reader).GetReal
Mangle: _ZNK9INIReader10ParseErrorEv, CPP: INIReader::ParseError(), Go: (*Reader).ParseError
=== Test ReadExistingSymbolTable ===
Symbols read from the file:

View File

@@ -27,11 +27,11 @@ func TestGetCommonSymbols() {
{
name: "Lua symbols",
dylibSymbols: []*nm.Symbol{
{Name: "_lua_absindex"},
{Name: "_lua_arith"},
{Name: "_lua_atpanic"},
{Name: "_lua_callk"},
{Name: "_lua_lib_nonexistent"},
{Name: symbol.AddSymbolPrefixUnder("lua_absindex", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_arith", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_atpanic", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_callk", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_lib_nonexistent", false)},
},
headerSymbols: map[string]*parse.SymbolInfo{
"lua_absindex": {ProtoName: "lua_absindex(lua_State *, int)", GoName: "Absindex"},
@@ -44,16 +44,16 @@ func TestGetCommonSymbols() {
{
name: "INIReader and Std library symbols",
dylibSymbols: []*nm.Symbol{
{Name: "_ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x"},
{Name: "_ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d"},
{Name: "_ZNK9INIReader10ParseErrorEv"},
{Name: symbol.AddSymbolPrefixUnder("ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x", true)},
{Name: symbol.AddSymbolPrefixUnder("ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d", true)},
{Name: symbol.AddSymbolPrefixUnder("ZNK9INIReader10ParseErrorEv", true)},
},
headerSymbols: map[string]*parse.SymbolInfo{
"ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x": {GoName: "(*Reader).GetInteger64", ProtoName: "INIReader::GetInteger64(const std::string &, const std::string &, int64_t)"},
"ZNK9INIReader13GetUnsigned64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_y": {GoName: "(*Reader).GetUnsigned64", ProtoName: "INIReader::GetUnsigned64(const std::string &, const std::string &, uint64_t)"},
"ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d": {GoName: "(*Reader).GetReal", ProtoName: "INIReader::GetReal(const std::string &, const std::string &, double)"},
"ZNK9INIReader10ParseErrorEv": {GoName: "(*Reader).ParseError", ProtoName: "INIReader::ParseError()"},
"ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b": {GoName: "(*Reader).GetBoolean", ProtoName: "INIReader::GetBoolean(const std::string &, const std::string &, bool)"},
"_ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x": {GoName: "(*Reader).GetInteger64", ProtoName: "INIReader::GetInteger64(const std::string &, const std::string &, int64_t)"},
"_ZNK9INIReader13GetUnsigned64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_y": {GoName: "(*Reader).GetUnsigned64", ProtoName: "INIReader::GetUnsigned64(const std::string &, const std::string &, uint64_t)"},
"_ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d": {GoName: "(*Reader).GetReal", ProtoName: "INIReader::GetReal(const std::string &, const std::string &, double)"},
"_ZNK9INIReader10ParseErrorEv": {GoName: "(*Reader).ParseError", ProtoName: "INIReader::ParseError()"},
"_ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b": {GoName: "(*Reader).GetBoolean", ProtoName: "INIReader::GetBoolean(const std::string &, const std::string &, bool)"},
},
},
}

View File

@@ -1,23 +1,23 @@
#stdout
=== Test Case: inireader ===
[{
"mangle": "ZN9INIReaderC1EPKc",
"mangle": "_ZN9INIReaderC1EPKc",
"c++": "INIReader::INIReader(const char *)",
"go": "(*Reader).Init"
}, {
"mangle": "ZN9INIReaderC1EPKcl",
"mangle": "_ZN9INIReaderC1EPKcl",
"c++": "INIReader::INIReader(const char *, long)",
"go": "(*Reader).Init__1"
}, {
"mangle": "ZN9INIReaderD1Ev",
"mangle": "_ZN9INIReaderD1Ev",
"c++": "INIReader::~INIReader()",
"go": "(*Reader).Dispose"
}, {
"mangle": "ZNK9INIReader10ParseErrorEv",
"mangle": "_ZNK9INIReader10ParseErrorEv",
"c++": "INIReader::ParseError()",
"go": "(*Reader).ParseError"
"go": "(*Reader).ModifyedParseError"
}, {
"mangle": "ZNK9INIReader3GetEPKcS1_S1_",
"mangle": "_ZNK9INIReader3GetEPKcS1_S1_",
"c++": "INIReader::Get(const char *, const char *, const char *)",
"go": "(*Reader).Get"
}]

View File

@@ -40,11 +40,11 @@ class INIReader {
isCpp: true,
prefixes: []string{"INI"},
dylibSymbols: []*nm.Symbol{
{Name: "__ZN9INIReaderC1EPKc"},
{Name: "__ZN9INIReaderC1EPKcl"},
{Name: "__ZN9INIReaderD1Ev"},
{Name: "__ZNK9INIReader10ParseErrorEv"},
{Name: "__ZNK9INIReader3GetEPKcS1_S1_"},
{Name: symbol.AddSymbolPrefixUnder("ZN9INIReaderC1EPKc", true)},
{Name: symbol.AddSymbolPrefixUnder("ZN9INIReaderC1EPKcl", true)},
{Name: symbol.AddSymbolPrefixUnder("ZN9INIReaderD1Ev", true)},
{Name: symbol.AddSymbolPrefixUnder("ZNK9INIReader10ParseErrorEv", true)},
{Name: symbol.AddSymbolPrefixUnder("ZNK9INIReader3GetEPKcS1_S1_", true)},
},
symbFileContent: `
[{
@@ -87,10 +87,10 @@ LUA_API void(lua_closeslot)(lua_State *L, int idx);
isCpp: false,
prefixes: []string{"lua_"},
dylibSymbols: []*nm.Symbol{
{Name: "_lua_error"},
{Name: "_lua_next"},
{Name: "_lua_concat"},
{Name: "_lua_stringtonumber"},
{Name: symbol.AddSymbolPrefixUnder("lua_error", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_next", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_concat", false)},
{Name: symbol.AddSymbolPrefixUnder("lua_stringtonumber", false)},
},
},
}

View File

@@ -2,6 +2,7 @@ package parse
import (
"errors"
"runtime"
"strconv"
"strings"
@@ -129,12 +130,13 @@ func (p *SymbolProcessor) collectFuncInfo(cursor clang.Cursor) {
symbol := cursor.Mangling()
defer symbol.Dispose()
// Remove all leading underscores from C++ symbol names
// On Linux, C++ symbols typically have one leading underscore
// On macOS, C++ symbols may have two leading underscores
// We remove all leading underscores to handle both cases consistently
// For consistency, we remove the first leading underscore on macOS
symbolName := c.GoString(symbol.CStr())
symbolName = strings.TrimLeft(symbolName, "_")
if runtime.GOOS == "darwin" {
symbolName = strings.TrimPrefix(symbolName, "_")
}
p.SymbolMap[symbolName] = &SymbolInfo{
GoName: p.genGoName(cursor),
ProtoName: p.genProtoName(cursor),

View File

@@ -158,7 +158,10 @@ func getPath(file string) []string {
func GetCommonSymbols(dylibSymbols []*nm.Symbol, headerSymbols map[string]*parse.SymbolInfo) []*types.SymbolInfo {
var commonSymbols []*types.SymbolInfo
for _, dylibSym := range dylibSymbols {
symName := strings.TrimLeft(dylibSym.Name, "_")
symName := dylibSym.Name
if runtime.GOOS == "darwin" {
symName = strings.TrimPrefix(symName, "_")
}
if symInfo, ok := headerSymbols[symName]; ok {
symbolInfo := &types.SymbolInfo{
Mangle: symName,
@@ -266,3 +269,15 @@ func GenerateAndUpdateSymbolTable(symbols []*nm.Symbol, headerInfos map[string]*
return symbolData, nil
}
// For mutiple os test,the nm output's symbol name is different.
func AddSymbolPrefixUnder(name string, isCpp bool) string {
prefix := ""
if runtime.GOOS == "darwin" {
prefix = prefix + "_"
}
if isCpp {
prefix = prefix + "_"
}
return prefix + name
}