From 7be85cf882405925396210a29179aaffecf24d53 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 16 Sep 2026 13:39:02 +0800 Subject: [PATCH] cl loadTypedef: support typedef --- cl/_testcpp/ctor_dtor/wrap.cpp | 2 +- cl/_testpp/macro/in.h | 4 ++++ cl/_testpp/macro/out.go | 6 ++++++ cl/compile.go | 20 +++++++++++++++++++- cl/compile_test.go | 4 ++++ cl/ctx.go | 6 +++--- cl/wrap.go | 4 +++- 7 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 cl/_testpp/macro/in.h create mode 100644 cl/_testpp/macro/out.go diff --git a/cl/_testcpp/ctor_dtor/wrap.cpp b/cl/_testcpp/ctor_dtor/wrap.cpp index 9ec43362..82eea653 100644 --- a/cl/_testcpp/ctor_dtor/wrap.cpp +++ b/cl/_testcpp/ctor_dtor/wrap.cpp @@ -4,7 +4,7 @@ extern "C" void _llcppg__ZN3barC1Ev(bar* this) { this->bar(); } -extern "C" void _llcppg__ZN3barC1EPKc(bar* this, const char * a) { +extern "C" void _llcppg__ZN3barC1EPKc(bar* this, const char *a) { this->bar(a); } diff --git a/cl/_testpp/macro/in.h b/cl/_testpp/macro/in.h new file mode 100644 index 00000000..b8d5d304 --- /dev/null +++ b/cl/_testpp/macro/in.h @@ -0,0 +1,4 @@ +#define TRUE 1 +#define FALSE 0 + +typedef char BOOL, *PBOOL; diff --git a/cl/_testpp/macro/out.go b/cl/_testpp/macro/out.go new file mode 100644 index 00000000..a1ec0d6d --- /dev/null +++ b/cl/_testpp/macro/out.go @@ -0,0 +1,6 @@ +package foo + +import "github.com/goplus/lib/c" + +type BOOL = c.Char +type PBOOL = *c.Char diff --git a/cl/compile.go b/cl/compile.go index 3703351b..f763dbe5 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -186,7 +186,7 @@ func loadDecl(ctx *pkgCtx, scope *scopeCtx, decl clang.Cursor) { case lc.CursorVarDecl: // compileVarDecl(ctx, decl, global) case lc.CursorTypedefDecl: - // TODO(xsw) + loadTypedef(ctx, decl) case lc.CursorEnumDecl: // compileEnum(ctx, decl, global) default: @@ -194,4 +194,22 @@ func loadDecl(ctx *pkgCtx, scope *scopeCtx, decl clang.Cursor) { } } +func loadTypedef(ctx *pkgCtx, decl clang.Cursor) { + ctx.compiles = append(ctx.compiles, func(ctx *pkgCtx) { + origName := clang.String(decl) + pkg := ctx.pkg + pkgTypes := pkg.Types + underlying := decl.TypedefDeclUnderlyingType() + if debugCompileDecl { + log.Println("typedef", origName, "-", clang.String(underlying)) + } + tunder := toType(ctx, pkgTypes, underlying, flagIsTypedef) + name, rewritten := ctx.getPubName(origName, -1) + t := pkg.NewTypeDefs().AliasType(name, tunder) + if rewritten { + pkgTypes.Scope().Insert(types.NewTypeName(token.NoPos, pkgTypes, origName, t)) + } + }) +} + // ----------------------------------------------------------------------------- diff --git a/cl/compile_test.go b/cl/compile_test.go index 971ad36f..891f3193 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -101,4 +101,8 @@ func TestCpp(t *testing.T) { testFromDir(t, "", "./_testcpp", cl.LanguageCXX) } +func TestPreprocessor(t *testing.T) { + testFromDir(t, "", "./_testpp", cl.LanguageC) +} + // ----------------------------------------------------------------------------- diff --git a/cl/ctx.go b/cl/ctx.go index a3c9e140..20f42443 100644 --- a/cl/ctx.go +++ b/cl/ctx.go @@ -141,12 +141,12 @@ func (p *pkgCtx) compile() { } } -func (p *pkgCtx) getPubName(fnName string, order int) (pubName string, rewritten bool) { - pubName = cPubName(fnName) +func (p *pkgCtx) getPubName(cName string, order int) (pubName string, rewritten bool) { + pubName = cPubName(cName) if order >= 0 { return pubName + "__" + strconv.FormatInt(int64(order), 36), true } - rewritten = fnName != pubName + rewritten = cName != pubName return } diff --git a/cl/wrap.go b/cl/wrap.go index bf0ba982..2f974ee8 100644 --- a/cl/wrap.go +++ b/cl/wrap.go @@ -122,7 +122,9 @@ func writeParam(b *writerT, typ lc.Type, name string) { return } b.WriteString(clang.String(typ)) - b.WriteByte(' ') + if typ.Kind != lc.TypePointer { + b.WriteByte(' ') + } b.WriteString(name) }