From 086fa214bb6f7a4d8e3cf3dca79b03aa9312587f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Wed, 21 Dec 2022 12:19:46 -0500 Subject: [PATCH] rpcgen: add g_auto function support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will eliminate the need to call xdr_free to clear pointers from data structures. Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrangé --- build-aux/syntax-check.mk | 2 +- scripts/rpcgen/main.py | 4 + scripts/rpcgen/rpcgen/generator.py | 64 +++++++++-- scripts/rpcgen/tests/demo.c | 144 +++++++++++++++++++++++++ scripts/rpcgen/tests/demo.h | 48 +++++++++ scripts/rpcgen/tests/test_demo.c | 91 ++++++++-------- scripts/rpcgen/tests/test_generator.py | 7 +- 7 files changed, 298 insertions(+), 62 deletions(-) diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 17cae3f23e..53dcf11a06 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -1471,7 +1471,7 @@ exclude_file_name_regexp--sc_prohibit_mixed_case_abbreviations = \ ^src/(vbox/vbox_CAPI.*.h|esx/esx_vi.(c|h)|esx/esx_storage_backend_iscsi.c)$$ exclude_file_name_regexp--sc_prohibit_empty_first_line = \ - ^tests/vmwareverdata/fusion-5.0.3.txt$$ + ^tests/vmwareverdata/fusion-5.0.3.txt|scripts/rpcgen/tests/demo\.c$$ exclude_file_name_regexp--sc_prohibit_useless_translation = \ ^tests/virpolkittest.c diff --git a/scripts/rpcgen/main.py b/scripts/rpcgen/main.py index bde6dcca2a..a41be48929 100755 --- a/scripts/rpcgen/main.py +++ b/scripts/rpcgen/main.py @@ -8,6 +8,7 @@ import sys from rpcgen.parser import XDRParser from rpcgen.generator import ( XDRTypeDeclarationGenerator, + XDRTypeImplementationGenerator, XDRMarshallDeclarationGenerator, XDRMarshallImplementationGenerator, ) @@ -59,6 +60,7 @@ def main(): if args.mode == "header": print("/* This file is auto-generated from %s */\n" % args.input, file=outfp) print("#include ", file=outfp) + print('#include "internal.h"', file=outfp) for h in args.header: print('#include "%s"' % h, file=outfp) print("", file=outfp) @@ -73,6 +75,8 @@ def main(): for h in args.header: print('#include "%s"' % h, file=outfp) print("", file=outfp) + generator = XDRTypeImplementationGenerator(spec) + print(generator.visit(), file=outfp) generator = XDRMarshallImplementationGenerator(spec) print(generator.visit(), file=outfp) elif args.mode == "repr": diff --git a/scripts/rpcgen/rpcgen/generator.py b/scripts/rpcgen/rpcgen/generator.py index ccaf7e74eb..73731e1497 100644 --- a/scripts/rpcgen/rpcgen/generator.py +++ b/scripts/rpcgen/rpcgen/generator.py @@ -30,24 +30,42 @@ class XDRTypeDeclarationGenerator(XDRVisitor): ) + "%stypedef enum %s %s;\n" % (indent, obj.name, obj.name) return code - def visit_definition_struct(self, obj, indent, context): - code = "%sstruct %s %s;\n" % ( + def generate_cleanup(self, name, indent): + code = "%svoid xdr_%s_clear(%s *objp);\n" % ( indent, - obj.name, - self.visit_object(obj.body, indent), - ) + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) + name, + name, + ) + "%sG_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(%s, xdr_%s_clear);\n" % ( + indent, + name, + name, + ) + return code + + def visit_definition_struct(self, obj, indent, context): + code = ( + "%sstruct %s %s;\n" + % (indent, obj.name, self.visit_object(obj.body, indent)) + + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) + + self.generate_cleanup(obj.name, indent) + ) return code def visit_definition_union(self, obj, indent, context): - code = "%sstruct %s %s;\n" % ( - indent, - obj.name, - self.visit_object(obj.body, indent, obj.name), - ) + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) + code = ( + "%sstruct %s %s;\n" + % (indent, obj.name, self.visit_object(obj.body, indent, obj.name)) + + "%stypedef struct %s %s;\n" % (indent, obj.name, obj.name) + + self.generate_cleanup(obj.name, indent) + ) return code def visit_definition_typedef(self, obj, indent, context): - return "%stypedef %s;\n" % (indent, self.visit_object(obj.decl, indent)) + code = "%stypedef %s;\n" % ( + indent, + self.visit_object(obj.decl, indent), + ) + self.generate_cleanup(obj.decl.identifier, indent) + return code def visit_declaration_scalar(self, obj, indent, context): return "%s %s" % (self.visit_object(obj.typ, indent), obj.identifier) @@ -169,6 +187,30 @@ class XDRTypeDeclarationGenerator(XDRVisitor): return code +class XDRTypeImplementationGenerator(XDRVisitor): + def visit_definition_enum(self, obj, indent, context): + pass + + def generate_cleanup(self, name, indent): + code = ( + "\n" + + "%svoid xdr_%s_clear(%s *objp)\n" % (indent, name, name) + + "%s{\n" % indent + + "%s xdr_free((xdrproc_t)xdr_%s, (char *)objp);\n" % (indent, name) + + "%s}\n" % indent + ) + return code + + def visit_definition_union(self, obj, indent, context): + return self.generate_cleanup(obj.name, indent) + + def visit_definition_struct(self, obj, indent, context): + return self.generate_cleanup(obj.name, indent) + + def visit_definition_typedef(self, obj, indent, context): + return self.generate_cleanup(obj.decl.identifier, indent) + + class XDRMarshallDeclarationGenerator(XDRVisitor): def visit_definition_enum(self, obj, indent, context): return "%sextern bool_t xdr_%s(XDR *, %s*);\n" % (indent, obj.name, obj.name) diff --git a/scripts/rpcgen/tests/demo.c b/scripts/rpcgen/tests/demo.c index 2ce618b9ce..182ed448f0 100644 --- a/scripts/rpcgen/tests/demo.c +++ b/scripts/rpcgen/tests/demo.c @@ -1,3 +1,147 @@ + +void xdr_TestStruct_clear(TestStruct *objp) +{ + xdr_free((xdrproc_t)xdr_TestStruct, (char *)objp); +} + + +void xdr_TestUnion_clear(TestUnion *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnion, (char *)objp); +} + + +void xdr_TestUnionVoidDefault_clear(TestUnionVoidDefault *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionVoidDefault, (char *)objp); +} + + +void xdr_TestUnionNoDefault_clear(TestUnionNoDefault *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionNoDefault, (char *)objp); +} + + +void xdr_TestIntScalar_clear(TestIntScalar *objp) +{ + xdr_free((xdrproc_t)xdr_TestIntScalar, (char *)objp); +} + + +void xdr_TestIntPointer_clear(TestIntPointer *objp) +{ + xdr_free((xdrproc_t)xdr_TestIntPointer, (char *)objp); +} + + +void xdr_TestIntFixedArray_clear(TestIntFixedArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestIntFixedArray, (char *)objp); +} + + +void xdr_TestIntVariableArray_clear(TestIntVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestIntVariableArray, (char *)objp); +} + + +void xdr_TestStringVariableArray_clear(TestStringVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestStringVariableArray, (char *)objp); +} + + +void xdr_TestOpaqueFixedArray_clear(TestOpaqueFixedArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestOpaqueFixedArray, (char *)objp); +} + + +void xdr_TestOpaqueVariableArray_clear(TestOpaqueVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestOpaqueVariableArray, (char *)objp); +} + + +void xdr_TestEnumScalar_clear(TestEnumScalar *objp) +{ + xdr_free((xdrproc_t)xdr_TestEnumScalar, (char *)objp); +} + + +void xdr_TestEnumPointer_clear(TestEnumPointer *objp) +{ + xdr_free((xdrproc_t)xdr_TestEnumPointer, (char *)objp); +} + + +void xdr_TestEnumFixedArray_clear(TestEnumFixedArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestEnumFixedArray, (char *)objp); +} + + +void xdr_TestEnumVariableArray_clear(TestEnumVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestEnumVariableArray, (char *)objp); +} + + +void xdr_TestStructScalar_clear(TestStructScalar *objp) +{ + xdr_free((xdrproc_t)xdr_TestStructScalar, (char *)objp); +} + + +void xdr_TestStructPointer_clear(TestStructPointer *objp) +{ + xdr_free((xdrproc_t)xdr_TestStructPointer, (char *)objp); +} + + +void xdr_TestStructFixedArray_clear(TestStructFixedArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestStructFixedArray, (char *)objp); +} + + +void xdr_TestStructVariableArray_clear(TestStructVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestStructVariableArray, (char *)objp); +} + + +void xdr_TestUnionScalar_clear(TestUnionScalar *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionScalar, (char *)objp); +} + + +void xdr_TestUnionPointer_clear(TestUnionPointer *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionPointer, (char *)objp); +} + + +void xdr_TestUnionFixedArray_clear(TestUnionFixedArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionFixedArray, (char *)objp); +} + + +void xdr_TestUnionVariableArray_clear(TestUnionVariableArray *objp) +{ + xdr_free((xdrproc_t)xdr_TestUnionVariableArray, (char *)objp); +} + + +void xdr_TestStructAllTypes_clear(TestStructAllTypes *objp) +{ + xdr_free((xdrproc_t)xdr_TestStructAllTypes, (char *)objp); +} + bool_t xdr_TestEnum(XDR *xdrs, TestEnum *objp) { diff --git a/scripts/rpcgen/tests/demo.h b/scripts/rpcgen/tests/demo.h index 6fac61e7e9..36bcb40916 100644 --- a/scripts/rpcgen/tests/demo.h +++ b/scripts/rpcgen/tests/demo.h @@ -9,6 +9,8 @@ struct TestStruct { char c2; }; typedef struct TestStruct TestStruct; +void xdr_TestStruct_clear(TestStruct *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStruct, xdr_TestStruct_clear); struct TestUnion { int type; @@ -19,6 +21,8 @@ struct TestUnion { } TestUnion_u; }; typedef struct TestUnion TestUnion; +void xdr_TestUnion_clear(TestUnion *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnion, xdr_TestUnion_clear); struct TestUnionVoidDefault { int type; @@ -28,6 +32,8 @@ struct TestUnionVoidDefault { } TestUnionVoidDefault_u; }; typedef struct TestUnionVoidDefault TestUnionVoidDefault; +void xdr_TestUnionVoidDefault_clear(TestUnionVoidDefault *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionVoidDefault, xdr_TestUnionVoidDefault_clear); struct TestUnionNoDefault { int type; @@ -37,59 +43,99 @@ struct TestUnionNoDefault { } TestUnionNoDefault_u; }; typedef struct TestUnionNoDefault TestUnionNoDefault; +void xdr_TestUnionNoDefault_clear(TestUnionNoDefault *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionNoDefault, xdr_TestUnionNoDefault_clear); typedef int TestIntScalar; +void xdr_TestIntScalar_clear(TestIntScalar *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntScalar, xdr_TestIntScalar_clear); typedef int *TestIntPointer; +void xdr_TestIntPointer_clear(TestIntPointer *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntPointer, xdr_TestIntPointer_clear); typedef int TestIntFixedArray[3]; +void xdr_TestIntFixedArray_clear(TestIntFixedArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntFixedArray, xdr_TestIntFixedArray_clear); typedef struct { u_int TestIntVariableArray_len; int *TestIntVariableArray_val; } TestIntVariableArray; +void xdr_TestIntVariableArray_clear(TestIntVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestIntVariableArray, xdr_TestIntVariableArray_clear); typedef char *TestStringVariableArray; +void xdr_TestStringVariableArray_clear(TestStringVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStringVariableArray, xdr_TestStringVariableArray_clear); typedef char TestOpaqueFixedArray[9]; +void xdr_TestOpaqueFixedArray_clear(TestOpaqueFixedArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestOpaqueFixedArray, xdr_TestOpaqueFixedArray_clear); typedef struct { u_int TestOpaqueVariableArray_len; char *TestOpaqueVariableArray_val; } TestOpaqueVariableArray; +void xdr_TestOpaqueVariableArray_clear(TestOpaqueVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestOpaqueVariableArray, xdr_TestOpaqueVariableArray_clear); typedef TestEnum TestEnumScalar; +void xdr_TestEnumScalar_clear(TestEnumScalar *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumScalar, xdr_TestEnumScalar_clear); typedef TestEnum *TestEnumPointer; +void xdr_TestEnumPointer_clear(TestEnumPointer *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumPointer, xdr_TestEnumPointer_clear); typedef TestEnum TestEnumFixedArray[13]; +void xdr_TestEnumFixedArray_clear(TestEnumFixedArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumFixedArray, xdr_TestEnumFixedArray_clear); typedef struct { u_int TestEnumVariableArray_len; TestEnum *TestEnumVariableArray_val; } TestEnumVariableArray; +void xdr_TestEnumVariableArray_clear(TestEnumVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestEnumVariableArray, xdr_TestEnumVariableArray_clear); typedef TestStruct TestStructScalar; +void xdr_TestStructScalar_clear(TestStructScalar *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructScalar, xdr_TestStructScalar_clear); typedef TestStruct *TestStructPointer; +void xdr_TestStructPointer_clear(TestStructPointer *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructPointer, xdr_TestStructPointer_clear); typedef TestStruct TestStructFixedArray[17]; +void xdr_TestStructFixedArray_clear(TestStructFixedArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructFixedArray, xdr_TestStructFixedArray_clear); typedef struct { u_int TestStructVariableArray_len; TestStruct *TestStructVariableArray_val; } TestStructVariableArray; +void xdr_TestStructVariableArray_clear(TestStructVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructVariableArray, xdr_TestStructVariableArray_clear); typedef TestUnion TestUnionScalar; +void xdr_TestUnionScalar_clear(TestUnionScalar *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionScalar, xdr_TestUnionScalar_clear); typedef TestUnion *TestUnionPointer; +void xdr_TestUnionPointer_clear(TestUnionPointer *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionPointer, xdr_TestUnionPointer_clear); typedef TestUnion TestUnionFixedArray[21]; +void xdr_TestUnionFixedArray_clear(TestUnionFixedArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionFixedArray, xdr_TestUnionFixedArray_clear); typedef struct { u_int TestUnionVariableArray_len; TestUnion *TestUnionVariableArray_val; } TestUnionVariableArray; +void xdr_TestUnionVariableArray_clear(TestUnionVariableArray *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestUnionVariableArray, xdr_TestUnionVariableArray_clear); #define TestConstDec 25 @@ -164,6 +210,8 @@ struct TestStructAllTypes { TestUnionVariableArray tuva; }; typedef struct TestStructAllTypes TestStructAllTypes; +void xdr_TestStructAllTypes_clear(TestStructAllTypes *objp); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TestStructAllTypes, xdr_TestStructAllTypes_clear); extern bool_t xdr_TestEnum(XDR *, TestEnum*); diff --git a/scripts/rpcgen/tests/test_demo.c b/scripts/rpcgen/tests/test_demo.c index e42374866e..d6be9e236d 100644 --- a/scripts/rpcgen/tests/test_demo.c +++ b/scripts/rpcgen/tests/test_demo.c @@ -73,13 +73,6 @@ static void test_xdr(xdrproc_t proc, void *vorig, void *vnew, const char *testna g_assert_cmpint(memcmp(buf, expected, actlen), ==, 0); xdr_destroy(&xdr); - /* Step 4: free mem from the new object only; the orig - * was on the stack so leave untouched */ - xdrmem_create(&xdr, buf, buflen, XDR_FREE); - - ret = !!proc(&xdr, vnew); - g_assert_cmpint(ret, ==, true); - cleanup: xdr_destroy(&xdr); } @@ -97,7 +90,7 @@ static void test_struct(void) TestStruct vorig = { .c1 = 'a', .c2 = 'b', }; - TestStruct vnew = {0}; + g_auto(TestStruct) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStruct, &vorig, &vnew, "struct", false); } @@ -107,7 +100,7 @@ static void test_union_case(void) TestUnion vorig = { .type = 20, .TestUnion_u = { .i1 = 1729 }, }; - TestUnion vnew = {0}; + g_auto(TestUnion) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_case", false); } @@ -117,7 +110,7 @@ static void test_union_default(void) TestUnion vorig = { .type = 87539319, .TestUnion_u = { .i3 = 1729 }, }; - TestUnion vnew = {0}; + g_auto(TestUnion) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnion, &vorig, &vnew, "union_default", false); } @@ -127,7 +120,7 @@ static void test_union_void_default_case(void) TestUnionVoidDefault vorig = { .type = 21, .TestUnionVoidDefault_u = { .i1 = 1729 }, }; - TestUnionVoidDefault vnew = {0}; + g_auto(TestUnionVoidDefault) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_case", false); } @@ -137,7 +130,7 @@ static void test_union_void_default_default(void) TestUnionVoidDefault vorig = { .type = 87539319 }; - TestUnionVoidDefault vnew = {0}; + g_auto(TestUnionVoidDefault) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionVoidDefault, &vorig, &vnew, "union_void_default_default", false); } @@ -147,7 +140,7 @@ static void test_union_no_default_case(void) TestUnionNoDefault vorig = { .type = 22, .TestUnionNoDefault_u = { .i1 = 1729 }, }; - TestUnionNoDefault vnew = {0}; + g_auto(TestUnionNoDefault) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_case", false); } @@ -157,7 +150,7 @@ static void test_union_no_default_default(void) TestUnionNoDefault vorig = { .type = 87539319, }; - TestUnionNoDefault vnew = {0}; + g_auto(TestUnionNoDefault) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionNoDefault, &vorig, &vnew, "union_no_default_default", true); } @@ -165,7 +158,7 @@ static void test_union_no_default_default(void) static void test_int_scalar(void) { TestIntScalar vorig = 1729; - TestIntScalar vnew = 0; + g_auto(TestIntScalar) vnew = 0; test_xdr((xdrproc_t)xdr_TestIntScalar, &vorig, &vnew, "int_scalar", false); } @@ -174,7 +167,7 @@ static void test_int_pointer_set(void) { int vorigp = 1729; TestIntPointer vorig = &vorigp; - TestIntPointer vnew = NULL; + g_auto(TestIntPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_set", false); } @@ -182,7 +175,7 @@ static void test_int_pointer_set(void) static void test_int_pointer_null(void) { TestIntPointer vorig = NULL; - TestIntPointer vnew = NULL; + g_auto(TestIntPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestIntPointer, &vorig, &vnew, "int_pointer_null", false); } @@ -190,7 +183,7 @@ static void test_int_pointer_null(void) static void test_int_fixed_array(void) { TestIntFixedArray vorig = { 1729, 0, 87539319 }; - TestIntFixedArray vnew = {0}; + g_auto(TestIntFixedArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestIntFixedArray, vorig, vnew, "int_fixed_array", false); @@ -202,7 +195,7 @@ static void test_int_variable_array_set(void) .TestIntVariableArray_len = 3, .TestIntVariableArray_val = (int[]) { 1729, 0, 87539319 } }; - TestIntVariableArray vnew = {0}; + g_auto(TestIntVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestIntVariableArray, &vorig, &vnew, "int_variable_array_set", false); @@ -214,7 +207,7 @@ static void test_int_variable_array_overflow(void) .TestIntVariableArray_len = 6, .TestIntVariableArray_val = (int[]) { 1729, 0, 87539319, 0, 1729 } }; - TestIntVariableArray vnew = {0}; + g_auto(TestIntVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestIntVariableArray, &vorig, &vnew, "int_variable_array_overflow", true); @@ -226,7 +219,7 @@ static void test_int_variable_array_empty(void) .TestIntVariableArray_len = 0, .TestIntVariableArray_val = (int[]) {0}, }; - TestIntVariableArray vnew = {0}; + g_auto(TestIntVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestIntVariableArray, &vorig, &vnew, "int_variable_array_empty", false); @@ -235,7 +228,7 @@ static void test_int_variable_array_empty(void) static void test_string_variable_array_set(void) { TestStringVariableArray vorig = (TestStringVariableArray) "taxis"; - TestStringVariableArray vnew = NULL; + g_auto(TestStringVariableArray) vnew = NULL; test_xdr((xdrproc_t)xdr_TestStringVariableArray, &vorig, &vnew, "string_variable_array_set", false); @@ -244,7 +237,7 @@ static void test_string_variable_array_set(void) static void test_string_variable_array_empty(void) { TestStringVariableArray vorig = (TestStringVariableArray)""; - TestStringVariableArray vnew = NULL; + g_auto(TestStringVariableArray) vnew = NULL; test_xdr((xdrproc_t)xdr_TestStringVariableArray, &vorig, &vnew, "string_variable_array_empty", false); @@ -253,7 +246,7 @@ static void test_string_variable_array_empty(void) static void test_opaque_fixed_array(void) { TestOpaqueFixedArray vorig = { 0xca, 0xfe, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 }; - TestOpaqueFixedArray vnew = {0}; + g_auto(TestOpaqueFixedArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestOpaqueFixedArray, vorig, vnew, "opaque_fixed_array", false); } @@ -264,7 +257,7 @@ static void test_opaque_variable_array_set(void) .TestOpaqueVariableArray_len = 3, .TestOpaqueVariableArray_val = (char[]) { 0xca, 0xfe, 0x12 }, }; - TestOpaqueVariableArray vnew = {0}; + g_auto(TestOpaqueVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, &vorig, &vnew, "opaque_variable_array_set", false); @@ -279,7 +272,7 @@ static void test_opaque_variable_array_overflow(void) 0xca, 0xfe, 0x12, 0xca, 0xfe, 0x12, }, }; - TestOpaqueVariableArray vnew = {0}; + g_auto(TestOpaqueVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, &vorig, &vnew, "opaque_variable_array_overflow", true); @@ -291,7 +284,7 @@ static void test_opaque_variable_array_empty(void) .TestOpaqueVariableArray_len = 0, .TestOpaqueVariableArray_val = (char[]) {0}, }; - TestOpaqueVariableArray vnew = {0}; + g_auto(TestOpaqueVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestOpaqueVariableArray, &vorig, &vnew, "opaque_variable_array_empty", false); @@ -300,7 +293,7 @@ static void test_opaque_variable_array_empty(void) static void test_enum_scalar(void) { TestEnumScalar vorig = TEST_ENUM_TWO; - TestEnumScalar vnew = 0; + g_auto(TestEnumScalar) vnew = 0; test_xdr((xdrproc_t)xdr_TestEnumScalar, &vorig, &vnew, "enum_scalar", false); @@ -310,7 +303,7 @@ static void test_enum_pointer_set(void) { TestEnum vorigp = TEST_ENUM_TWO; TestEnumPointer vorig = &vorigp; - TestEnumPointer vnew = NULL; + g_auto(TestEnumPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestEnumPointer, &vorig, &vnew, "enum_pointer_set", false); @@ -319,7 +312,7 @@ static void test_enum_pointer_set(void) static void test_enum_pointer_null(void) { TestEnumPointer vorig = NULL; - TestEnumPointer vnew = NULL; + g_auto(TestEnumPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestEnumPointer, &vorig, &vnew, "enum_pointer_null", false); @@ -332,7 +325,7 @@ static void test_enum_fixed_array(void) TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE }; - TestEnumFixedArray vnew = {0}; + g_auto(TestEnumFixedArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestEnumFixedArray, vorig, vnew, "enum_fixed_array", false); } @@ -345,7 +338,7 @@ static void test_enum_variable_array_set(void) TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, }, }; - TestEnumVariableArray vnew = {0}; + g_auto(TestEnumVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestEnumVariableArray, &vorig, &vnew, "enum_variable_array_set", false); @@ -362,7 +355,7 @@ static void test_enum_variable_array_overflow(void) TEST_ENUM_ONE, TEST_ENUM_TWO, TEST_ENUM_ONE, TEST_ENUM_TWO, } }; - TestEnumVariableArray vnew = {0}; + g_auto(TestEnumVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestEnumVariableArray, &vorig, &vnew, "enum_variable_array_overflow", true); @@ -374,7 +367,7 @@ static void test_enum_variable_array_empty(void) .TestEnumVariableArray_len = 0, .TestEnumVariableArray_val = (TestEnum[]) {0}, }; - TestEnumVariableArray vnew = {0}; + g_auto(TestEnumVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestEnumVariableArray, &vorig, &vnew, "enum_variable_array_empty", false); @@ -386,7 +379,7 @@ static void test_enum_variable_array_empty(void) static void test_struct_scalar(void) { TestStructScalar vorig = TEST_STRUCT_INIT; - TestStructScalar vnew = {0}; + g_auto(TestStructScalar) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructScalar, &vorig, &vnew, "struct_scalar", false); @@ -396,7 +389,7 @@ static void test_struct_pointer_set(void) { TestStruct vorigp = TEST_STRUCT_INIT; TestStructPointer vorig = &vorigp; - TestStructPointer vnew = NULL; + g_auto(TestStructPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestStructPointer, &vorig, &vnew, "struct_pointer_set", false); @@ -405,7 +398,7 @@ static void test_struct_pointer_set(void) static void test_struct_pointer_null(void) { TestStructPointer vorig = NULL; - TestStructPointer vnew = NULL; + g_auto(TestStructPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestStructPointer, &vorig, &vnew, "struct_pointer_null", false); @@ -420,7 +413,7 @@ static void test_struct_fixed_array(void) TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT_ALT }; - TestStructFixedArray vnew = {0}; + g_auto(TestStructFixedArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructFixedArray, vorig, vnew, "struct_fixed_array", false); } @@ -433,7 +426,7 @@ static void test_struct_variable_array_set(void) TEST_STRUCT_INIT, TEST_STRUCT_INIT_ALT, TEST_STRUCT_INIT_ALT, }, }; - TestStructVariableArray vnew = {0}; + g_auto(TestStructVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructVariableArray, &vorig, &vnew, "struct_variable_array_set", false); @@ -451,7 +444,7 @@ static void test_struct_variable_array_overflow(void) TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, TEST_STRUCT_INIT, } }; - TestStructVariableArray vnew = {0}; + g_auto(TestStructVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructVariableArray, &vorig, &vnew, "struct_variable_array_overflow", true); @@ -463,7 +456,7 @@ static void test_struct_variable_array_empty(void) .TestStructVariableArray_len = 0, .TestStructVariableArray_val = (TestStruct[]) {}, }; - TestStructVariableArray vnew = {0}; + g_auto(TestStructVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructVariableArray, &vorig, &vnew, "struct_variable_array_empty", false); @@ -475,7 +468,7 @@ static void test_struct_variable_array_empty(void) static void test_union_scalar(void) { TestUnionScalar vorig = TEST_UNION_INIT; - TestUnionScalar vnew = {0}; + g_auto(TestUnionScalar) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionScalar, &vorig, &vnew, "union_scalar", false); @@ -485,7 +478,7 @@ static void test_union_pointer_set(void) { TestUnion vorigp = TEST_UNION_INIT; TestUnionPointer vorig = &vorigp; - TestUnionPointer vnew = NULL; + g_auto(TestUnionPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestUnionPointer, &vorig, &vnew, "union_pointer_set", false); @@ -494,7 +487,7 @@ static void test_union_pointer_set(void) static void test_union_pointer_null(void) { TestUnionPointer vorig = NULL; - TestUnionPointer vnew = NULL; + g_auto(TestUnionPointer) vnew = NULL; test_xdr((xdrproc_t)xdr_TestUnionPointer, &vorig, &vnew, "union_pointer_null", false); @@ -509,7 +502,7 @@ static void test_union_fixed_array(void) TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT_ALT }; - TestUnionFixedArray vnew = {0}; + g_auto(TestUnionFixedArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionFixedArray, vorig, vnew, "union_fixed_array", false); } @@ -522,7 +515,7 @@ static void test_union_variable_array_set(void) TEST_UNION_INIT, TEST_UNION_INIT_ALT, TEST_UNION_INIT_ALT, }, }; - TestUnionVariableArray vnew = {0}; + g_auto(TestUnionVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionVariableArray, &vorig, &vnew, "union_variable_array_set", false); @@ -541,7 +534,7 @@ static void test_union_variable_array_overflow(void) TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, TEST_UNION_INIT, } }; - TestUnionVariableArray vnew = {0}; + g_auto(TestUnionVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionVariableArray, &vorig, &vnew, "union_variable_array_overflow", true); @@ -553,7 +546,7 @@ static void test_union_variable_array_empty(void) .TestUnionVariableArray_len = 0, .TestUnionVariableArray_val = (TestUnion[]) {}, }; - TestUnionVariableArray vnew = {0}; + g_auto(TestUnionVariableArray) vnew = {0}; test_xdr((xdrproc_t)xdr_TestUnionVariableArray, &vorig, &vnew, "union_variable_array_empty", false); @@ -721,7 +714,7 @@ static void test_struct_all_types(void) }, }, }; - TestStructAllTypes vnew = {0}; + g_auto(TestStructAllTypes) vnew = {0}; test_xdr((xdrproc_t)xdr_TestStructAllTypes, &vorig, &vnew, "test_struct_all_types", false); diff --git a/scripts/rpcgen/tests/test_generator.py b/scripts/rpcgen/tests/test_generator.py index bc7660a6fc..6660810f41 100644 --- a/scripts/rpcgen/tests/test_generator.py +++ b/scripts/rpcgen/tests/test_generator.py @@ -6,6 +6,7 @@ from pathlib import Path from rpcgen.parser import XDRParser from rpcgen.generator import ( XDRTypeDeclarationGenerator, + XDRTypeImplementationGenerator, XDRMarshallDeclarationGenerator, XDRMarshallImplementationGenerator, ) @@ -42,7 +43,11 @@ def test_generate_source(): parser = XDRParser(fp) spec = parser.parse() - got = XDRMarshallImplementationGenerator(spec).visit() + got = ( + XDRTypeImplementationGenerator(spec).visit() + + "\n" + + XDRMarshallImplementationGenerator(spec).visit() + ) with h.open("r") as fp: want = fp.read()