diff --git a/protos_generator/protoc-gen-upb-protos.cc b/protos_generator/protoc-gen-upb-protos.cc index af6bc52401..41c3487240 100644 --- a/protos_generator/protoc-gen-upb-protos.cc +++ b/protos_generator/protoc-gen-upb-protos.cc @@ -5,7 +5,11 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +#include #include +#include +#include +#include #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/compiler/code_generator.h" @@ -17,7 +21,6 @@ #include "protos_generator/gen_utils.h" #include "protos_generator/names.h" #include "protos_generator/output.h" -#include "upb_generator/file_layout.h" namespace protos_generator { namespace { @@ -25,6 +28,7 @@ namespace { namespace protoc = ::google::protobuf::compiler; namespace protobuf = ::google::protobuf; using FileDescriptor = ::google::protobuf::FileDescriptor; +using google::protobuf::Edition; void WriteSource(const protobuf::FileDescriptor* file, Output& output, bool fasttable_enabled); @@ -42,13 +46,16 @@ void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file, class Generator : public protoc::CodeGenerator { public: - ~Generator() override {} + ~Generator() override = default; bool Generate(const protobuf::FileDescriptor* file, const std::string& parameter, protoc::GeneratorContext* context, std::string* error) const override; uint64_t GetSupportedFeatures() const override { - return FEATURE_PROTO3_OPTIONAL; + return Feature::FEATURE_PROTO3_OPTIONAL | + Feature::FEATURE_SUPPORTS_EDITIONS; } + Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; } + Edition GetMaximumEdition() const override { return Edition::EDITION_2023; } }; bool Generator::Generate(const protobuf::FileDescriptor* file, @@ -71,14 +78,21 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, } // Write model.upb.fwd.h - Output forwarding_header_output( + std::unique_ptr output_stream( context->Open(ForwardingHeaderFilename(file))); + Output forwarding_header_output(output_stream.get()); WriteForwardingHeader(file, forwarding_header_output); + // Write model.upb.proto.h - Output header_output(context->Open(CppHeaderFilename(file))); + std::unique_ptr header_output_stream( + context->Open(CppHeaderFilename(file))); + Output header_output(header_output_stream.get()); WriteHeader(file, header_output); + // Write model.upb.proto.cc - Output cc_output(context->Open(CppSourceFilename(file))); + std::unique_ptr cc_output_stream( + context->Open(CppSourceFilename(file))); + Output cc_output(cc_output_stream.get()); WriteSource(file, cc_output, fasttable_enabled); return true; } diff --git a/protos_generator/tests/BUILD b/protos_generator/tests/BUILD index 98042ca09c..56af806d87 100644 --- a/protos_generator/tests/BUILD +++ b/protos_generator/tests/BUILD @@ -112,6 +112,20 @@ cc_proto_library( # visibility = ["//visibility:private"], # deps = [":legacy_name_proto"], # ) +# +# proto_library( +# name = "basic_editions_proto", +# srcs = [ +# "basic_test_editions.proto", +# ], +# deps = [":imported_proto"], +# ) +# +# upb_cc_proto_library( +# name = "basic_test_editions_proto", +# visibility = ["//visibility:private"], +# deps = [":basic_editions_proto"], +# ) # end:google_only cc_test( diff --git a/protos_generator/tests/basic_test_editions.proto b/protos_generator/tests/basic_test_editions.proto new file mode 100644 index 0000000000..f5dc515b6c --- /dev/null +++ b/protos_generator/tests/basic_test_editions.proto @@ -0,0 +1,18 @@ +edition = "2023"; + +package editions_upb_test; + +option features.enum_type = CLOSED; +option features.field_presence = IMPLICIT; + +message TestFeaturesMessage { + int32 implicit = 1; + int32 explicit = 2 [features.field_presence = EXPLICIT]; + int32 legacy_required = 3 [features.field_presence = LEGACY_REQUIRED]; + + repeated int32 packed = 50; + repeated int32 expanded = 51 [features.repeated_field_encoding = EXPANDED]; + + TestFeaturesMessage delimited = 100 [features.message_encoding = DELIMITED]; + TestFeaturesMessage length_prefixed = 101; +}