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
2 changes: 1 addition & 1 deletion cl/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func emitClass(ctx *pkgCtx, cls clang.Cursor, clsName string, kind typeTag) *typ
}
typStruc := types.NewStruct(scope.fields, nil)
typDecl.InitType(pkg, typStruc)
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
ctx.addCompileUnit(func(ctx *pkgCtx) {
compileClass(ctx, scope)
})
return typNamed
Expand Down
16 changes: 12 additions & 4 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ type Config struct {
// wrapper file (optional).
WrapFileHeader string

// DefaultGoFile specifies default file name (optional).
DefaultGoFile string

// NameLookup looks up the archive path for a given mangling name. It returns the
// archive path and a boolean indicating whether the lookup was successful. If not
// specified, llcppg uses a default lookup function that returns an empty archivePath
Expand Down Expand Up @@ -138,7 +141,10 @@ type Config struct {
// -----------------------------------------------------------------------------

// Source represents a source file to be processed by llcppg.
type Source = clang.TranslationUnit
type Source struct {
TU clang.TranslationUnit
GoFile string
}

// NewPackage loads a translation unit and generates a Go package with the given package
// path, name and configuration.
Expand All @@ -155,7 +161,7 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack
NewBuiltin: nil,
NodeInterpreter: interp,
CanImplicitCast: nil,
DefaultGoFile: "",
DefaultGoFile: conf.DefaultGoFile,
}
pkg := gogen.NewPackage(pkgPath, pkgName, confGox)
pkg.SetRedeclarable(true)
Expand Down Expand Up @@ -200,12 +206,14 @@ func defaultNameLookup(manglingName string) (archivePath string, ok bool) {
// -----------------------------------------------------------------------------

func loadFiles(ctx *pkgCtx, files []Source, myPkgPath string) {
pkg := ctx.pkg
pkgOf := ctx.pkgOf
scope := &ctx.scopeCtx
lastSeen := ctx.lastSeen
for _, tu := range files {
for _, f := range files {
pkg.SetCurFile(f.GoFile, true)
ctx.thisSeen = make(map[string]none) // reset for each file
clang.VisitChildren(tu.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult {
clang.VisitChildren(f.TU.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult {
if pkgOf != nil {
at := clang.PresumedFile(decl.Location())
if _, ok := lastSeen[at]; ok {
Expand Down
2 changes: 1 addition & 1 deletion cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func testFromDir(t *testing.T, sel, relDir string, lang cl.Language) {
u := idx.ParseTranslationUnit(
clang.DetailedPreprocessingRecord, presumedFile, "-x", cltest.LanguageOf(lang))
defer u.Dispose()
files[i] = u
files[i] = cl.Source{TU: u}
u.VisitDiagnostics(func(diag clang.Diagnostic) {
fmt.Fprintln(os.Stderr, diag.Format(options))
})
Expand Down
19 changes: 16 additions & 3 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func (p *nodeInterp) LoadExpr(v ast.Node) string {

type none struct{}
type compileFunc = func(ctx *pkgCtx)
type compileUnit struct {
fn compileFunc
at *gogen.File
}

type pkgCtx struct {
scopeCtx
Expand Down Expand Up @@ -120,7 +124,7 @@ type pkgCtx struct {
lastSeen map[string]none // last seen include file set (loaded include files)
thisSeen map[string]none // include file set seen in this translation unit

compiles []compileFunc
compiles []compileUnit
pubs []Entry

// anonUnionSeq is the per-package counter that names tagless inline unions
Expand Down Expand Up @@ -166,9 +170,18 @@ func (p *pkgCtx) getFileBase(c clang.Cursor, file clang.File) int {
return base
}

func (p *pkgCtx) addCompileUnit(f compileFunc) {
p.compiles = append(p.compiles, compileUnit{
fn: f,
at: p.pkg.CurFile(),
})
}

func (p *pkgCtx) compile() {
for _, compile := range p.compiles {
compile(p)
pkg := p.pkg
for _, c := range p.compiles {
pkg.RestoreCurFile(c.at)
c.fn(p)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cl/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func loadGlobalFunc(ctx *pkgCtx, scope *scopeCtx, decl clang.Cursor, ns string) {
name := nameWithNS(clang.String(decl), ns)
if obj, ok := scope.addFunc(ctx, name, decl); ok {
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
ctx.addCompileUnit(func(ctx *pkgCtx) {
compileFuncOrMethod(ctx, obj, nil)
})
}
Expand Down
2 changes: 1 addition & 1 deletion cl/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func emitUnion(ctx *pkgCtx, decl clang.Cursor, uName string) *types.Named {
})

recvPtr := types.NewPointer(typNamed)
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
ctx.addCompileUnit(func(ctx *pkgCtx) {
for _, m := range members {
genUnionAccessor(ctx, recvPtr, m)
}
Expand Down
2 changes: 1 addition & 1 deletion cl/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// handled). Non-static class member variables are fields, not VarDecls, and are
// handled separately in loadClassMember.
func loadVar(ctx *pkgCtx, decl clang.Cursor, ns string) {
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
ctx.addCompileUnit(func(ctx *pkgCtx) {
compileVar(ctx, decl, ns)
})
}
Expand Down
2 changes: 1 addition & 1 deletion tool/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func dumpSources(filenames []string, files []cl.Source) {
for i, f := range files {
filename := filenames[i]
log.Println("==> dump", filename)
Dump(f.Cursor(), "", filepath.Dir(filename))
Dump(f.TU.Cursor(), "", filepath.Dir(filename))
}
}
6 changes: 3 additions & 3 deletions tool/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func ParseSources(index clang.Index, headerFiles, includeDirs []string, lang str
flags[n+1] = lang
files := make([]cl.Source, 0, len(headerFiles))
for tu := range index.ParseTranslationUnits(clang.DetailedPreprocessingRecord, headerFiles, flags...) {
files = append(files, tu)
files = append(files, cl.Source{TU: tu})
tu.VisitDiagnostics(func(diag clang.Diagnostic) {
fmt.Fprintln(os.Stderr, diag.Format(options))
})
Expand All @@ -265,8 +265,8 @@ func ParseSources(index clang.Index, headerFiles, includeDirs []string, lang str

// DisposeSources disposes the given translation units.
func DisposeSources(sources []cl.Source) {
for _, tu := range sources {
tu.Dispose()
for _, f := range sources {
f.TU.Dispose()
}
}

Expand Down
2 changes: 1 addition & 1 deletion tool/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func testSingleFile(t *testing.T, idx clang.Index, pkgDir, headerDir, headerFile
})

const pkgPrefix = "clang/"
files := []cl.Source{u}
files := []cl.Source{{TU: u}}
imp := packages.NewImporter(nil, headerDir)
pkg, err := cl.NewPackage(pkgPrefix+myPkgName, myPkgName, files, &cl.Config{
Importer: imp,
Expand Down
Loading