Skip to content

Commit 5cf6503

Browse files
authored
gogensig:panic when convert fail (#271)
* gogensig:panic when convert func fail * gogensig:panic when type complete fail * gogensig:panic when typedef fail * gogensig:panic when enum item fail * gogensig:remove unuse comment
1 parent d6ee940 commit 5cf6503

File tree

2 files changed

+94
-31
lines changed

2 files changed

+94
-31
lines changed

cmd/gogensig/convert/package.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (p *Package) newReceiver(typ *ast.FuncType) *types.Var {
181181
recvField := typ.Params.List[0]
182182
recvType, err := p.ToType(recvField.Type)
183183
if err != nil {
184-
log.Println(err)
184+
log.Panicf("newReceiver:failed to convert type: %s", err.Error())
185185
}
186186
return p.p.NewParam(token.NoPos, "recv_", recvType)
187187
}
@@ -292,7 +292,7 @@ func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
292292
log.Printf("NewFuncDecl: %v\n", funcDecl.Name)
293293
}
294294
if anony {
295-
return errs.NewAnonymousFuncNotSupportError()
295+
log.Panicln("NewFuncDecl: fail convert anonymous function") // Unreachable
296296
}
297297

298298
fnSpec, err := p.LookupSymbol(funcDecl.MangledName)
@@ -312,7 +312,7 @@ func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
312312

313313
sig, err := p.ToSigSignature(recv, funcDecl)
314314
if err != nil {
315-
return err
315+
log.Panicf("NewFuncDecl: fail convert signature : %s\n", err.Error())
316316
}
317317
return p.handleFuncDecl(fnSpec, sig, funcDecl)
318318
}
@@ -353,10 +353,7 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
353353
log.Printf("NewTypeDecl: %v\n", typeDecl.Name)
354354
}
355355
if anony {
356-
if dbg.GetDebugLog() {
357-
log.Println("NewTypeDecl:Skip a anonymous type")
358-
}
359-
return nil
356+
log.Panicln("NewFuncDecl: fail convert anonymous type") // Unreachable
360357
}
361358

362359
cname := typeDecl.Name.Name
@@ -378,7 +375,7 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
378375

