Skip to content

Commit

Permalink
gogensig:redefined forward decl not cause fail output
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Dec 23, 2024
1 parent 836ab18 commit 2ac32df
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forwarddecl",
"include": ["temp.h"],
"include": ["temp.h", "impl.h"],
"trimPrefixes": ["sqlite3_","lua_"],
"cplusplus":false
}
9 changes: 5 additions & 4 deletions cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type Foo struct {
A c.Long
}

type File struct {
Unused [8]uint8
}

===== temp.go =====
package forwarddecl

Expand All @@ -22,10 +26,6 @@ type Bar struct {
A *Foo
}

type File struct {
PMethods *IoMethods
}

type IoMethods struct {
XUnfetch unsafe.Pointer
}
Expand Down Expand Up @@ -112,6 +112,7 @@ Fts5Context
Fts5ExtensionApi
Fts5PhraseIter
bar Bar
foo Foo
fts5_extension_function Fts5ExtensionFunction
lua_Debug Debug
lua_State State
Expand Down
7 changes: 5 additions & 2 deletions cmd/gogensig/convert/_testdata/forwarddecl/hfile/impl.h
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 7 additions & 14 deletions cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
#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;
};

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 *);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};
6 changes: 5 additions & 1 deletion cmd/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2ac32df

Please sign in to comment.