Internal changes PiperOrigin-RevId: 658038844
diff --git a/cmake/upb_generators.cmake b/cmake/upb_generators.cmake index b2e66b1..c120e45 100644 --- a/cmake/upb_generators.cmake +++ b/cmake/upb_generators.cmake
@@ -17,6 +17,7 @@ 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} )
diff --git a/hpb_generator/protoc-gen-upb-protos.cc b/hpb_generator/protoc-gen-upb-protos.cc index d946fb2..b431e0d 100644 --- a/hpb_generator/protoc-gen-upb-protos.cc +++ b/hpb_generator/protoc-gen-upb-protos.cc
@@ -31,8 +31,9 @@ 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 @@ 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 @@ 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, ¶ms); @@ -70,7 +73,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ // 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 @@ 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 @@ /// 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");
diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 34d65ce..c7c3f9d 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel
@@ -225,6 +225,7 @@ name = "protoc-gen-upb", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"], @@ -237,6 +238,7 @@ name = "protoc-gen-upbdefs", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"], @@ -249,6 +251,7 @@ name = "protoc-gen-upb_minitable", dist_deps = [ ":protobuf", + ":protoc", ":upb", ], tags = ["manual"],
diff --git a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc index 6aefb50..1e8d3f2 100644 --- a/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc +++ b/src/google/protobuf/compiler/csharp/csharp_reflection_class.cc
@@ -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 @@ "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(");
diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index 52a120f..0f44c88 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc
@@ -215,6 +215,11 @@ {"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(
diff --git a/upb_generator/BUILD b/upb_generator/BUILD index c9ce196..43be436 100644 --- a/upb_generator/BUILD +++ b/upb_generator/BUILD
@@ -103,6 +103,7 @@ visibility = ["//upb:friend_generators"], deps = [ ":mangle", + "//src/google/protobuf:port", "//upb:mini_table", "//upb:port", "@com_google_absl//absl/strings", @@ -198,6 +199,7 @@ "//upb:friends", ], deps = [ + "//src/google/protobuf:port", "@com_google_absl//absl/strings", ], ) @@ -273,6 +275,7 @@ 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 @@ copts = UPB_DEFAULT_CPPOPTS, visibility = ["//upb:friends"], deps = [ + "//src/google/protobuf/compiler:code_generator", "//upb:base", "//upb:mem", "//upb:mini_table",
diff --git a/upb_generator/common.h b/upb_generator/common.h index ce92f4f..2afa005 100644 --- a/upb_generator/common.h +++ b/upb_generator/common.h
@@ -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 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 @@ } // namespace generator } // namespace upb +#include "google/protobuf/port_undef.inc" + #endif // UPB_GENERATOR_COMMON_H
diff --git a/upb_generator/mangle.h b/upb_generator/mangle.h index de8c804..04b7c98 100644 --- a/upb_generator/mangle.h +++ b/upb_generator/mangle.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_
diff --git a/upb_generator/protoc-gen-upb.cc b/upb_generator/protoc-gen-upb.cc index f1ef7e0..10b51f0 100644 --- a/upb_generator/protoc-gen-upb.cc +++ b/upb_generator/protoc-gen-upb.cc
@@ -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 @@ struct Options { bool bootstrap = false; + bool strip_nonfunctional_codegen = false; }; std::string SourceFilename(upb::FileDefPtr file) { @@ -894,11 +896,14 @@ 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 @@ 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 @@ 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;
diff --git a/upb_generator/protoc-gen-upb_minitable-main.cc b/upb_generator/protoc-gen-upb_minitable-main.cc index e58d3ed..2073f07 100644 --- a/upb_generator/protoc-gen-upb_minitable-main.cc +++ b/upb_generator/protoc-gen-upb_minitable-main.cc
@@ -50,7 +50,7 @@ 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 {
diff --git a/upb_generator/protoc-gen-upb_minitable.cc b/upb_generator/protoc-gen-upb_minitable.cc index c5fffae..333ab2e 100644 --- a/upb_generator/protoc-gen-upb_minitable.cc +++ b/upb_generator/protoc-gen-upb_minitable.cc
@@ -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 @@ 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 @@ 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 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 @@ 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());
diff --git a/upb_generator/protoc-gen-upb_minitable.h b/upb_generator/protoc-gen-upb_minitable.h index cbc3e8d..5023771 100644 --- a/upb_generator/protoc-gen-upb_minitable.h +++ b/upb_generator/protoc-gen-upb_minitable.h
@@ -26,6 +26,7 @@ struct MiniTableOptions { bool one_output_per_message = false; + bool strip_nonfunctional_codegen = false; }; void WriteMiniTableSource(const DefPoolPair& pools, upb::FileDefPtr file,