From c0004a8fb137da3f4775067e0667c3e0b4639e6d Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Thu, 12 Oct 2023 14:00:45 -0700 Subject: [PATCH] Internal changes PiperOrigin-RevId: 573005248 --- src/google/protobuf/compiler/code_generator.cc | 8 ++++++++ src/google/protobuf/compiler/code_generator.h | 3 +++ src/google/protobuf/compiler/cpp/file.cc | 3 ++- src/google/protobuf/editions/BUILD | 2 -- .../editions/golden/editions_transform_proto2.proto | 13 +++++++++++++ .../editions/golden/editions_transform_proto3.proto | 5 +++++ .../editions/proto/editions_transform_proto2.proto | 7 +++++++ .../editions/proto/editions_transform_proto3.proto | 5 +++++ 8 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/compiler/code_generator.cc b/src/google/protobuf/compiler/code_generator.cc index 2c229dace7..863192d735 100644 --- a/src/google/protobuf/compiler/code_generator.cc +++ b/src/google/protobuf/compiler/code_generator.cc @@ -120,6 +120,14 @@ std::string StripProto(absl::string_view filename) { } } +bool IsKnownFeatureProto(absl::string_view filename) { + if (filename == "google/protobuf/cpp_features.proto" || + filename == "google/protobuf/java_features.proto") { + return true; + } + return false; +} + } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/code_generator.h b/src/google/protobuf/compiler/code_generator.h index 35f105b261..b8bc3792d0 100644 --- a/src/google/protobuf/compiler/code_generator.h +++ b/src/google/protobuf/compiler/code_generator.h @@ -230,6 +230,9 @@ PROTOC_EXPORT void ParseGeneratorParameter( // Strips ".proto" or ".protodevel" from the end of a filename. PROTOC_EXPORT std::string StripProto(absl::string_view filename); +// Returns true if the proto path corresponds to a known feature file. +PROTOC_EXPORT bool IsKnownFeatureProto(absl::string_view filename); + } // namespace compiler } // namespace protobuf } // namespace google diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index da1c6c768e..ec53d9d0ec 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -29,6 +29,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/strings/strip.h" +#include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/compiler/cpp/enum.h" #include "google/protobuf/compiler/cpp/extension.h" #include "google/protobuf/compiler/cpp/helpers.h" @@ -247,7 +248,7 @@ bool FileGenerator::ShouldSkipDependencyImports( // Skip feature imports, which are a visible (but non-functional) deviation // between editions and legacy syntax. if (options_.strip_nonfunctional_codegen && - dep->name() == "third_party/protobuf/cpp_features.proto") { + IsKnownFeatureProto(dep->name())) { return true; } diff --git a/src/google/protobuf/editions/BUILD b/src/google/protobuf/editions/BUILD index cd38e08217..04e1967ac1 100644 --- a/src/google/protobuf/editions/BUILD +++ b/src/google/protobuf/editions/BUILD @@ -5,7 +5,6 @@ proto_library( testonly = True, srcs = ["golden/test_messages_proto2.proto"], strip_import_prefix = "/src", - deps = ["//src/google/protobuf:cpp_features_proto"], ) cc_proto_library( @@ -26,7 +25,6 @@ proto_library( "//:struct_proto", "//:timestamp_proto", "//:wrappers_proto", - "//src/google/protobuf:cpp_features_proto", ], ) diff --git a/src/google/protobuf/editions/golden/editions_transform_proto2.proto b/src/google/protobuf/editions/golden/editions_transform_proto2.proto index 027f6c7132..3e7f5a533d 100644 --- a/src/google/protobuf/editions/golden/editions_transform_proto2.proto +++ b/src/google/protobuf/editions/golden/editions_transform_proto2.proto @@ -14,6 +14,10 @@ edition = "2023"; package protobuf_editions_test; +import "third_party/java_src/protobuf/current/java/com/google/protobuf/java_features.proto"; +import "google/protobuf/cpp_features.proto"; +import "google/protobuf/editions/proto/editions_transform_proto3.proto"; + option features.enum_type = CLOSED; option features.repeated_field_encoding = EXPANDED; option features.utf8_validation = UNVERIFIED; @@ -113,3 +117,12 @@ enum TestEnum { BAZ = 3; NEG = -1; // Intentionally negative. } + +message TestOpenEnumMessage { + TestEnumProto3 open_enum_field = 1 [ + features.(pb.cpp).legacy_closed_enum = true, + features.(pb.java).legacy_closed_enum = true + ]; + + TestEnum closed_enum_field = 2; +} diff --git a/src/google/protobuf/editions/golden/editions_transform_proto3.proto b/src/google/protobuf/editions/golden/editions_transform_proto3.proto index 82826f6dc3..80123f4e50 100644 --- a/src/google/protobuf/editions/golden/editions_transform_proto3.proto +++ b/src/google/protobuf/editions/golden/editions_transform_proto3.proto @@ -11,6 +11,11 @@ package protobuf_editions_test; option features.field_presence = IMPLICIT; +enum TestEnumProto3 { + TEST_ENUM_PROTO3_UNKNOWN = 0; + TEST_ENUM_PROTO3_VALUE = 1; +} + message TestMessageProto3 { string string_field = 1; map string_map_field = 4; diff --git a/src/google/protobuf/editions/proto/editions_transform_proto2.proto b/src/google/protobuf/editions/proto/editions_transform_proto2.proto index a10fdebf41..e502cde57a 100644 --- a/src/google/protobuf/editions/proto/editions_transform_proto2.proto +++ b/src/google/protobuf/editions/proto/editions_transform_proto2.proto @@ -7,6 +7,8 @@ syntax = "proto2"; +import "google/protobuf/editions/proto/editions_transform_proto3.proto"; + // This file contains various edge cases we've collected from migrating real // protos in order to lock down the transformations. @@ -82,3 +84,8 @@ enum TestEnum { BAZ = 3; NEG = -1; // Intentionally negative. } + +message TestOpenEnumMessage { + optional TestEnumProto3 open_enum_field = 1; + optional TestEnum closed_enum_field = 2; +} \ No newline at end of file diff --git a/src/google/protobuf/editions/proto/editions_transform_proto3.proto b/src/google/protobuf/editions/proto/editions_transform_proto3.proto index 0c9ec4f00d..6dc8b4f5a5 100644 --- a/src/google/protobuf/editions/proto/editions_transform_proto3.proto +++ b/src/google/protobuf/editions/proto/editions_transform_proto3.proto @@ -9,6 +9,11 @@ syntax = "proto3"; package protobuf_editions_test; +enum TestEnumProto3 { + TEST_ENUM_PROTO3_UNKNOWN = 0; + TEST_ENUM_PROTO3_VALUE = 1; +} + message TestMessageProto3 { string string_field = 1;