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/_testpp/macro/in.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#define TRUE 1
#define f() 1
#define g(x) x

#define TRUE 1
#define FALSE 0

#define THREE (TRUE + 2)
#define FVAL (TRUE * 3.14)

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

typedef char BOOL, *PBOOL;
7 changes: 7 additions & 0 deletions cl/_testpp/macro/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ package foo

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

const TRUE = 1
const FALSE = 0
const THREE = 3
const FVAL = 3.14
const MASK = -1
const TWO = 2

type BOOL = c.Char
type PBOOL = *c.Char
51 changes: 41 additions & 10 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ type Config struct {
// specified, llcppg uses a default lookup function that returns an empty archivePath
// and true (it means any mangling name is considered found).
NameLookup func(manglingName string) (archivePath string, ok bool)

// PresumedFiles specifies the list of files that are presumed to be included in the
// compilation. This is used to determine which files are considered part of the
// package being compiled (optional).
PresumedFiles []string
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -139,9 +134,10 @@ func NewPackage(pkgPath, pkgName string, conf *Config, tu clang.TranslationUnit,
nameLookup = defaultNameLookup
}
ctx := &pkgCtx{
pkg: pkg, cb: pkg.CB(), llgo: llgo, fset: pkg.Fset, tu: tu, c: c,
lang: conf.Language, cflags: conf.CFlags, wrapFileHeader: conf.WrapFileHeader,
nameLookup: nameLookup, methods: make(map[string]*classMethod),
pkg: pkg, cb: pkg.CB(), llgo: llgo, fset: pkg.Fset, tu: tu,
c: c, lang: conf.Language, cflags: conf.CFlags,
wrapFileHeader: conf.WrapFileHeader, nameLookup: nameLookup,
methods: make(map[string]*classMethod), macroVals: make(map[string]any),
}
ctx.initFiles(files)
loadFiles(ctx)
Expand All @@ -162,6 +158,13 @@ func loadFiles(ctx *pkgCtx) {
overloads: make(map[string]*overloads),
}
clang.VisitChildren(ctx.tu.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult {
presumedFiles := ctx.presumedFiles
if presumedFiles != nil {
at := clang.PresumedFile(decl.Location())
if _, ok := presumedFiles[at]; !ok {
return clang.Continue
}
}
loadDecl(ctx, scope, decl)
return clang.Continue
})
Expand All @@ -183,17 +186,45 @@ func loadDecl(ctx *pkgCtx, scope *scopeCtx, decl clang.Cursor) {
loadClass(ctx, decl, defaultInPublic)
case lc.CursorCXXMethod, lc.CursorConstructor, lc.CursorDestructor:
loadOutsideMethod(ctx, decl)
case lc.CursorVarDecl:
// compileVarDecl(ctx, decl, global)
case lc.CursorTypedefDecl:
loadTypedef(ctx, decl)
case lc.CursorEnumDecl:
// compileEnum(ctx, decl, global)
case lc.CursorMacroDefinition:
loadMacro(ctx, decl)
case lc.CursorVarDecl:
// compileVarDecl(ctx, decl, global)
default:
log.Panicln("compileDecl: unknown kind =", decl.Kind)
}
}

func loadMacro(ctx *pkgCtx, decl clang.Cursor) {
if decl.IsMacroFunctionLike() != 0 {
return
}
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
origName := clang.String(decl)
tokens, dispose := ctx.tu.Tokenize(decl.Extent())
defer dispose()
if debugCompileDecl {
log.Println("macro", origName, "-", len(tokens), "tokens")
}
if len(tokens) > 1 {
if v, ok := evalConstExpr(ctx, tokens[1:]); ok {
pkg := ctx.pkg
pkgTypes := pkg.Types
ctx.macroVals[origName] = v
name, _ := ctx.getPubName(origName, -1)
pkg.NewConstDefs(pkgTypes.Scope()).New(func(cb *gogen.CodeBuilder) int {
cb.Val(v)
return 1
}, 0, token.NoPos, nil, name)
}
}
})
}

func loadTypedef(ctx *pkgCtx, decl clang.Cursor) {
ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) {
origName := clang.String(decl)
Expand Down
3 changes: 2 additions & 1 deletion cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func testFromDir(t *testing.T, sel, relDir string, lang cl.Language) {
defer idx.Dispose()

filename := pkgDir + "/in.h"
u := idx.ParseTranslationUnit(0, filename, "-x", cltest.LanguageOf(lang))
u := idx.ParseTranslationUnit(
clang.DetailedPreprocessingRecord, filename, "-x", cltest.LanguageOf(lang))
defer u.Dispose()

conf, _ := cltest.LoadConf(pkgDir + "/in.cfg")
Expand Down
Loading
Loading