llcppsymg:refine logic & parse symbol test

This commit is contained in:
luoliwoshang
2024-09-24 19:17:36 +08:00
parent 29d527bee1
commit a83f7a822e
5 changed files with 292 additions and 68 deletions

View File

@@ -16,6 +16,10 @@ type Config struct {
Index *clang.Index Index *clang.Index
} }
type Visitor func(cursor, parent clang.Cursor) clang.ChildVisitResult
const TEMP_FILE = "temp.h"
func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) {
// default use the c/c++ standard of clang; c:gnu17 c++:gnu++17 // default use the c/c++ standard of clang; c:gnu17 c++:gnu++17
// https://clang.llvm.org/docs/CommandGuide/clang.html // https://clang.llvm.org/docs/CommandGuide/clang.html
@@ -42,7 +46,7 @@ func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit
if config.Temp { if config.Temp {
content := c.AllocaCStr(config.File) content := c.AllocaCStr(config.File)
tempFile := &clang.UnsavedFile{ tempFile := &clang.UnsavedFile{
Filename: c.Str("temp.h"), Filename: c.Str(TEMP_FILE),
Contents: content, Contents: content,
Length: c.Ulong(c.Strlen(content)), Length: c.Ulong(c.Strlen(content)),
} }
@@ -83,3 +87,10 @@ func BuildScopingParts(cursor clang.Cursor) []string {
} }
return parts return parts
} }
func VisitChildren(cursor clang.Cursor, fn Visitor) c.Uint {
return clang.VisitChildren(cursor, func(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult {
cfn := *(*Visitor)(clientData)
return cfn(cursor, parent)
}, unsafe.Pointer(&fn))
}

View File

@@ -47,7 +47,6 @@ func main() {
data, err = os.ReadFile(cfgFile) data, err = os.ReadFile(cfgFile)
} }
check(err) check(err)
conf, err := config.GetConf(data) conf, err := config.GetConf(data)
check(err) check(err)
defer conf.Delete() defer conf.Delete()
@@ -60,7 +59,7 @@ func main() {
check(err) check(err)
filepaths := genHeaderFilePath(conf.CFlags, conf.Include) filepaths := genHeaderFilePath(conf.CFlags, conf.Include)
headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus) headerInfos, err := parse.ParseHeaderFile(filepaths, conf.TrimPrefixes, conf.Cplusplus, false)
check(err) check(err)
symbolInfo := getCommonSymbols(symbols, headerInfos, conf.TrimPrefixes) symbolInfo := getCommonSymbols(symbols, headerInfos, conf.TrimPrefixes)

View File

