Internal changes

PiperOrigin-RevId: 658038844
pull/17666/head
Mike Kruskal 4 months ago committed by Copybara-Service
parent 5ac8ee1a20
commit 149407e24b
  1. 1
      cmake/upb_generators.cmake
  2. 35
      hpb_generator/protoc-gen-upb-protos.cc
  3. 3
      pkg/BUILD.bazel
  4. 14
      src/google/protobuf/compiler/csharp/csharp_reflection_class.cc
  5. 5
      src/google/protobuf/compiler/rust/generator.cc
  6. 4
      upb_generator/BUILD
  7. 7
      upb_generator/common.h
  8. 7
      upb_generator/mangle.h
  9. 17
      upb_generator/protoc-gen-upb.cc
  10. 2
      upb_generator/protoc-gen-upb_minitable-main.cc
  11. 18
      upb_generator/protoc-gen-upb_minitable.cc
  12. 1
      upb_generator/protoc-gen-upb_minitable.h

@ -17,6 +17,7 @@ foreach(generator upb upbdefs upb_minitable)
target_include_directories(protoc-gen-${generator} PRIVATE ${bootstrap_cmake_dir})
target_link_libraries(protoc-gen-${generator}
${protobuf_LIB_PROTOBUF}
${protobuf_LIB_PROTOC}
${protobuf_LIB_UPB}
${protobuf_ABSL_USED_TARGETS}
)

