From b409f8cd859da48c74f9435883d65ffe22ecc884 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 20 Feb 2020 18:53:47 -0800 Subject: [PATCH] Fixed code generator for upbdefs when a file has no messages. --- BUILD | 12 ++++++++++++ tests/empty.proto | 2 ++ upbc/generator.cc | 18 ++++++++++++------ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 tests/empty.proto diff --git a/BUILD b/BUILD index c85ece940b..61601a067e 100644 --- a/BUILD +++ b/BUILD @@ -389,11 +389,23 @@ cc_test( srcs = ["tests/test_generated_code.c"], deps = [ ":test_messages_proto3_proto_upb", + ":empty_upbdefs_proto", ":test_upbproto", ":upb_test", ], ) +proto_library( + name = "empty_proto", + srcs = ["tests/empty.proto"], +) + +upb_proto_reflection_library( + name = "empty_upbdefs_proto", + testonly = 1, + deps = [":empty_proto"], +) + upb_proto_library( name = "test_messages_proto3_proto_upb", testonly = 1, diff --git a/tests/empty.proto b/tests/empty.proto new file mode 100644 index 0000000000..fb45796635 --- /dev/null +++ b/tests/empty.proto @@ -0,0 +1,2 @@ +syntax = "proto2"; + diff --git a/upbc/generator.cc b/upbc/generator.cc index 50fb42d564..47cc07ad10 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -853,12 +853,14 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { } output("\n"); - output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size()); - for (auto message : file_messages) { - output(" &$0,\n", MessageInit(message)); + if (!file_messages.empty()) { + output("static const upb_msglayout *layouts[$0] = {\n", file_messages.size()); + for (auto message : file_messages) { + output(" &$0,\n", MessageInit(message)); + } + output("};\n"); + output("\n"); } - output("};\n"); - output("\n"); protobuf::FileDescriptorProto file_proto; file->CopyTo(&file_proto); @@ -905,7 +907,11 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { output("upb_def_init $0 = {\n", DefInitSymbol(file)); output(" deps,\n"); - output(" layouts,\n"); + if (file_messages.empty()) { + output(" NULL,\n"); + } else { + output(" layouts,\n"); + } output(" \"$0\",\n", file->name()); output(" UPB_STRVIEW_INIT(descriptor, $0)\n", file_data.size()); output("};\n");