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
31 changes: 17 additions & 14 deletions cl/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func compileClass(ctx *pkgCtx, this *classCtx) {
}
}

func newTypeParams(ctx *pkgCtx, pkg *types.Package, cls clang.Cursor, feats *int) []*types.TypeParam {
func newTypeParams(ctx *pkgCtx, pkg *types.Package, cls clang.Cursor) (ret []*types.TypeParam, quietIgnore bool) {
idx := 0
ret := make([]*types.TypeParam, 0, 2)
ret = make([]*types.TypeParam, 0, 2)
clang.VisitChildren(cls, func(decl, parent clang.Cursor) clang.ChildVisitResult {
switch decl.Kind {
case lc.Cursor_TemplateTypeParameter:
Expand All @@ -96,13 +96,14 @@ func newTypeParams(ctx *pkgCtx, pkg *types.Package, cls clang.Cursor, feats *int
objName := types.NewTypeName(0, pkg, name, nil)
ret = append(ret, types.NewTypeParam(objName, ctx.any()))
case lc.Cursor_NonTypeTemplateParameter, lc.Cursor_TemplateTemplateParameter:
*feats |= featIgnored
quietIgnore = true
fallthrough
default:
return clang.Break
}
return clang.Continue
})
return ret
return
}

func loadTemplateClass(ctx *pkgCtx, cls clang.Cursor, ns string, parent *scopeCtx) {
Expand All @@ -123,10 +124,11 @@ func compileTemplateClass(ctx *pkgCtx, obj *templateClass, parent *scopeCtx) {

pkg := ctx.pkg
pkgTypes := pkg.Types
feats := 0
tparams := newTypeParams(ctx, pkgTypes, cls, &feats)
if feats&featIgnored != 0 || obj.order() >= 0 {
ctx.logf(cls, "class %s: unsupported template params, skipped", clang.String(cls))
tparams, quietIgnore := newTypeParams(ctx, pkgTypes, cls)
if quietIgnore || obj.order() >= 0 {
if debugQuietIgnore {
ctx.logf(cls, "class %s: unsupported template params, ignored", clang.String(cls))
}
return
}

Expand Down Expand Up @@ -188,8 +190,8 @@ func newType(ctx *pkgCtx, cls clang.Cursor, clsName string, kind typeTag) (ret t
func initClassType(ctx *pkgCtx, typDecl typDecl, cls clang.Cursor, clsName string, tparams []*types.TypeParam, parent *scopeCtx) bool {
feats := 0
initClassTypeEx(ctx, typDecl, cls, clsName, tparams, parent, &feats)
if feats&featIgnored != 0 {
ctx.logf(cls, "class %s: unsupported features, skipped", clang.String(cls))
if feats&featAllIgnore != 0 {
ctx.ignoref(feats, cls, "class %s: unsupported features, ignored", clang.String(cls))
typDecl.Delete()
return false
}
Expand Down Expand Up @@ -217,7 +219,7 @@ func initClassTypeEx(ctx *pkgCtx, typDecl typDecl, cls clang.Cursor, clsName str
loadClassMember(ctx, pkgTypes, this, clsName, decl, feats)
return clang.Continue
})
if *feats&featIgnored != 0 {
if *feats&featAllIgnore != 0 {
return
}
// Establish the layout at offset 0, following the C++ Itanium ABI. A
Expand Down Expand Up @@ -297,7 +299,7 @@ func loadClassMember(ctx *pkgCtx, pkg *types.Package, this *classCtx, clsName st
}
}
if !anonymous {
if fldType = toTypeEx(ctx, pkg, ft, flagIsVarDef, feats, this.scope()); *feats&featIgnored != 0 {
if fldType = toTypeEx(ctx, pkg, ft, flagIsVarDef, feats, this.scope()); *feats&featAllIgnore != 0 {
return
}
}
Expand Down Expand Up @@ -481,10 +483,11 @@ func baseClass(ctx *pkgCtx, decl clang.Cursor, feats *int) *types.TypeName {
return t
}
case lc.Type_Unexposed:
*feats |= featIgnored
ctx.logf(decl, "baseClass %s: with unexposed type, skipped", clang.String(decl))
*feats |= featExplicitIgnore
return nil
}
ctx.panicf(decl, "baseClass: unknown base class type - %s (%d)", clang.String(t), t.Kind)
ctx.panicf(decl, "baseClass %s: unknown base class - %s (%d)", clang.String(decl), clang.String(t), t.Kind)
return nil
}

Expand Down
5 changes: 4 additions & 1 deletion cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ import (
const (
DbgFlagCompileDecl = 1 << iota
DbgFlagMajorProc
DbgFlagAll = DbgFlagCompileDecl | DbgFlagMajorProc
DbgFlagQuietIgnore
DbgFlagAll = DbgFlagCompileDecl | DbgFlagMajorProc | DbgFlagQuietIgnore
)

var (
debugCompileDecl bool
debugMajorProc bool
debugQuietIgnore bool
)

func SetDebug(flags int) {
debugCompileDecl = (flags & DbgFlagCompileDecl) != 0
debugMajorProc = (flags & DbgFlagMajorProc) != 0
debugQuietIgnore = (flags & DbgFlagQuietIgnore) != 0
}

// -----------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ type pkgCtx struct {
keepDoc bool
}

func (p *pkgCtx) ignoref(feats int, decl clang.Cursor, format string, args ...any) {
if feats&featQuietIgnore == 0 || debugQuietIgnore {
p.logf(decl, format, args...)
}
}

func (p *pkgCtx) logf(decl clang.Cursor, format string, args ...any) {
pos := p.fset.Position(goNodePos(p, decl))
log.Printf("%s: %s", pos, fmt.Sprintf(format, args...))
Expand Down Expand Up @@ -271,9 +277,9 @@ func (p *pkgCtx) addType(kind typeTag, decl clang.Cursor, typNamed *types.Named)
}
}

func (p *pkgCtx) ignoreType(cName string) {
func (p *pkgCtx) ignoreType(cName string, feats int) {
if _, ok := p.types[cName]; !ok {
p.types[cName] = typeObj{nil, featIgnored}
p.types[cName] = typeObj{nil, feats}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cl/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func loadEnum(ctx *pkgCtx, decl clang.Cursor, ns string) {

feats := 0
underType := toTypeEx(ctx, pkgTypes, typ, flagIsTypeDef, &feats, nil)
if feats&featIgnored != 0 {
if feats&featAllIgnore != 0 {
ctx.panicf(decl, "enum %s: unsupported underlying type %s (%d)", name, clang.String(typ), typ.Kind)
}
typDecl.InitType(pkg, underType)
Expand Down
6 changes: 3 additions & 3 deletions cl/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *funcObj, this *classCtx) {
scope := this.scope()
params, variadic := newParams(ctx, pkgTypes, fn, &feats, scope)
results := toFuncResults(ctx, pkgTypes, fn.ResultType(), &feats, scope)
if feats&featIgnored != 0 {
ctx.logf(fn, "func %s: function with unsupported features, skipped", clang.String(fn))
if feats&featAllIgnore != 0 {
ctx.ignoref(feats, fn, "func %s: function with unsupported features, ignored", clang.String(fn))
return
}

if fn.IsFunctionInlined() != 0 {
if ctx.cflags == "" {
ctx.logf(fn, "func %s: inline function but no CFlags in config, skipped", clang.String(fn))
ctx.ignoref(InlineFuncIgnore, fn, "func %s: inline function but no CFlags in config, ignored", clang.String(fn))
Comment thread
xushiwei marked this conversation as resolved.
return
}
manglingName = wrapInlineFunc(ctx, manglingName, fn, this)
Expand Down
21 changes: 15 additions & 6 deletions cl/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,23 @@ func newPointer(ctx *pkgCtx, typ types.Type) types.Type {

const (
featHasCallback = 1 << iota
featIgnored
featExplicitIgnore
featQuietIgnore
featAllIgnore = featExplicitIgnore | featQuietIgnore
)

const (
QuietIgnoreInline = featQuietIgnore
)

var (
InlineFuncIgnore = featExplicitIgnore
)

func toType(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, scope *scopeCtx) types.Type {
var feats int
ret := toTypeEx(ctx, pkg, typ, flags, &feats, scope)
if feats&featIgnored != 0 {
if feats&featAllIgnore != 0 {
panic("unsupported type - " + clang.String(typ))
}
return ret
Expand Down Expand Up @@ -129,7 +139,7 @@ func toTypeEx(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, feats *in
if o, ok := ctx.types[cName]; ok {
if o.feats != 0 {
*feats |= o.feats
if o.feats&featIgnored != 0 {
if o.feats&featAllIgnore != 0 {
return types.Typ[types.Invalid] // ignored type
}
}
Expand Down Expand Up @@ -167,14 +177,13 @@ func toTypeEx(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, feats *in
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
*feats |= featQuietIgnore // will always be ignored
Comment thread
xushiwei marked this conversation as resolved.
return types.Typ[types.Invalid]
default:
ctx.logtf(typ, "toType: unknown kind - %v", typ.Kind)
}
ctx.logtf(typ, "unsupported type - %s", clang.String(typ))
*feats |= featIgnored
*feats |= featExplicitIgnore
return types.Typ[types.Invalid]
}

Expand Down
19 changes: 11 additions & 8 deletions cl/typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ func loadTypedef(ctx *pkgCtx, decl clang.Cursor, ns string, scope *scopeCtx) {
cName := clang.String(decl.Type())
localName := clang.String(decl) // TODO(xsw): use full name with namespace
if ctx.isTypeIgnored(localName) {
if debugCompileDecl {
log.Println("typedef", cName, "- ignored")
}
ctx.ignoreType(cName)
ctx.ignoref(featQuietIgnore, decl, "typedef %s: ignored by config", localName)
ctx.ignoreType(cName, featQuietIgnore)
return
}

origName := nameWithNS(localName, ns)
if decl.Kind == lc.Cursor_TypeAliasTemplateDecl {
ctx.logf(decl, "typedef %s: template type alias, skipped", origName)
ctx.logf(decl, "typedef %s: template type alias, skipped", localName)
return
}

Expand All @@ -52,9 +50,14 @@ func loadTypedef(ctx *pkgCtx, decl clang.Cursor, ns string, scope *scopeCtx) {

feats := 0
tunder := toTypeEx(ctx, pkgTypes, underlying, flagIsTypeDef, &feats, scope)
if feats&featIgnored != 0 || isTypedefUnsupported(tunder) {
ctx.logf(decl, "typedef %s: unsupported underlying type, skipped", origName)
ctx.ignoreType(cName)
if feats&featQuietIgnore != 0 || isTypedefUnsupported(tunder) {
ctx.ignoref(featQuietIgnore, decl, "typedef %s: unsupported underlying type, ignored", localName)
ctx.ignoreType(cName, featQuietIgnore)
return
}
if feats&featExplicitIgnore != 0 {
ctx.logf(decl, "typedef %s: unsupported underlying type, skipped", localName)
ctx.ignoreType(cName, featExplicitIgnore)
Comment thread
xushiwei marked this conversation as resolved.
return
}

Expand Down
4 changes: 2 additions & 2 deletions cl/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func compileVar(ctx *pkgCtx, decl clang.Cursor, ns string) {

feats := 0
typ := toTypeEx(ctx, pkgTypes, decl.Type(), flagIsVarDef, &feats, nil)
if feats&featIgnored != 0 {
ctx.logf(decl, "var %s: unsupported type, skipped", origName)
if feats&featAllIgnore != 0 {
ctx.ignoref(feats, decl, "var %s: unsupported type, ignored", localName)
return
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/llcppg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

"github.com/goplus/gogen"
"github.com/goplus/llcppg/cl"
"github.com/goplus/llcppg/clang"
"github.com/goplus/llcppg/tool"
"github.com/qiniu/x/errors"
Expand All @@ -44,6 +45,10 @@ func Gen(destDir, srcDir string, index clang.Index) (err error) {
}
mainPkgName := cfg.Name

if cfg.IgnoreInline {
cl.InlineFuncIgnore = cl.QuietIgnoreInline // quiet ignore inline functions
Comment thread
xushiwei marked this conversation as resolved.
}

if len(cfg.Pkgs) == 0 {
return genPkg(destDir, srcDir, mainPkgName, index, &cfg)
}
Expand Down
1 change: 1 addition & 0 deletions tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config struct {
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
IgnoreInline bool `json:"IgnoreInline"` // quietly ignore inline functions
Comment thread
xushiwei marked this conversation as resolved.
GroupSubdir bool `json:"GroupSubdir"` // treats sub-directory files as a single file
}

Expand Down
Loading