@ -31,8 +31,9 @@ using FileDescriptor = ::google::protobuf::FileDescriptor;
using google::protobuf::Edition;
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled);
void WriteHeader(const protobuf::FileDescriptor* file, Output& output);
bool fasttable_enabled, bool strip_feature_includes);
void WriteHeader(const protobuf::FileDescriptor* file, Output& output,
bool strip_feature_includes);
void WriteForwardingHeader(const protobuf::FileDescriptor* file,
Output& output);
void WriteMessageImplementations(const protobuf::FileDescriptor* file,
@ -42,7 +43,8 @@ void WriteTypedefForwardingHeader(
const std::vector<const protobuf::Descriptor*>& file_messages,
Output& output);
void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file,
Output& output);
Output& output,
bool strip_feature_includes);
class Generator : public protoc::CodeGenerator {
public:
@ -63,6 +65,7 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
protoc::GeneratorContext* context,
std::string* error) const {
bool fasttable_enabled = false;
bool strip_nonfunctional_codegen = false;
std::vector<std::pair<std::string, std::string>> params;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &params);
@ -70,7 +73,7 @@ bool Generator::Generate(const protobuf::FileDescriptor* file,
if (pair.first == "fasttable") {
fasttable_enabled = true;
} else if (pair.first == "experimental_strip_nonfunctional_codegen") {
continue;
strip_nonfunctional_codegen = true;
} else {
*error = "Unknown parameter: " + pair.first;
return false;
@ -87,13 +90,13 @@ bool Generator::Generate(const protobuf::FileDescriptor* 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);
WriteHeader(file, header_output, strip_nonfunctional_codegen);
// Write model.upb.proto.cc
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);
WriteSource(file, cc_output, fasttable_enabled, strip_nonfunctional_codegen);
return true;
}
@ -123,7 +126,8 @@ void WriteForwardingHeader(const protobuf::FileDescriptor* file,
output("#endif /* $0_UPB_FWD_H_ */\n", ToPreproc(file->name()));
}
void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
void WriteHeader(const protobuf::FileDescriptor* file, Output& output,
bool strip_feature_includes) {
EmitFileWarning(file, output);
output(
R"cc(
@ -161,7 +165,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
output("\n");
}
WriteHeaderMessageForwardDecls(file, output);
WriteHeaderMessageForwardDecls(file, output, strip_feature_includes);
WriteStartNamespace(file, output);
std::vector<const protobuf::EnumDescriptor*> this_file_enums =
@ -190,7 +194,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
// Writes a .upb.cc source file.
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled) {
bool fasttable_enabled, bool strip_feature_includes) {
EmitFileWarning(file, output);
output(
@ -203,6 +207,11 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output,
CppHeaderFilename(file));
for (int i = 0; i < file->dependency_count(); i++) {
if (strip_feature_includes &&
compiler::IsKnownFeatureProto(file->dependency(i)->name())) {
// Strip feature imports for editions codegen tests.
continue;
}
output("#include \"$0\"\n", CppHeaderFilename(file->dependency(i)));
}
output("#include \"upb/port/def.inc\"\n");
@ -253,12 +262,18 @@ void WriteTypedefForwardingHeader(
/// Writes includes for upb C minitables and fwd.h for transitive typedefs.
void WriteHeaderMessageForwardDecls(const protobuf::FileDescriptor* file,
Output& output) {
Output& output,
bool strip_feature_includes) {
// Import forward-declaration of types defined in this file.
output("#include \"$0\"\n", UpbCFilename(file));
output("#include \"$0\"\n", ForwardingHeaderFilename(file));
// Import forward-declaration of types in dependencies.
for (int i = 0; i < file->dependency_count(); ++i) {
if (strip_feature_includes &&
compiler::IsKnownFeatureProto(file->dependency(i)->name())) {
// Strip feature imports for editions codegen tests.
continue;
}
output("#include \"$0\"\n", ForwardingHeaderFilename(file->dependency(i)));
}
output("\n");

@ -225,6 +225,7 @@ cc_dist_library(
name = "protoc-gen-upb",
dist_deps = [
":protobuf",
":protoc",
":upb",
],
tags = ["manual"],
@ -237,6 +238,7 @@ cc_dist_library(
name = "protoc-gen-upbdefs",
dist_deps = [
":protobuf",
":protoc",
":upb",
],
tags = ["manual"],
@ -249,6 +251,7 @@ cc_dist_library(
name = "protoc-gen-upb_minitable",
dist_deps = [
":protobuf",
":protoc",
":upb",
],
tags = ["manual"],

@ -9,8 +9,8 @@
#include <sstream>
#include "google/protobuf/compiler/code_generator.h"
#include "absl/strings/str_join.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/csharp/csharp_enum.h"
#include "google/protobuf/compiler/csharp/csharp_field_base.h"
#include "google/protobuf/compiler/csharp/csharp_helpers.h"
@ -170,10 +170,14 @@ void ReflectionClassGenerator::WriteDescriptor(io::Printer* printer) {
"descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,\n");
printer->Print(" new pbr::FileDescriptor[] { ");
for (int i = 0; i < file_->dependency_count(); i++) {
printer->Print(
"$full_reflection_class_name$.Descriptor, ",
"full_reflection_class_name",
GetReflectionClassName(file_->dependency(i)));
if (options()->strip_nonfunctional_codegen &&
IsKnownFeatureProto(file_->dependency(i)->name())) {
// Strip feature imports for editions codegen tests.
continue;
}
printer->Print("$full_reflection_class_name$.Descriptor, ",
"full_reflection_class_name",
GetReflectionClassName(file_->dependency(i)));
}
printer->Print("},\n"
" new pbr::GeneratedClrTypeInfo(");

@ -215,6 +215,11 @@ bool RustGenerator::Generate(const FileDescriptor* file,
{"proto_deps_h",
[&] {
for (int i = 0; i < file->dependency_count(); i++) {
if (opts->strip_nonfunctional_codegen &&
IsKnownFeatureProto(file->dependency(i)->name())) {
// Strip feature imports for editions codegen tests.
continue;
}
thunks_printer->Emit(
{{"proto_dep_h", GetHeaderFile(ctx, *file->dependency(i))}},
R"cc(

@ -103,6 +103,7 @@ bootstrap_cc_library(
visibility = ["//upb:friend_generators"],
deps = [
":mangle",
"//src/google/protobuf:port",
"//upb:mini_table",
"//upb:port",
"@com_google_absl//absl/strings",
@ -198,6 +199,7 @@ cc_library(
"//upb:friends",
],
deps = [
"//src/google/protobuf:port",
"@com_google_absl//absl/strings",
],
)
@ -273,6 +275,7 @@ bootstrap_cc_library(
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//pkg:__pkg__"],
deps = [
"//src/google/protobuf/compiler:code_generator",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
@ -313,6 +316,7 @@ bootstrap_cc_library(
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//upb:friends"],
deps = [
"//src/google/protobuf/compiler:code_generator",
"//upb:base",
"//upb:mem",
"//upb:mini_table",

@ -15,6 +15,9 @@
#include "absl/strings/substitute.h"
#include "upb/reflection/def.hpp"
// Must be last.
#include "google/protobuf/port_def.inc"
namespace upb {
namespace generator {
@ -59,7 +62,7 @@ std::string StripExtension(absl::string_view fname);
std::string ToCIdent(absl::string_view str);
std::string ToPreproc(absl::string_view str);
void EmitFileWarning(absl::string_view name, Output& output);
std::string MessageInit(absl::string_view full_name);
PROTOC_EXPORT std::string MessageInit(absl::string_view full_name);
std::string MessageInitName(upb::MessageDefPtr descriptor);
std::string MessageName(upb::MessageDefPtr descriptor);
std::string FileLayoutName(upb::FileDefPtr file);
@ -81,4 +84,6 @@ std::string GetFieldRep(const upb_MiniTableField* field32,
} // namespace generator
} // namespace upb
#include "google/protobuf/port_undef.inc"
#endif // UPB_GENERATOR_COMMON_H

@ -5,12 +5,17 @@
#include "absl/strings/string_view.h"
// Must be last.
#include "google/protobuf/port_def.inc"
namespace upb {
namespace generator {
std::string MessageInit(absl::string_view full_name);
PROTOC_EXPORT std::string MessageInit(absl::string_view full_name);
} // namespace generator
} // namespace upb
#include "google/protobuf/port_undef.inc"
#endif // THIRD_PARTY_UPB_UPB_GENERATOR_MANGLE_H_

@ -25,6 +25,7 @@
#include "absl/strings/str_replace.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/code_generator.h"
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.hpp"
#include "upb/base/string_view.h"
@ -45,6 +46,7 @@ namespace {
struct Options {
bool bootstrap = false;
bool strip_nonfunctional_codegen = false;
};
std::string SourceFilename(upb::FileDefPtr file) {
@ -894,11 +896,14 @@ void WriteHeader(const DefPoolPair& pools, upb::FileDefPtr file,
if (!options.bootstrap) {
output("#include \"$0\"\n\n", MiniTableHeaderFilename(file));
for (int i = 0; i < file.dependency_count(); i++) {
if (options.strip_nonfunctional_codegen &&
google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) {
// Strip feature imports for editions codegen tests.
continue;
}
output("#include \"$0\"\n", MiniTableHeaderFilename(file.dependency(i)));
}
if (file.dependency_count() > 0) {
output("\n");
}
output("\n");
}
output(
@ -1107,6 +1112,10 @@ void WriteMiniDescriptorSource(const DefPoolPair& pools, upb::FileDefPtr file,
CApiHeaderFilename(file));
for (int i = 0; i < file.dependency_count(); i++) {
if (options.strip_nonfunctional_codegen &&
google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) {
continue;
}
output("#include \"$0\"\n", CApiHeaderFilename(file.dependency(i)));
}
@ -1155,7 +1164,7 @@ bool ParseOptions(Plugin* plugin, Options* options) {
if (pair.first == "bootstrap_upb") {
options->bootstrap = true;
} else if (pair.first == "experimental_strip_nonfunctional_codegen") {
continue;
options->strip_nonfunctional_codegen = true;
} else {
plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
return false;

@ -50,7 +50,7 @@ void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
bool ParseOptions(MiniTableOptions* options, Plugin* plugin) {
for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) {
if (pair.first == "experimental_strip_nonfunctional_codegen") {
continue;
options->strip_nonfunctional_codegen = true;
} else if (pair.first == "one_output_per_message") {
options->one_output_per_message = true;
} else {

@ -24,6 +24,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/compiler/code_generator.h"
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
@ -550,7 +551,9 @@ void WriteMiniTableHeader(const DefPoolPair& pools, upb::FileDefPtr file,
ToPreproc(file.name()));
}
void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) {
void WriteMiniTableSourceIncludes(upb::FileDefPtr file,
const MiniTableOptions& options,
Output& output) {
EmitFileWarning(file.name(), output);
output(
@ -560,6 +563,11 @@ void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) {
MiniTableHeaderFilename(file));
for (int i = 0; i < file.dependency_count(); i++) {
if (options.strip_nonfunctional_codegen &&
google::protobuf::compiler::IsKnownFeatureProto(file.dependency(i).name())) {
// Strip feature imports for editions codegen tests.
continue;
}
output("#include \"$0\"\n", MiniTableHeaderFilename(file.dependency(i)));
}
@ -576,7 +584,7 @@ void WriteMiniTableSourceIncludes(upb::FileDefPtr file, Output& output) {
void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file,
const MiniTableOptions& options, Output& output) {
WriteMiniTableSourceIncludes(file, output);
WriteMiniTableSourceIncludes(file, options, output);
std::vector<upb::MessageDefPtr> messages = SortedMessages(file);
std::vector<upb::FieldDefPtr> extensions = SortedExtensions(file);
@ -673,21 +681,21 @@ void WriteMiniTableMultipleSources(const DefPoolPair& pools,
for (auto message : messages) {
Output output;
WriteMiniTableSourceIncludes(file, output);
WriteMiniTableSourceIncludes(file, options, output);
WriteMessage(message, pools, options, output);
plugin->AddOutputFile(MultipleSourceFilename(file, message.full_name(), &i),
output.output());
}
for (const auto e : enums) {
Output output;
WriteMiniTableSourceIncludes(file, output);
WriteMiniTableSourceIncludes(file, options, output);
WriteEnum(e, output);
plugin->AddOutputFile(MultipleSourceFilename(file, e.full_name(), &i),
output.output());
}
for (const auto ext : extensions) {
Output output;
WriteMiniTableSourceIncludes(file, output);
WriteMiniTableSourceIncludes(file, options, output);
WriteExtension(pools, ext, output);
plugin->AddOutputFile(MultipleSourceFilename(file, ext.full_name(), &i),
output.output());

@ -26,6 +26,7 @@ namespace generator {
struct MiniTableOptions {
bool one_output_per_message = false;
bool strip_nonfunctional_codegen = false;
};
void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file,

Loading…
Cancel
Save