diff --git a/cl/compile.go b/cl/compile.go index 7605037b..d4520b7f 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -119,6 +119,10 @@ type Config struct { // name will be used in function names (optional). TypeAbbr map[string]string + // TypeAbbrSuffix specifies the suffix to remove from Go type name when generating function + // names. It is only valid for types that are not present in TypeAbbr (optional). + TypeAbbrSuffix []string + // TypePrefix/TypeSuffix specifies the prefix/suffix to remove from C/C++ type names // when generating Go type names (optional). TypePrefix, TypeSuffix []string @@ -213,7 +217,8 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack overloads: make(map[string]*overloads), pkg: pkg, cb: pkg.CB(), llgo: llgo, fset: pkg.Fset, c: c, lang: conf.Language, keepDoc: !conf.DontKeepDoc, stdRecvName: conf.UseStdRecvName, - cflags: conf.CFlags, wrapFileHeader: conf.WrapFileHeader, typeAbbr: conf.TypeAbbr, + cflags: conf.CFlags, wrapFileHeader: conf.WrapFileHeader, + typeAbbr: conf.TypeAbbr, typeAbbrSuffix: conf.TypeAbbrSuffix, typePrefix: conf.TypePrefix, typeSuffix: conf.TypeSuffix, fnPrefix: conf.FuncPrefix, enumPrefix: conf.EnumPrefix, macroPrefix: conf.MacroPrefix, varPrefix: conf.VarPrefix, nsIgnores: conf.NSIgnore, typeIgnores: conf.TypeIgnore, rename: rename, diff --git a/cl/ctx.go b/cl/ctx.go index 91391467..0e74262d 100644 --- a/cl/ctx.go +++ b/cl/ctx.go @@ -118,6 +118,8 @@ type pkgCtx struct { wrapFileHeader string + typeAbbrSuffix []string + fnPrefix []string varPrefix []string enumPrefix []string @@ -342,6 +344,8 @@ func (p *pkgCtx) funcName(name string, order int, typName, typCName string, glob if typName != "" { if v, ok := p.typeAbbr[typName]; ok { // Go type name => abbreviated name typName = v + } else { + typName = rmSuffix(typName, p.typeAbbrSuffix) } name = cutMethodPrefix(strings.TrimSuffix(name, typName), typName) } diff --git a/tool/_testc/clang-c-22.1.8/llcppg.cfg b/tool/_testc/clang-c-22.1.8/llcppg.cfg index c5f79cf3..7990dbcf 100644 --- a/tool/_testc/clang-c-22.1.8/llcppg.cfg +++ b/tool/_testc/clang-c-22.1.8/llcppg.cfg @@ -9,9 +9,9 @@ "FuncPrefix": ["clang_codeComplete", "clang_getCompletion", "clang_index_", "clang_"], "EnumPrefix": ["CX_", "CX"], "TypePrefix": ["CX_", "CX"], + "TypeAbbrSuffix": ["ping"], "TypeAbbr": { "IdxLoc": "IndexLoc", - "Remapping": "Remap", "TUResourceUsageKind": "TUResourceUsage", "SourceLocation": "Location", "SourceRange": "Range" diff --git a/tool/config.go b/tool/config.go index 3a78f870..c79f9fc1 100644 --- a/tool/config.go +++ b/tool/config.go @@ -28,27 +28,28 @@ import ( // ----------------------------------------------------------------------------- type Config struct { - Name string `json:"Name"` // required, sub package name should start with '/' - Language string `json:"Language"` // c, c++, etc. required - Dir string `json:"Dir"` // dir or dir/... (recursive), required - Files []string `json:"Files"` // selected header files relative to Dir; overrides auto-discovery, optional. - Stdlib string `json:"Stdlib"` // C stdlib include dir, optional - LLGoPackage string `json:"LLGoPackage"` // optional - CFlags string `json:"CFlags"` // optional - Deps []string `json:"Deps"` // dependencies (module paths), optional - Class []string `json:"Class"` // typedef names to be treated as classes - NonClass []string `json:"NonClass"` // typedef names to be treated as non-classes - FuncPrefix []string `json:"FuncPrefix"` // global function prefix to remove - VarPrefix []string `json:"VarPrefix"` // global variable prefix to remove - EnumPrefix []string `json:"EnumPrefix"` // enum value prefix to remove - MacroPrefix []string `json:"MacroPrefix"` // macro prefix to remove - TypePrefix []string `json:"TypePrefix"` // type prefix to remove - TypeSuffix []string `json:"TypeSuffix"` // type suffix to remove - TypeAbbr map[string]string `json:"TypeAbbr"` // Go type name to its abbr, used in function names - Rename map[string]string `json:"Rename"` // renaming of C/C++ names to Go names - TypeIgnore []string `json:"TypeIgnore"` // C/C++ type names to ignore - NSIgnore []string `json:"NSIgnore"` // namespaces (Go style names) to ignore - Pkgs []string `json:"Pkgs"` // sub-packages to generate, optional + Name string `json:"Name"` // required, sub package name should start with '/' + Language string `json:"Language"` // c, c++, etc. required + Dir string `json:"Dir"` // dir or dir/... (recursive), required + Files []string `json:"Files"` // selected header files relative to Dir; overrides auto-discovery, optional. + Stdlib string `json:"Stdlib"` // C stdlib include dir, optional + LLGoPackage string `json:"LLGoPackage"` // optional + CFlags string `json:"CFlags"` // optional + Deps []string `json:"Deps"` // dependencies (module paths), optional + Class []string `json:"Class"` // typedef names to be treated as classes + NonClass []string `json:"NonClass"` // typedef names to be treated as non-classes + FuncPrefix []string `json:"FuncPrefix"` // global function prefix to remove + VarPrefix []string `json:"VarPrefix"` // global variable prefix to remove + EnumPrefix []string `json:"EnumPrefix"` // enum value prefix to remove + MacroPrefix []string `json:"MacroPrefix"` // macro prefix to remove + TypePrefix []string `json:"TypePrefix"` // type prefix to remove + TypeSuffix []string `json:"TypeSuffix"` // type suffix to remove + TypeAbbrSuffix []string `json:"TypeAbbrSuffix"` // type abbr suffix to remove, only valid for types that is not in TypeAbbr + TypeAbbr map[string]string `json:"TypeAbbr"` // Go type name to its abbr, used in function names + Rename map[string]string `json:"Rename"` // renaming of C/C++ names to Go names + TypeIgnore []string `json:"TypeIgnore"` // C/C++ type names to ignore + NSIgnore []string `json:"NSIgnore"` // namespaces (Go style names) to ignore + Pkgs []string `json:"Pkgs"` // sub-packages to generate, optional } // ----------------------------------------------------------------------------- diff --git a/tool/gen.go b/tool/gen.go index 761433b0..698b9de7 100644 --- a/tool/gen.go +++ b/tool/gen.go @@ -175,6 +175,7 @@ func (cfg *Config) NewPackage(pkgPath, pkgName, workDir string, index clang.Inde TypePrefix: cfg.TypePrefix, TypeSuffix: cfg.TypeSuffix, TypeAbbr: cfg.TypeAbbr, + TypeAbbrSuffix: cfg.TypeAbbrSuffix, Rename: cfg.Rename, TypeIgnore: cfg.TypeIgnore, NSIgnore: cfg.NSIgnore, diff --git a/tool/gen_test.go b/tool/gen_test.go index 331105f7..3f388584 100644 --- a/tool/gen_test.go +++ b/tool/gen_test.go @@ -121,22 +121,23 @@ func testSingleFile(t *testing.T, idx clang.Index, pkgDir, headerDir, headerFile pkgPrefix += "/" } pkg, err := cl.NewPackage(pkgPrefix+myPkgName, myPkgName, files, &cl.Config{ - Importer: imp, - LLGoPackage: conf.LLGoPackage, - Language: lang, - Class: conf.Class, - NonClass: conf.NonClass, - FuncPrefix: conf.FuncPrefix, - EnumPrefix: conf.EnumPrefix, - MacroPrefix: conf.MacroPrefix, - VarPrefix: conf.VarPrefix, - TypePrefix: conf.TypePrefix, - TypeSuffix: conf.TypeSuffix, - TypeAbbr: conf.TypeAbbr, - Rename: conf.Rename, - TypeIgnore: conf.TypeIgnore, - NSIgnore: conf.NSIgnore, - NameLookup: nil, + Importer: imp, + LLGoPackage: conf.LLGoPackage, + Language: lang, + Class: conf.Class, + NonClass: conf.NonClass, + FuncPrefix: conf.FuncPrefix, + EnumPrefix: conf.EnumPrefix, + MacroPrefix: conf.MacroPrefix, + VarPrefix: conf.VarPrefix, + TypePrefix: conf.TypePrefix, + TypeSuffix: conf.TypeSuffix, + TypeAbbr: conf.TypeAbbr, + TypeAbbrSuffix: conf.TypeAbbrSuffix, + Rename: conf.Rename, + TypeIgnore: conf.TypeIgnore, + NSIgnore: conf.NSIgnore, + NameLookup: nil, PubFileLookup: func(pkgPath string) (pubFile string, ok bool) { if name, ok := strings.CutPrefix(pkgPath, pkgPrefix); ok { return filepath.Join(pkgDir, name, "llcppg.pub"), true