Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ type pkgCtx struct {

wrapFileHeader string

typeAbbrSuffix []string

fnPrefix []string
varPrefix []string
enumPrefix []string
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tool/_testc/clang-c-22.1.8/llcppg.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 22 additions & 21 deletions tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
xushiwei marked this conversation as resolved.
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
}

// -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions tool/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 17 additions & 16 deletions tool/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading