Skip to content

Commit 5a45301

Browse files
Internal change
PiperOrigin-RevId: 756891268
1 parent f69180a commit 5a45301

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

editions/golden/compare_cpp_codegen_failure.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
cached_has_bits = this_._impl_._has_bits_[0];
3434
if ((cached_has_bits & 0x00000001U) != 0) {
3535
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(
36+
@@ @@
37+
::_pbi::v2::BatchSizeHelper helper;
38+
39+
{
40+
- // optional int32 int32_field = 1;
41+
+ // int32 int32_field = 1;
42+
cached_has_bits = this_._impl_._has_bits_[0];
43+
if ((cached_has_bits & 0x00000001U) != 0) {
44+
helper.AddInt32();
3645
[ FAILED ] third_party/protobuf/editions/golden/simple_proto3.pb.cc
3746
[ RUN ] third_party/protobuf/editions/golden/simple_proto3.pb.h
3847
@@ @@

editions/golden/compare_cpp_codegen_failure.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
<testsuites tests="1" name="AllTests">
33
<testsuite name="EditionsCodegenTests">
44
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.cc" status="run" result="completed" classname="DiffTest">
5-
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.cc&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;&#x0A;#include &quot;third_party/protobuf/editions/golden/simple_proto3.pb.h&quot;&#x0A;&#x0A;#include &lt;algorithm&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/extension_set.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_util.h&quot;&#x0A;#include &quot;third_party/protobuf/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table_impl.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;, with the difference:&#x0A;@@ @@&#x0A; ::_pbi::TcParser::GetTable&lt;::protobuf_editions_test::golden::SimpleProto3&gt;(), // to_prefetch&#x0A; #endif // PROTOBUF_PREFETCH_PARSE_TABLE&#x0A; }, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {::_pbi::TcParser::FastV32S1,&#x0A; {8, 0, 0, PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_)}},&#x0A; }}, {{&#x0A; 65535, 65535&#x0A; }}, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_), _Internal::kHasBitsOffset + 0, 0, (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#x0A; // no aux_entries&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; if ((cached_has_bits &amp; 0x00000001U) != 0) {&#x0A; target =&#x0A; ::proto2::internal::WireFormatLite::WriteInt32ToArrayWithField&lt;1&gt;(&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; {&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A; if ((cached_has_bits &amp; 0x00000001U) != 0) {&#x0A; total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(" type=""></failure>
5+
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.cc&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;&#x0A;#include &quot;third_party/protobuf/editions/golden/simple_proto3.pb.h&quot;&#x0A;&#x0A;#include &lt;algorithm&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/extension_set.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_util.h&quot;&#x0A;#include &quot;third_party/protobuf/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table_impl.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;, with the difference:&#x0A;@@ @@&#x0A; ::_pbi::TcParser::GetTable&lt;::protobuf_editions_test::golden::SimpleProto3&gt;(), // to_prefetch&#x0A; #endif // PROTOBUF_PREFETCH_PARSE_TABLE&#x0A; }, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {::_pbi::TcParser::FastV32S1,&#x0A; {8, 0, 0, PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_)}},&#x0A; }}, {{&#x0A; 65535, 65535&#x0A; }}, {{&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; {PROTOBUF_FIELD_OFFSET(SimpleProto3, _impl_.int32_field_), _Internal::kHasBitsOffset + 0, 0, (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#x0A; // no aux_entries&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; if ((cached_has_bits &amp; 0x00000001U) != 0) {&#x0A; target =&#x0A; ::proto2::internal::WireFormatLite::WriteInt32ToArrayWithField&lt;1&gt;(&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; {&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A; if ((cached_has_bits &amp; 0x00000001U) != 0) {&#x0A; total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(&#x0A;@@ @@&#x0A; ::_pbi::v2::BatchSizeHelper helper;&#x0A; &#x0A; {&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; cached_has_bits = this_._impl_._has_bits_[0];&#x0A; if ((cached_has_bits &amp; 0x00000001U) != 0) {&#x0A; helper.AddInt32();" type=""></failure>
66
</testcase>
77
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.h" status="run" result="completed" classname="DiffTest">
8-
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.h&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;&#x0A;#ifndef third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;#define third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;&#x0A;#include &lt;limits&gt;&#x0A;#include &lt;string&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &lt;utility&gt;&#x0A;&#x0A;#include &quot;third_party/protobuf/runtime_version.h&quot;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/arena.h&quot;&#x0A;#include &quot;third_party/protobuf/arenastring.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_decl.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_util.h&quot;&#x0A;#include &quot;third_party/protobuf/metadata_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/message_lite.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;&#x0A;#endif // third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;, with the difference:&#x0A;@@ @@&#x0A; enum : int {&#x0A; kInt32FieldFieldNumber = 1,&#x0A; };&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; bool has_int32_field() const;&#x0A; void clear_int32_field() ;&#x0A; ::int32_t int32_field() const;&#x0A;@@ @@&#x0A; &#x0A; // SimpleProto3&#x0A; &#x0A;-// optional int32 int32_field = 1;&#x0A;+// int32 int32_field = 1;&#x0A; inline bool SimpleProto3::has_int32_field() const {&#x0A; bool value = (_impl_._has_bits_[0] &amp; 0x00000001U) != 0;&#x0A; return value;" type=""></failure>
8+
<failure message="Value of: third_party/protobuf/editions/golden/simple_proto3.pb.h&#x0A;Expected: &#x0A;// Generated by the protocol buffer compiler. DO NOT EDIT!&#x0A;// NO CHECKED-IN PROTOBUF GENCODE&#x0A;// source: third_party/protobuf/editions/golden/simple_proto3.proto&#x0A;&#x0A;#ifndef third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;#define third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;&#x0A;#include &lt;limits&gt;&#x0A;#include &lt;string&gt;&#x0A;#include &lt;type_traits&gt;&#x0A;#include &lt;utility&gt;&#x0A;&#x0A;#include &quot;third_party/protobuf/runtime_version.h&quot;&#x0A;#include &quot;third_party/protobuf/io/coded_stream.h&quot;&#x0A;#include &quot;third_party/protobuf/arena.h&quot;&#x0A;#include &quot;third_party/protobuf/arenastring.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_tctable_decl.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/generated_message_table_impl.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/batch_builder.h&quot;&#x0A;#include &quot;third_party/protobuf/v2/batch_wire_format.h&quot;&#x0A;#include &quot;third_party/protobuf/generated_message_util.h&quot;&#x0A;#include &quot;third_party/protobuf/metadata_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/message_lite.h&quot;&#x0A;// @@protoc_insertion_point(includes)&#x0A;&#x0A;// Must be included last.&#x0A;&#x0A;#endif // third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;, with the difference:&#x0A;@@ @@&#x0A; enum : int {&#x0A; kInt32FieldFieldNumber = 1,&#x0A; };&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; bool has_int32_field() const;&#x0A; void clear_int32_field() ;&#x0A; ::int32_t int32_field() const;&#x0A;@@ @@&#x0A; &#x0A; // SimpleProto3&#x0A; &#x0A;-// optional int32 int32_field = 1;&#x0A;+// int32 int32_field = 1;&#x0A; inline bool SimpleProto3::has_int32_field() const {&#x0A; bool value = (_impl_._has_bits_[0] &amp; 0x00000001U) != 0;&#x0A; return value;" type=""></failure>
99
</testcase>
1010
<testcase name="third_party/protobuf/editions/golden/simple_proto3.proto.static_reflection.h" status="run" result="completed" classname="DiffTest">
1111
</testcase>

src/google/protobuf/compiler/cpp/field.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ std::vector<Sub> FieldVars(const FieldDescriptor* field, const Options& opts) {
5757

5858
{"field_", FieldMemberName(field, split)},
5959
{"DeclaredType", DeclaredTypeMethodName(field->type())},
60+
{"DeclaredCppType", DeclaredCppTypeMethodName(field->cpp_type())},
6061
{"kTagBytes", WireFormat::TagSize(field->number(), field->type())},
6162
Sub("PrepareSplitMessageForWrite",
6263
split ? "PrepareSplitMessageForWrite();" : "")

src/google/protobuf/compiler/cpp/file.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* p) {
16571657
IncludeFile("third_party/protobuf/generated_message_tctable_decl.h", p);
16581658
}
16591659

1660+
16601661
IncludeFile("third_party/protobuf/generated_message_util.h", p);
16611662
IncludeFile("third_party/protobuf/metadata_lite.h", p);
16621663

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,36 @@ const char* DeclaredTypeMethodName(FieldDescriptor::Type type) {
882882
return "";
883883
}
884884

885+
absl::string_view DeclaredCppTypeMethodName(FieldDescriptor::CppType type) {
886+
switch (type) {
887+
case FieldDescriptor::CPPTYPE_INT32:
888+
return "Int32";
889+
case FieldDescriptor::CPPTYPE_INT64:
890+
return "Int64";
891+
case FieldDescriptor::CPPTYPE_UINT32:
892+
return "UInt32";
893+
case FieldDescriptor::CPPTYPE_UINT64:
894+
return "UInt64";
895+
case FieldDescriptor::CPPTYPE_DOUBLE:
896+
return "Double";
897+
case FieldDescriptor::CPPTYPE_FLOAT:
898+
return "Float";
899+
case FieldDescriptor::CPPTYPE_BOOL:
900+
return "Bool";
901+
case FieldDescriptor::CPPTYPE_ENUM:
902+
return "Enum";
903+
case FieldDescriptor::CPPTYPE_STRING:
904+
return "String";
905+
case FieldDescriptor::CPPTYPE_MESSAGE:
906+
return "Message";
907+
908+
// No default because we want the compiler to complain if any new
909+
// types are added.
910+
}
911+
ABSL_LOG(FATAL) << "Can't get here.";
912+
return "";
913+
}
914+
885915
std::string Int32ToString(int number) {
886916
if (number == std::numeric_limits<int32_t>::min()) {
887917
// This needs to be special-cased, see explanation here:
@@ -1308,6 +1338,17 @@ bool IsV2EnabledForMessage(const Descriptor* descriptor,
13081338
return false;
13091339
}
13101340

1341+
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
1342+
bool IsEligibleForV2Batching(const FieldDescriptor* field) {
1343+
// Non-message fields whose numbers fit into 2B should be considered for
1344+
// batching although the actual batching depends on the current batching, the
1345+
// payload size, etc.
1346+
return field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE &&
1347+
!field->is_map() &&
1348+
field->number() < std::numeric_limits<uint16_t>::max();
1349+
}
1350+
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
1351+
13111352
bool HasV2MessageTable(const FileDescriptor* file, const Options& options) {
13121353
for (int i = 0; i < file->message_type_count(); ++i) {
13131354
if (HasV2Table(file->message_type(i), options)) return true;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ std::string PrimitiveTypeName(const Options& options,
255255
// methods of WireFormat. For example, TYPE_INT32 becomes "Int32".
256256
const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
257257

258+
// Get the declared cpp_type name in CamelCase format, as is used e.g. for the
259+
// methods of v2 WireFormat. For example, CPPTYPE_INT32 becomes "Int32".
260+
absl::string_view DeclaredCppTypeMethodName(FieldDescriptor::CppType type);
261+
258262
// Return the code that evaluates to the number when compiled.
259263
std::string Int32ToString(int number);
260264

@@ -510,6 +514,11 @@ bool IsV2ParseEnabledForMessage(const Descriptor* descriptor);
510514
bool IsV2EnabledForMessage(const Descriptor* descriptor,
511515
const Options& options);
512516

517+
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
518+
// Returns true if a field can be batched.
519+
bool IsEligibleForV2Batching(const FieldDescriptor* field);
520+
#endif // PROTOBUF_INTERNAL_V2_EXPERIMENT
521+
513522
// Does this file have generated parsing, serialization, and other
514523
// standard methods for which reflection-based fallback implementations exist?
515524
inline bool HasGeneratedMethods(const FileDescriptor* file,

0 commit comments

Comments
 (0)