379376
if !isForward {
380377
if err := p.handleCompleteType(incom, typeDecl.Type, cname); err != nil {
381-
return err
378+
log.Panicf("NewTypeDecl: fail to complete type : %s\n", err.Error())
382379
}
383380
}
384381
return nil
@@ -410,7 +407,6 @@ func (p *Package) handleCompleteType(incom *Incomplete, typ *ast.RecordType, nam
410407
structType, err := p.cvt.RecordTypeToStruct(typ)
411408
if err != nil {
412409
// For incomplete type's conerter error, we use default struct type
413-
incom.decl.InitType(p.p, types.NewStruct(p.cvt.defaultRecordField(), nil))
414410
return err
415411
}
416412
incom.decl.InitType(p.p, structType)
@@ -480,8 +476,7 @@ func (p *Package) NewTypedefDecl(typedefDecl *ast.TypedefDecl) error {
480476

481477
typ, err := p.ToType(typedefDecl.Type)
482478
if err != nil {
483-
typeSpecdecl.InitType(p.p, types.NewStruct(p.cvt.defaultRecordField(), nil))
484-
return err
479+
log.Panicf("NewTypedefDecl:fail to convert type : %s\n", err.Error())
485480
}
486481

487482
typeSpecdecl.InitType(p.p, typ)
@@ -593,7 +588,7 @@ func (p *Package) createEnumItems(items []*ast.EnumItem, enumType types.Type) er
593588
}
594589
val, err := Expr(item.Value).ToInt()
595590
if err != nil {
596-
return err
591+
log.Panicf("createEnumItems:fail to convert %T to int:%s", item.Value, err.Error())
597592
}
598593
defs.New(val, enumType, name)
599594
if changed {

cmd/gogensig/convert/package_test.go

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func Foo(__llgo_va_list ...interface{})`,
419419
GoName: "InvalidFunc",
420420
},
421421
},
422-
expectedErr: "not found in type map",
422+
expectedPanic: "NewFuncDecl: fail convert signature : not found in type map",
423423
},
424424
{
425425
name: "explict void return",
@@ -747,7 +747,7 @@ func Foo(a *c.Uint, b *c.Double) **c.Char
747747
GoName: "Foo",
748748
},
749749
},
750-
expectedErr: "error convert elem type",
750+
expectedPanic: "NewFuncDecl: fail convert signature : error convert elem type: not found in type map",
751751
},
752752
{
753753
name: "error return type",
@@ -766,7 +766,7 @@ func Foo(a *c.Uint, b *c.Double) **c.Char
766766
GoName: "Foo",
767767
},
768768
},
769-
expectedErr: "error convert return type",
769+
expectedPanic: "NewFuncDecl: fail convert signature : error convert return type: not found in type map",
770770
},
771771
{
772772
name: "error nil param",
@@ -789,7 +789,53 @@ func Foo(a *c.Uint, b *c.Double) **c.Char
789789
GoName: "Foo",
790790
},
791791
},
792-
expectedErr: "unexpected nil field",
792+
expectedPanic: "NewFuncDecl: fail convert signature : error convert type: unexpected nil field",
793+
},
794+
{
795+
name: "error receiver",
796+
decl: &ast.FuncDecl{
797+
DeclBase: ast.DeclBase{
798+
Loc: &ast.Location{File: tempFile.File},
799+
},
800+
Name: &ast.Ident{Name: "foo"},
801+
MangledName: "foo",
802+
Type: &ast.FuncType{
803+
Params: &ast.FieldList{
804+
List: []*ast.Field{
805+
{
806+
Type: &ast.BuiltinType{Kind: ast.Int, Flags: ast.Double},
807+
},
808+
},
809+
},
810+
},
811+
},
812+
symbs: []config.SymbolEntry{
813+
{
814+
CppName: "foo",
815+
MangleName: "foo",
816+
GoName: "(*Foo).foo",
817+
},
818+
},
819+
expectedPanic: "newReceiver:failed to convert type",
820+
},
821+
{
822+
name: "anony func",
823+
decl: &ast.FuncDecl{
824+
Name: nil,
825+
MangledName: "foo",
826+
Type: &ast.FuncType{
827+
Params: nil,
828+
Ret: &ast.BuiltinType{Kind: ast.Void},
829+
},
830+
},
831+
symbs: []config.SymbolEntry{
832+
{
833+
CppName: "foo",
834+
MangleName: "foo",
835+
GoName: "Foo",
836+
},
837+
},
838+
expectedPanic: "NewFuncDecl: fail convert anonymous function",
793839
},
794840
}
795841
for _, tc := range testCases {
@@ -836,7 +882,7 @@ type Foo struct {
836882
},
837883
},
838884
},
839-
expectedErr: "not found in type map",
885+
expectedPanic: "NewTypeDecl: fail to complete type : not found in type map",
840886
},
841887
// struct Foo { int a; double b; bool c; }
842888
{
@@ -1051,10 +1097,8 @@ type Foo struct {
10511097
Fields: &ast.FieldList{},
10521098
},
10531099
},
1054-
expected: `
1055-
package testpkg
1056-
import _ "unsafe"
1057-
`},
1100+
expectedPanic: "NewFuncDecl: fail convert anonymous type",
1101+
},
10581102
{
10591103
name: "struct array field without len",
10601104
decl: &ast.TypeDecl{
@@ -1076,7 +1120,7 @@ import _ "unsafe"
10761120
},
10771121
},
10781122
},
1079-
expectedErr: "unsupport field with array without length",
1123+
expectedPanic: "NewTypeDecl: fail to complete type : unsupport field with array without length",
10801124
},
10811125
{
10821126
name: "struct array field without len",
@@ -1100,7 +1144,7 @@ import _ "unsafe"
11001144
},
11011145
},
11021146
},
1103-
expectedErr: "can't determine the array length",
1147+
expectedPanic: "NewTypeDecl: fail to complete type : can't determine the array length",
11041148
},
11051149
}
11061150

@@ -1303,7 +1347,7 @@ type DOUBLE c.Double`,
13031347
Flags: ast.Double,
13041348
},
13051349
},
1306-
expectedErr: "not found in type map",
1350+
expectedPanic: "NewTypedefDecl:fail to convert type : not found in type map",
13071351
},
13081352
// typedef int INT;
13091353
{
@@ -1400,7 +1444,7 @@ type Name *c.Char`,
14001444
},
14011445
},
14021446
},
1403-
expectedErr: "error convert baseType",
1447+
expectedPanic: "NewTypedefDecl:fail to convert type : error convert baseType: not found in type map",
14041448
},
14051449
}
14061450

@@ -1466,6 +1510,18 @@ const (
14661510
Blue c.Int = 2
14671511
)`,
14681512
},
1513+
{
1514+
name: "invalid enum item",
1515+
decl: &ast.EnumTypeDecl{
1516+
Name: nil,
1517+
Type: &ast.EnumType{
1518+
Items: []*ast.EnumItem{
1519+
{Name: &ast.Ident{Name: "red"}, Value: &ast.ArrayType{Elt: &ast.BuiltinType{Kind: ast.Bool}}},
1520+
},
1521+
},
1522+
},
1523+
expectedPanic: "createEnumItems:fail to convert *ast.ArrayType to int",
1524+
},
14691525
}
14701526
for _, tc := range testCases {
14711527
t.Run(tc.name, func(t *testing.T) {
@@ -1662,16 +1718,28 @@ type Foo struct {
16621718
}
16631719

16641720
type genDeclTestCase struct {
1665-
name string
1666-
decl ast.Decl
1667-
symbs []config.SymbolEntry
1668-
cppgconf *llcppg.Config
1669-
expected string
1670-
expectedErr string
1721+
name string
1722+
decl ast.Decl
1723+
symbs []config.SymbolEntry
1724+
cppgconf *llcppg.Config
1725+
expected string
1726+
expectedErr string
1727+
expectedPanic string
16711728
}
16721729

16731730
func testGenDecl(t *testing.T, tc genDeclTestCase) {
16741731
t.Helper()
1732+
defer func() {
1733+
if r := recover(); r != nil {
1734+
if tc.expectedPanic != "" {
1735+
if !strings.HasPrefix(r.(string), tc.expectedPanic) {
1736+
t.Errorf("Expected panic %s, but got: %v", tc.expectedPanic, r)
1737+
}
1738+
} else {
1739+
t.Fatal("unexpect panic", r)
1740+
}
1741+
}
1742+
}()
16751743
pkg := createTestPkg(t, &convert.PackageConfig{
16761744
SymbolTable: config.CreateSymbolTable(tc.symbs),
16771745
PkgBase: convert.PkgBase{

0 commit comments

Comments
 (0)