@@ -15,27 +15,27 @@ type SymbolInfo struct {
ProtoName string ProtoName string
} }
type Context struct { type SymbolProcessor struct {
prefixes []string Prefixes []string
symbolMap map[string]*SymbolInfo SymbolMap map[string]*SymbolInfo
currentFile string CurrentFile string
nameCounts map[string]int NameCounts map[string]int
} }
func newContext(prefixes []string) *Context { func NewSymbolProcessor(Prefixes []string) *SymbolProcessor {
return &Context{ return &SymbolProcessor{
prefixes: prefixes, Prefixes: Prefixes,
symbolMap: make(map[string]*SymbolInfo), SymbolMap: make(map[string]*SymbolInfo),
nameCounts: make(map[string]int), NameCounts: make(map[string]int),
} }
} }
func (c *Context) setCurrentFile(filename string) { func (p *SymbolProcessor) setCurrentFile(filename string) {
c.currentFile = filename p.CurrentFile = filename
} }
func (c *Context) removePrefix(str string) string { func (p *SymbolProcessor) TrimPrefixes(str string) string {
for _, prefix := range c.prefixes { for _, prefix := range p.Prefixes {
if strings.HasPrefix(str, prefix) { if strings.HasPrefix(str, prefix) {
return strings.TrimPrefix(str, prefix) return strings.TrimPrefix(str, prefix)
} }
@@ -47,10 +47,10 @@ func toTitle(s string) string {
if s == "" { if s == "" {
return "" return ""
} }
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:]) return strings.ToUpper(s[:1]) + (s[1:])
} }
func toCamel(originName string) string { func toUpperCamelCase(originName string) string {
if originName == "" { if originName == "" {
return "" return ""
} }
@@ -64,38 +64,44 @@ func toCamel(originName string) string {
// 1. remove prefix from config // 1. remove prefix from config
// 2. convert to camel case // 2. convert to camel case
func (c *Context) toGoName(name string) string { func (p *SymbolProcessor) ToGoName(name string) string {
name = c.removePrefix(name) return toUpperCamelCase(p.TrimPrefixes(name))
return toCamel(name)
} }
func (p *Context) genGoName(cursor clang.Cursor) string { func (p *SymbolProcessor) GenMethodName(class, name string, isDestructor bool) string {
funcName := cursor.String()
defer funcName.Dispose()
name := p.toGoName(c.GoString(funcName.CStr()))
if parent := cursor.SemanticParent(); parent.Kind == clang.CursorClassDecl {
parentName := parent.String()
defer parentName.Dispose()
class := p.toGoName(c.GoString(parentName.CStr()))
return p.addSuffix(p.genMethodName(class, name))
}
return p.addSuffix(name)
}
func (c *Context) genMethodName(class, name string) string {
prefix := "(*" + class + ")." prefix := "(*" + class + ")."
if isDestructor {
return prefix + "Dispose"
}
if class == name { if class == name {
return prefix + "Init" return prefix + "Init"
} }
if name == "~"+class {
return prefix + "Dispose"
}
return prefix + name return prefix + name
} }
func (p *Context) genProtoName(cursor clang.Cursor) string { func (p *SymbolProcessor) genGoName(cursor clang.Cursor) string {
funcName := cursor.String()
defer funcName.Dispose()
originName := c.GoString(funcName.CStr())
isDestructor := cursor.Kind == clang.CursorDestructor
var convertedName string
if isDestructor {
convertedName = p.ToGoName(originName[1:])
} else {
convertedName = p.ToGoName(originName)
}
if parent := cursor.SemanticParent(); parent.Kind == clang.CursorClassDecl {
parentName := parent.String()
defer parentName.Dispose()
class := p.ToGoName(c.GoString(parentName.CStr()))
return p.AddSuffix(p.GenMethodName(class, convertedName, isDestructor))
}
return p.AddSuffix(convertedName)
}
func (p *SymbolProcessor) genProtoName(cursor clang.Cursor) string {
displayName := cursor.DisplayName() displayName := cursor.DisplayName()
defer displayName.Dispose() defer displayName.Dispose()
@@ -111,36 +117,32 @@ func (p *Context) genProtoName(cursor clang.Cursor) string {
return builder.String() return builder.String()
} }
func (c *Context) addSuffix(name string) string { func (p *SymbolProcessor) AddSuffix(name string) string {
c.nameCounts[name]++ p.NameCounts[name]++
count := c.nameCounts[name] if count := p.NameCounts[name]; count > 1 {
if count > 1 {
return name + "__" + strconv.Itoa(count-1) return name + "__" + strconv.Itoa(count-1)
} }
return name return name
} }
var context = newContext([]string{}) func (p *SymbolProcessor) collectFuncInfo(cursor clang.Cursor) {
func collectFuncInfo(cursor clang.Cursor) {
symbol := cursor.Mangling() symbol := cursor.Mangling()
defer symbol.Dispose()
symbolName := c.GoString(symbol.CStr()) symbolName := c.GoString(symbol.CStr())
if len(symbolName) >= 1 && symbolName[0] == '_' { if len(symbolName) >= 1 && symbolName[0] == '_' {
symbolName = symbolName[1:] symbolName = symbolName[1:]
} }
defer symbol.Dispose() p.SymbolMap[symbolName] = &SymbolInfo{
GoName: p.genGoName(cursor),
context.symbolMap[symbolName] = &SymbolInfo{ ProtoName: p.genProtoName(cursor),
GoName: context.genGoName(cursor),
ProtoName: context.genProtoName(cursor),
} }
} }
func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitResult { func (p *SymbolProcessor) visitTop(cursor, parent clang.Cursor) clang.ChildVisitResult {
switch cursor.Kind { switch cursor.Kind {
case clang.CursorNamespace, clang.CursorClassDecl: case clang.CursorNamespace, clang.CursorClassDecl:
clang.VisitChildren(cursor, visit, nil) clangutils.VisitChildren(cursor, p.visitTop)
case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor: case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor:
loc := cursor.Location() loc := cursor.Location()
var file clang.File var file clang.File
@@ -148,35 +150,38 @@ func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitRe
filename := file.FileName() filename := file.FileName()
defer filename.Dispose() defer filename.Dispose()
isCurrentFile := c.Strcmp(filename.CStr(), c.AllocaCStr(context.currentFile)) == 0 isCurrentFile := c.Strcmp(filename.CStr(), c.AllocaCStr(p.CurrentFile)) == 0
isPublicMethod := (cursor.CXXAccessSpecifier() == clang.CXXPublic) && cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor isPublicMethod := (cursor.CXXAccessSpecifier() == clang.CXXPublic) && cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor
if isCurrentFile && (cursor.Kind == clang.CursorFunctionDecl || isPublicMethod) { if isCurrentFile && (cursor.Kind == clang.CursorFunctionDecl || isPublicMethod) {
collectFuncInfo(cursor) p.collectFuncInfo(cursor)
} }
} }
return clang.ChildVisit_Continue return clang.ChildVisit_Continue
} }
func ParseHeaderFile(filepaths []string, prefixes []string, isCpp bool) (map[string]*SymbolInfo, error) { func ParseHeaderFile(files []string, Prefixes []string, isCpp bool, isTemp bool) (map[string]*SymbolInfo, error) {
context = newContext(prefixes) processer := NewSymbolProcessor(Prefixes)
index := clang.CreateIndex(0, 0) index := clang.CreateIndex(0, 0)
for _, filename := range filepaths { for _, file := range files {
_, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{ _, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: filename, File: file,
Temp: false, Temp: isTemp,
IsCpp: isCpp, IsCpp: isCpp,
Index: index, Index: index,
}) })
if err != nil { if err != nil {
return nil, errors.New("Unable to parse translation unit for file " + filename) return nil, errors.New("Unable to parse translation unit for file " + file)
} }
cursor := unit.Cursor() cursor := unit.Cursor()
context.setCurrentFile(filename) if isTemp {
clang.VisitChildren(cursor, visit, nil) processer.setCurrentFile(clangutils.TEMP_FILE)
} else {
processer.setCurrentFile(file)
}
clangutils.VisitChildren(cursor, processer.visitTop)
unit.Dispose() unit.Dispose()
} }
index.Dispose() index.Dispose()
return context.symbolMap, nil return processer.SymbolMap, nil
} }

View File

@@ -0,0 +1,44 @@
#stdout
=== Test NewSymbolProcessor ===
Before: No prefixes After: Prefixes: [lua_ luaL_]
=== Test RemovePrefix ===
Before: lua_closethread After: closethread
Before: luaL_checknumber After: checknumber
=== Test ToGoName ===
Before: lua_closethread After: Closethread
Before: luaL_checknumber After: Checknumber
Before: sqlite3_close_v2 After: CloseV2
Before: sqlite3_callback After: Callback
Before: GetReal After: GetReal
Before: GetBoolean After: GetBoolean
Before: INIReader After: Reader
=== Test GenMethodName ===
Before: Class: INIReader, Name: INIReader After: (*INIReader).Init
Before: Class: INIReader, Name: INIReader After: (*INIReader).Dispose
Before: Class: INIReader, Name: HasValue After: (*INIReader).HasValue
=== Test AddSuffix ===
Before: Class: INIReader, Method: INIReader After: (*Reader).Init
Before: Class: INIReader, Method: INIReader After: (*Reader).Init__1
Before: Class: INIReader, Method: ParseError After: (*Reader).ParseError
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
=== Test Case: C Functions ===
Parsed Symbols:
Symbol Map GoName: Compare, ProtoName In HeaderFile: lua_compare(lua_State *, int, int, int), MangledName: lua_compare
Symbol Map GoName: Rawequal, ProtoName In HeaderFile: lua_rawequal(lua_State *, int, int), MangledName: lua_rawequal
#stderr
#exit 0

View File

@@ -0,0 +1,165 @@
package main
import (
"fmt"
"sort"
"github.com/goplus/llgo/chore/_xtool/llcppsymg/parse"
)
func main() {
TestNewSymbolProcessor()
TestRemovePrefix()
TestToGoName()
TestGenMethodName()
TestAddSuffix()
TestParseHeaderFile()
}
func TestNewSymbolProcessor() {
fmt.Println("=== Test NewSymbolProcessor ===")
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
fmt.Printf("Before: No prefixes After: Prefixes: %v\n", process.Prefixes)
fmt.Println()
}
func TestRemovePrefix() {
fmt.Println("=== Test RemovePrefix ===")
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
testCases := []string{"lua_closethread", "luaL_checknumber"}
for _, input := range testCases {
result := process.TrimPrefixes(input)
fmt.Printf("Before: %s After: %s\n", input, result)
}
fmt.Println()
}
func TestToGoName() {
fmt.Println("=== Test ToGoName ===")
process1 := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
process2 := parse.NewSymbolProcessor([]string{"sqlite3_", "sqlite3_"})
process3 := parse.NewSymbolProcessor([]string{"INI"})
testCases := []struct {
processor *parse.SymbolProcessor
input string
}{
{process1, "lua_closethread"},
{process1, "luaL_checknumber"},
{process2, "sqlite3_close_v2"},
{process2, "sqlite3_callback"},
{process3, "GetReal"},
{process3, "GetBoolean"},
{process3, "INIReader"},
}
for _, tc := range testCases {
result := tc.processor.ToGoName(tc.input)
fmt.Printf("Before: %s After: %s\n", tc.input, result)
}
fmt.Println()
}
func TestGenMethodName() {
fmt.Println("=== Test GenMethodName ===")
process := &parse.SymbolProcessor{}
testCases := []struct {
class string
name string
isDestructor bool
}{
{"INIReader", "INIReader", false},
{"INIReader", "INIReader", true},
{"INIReader", "HasValue", false},
}
for _, tc := range testCases {
input := fmt.Sprintf("Class: %s, Name: %s", tc.class, tc.name)
result := process.GenMethodName(tc.class, tc.name, tc.isDestructor)
fmt.Printf("Before: %s After: %s\n", input, result)
}
fmt.Println()
}
func TestAddSuffix() {
fmt.Println("=== Test AddSuffix ===")
process := parse.NewSymbolProcessor([]string{"INI"})
methods := []string{
"INIReader",
"INIReader",
"ParseError",
"HasValue",
}
for _, method := range methods {
goName := process.ToGoName(method)
className := process.ToGoName("INIReader")
methodName := process.GenMethodName(className, goName, false)
finalName := process.AddSuffix(methodName)
input := fmt.Sprintf("Class: INIReader, Method: %s", method)
fmt.Printf("Before: %s After: %s\n", input, finalName)
}
fmt.Println()
}
func TestParseHeaderFile() {
testCases := []struct {
name string
content string
isCpp bool
prefixes []string
}{
{
name: "C++ Class with Methods",
content: `
class INIReader {
public:
INIReader(const std::string &filename);
INIReader(const char *buffer, size_t buffer_size);
~INIReader();
int ParseError() const;
private:
static std::string MakeKey(const std::string &section, const std::string &name);
};
`,
isCpp: true,
prefixes: []string{"INI"},
},
{
name: "C Functions",
content: `
typedef struct lua_State lua_State;
int(lua_rawequal)(lua_State *L, int idx1, int idx2);
int(lua_compare)(lua_State *L, int idx1, int idx2, int op);
`,
isCpp: false,
prefixes: []string{"lua_"},
},
}
for _, tc := range testCases {
fmt.Printf("=== Test Case: %s ===\n", tc.name)
symbolMap, err := parse.ParseHeaderFile([]string{tc.content}, tc.prefixes, tc.isCpp, true)
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
fmt.Println("Parsed Symbols:")
var keys []string
for key := range symbolMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
info := symbolMap[key]
fmt.Printf("Symbol Map GoName: %s, ProtoName In HeaderFile: %s, MangledName: %s\n", info.GoName, info.ProtoName, key)
}
fmt.Println()
}
}