@@ -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
16641720type 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
16731730func 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