Skip to content

Commit d56cb7d

Browse files
mkruskal-googlezhangskz
authored andcommitted
Split bootstrapped java_features.proto to keep it from leaking out.
This can cause ODR violations in downstream users who link against both the bootstrapped proto and transitive C++ gencode of java_features.proto. Once protoc is split up, we can turn the bootstrapped proto into a real cc_proto_library target and avoid this problem altogether. PiperOrigin-RevId: 630099889
1 parent bd81fd9 commit d56cb7d

File tree

15 files changed

+213
-119
lines changed

15 files changed

+213
-119
lines changed

src/google/protobuf/compiler/java/BUILD.bazel

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ cc_library(
3535
hdrs = [
3636
"context.h",
3737
"doc_comment.h",
38-
"generator.h",
3938
"helpers.h",
4039
"name_resolver.h",
4140
"names.h",
@@ -48,7 +47,6 @@ cc_library(
4847
"//src/google/protobuf/compiler/java:__subpackages__",
4948
],
5049
deps = [
51-
":java_features_bootstrap",
5250
"//src/google/protobuf",
5351
"//src/google/protobuf:port",
5452
"//src/google/protobuf/compiler:code_generator",
@@ -64,6 +62,25 @@ cc_library(
6462
],
6563
)
6664

65+
cc_library(
66+
name = "internal_helpers",
67+
srcs = ["internal_helpers.cc"],
68+
hdrs = [
69+
"generator.h",
70+
"internal_helpers.h",
71+
],
72+
strip_include_prefix = "/src",
73+
visibility = ["//src/google/protobuf/compiler/java:__subpackages__"],
74+
deps = [
75+
":helpers",
76+
":java_features_bootstrap",
77+
"//src/google/protobuf",
78+
"//src/google/protobuf:port",
79+
"//src/google/protobuf/compiler:code_generator",
80+
"@com_google_absl//absl/log:absl_log",
81+
],
82+
)
83+
6784
cc_library(
6885
name = "java_features_bootstrap",
6986
srcs = ["java_features.pb.cc"],

src/google/protobuf/compiler/java/helpers.cc

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -878,86 +878,6 @@ void WriteUInt32ToUtf16CharSequence(uint32_t number,
878878
output->push_back(static_cast<uint16_t>(number));
879879
}
880880

881-
int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) {
882-
// j/c/g/protobuf/FieldType.java lists field types in a slightly different
883-
// order from FieldDescriptor::Type so we can't do a simple cast.
884-
//
885-
// TODO: Make j/c/g/protobuf/FieldType.java follow the same order.
886-
int result = field->type();
887-
if (result == FieldDescriptor::TYPE_GROUP) {
888-
return 17;
889-
} else if (result < FieldDescriptor::TYPE_GROUP) {
890-
return result - 1;
891-
} else {
892-
return result - 2;
893-
}
894-
}
895-
896-
int GetExperimentalJavaFieldTypeForRepeated(const FieldDescriptor* field) {
897-
if (field->type() == FieldDescriptor::TYPE_GROUP) {
898-
return 49;
899-
} else {
900-
return GetExperimentalJavaFieldTypeForSingular(field) + 18;
901-
}
902-
}
903-
904-
int GetExperimentalJavaFieldTypeForPacked(const FieldDescriptor* field) {
905-
int result = field->type();
906-
if (result < FieldDescriptor::TYPE_STRING) {
907-
return result + 34;
908-
} else if (result > FieldDescriptor::TYPE_BYTES) {
909-
return result + 30;
910-
} else {
911-
ABSL_LOG(FATAL) << field->full_name() << " can't be packed.";
912-
return 0;
913-
}
914-
}
915-
916-
int GetExperimentalJavaFieldType(const FieldDescriptor* field) {
917-
static const int kMapFieldType = 50;
918-
static const int kOneofFieldTypeOffset = 51;
919-
920-
static const int kRequiredBit = 0x100;
921-
static const int kUtf8CheckBit = 0x200;
922-
static const int kCheckInitialized = 0x400;
923-
static const int kLegacyEnumIsClosedBit = 0x800;
924-
static const int kHasHasBit = 0x1000;
925-
int extra_bits = field->is_required() ? kRequiredBit : 0;
926-
if (field->type() == FieldDescriptor::TYPE_STRING && CheckUtf8(field)) {
927-
extra_bits |= kUtf8CheckBit;
928-
}
929-
if (field->is_required() || (GetJavaType(field) == JAVATYPE_MESSAGE &&
930-
HasRequiredFields(field->message_type()))) {
931-
extra_bits |= kCheckInitialized;
932-
}
933-
if (HasHasbit(field)) {
934-
extra_bits |= kHasHasBit;
935-
}
936-
if (GetJavaType(field) == JAVATYPE_ENUM && !SupportUnknownEnumValue(field)) {
937-
extra_bits |= kLegacyEnumIsClosedBit;
938-
}
939-
940-
if (field->is_map()) {
941-
if (!SupportUnknownEnumValue(MapValueField(field))) {
942-
const FieldDescriptor* value = field->message_type()->map_value();
943-
if (GetJavaType(value) == JAVATYPE_ENUM) {
944-
extra_bits |= kLegacyEnumIsClosedBit;
945-
}
946-
}
947-
return kMapFieldType | extra_bits;
948-
} else if (field->is_packed()) {
949-
return GetExperimentalJavaFieldTypeForPacked(field) | extra_bits;
950-
} else if (field->is_repeated()) {
951-
return GetExperimentalJavaFieldTypeForRepeated(field) | extra_bits;
952-
} else if (IsRealOneof(field)) {
953-
return (GetExperimentalJavaFieldTypeForSingular(field) +
954-
kOneofFieldTypeOffset) |
955-
extra_bits;
956-
} else {
957-
return GetExperimentalJavaFieldTypeForSingular(field) | extra_bits;
958-
}
959-
}
960-
961881
// Escape a UTF-16 character to be embedded in a Java string.
962882
void EscapeUtf16ToString(uint16_t code, std::string* output) {
963883
if (code == '\t') {

src/google/protobuf/compiler/java/helpers.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <string>
1717

1818
#include "absl/strings/string_view.h"
19-
#include "google/protobuf/compiler/java/generator.h"
20-
#include "google/protobuf/compiler/java/java_features.pb.h"
2119
#include "google/protobuf/compiler/java/names.h"
2220
#include "google/protobuf/compiler/java/options.h"
2321
#include "google/protobuf/descriptor.h"
@@ -344,18 +342,6 @@ inline bool HasHasbit(const FieldDescriptor* descriptor) {
344342
return internal::cpp::HasHasbit(descriptor);
345343
}
346344

347-
// Whether unknown enum values are kept (i.e., not stored in UnknownFieldSet
348-
// but in the message and can be queried using additional getters that return
349-
// ints.
350-
inline bool SupportUnknownEnumValue(const FieldDescriptor* field) {
351-
if (JavaGenerator::GetResolvedSourceFeatures(*field)
352-
.GetExtension(pb::java)
353-
.legacy_closed_enum()) {
354-
return false;
355-
}
356-
return field->enum_type() != nullptr && !field->enum_type()->is_closed();
357-
}
358-
359345
// Check whether a message has repeated fields.
360346
bool HasRepeatedFields(const Descriptor* descriptor);
361347

@@ -375,18 +361,6 @@ inline bool IsWrappersProtoFile(const FileDescriptor* descriptor) {
375361
return descriptor->name() == "google/protobuf/wrappers.proto";
376362
}
377363

378-
inline bool CheckUtf8(const FieldDescriptor* descriptor) {
379-
if (JavaGenerator::GetResolvedSourceFeatures(*descriptor)
380-
.GetExtension(pb::java)
381-
.utf8_validation() == pb::JavaFeatures::VERIFY) {
382-
return true;
383-
}
384-
return JavaGenerator::GetResolvedSourceFeatures(*descriptor)
385-
.utf8_validation() == FeatureSet::VERIFY ||
386-
// For legacy syntax. This is not allowed under Editions.
387-
descriptor->file()->options().java_string_check_utf8();
388-
}
389-
390364
void WriteUInt32ToUtf16CharSequence(uint32_t number,
391365
std::vector<uint16_t>* output);
392366

@@ -398,16 +372,6 @@ inline void WriteIntToUtf16CharSequence(int value,
398372
// Escape a UTF-16 character so it can be embedded in a Java string literal.
399373
void EscapeUtf16ToString(uint16_t code, std::string* output);
400374

401-
// Only the lowest two bytes of the return value are used. The lowest byte
402-
// is the integer value of a j/c/g/protobuf/FieldType enum. For the other
403-
// byte:
404-
// bit 0: whether the field is required.
405-
// bit 1: whether the field requires UTF-8 validation.
406-
// bit 2: whether the field needs isInitialized check.
407-
// bit 3: whether the field is a map field with proto2 enum value.
408-
// bits 4-7: unused
409-
int GetExperimentalJavaFieldType(const FieldDescriptor* field);
410-
411375
// To get the total number of entries need to be built for experimental runtime
412376
// and the first field number that are not in the table part
413377
std::pair<int, int> GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber(

src/google/protobuf/compiler/java/immutable/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ cc_library(
3535
"//src/google/protobuf:port",
3636
"//src/google/protobuf/compiler/java:generator_common",
3737
"//src/google/protobuf/compiler/java:helpers",
38+
"//src/google/protobuf/compiler/java:internal_helpers",
3839
"//src/google/protobuf/io:printer",
3940
"@com_google_absl//absl/container:flat_hash_map",
4041
"@com_google_absl//absl/log:absl_check",

src/google/protobuf/compiler/java/immutable/enum_field.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "google/protobuf/compiler/java/doc_comment.h"
2323
#include "google/protobuf/compiler/java/field_common.h"
2424
#include "google/protobuf/compiler/java/helpers.h"
25+
#include "google/protobuf/compiler/java/internal_helpers.h"
2526
#include "google/protobuf/compiler/java/name_resolver.h"
2627
#include "google/protobuf/io/printer.h"
2728
#include "google/protobuf/wire_format.h"

src/google/protobuf/compiler/java/immutable/map_field.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "google/protobuf/compiler/java/doc_comment.h"
1616
#include "google/protobuf/compiler/java/field_common.h"
1717
#include "google/protobuf/compiler/java/helpers.h"
18+
#include "google/protobuf/compiler/java/internal_helpers.h"
1819
#include "google/protobuf/compiler/java/name_resolver.h"
1920
#include "google/protobuf/io/printer.h"
2021

src/google/protobuf/compiler/java/immutable/string_field.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "google/protobuf/compiler/java/doc_comment.h"
2323
#include "google/protobuf/compiler/java/field_common.h"
2424
#include "google/protobuf/compiler/java/helpers.h"
25+
#include "google/protobuf/compiler/java/internal_helpers.h"
2526
#include "google/protobuf/compiler/java/name_resolver.h"
2627
#include "google/protobuf/io/printer.h"
2728
#include "google/protobuf/wire_format.h"
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
//
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file or at
6+
// https://developers.google.com/open-source/licenses/bsd
7+
8+
// Author: kenton@google.com (Kenton Varda)
9+
// Based on original Protocol Buffers design by
10+
// Sanjay Ghemawat, Jeff Dean, and others.
11+
12+
#include "google/protobuf/compiler/java/internal_helpers.h"
13+
14+
#include "absl/log/absl_log.h"
15+
#include "google/protobuf/compiler/java/helpers.h"
16+
#include "google/protobuf/compiler/java/name_resolver.h"
17+
#include "google/protobuf/descriptor.pb.h"
18+
19+
// Must be last.
20+
#include "google/protobuf/port_def.inc"
21+
22+
namespace google {
23+
namespace protobuf {
24+
namespace compiler {
25+
namespace java {
26+
namespace {
27+
28+
int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) {
29+
// j/c/g/protobuf/FieldType.java lists field types in a slightly different
30+
// order from FieldDescriptor::Type so we can't do a simple cast.
31+
//
32+
// TODO: Make j/c/g/protobuf/FieldType.java follow the same order.
33+
int result = field->type();
34+
if (result == FieldDescriptor::TYPE_GROUP) {
35+
return 17;
36+
} else if (result < FieldDescriptor::TYPE_GROUP) {
37+
return result - 1;
38+
} else {
39+
return result - 2;
40+
}
41+
}
42+
43+
int GetExperimentalJavaFieldTypeForRepeated(const FieldDescriptor* field) {
44+
if (field->type() == FieldDescriptor::TYPE_GROUP) {
45+
return 49;
46+
} else {
47+
return GetExperimentalJavaFieldTypeForSingular(field) + 18;
48+
}
49+
}
50+
51+
int GetExperimentalJavaFieldTypeForPacked(const FieldDescriptor* field) {
52+
int result = field->type();
53+
if (result < FieldDescriptor::TYPE_STRING) {
54+
return result + 34;
55+
} else if (result > FieldDescriptor::TYPE_BYTES) {
56+
return result + 30;
57+
} else {
58+
ABSL_LOG(FATAL) << field->full_name() << " can't be packed.";
59+
return 0;
60+
}
61+
}
62+
} // namespace
63+
64+
int GetExperimentalJavaFieldType(const FieldDescriptor* field) {
65+
static const int kMapFieldType = 50;
66+
static const int kOneofFieldTypeOffset = 51;
67+
68+
static const int kRequiredBit = 0x100;
69+
static const int kUtf8CheckBit = 0x200;
70+
static const int kCheckInitialized = 0x400;
71+
static const int kLegacyEnumIsClosedBit = 0x800;
72+
static const int kHasHasBit = 0x1000;
73+
int extra_bits = field->is_required() ? kRequiredBit : 0;
74+
if (field->type() == FieldDescriptor::TYPE_STRING && CheckUtf8(field)) {
75+
extra_bits |= kUtf8CheckBit;
76+
}
77+
if (field->is_required() || (GetJavaType(field) == JAVATYPE_MESSAGE &&
78+
HasRequiredFields(field->message_type()))) {
79+
extra_bits |= kCheckInitialized;
80+
}
81+
if (HasHasbit(field)) {
82+
extra_bits |= kHasHasBit;
83+
}
84+
if (GetJavaType(field) == JAVATYPE_ENUM && !SupportUnknownEnumValue(field)) {
85+
extra_bits |= kLegacyEnumIsClosedBit;
86+
}
87+
88+
if (field->is_map()) {
89+
if (!SupportUnknownEnumValue(MapValueField(field))) {
90+
const FieldDescriptor* value = field->message_type()->map_value();
91+
if (GetJavaType(value) == JAVATYPE_ENUM) {
92+
extra_bits |= kLegacyEnumIsClosedBit;
93+
}
94+
}
95+
return kMapFieldType | extra_bits;
96+
} else if (field->is_packed()) {
97+
return GetExperimentalJavaFieldTypeForPacked(field) | extra_bits;
98+
} else if (field->is_repeated()) {
99+
return GetExperimentalJavaFieldTypeForRepeated(field) | extra_bits;
100+
} else if (IsRealOneof(field)) {
101+
return (GetExperimentalJavaFieldTypeForSingular(field) +
102+
kOneofFieldTypeOffset) |
103+
extra_bits;
104+
} else {
105+
return GetExperimentalJavaFieldTypeForSingular(field) | extra_bits;
106+
}
107+
}
108+
109+
} // namespace java
110+
} // namespace compiler
111+
} // namespace protobuf
112+
} // namespace google
113+
114+
#include "google/protobuf/port_undef.inc"

0 commit comments

Comments
 (0)