diff --git a/cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg b/cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg index 1bc8f49..92eadf2 100644 --- a/cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg +++ b/cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg @@ -1,6 +1,6 @@ { "name": "forwarddecl", - "include": ["temp.h"], + "include": ["temp.h", "impl.h"], "trimPrefixes": ["sqlite3_","lua_"], "cplusplus":false } diff --git a/cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect b/cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect index 41a7066..900fe37 100644 --- a/cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect +++ b/cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect @@ -10,6 +10,10 @@ type Foo struct { A c.Long } +type File struct { + Unused [8]uint8 +} + ===== temp.go ===== package forwarddecl @@ -22,10 +26,6 @@ type Bar struct { A *Foo } -type File struct { - PMethods *IoMethods -} - type IoMethods struct { XUnfetch unsafe.Pointer } @@ -112,6 +112,7 @@ Fts5Context Fts5ExtensionApi Fts5PhraseIter bar Bar +foo Foo fts5_extension_function Fts5ExtensionFunction lua_Debug Debug lua_State State diff --git a/cmd/gogensig/convert/_testdata/forwarddecl/hfile/impl.h b/cmd/gogensig/convert/_testdata/forwarddecl/hfile/impl.h index 9bf447b..c2d3d88 100644 --- a/cmd/gogensig/convert/_testdata/forwarddecl/hfile/impl.h +++ b/cmd/gogensig/convert/_testdata/forwarddecl/hfile/impl.h @@ -1,4 +1,7 @@ -struct foo -{ +struct foo { long a; }; + +// Forward declaration of sqlite3_file +// todo: the sqlite3_file 's real type should be defined. +struct sqlite3_file; diff --git a/cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h b/cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h index 1bb32ae..26ee1e7 100644 --- a/cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h +++ b/cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h @@ -1,25 +1,21 @@ #include "impl.h" typedef struct foo foo; -struct bar -{ +struct bar { foo *a; }; typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file -{ +struct sqlite3_file { const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ }; typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods -{ +struct sqlite3_io_methods { int (*xUnfetch)(sqlite3_file *, int iOfst, void *p); }; typedef struct sqlite3_pcache_page sqlite3_pcache_page; -struct sqlite3_pcache_page -{ +struct sqlite3_pcache_page { void *pBuf; void *pExtra; }; @@ -27,8 +23,7 @@ struct sqlite3_pcache_page typedef struct sqlite3_pcache sqlite3_pcache; typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; -struct sqlite3_pcache_methods2 -{ +struct sqlite3_pcache_methods2 { int iVersion; void *pArg; int (*xInit)(void *); @@ -52,8 +47,7 @@ typedef struct lua_Debug lua_Debug; int(lua_getstack)(lua_State *L, int level, lua_Debug *ar); -struct lua_Debug -{ +struct lua_Debug { int event; const char *name; const char *namewhat; @@ -86,8 +80,7 @@ typedef void (*fts5_extension_function)(const Fts5ExtensionApi *pApi, /* API off sqlite3_value **apVal /* Array of trailing arguments */ ); -struct Fts5PhraseIter -{ +struct Fts5PhraseIter { const unsigned char *a; const unsigned char *b; }; \ No newline at end of file diff --git a/cmd/gogensig/convert/package.go b/cmd/gogensig/convert/package.go index c467195..bcaef32 100644 --- a/cmd/gogensig/convert/package.go +++ b/cmd/gogensig/convert/package.go @@ -236,8 +236,12 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error { } cname := typeDecl.Name.Name + isForward := p.cvt.inComplete(typeDecl.Type) name, changed, err := p.DeclName(cname) if err != nil { + if isForward { + return nil + } return err } p.CollectNameMapping(cname, name) @@ -248,7 +252,7 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error { substObj(p.p.Types, p.p.Types.Scope(), cname, decl.Type().Obj()) } - if !p.cvt.inComplete(typeDecl.Type) { + if !isForward { if err := p.handleCompleteType(decl, typeDecl.Type, cname); err != nil { return err } diff --git a/cmd/gogensig/convert/package_test.go b/cmd/gogensig/convert/package_test.go index 72dace4..95629bd 100644 --- a/cmd/gogensig/convert/package_test.go +++ b/cmd/gogensig/convert/package_test.go @@ -1588,17 +1588,17 @@ func TestForwardDecl(t *testing.T) { ), }) - // forward decl - err := pkg.NewTypeDecl(&ast.TypeDecl{ + forwardDecl := &ast.TypeDecl{ Name: &ast.Ident{Name: "Foo"}, Type: &ast.RecordType{ Tag: ast.Struct, Fields: &ast.FieldList{}, }, - }) - + } + // forward decl + err := pkg.NewTypeDecl(forwardDecl) if err != nil { - t.Fatalf("NewTypeDecl failed: %v", err) + t.Fatalf("Forward decl failed: %v", err) } // complete decl @@ -1621,6 +1621,12 @@ func TestForwardDecl(t *testing.T) { t.Fatalf("NewTypeDecl failed: %v", err) } + err = pkg.NewTypeDecl(forwardDecl) + + if err != nil { + t.Fatalf("NewTypeDecl failed: %v", err) + } + expect := ` package testpkg