mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
Upb C++ generator editions support.
PiperOrigin-RevId: 642002041
This commit is contained in:
parent
1cb534f3c0
commit
ba8a9744f2
3 changed files with 52 additions and 6 deletions
|
|
@ -5,7 +5,11 @@
|
|||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#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<google::protobuf::io::ZeroCopyOutputStream> 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<google::protobuf::io::ZeroCopyOutputStream> 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<google::protobuf::io::ZeroCopyOutputStream> cc_output_stream(
|
||||
context->Open(CppSourceFilename(file)));
|
||||
Output cc_output(cc_output_stream.get());
|
||||
WriteSource(file, cc_output, fasttable_enabled);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
18
protos_generator/tests/basic_test_editions.proto
Normal file
18
protos_generator/tests/basic_test_editions.proto
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue