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
11 changes: 10 additions & 1 deletion cl/_testcpp/enum/in.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
enum Global {
enum Global : int;

enum Global : int {
GA,
GB
};

enum class Local : int;

enum class Local : int {
LA,
LB
};

namespace bar {
enum Color {
Red,
Expand Down
9 changes: 8 additions & 1 deletion cl/_testcpp/enum/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package foo

import "github.com/goplus/lib/c"

type Global c.Uint
type Global c.Int

const (
GA Global = 0
GB Global = 1
)

type Local c.Int

const (
Local_LA Local = 0
Local_LB Local = 1
)

type BarColor c.Uint

const (
Expand Down
3 changes: 3 additions & 0 deletions cl/_testpp/macro/in.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"MacroIgnore": ["MASK2"]
}
1 change: 1 addition & 0 deletions cl/_testpp/macro/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define FVAL (TRUE * 3.14)

#define MASK (~FALSE)
#define MASK2 (~FALSE)
#define TWO (THREE * TRUE + -TRUE)

typedef char BOOL, *PBOOL;
1 change: 1 addition & 0 deletions cl/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
WrapFileHeader string `json:"WrapFileHeader"`
CFlags string `json:"CFlags"`
Files []string `json:"Files"`
MacroIgnore []string `json:"MacroIgnore"`
KeepDoc bool `json:"KeepDoc"`
}

Expand Down
18 changes: 11 additions & 7 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ type Config struct {
// to be ignored (optional).
NSIgnore []string

// MacroIgnore specifies a list of C/C++ macro names to be ignored (optional).
MacroIgnore []string

// TypeIgnore specifies a list of C/C++ type names to be ignored (optional).
TypeIgnore []string

// TypeAbbr specifies a mapping of Go type name to its abbreviated name. The abbreviated
// name will be used in function names (optional).
TypeAbbr map[string]string
// TypeAbbr specifies a mapping of Go type name to its abbreviated name(s). The abbreviated
// name(s) can be a name or name list. They will be used in function names (optional).
TypeAbbr map[string]any

// 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).
Expand Down Expand Up @@ -219,9 +222,10 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack
keepDoc: !conf.DontKeepDoc, stdRecvName: conf.UseStdRecvName,
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,
typePrefix: conf.TypePrefix, typeSuffix: conf.TypeSuffix,
fnPrefix: conf.FuncPrefix, enumPrefix: conf.EnumPrefix,
macroPrefix: conf.MacroPrefix, varPrefix: conf.VarPrefix, rename: rename,
nsIgnores: conf.NSIgnore, macroIgnore: conf.MacroIgnore, typeIgnores: conf.TypeIgnore,
classes: conf.Class, nonClasses: conf.NonClass, typdecls: typdecls,
pkgOf: conf.PackageOf, nameLookup: nameLookup, pubLookup: conf.PubFileLookup,
fileBases: make(map[clang.File]int), fns: make(map[string]*funcObj),
Expand Down Expand Up @@ -312,7 +316,7 @@ func loadDecl(ctx *pkgCtx, scope *scopeCtx, decl clang.Cursor, ns string) {
// TODO(xsw): ignore for now
case lc.Cursor_ClassTemplate, lc.Cursor_ClassTemplatePartialSpecialization:
loadTemplateClass(ctx, decl, ns, nil)
case lc.Cursor_UnexposedDecl:
case lc.Cursor_UnexposedDecl, lc.Cursor_UnexposedAttr:
// noop
default:
ctx.panicf(decl, "loadDecl: unknown kind - %v", decl.Kind)
Expand Down
1 change: 1 addition & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func testFromDir(t *testing.T, sel, relDir string, lang cl.Language) {
LLGoPackage: conf.LLGoPackage,
Language: lang,
WrapFileHeader: conf.WrapFileHeader,
MacroIgnore: conf.MacroIgnore,
CFlags: conf.CFlags,
NameLookup: nil,
DontKeepDoc: !conf.KeepDoc,
Expand Down
29 changes: 26 additions & 3 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ type pkgCtx struct {
typeSuffix []string
classes []string // typedef names to be treated as classes
nonClasses []string // typedef names to be treated as non-classes
typeAbbr map[string]string // Go type name => abbreviated name, used in function names
typeAbbr map[string]any // Go type name => abbreviated name(s), used in function names
rename map[string]string // C/C++ name => Go name
typeIgnores []string // C/C++ type names to be ignored
macroIgnore []string // C/C++ macro names to be ignored
nsIgnores []string // Go style namespace names to be ignored

nameLookup func(manglingName string) (archivePath string, ok bool)
Expand Down Expand Up @@ -280,6 +281,10 @@ func (p *pkgCtx) isTypeIgnored(cName string) bool {
return contains(trimTypeTag(cName), p.typeIgnores)
}

func (p *pkgCtx) isMacroIgnored(name string) bool {
return contains(name, p.macroIgnore)
}

func (p *pkgCtx) isNSIgnored(ns string) bool {
return contains(ns, p.nsIgnores)
}
Expand Down Expand Up @@ -342,12 +347,30 @@ func (p *pkgCtx) funcName(name string, order int, typName, typCName string, glob
if !strings.HasPrefix(name, "XGo_") { // avoid rewriting XGo_xxx names
name = p.cstyleToGo(name, true)
if typName != "" {
var typSuffix []string
if v, ok := p.typeAbbr[typName]; ok { // Go type name => abbreviated name
typName = v
switch v := v.(type) {
case string:
typName = v
typSuffix = []string{v}
case []any:
if n := len(v); n > 0 {
abbrs := make([]string, n)
for i, v := range v {
abbrs[i] = v.(string)
}
typName = abbrs[n-1]
typSuffix = abbrs
}
default:
panic(fmt.Errorf("invalid TypeAbbr for %q: %v", typName, v))
}
Comment thread
xushiwei marked this conversation as resolved.
Comment thread
xushiwei marked this conversation as resolved.
} else {
typName = rmSuffix(typName, p.typeAbbrSuffix)
typSuffix = []string{typName}
}
name = cutMethodPrefix(strings.TrimSuffix(name, typName), typName)
name = rmSuffix(name, typSuffix)
name = cutMethodPrefix(name, typName)
}
}
if order >= 0 {
Expand Down
31 changes: 22 additions & 9 deletions cl/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,39 @@ func loadEnum(ctx *pkgCtx, decl clang.Cursor, ns string) {
origName := nameWithNS(name, ns)
anonymous := decl.IsAnonymous() != 0
scoped := decl.EnumDeclIsScoped() != 0
definition := decl.IsCursorDefinition() != 0 // TODO(xsw): rename to IsDefinition
typ := decl.EnumDeclIntegerType()
if debugCompileDecl {
log.Println("enum", origName, clang.String(typ), "anonymous:", anonymous, "scoped:", scoped)
log.Println(
"enum", origName, clang.String(typ), "anonymous:", anonymous,
"scoped:", scoped, "definition:", definition)
}

pkg := ctx.pkg
pkgTypes := pkg.Types

var ok bool
var typDecl typDecl
var enumType types.Type
if !anonymous {
typDefs := pkg.NewTypeDefs()
typDecl, ok = ctx.typdecls[origName]
if !ok {
clsName := ctx.typeName(origName, true)
typDecl = newType(ctx, decl, clsName, tagEnum)
ctx.typdecls[origName] = typDecl

underType := toType(ctx, pkgTypes, typ, flagIsTypeDef, nil)
typDecl.InitType(pkg, underType)
}
}
Comment thread
xushiwei marked this conversation as resolved.
if !definition {
return // declaration only, no definition
}
if !anonymous {
if doc := ctx.docCommentGroup(decl); doc != nil {
typDefs.SetComments(doc)
typDecl.defs.SetComments(doc)
}
typeName := ctx.typeName(origName, true)
typDecl := typDefs.NewType(typeName, goNode(ctx, decl))
underType := toType(ctx, pkgTypes, typ, flagIsTypeDef, nil)
typNamed := typDecl.InitType(pkg, underType)
ctx.addType(tagEnum, decl, typNamed)
enumType = typNamed
enumType = typDecl.Type()
}

defs := pkg.NewConstDefs(pkgTypes.Scope())
Expand Down
8 changes: 8 additions & 0 deletions cl/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ func loadMacro(ctx *pkgCtx, decl clang.Cursor) {
if decl.IsMacroFunctionLike() != 0 {
return
}

origName := clang.String(decl)
if ctx.isMacroIgnored(origName) {
if debugCompileDecl {
log.Println("macro", origName, "- ignored")
}
return
}

tu := clang.TU(decl)
tokens, dispose := tu.Tokenize(decl.Extent())
defer dispose()
Expand Down
26 changes: 15 additions & 11 deletions cl/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func toTypeEx(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, feats *in
if t, ok := scope.lookupType(name); ok {
return t
}
case lc.Type_LongDouble:
return ctx.basicTyp(cLongDouble)
case lc.Type_BlockPointer:
ctx.logtf(typ, "type %s: C blocks (closures) are unsupported, ignored", clang.String(typ))
*feats |= featIgnored
Expand Down Expand Up @@ -239,21 +241,23 @@ const (
cFloat
cDouble
cPointer
cLongDouble
cBasicMax
)

var ctypBasic = [cBasicMax]string{
cVoid: "Void",
cChar: "Char",
cInt: "Int",
cUint: "Uint",
cLong: "Long",
cUlong: "Ulong",
cLongLong: "LongLong",
cUlongLong: "UlongLong",
cFloat: "Float",
cDouble: "Double",
cPointer: "Pointer",
cVoid: "Void",
cChar: "Char",
cInt: "Int",
cUint: "Uint",
cLong: "Long",
cUlong: "Ulong",
cLongLong: "LongLong",
cUlongLong: "UlongLong",
cFloat: "Float",
cDouble: "Double",
cPointer: "Pointer",
cLongDouble: "LongDouble",
}

// -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.27.0 // llgo 1.0

require (
github.com/goplus/gogen v1.25.3
github.com/goplus/lib v0.5.4
github.com/goplus/lib v0.5.5
github.com/goplus/mod v0.22.1
github.com/llarhub/clang-c v0.7.0
github.com/qiniu/x v1.18.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/goplus/gogen v1.25.3 h1:g8VWgNcCwTG9DFs08umVNtxl/BSLHH1cbvqWzyxrPVk=
github.com/goplus/gogen v1.25.3/go.mod h1:Y7ulYW3wonQ3d9er00b0uGFEV/IUZa6okWJZh892ACQ=
github.com/goplus/lib v0.5.4 h1:Ltrj+lY844/jx0nJqCRQEq3YzBkHb3jBjCQZWFUdlTc=
github.com/goplus/lib v0.5.4/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
github.com/goplus/lib v0.5.5 h1:COcqsafjaXBI6ikcyJgnc7l9Hu03K8kymUSIZtXNTz8=
github.com/goplus/lib v0.5.5/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
github.com/goplus/mod v0.22.1 h1:3GQZqKlcQAiMpRUDAHvFryGYie+mT0JMjguEqOtIjkE=
github.com/goplus/mod v0.22.1/go.mod h1:APrczG2FtFcQelU4vTq9xw+GrVs4sPPKnRfGidWBlXY=
github.com/llarhub/clang-c v0.7.0 h1:YkO1FR1R1ScKJijF1PUVIpHnIC4uNgsSg/7CLzEBpFE=
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 @@ -13,6 +13,7 @@
"TypeAbbr": {
"IdxLoc": "IndexLoc",
"TUResourceUsageKind": "TUResourceUsage",
"IndexOptions": ["WithOptions"],
"SourceLocation": "Location",
"SourceRange": "Range"
},
Expand All @@ -24,7 +25,6 @@
"clang_sortCodeCompletionResults": "Sort",
"clang_getNumCompletionChunks": "NumChunks",
"clang_codeCompleteAt": "CodeCompleteAt",
"clang_createIndexWithOptions": "CreateIndex",
"clang_createCXCursorSet": "CreateCursorSet",
"getCString": "CStr"
},
Expand Down
24 changes: 12 additions & 12 deletions tool/_testcpp/llvm-22.1.8-support/system/AMDHSAKernelDescriptor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tool/_testcpp/llvm-22.1.8-support/system/Atomic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type Config struct {
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
TypeAbbrSuffix []string `json:"TypeAbbrSuffix"` // type abbr suffix to remove, only valid for types that are not present in TypeAbbr
TypeAbbr map[string]any `json:"TypeAbbr"` // Go type name to its abbr(s), 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
MacroIgnore []string `json:"MacroIgnore"` // C/C++ macro names to ignore
NSIgnore []string `json:"NSIgnore"` // namespaces (Go style names) to ignore
Pkgs []string `json:"Pkgs"` // sub-packages to generate, optional
}
Expand Down
Loading
Loading