Skip to content

Commit 89edc6b

Browse files
Internal change
PiperOrigin-RevId: 758813229
1 parent e004c8b commit 89edc6b

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

editions/golden/compare_cpp_codegen_failure.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
if ((cached_has_bits & 0x00000001U) != 0) {
4343
helper.AddInt32();
4444
}
45+
@@ @@
46+
target = ::_pbi::v2::codegen::WritePoisonHeaderToArray(target, stream);
47+
48+
cached_has_bits = this_._impl_._has_bits_[0];
49+
- // optional int32 int32_field = 1;
50+
+ // int32 int32_field = 1;
51+
if ((cached_has_bits & 0x00000001U) != 0) {
52+
target = builder.WriteInt32(
53+
1, this_._internal_int32_field(), target, stream);
4554
[ FAILED ] third_party/protobuf/editions/golden/simple_proto3.pb.cc
4655
[ RUN ] third_party/protobuf/editions/golden/simple_proto3.pb.h
4756
@@ @@

editions/golden/compare_cpp_codegen_failure.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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(&#x0A;@@ @@&#x0A; (void)helper;&#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; helper.AddInt32();&#x0A; }" 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; (void)helper;&#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; helper.AddInt32();&#x0A; }&#x0A;@@ @@&#x0A; target = ::_pbi::v2::codegen::WritePoisonHeaderToArray(target, stream);&#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 = builder.WriteInt32(&#x0A; 1, this_._internal_int32_field(), target, stream);" type=""></failure>
66
</testcase>
77
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.h" status="run" result="completed" classname="DiffTest">
88
<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>

src/google/protobuf/compiler/cpp/field_generators/primitive_field.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "absl/log/absl_check.h"
1717
#include "absl/log/absl_log.h"
1818
#include "absl/memory/memory.h"
19+
#include "absl/strings/string_view.h"
1920
#include "absl/types/optional.h"
2021
#include "google/protobuf/compiler/cpp/field.h"
2122
#include "google/protobuf/compiler/cpp/field_generators/generators.h"
@@ -289,6 +290,7 @@ void SingularPrimitive::GenerateByteSize(io::Printer* p) const {
289290
)cc");
290291
}
291292

293+
292294
class RepeatedPrimitive final : public FieldGeneratorBase {
293295
public:
294296
RepeatedPrimitive(const FieldDescriptor* field, const Options& opts,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,12 @@ bool IsV2EnabledForMessage(const Descriptor* descriptor,
13391339
}
13401340

13411341
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
1342+
bool ShouldGenerateV2Code(const Descriptor* descriptor,
1343+
const Options& options) {
1344+
return !options.opensource_runtime && !options.bootstrap &&
1345+
!HasSimpleBaseClass(descriptor, options);
1346+
}
1347+
13421348
bool IsEligibleForV2Batching(const FieldDescriptor* field) {
13431349
// Non-message fields whose numbers fit into 2B should be considered for
13441350
// batching although the actual batching depends on the current batching, the

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ bool IsV2EnabledForMessage(const Descriptor* descriptor,
515515
const Options& options);
516516

517517
#ifdef PROTOBUF_INTERNAL_V2_EXPERIMENT
518+
bool ShouldGenerateV2Code(const Descriptor* descriptor, const Options& options);
519+
518520
// Returns true if a field can be batched.
519521
bool IsEligibleForV2Batching(const FieldDescriptor* field);
520522
bool HasFieldEligibleForV2Batching(const Descriptor* descriptor);

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,6 @@ void MessageGenerator::MaybeEmitUpdateCachedHasbits(
13551355
)cc");
13561356
}
13571357

1358-
template <bool kIsV2>
13591358
void MessageGenerator::EmitUpdateByteSizeForField(
13601359
const FieldDescriptor* field, io::Printer* p,
13611360
int& cached_has_word_index) const {
@@ -1364,7 +1363,9 @@ void MessageGenerator::EmitUpdateByteSizeForField(
13641363
{"update_cached_has_bits",
13651364
[&] { MaybeEmitUpdateCachedHasbits(field, p, cached_has_word_index); }},
13661365
{"check_and_update_byte_size_for_field",
1367-
[&]() { EmitCheckAndUpdateByteSizeForField<kIsV2>(field, p); }}},
1366+
[&]() {
1367+
EmitCheckAndUpdateByteSizeForField</*kIsV2=*/false>(field, p);
1368+
}}},
13681369
R"cc(
13691370
$comment$;
13701371
$update_cached_has_bits$;
@@ -5414,6 +5415,47 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
54145415
void MessageGenerator::GenerateByteSizeV2(io::Printer* p) {
54155416
}
54165417

5418+
void MessageGenerator::EmitCheckAndSerializeField(const FieldDescriptor* field,
5419+
io::Printer* p) const {
5420+
absl::AnyInvocable<void()> emit_body = [&] {
5421+
};
5422+
if (!HasHasbit(field)) {
5423+
MayEmitIfNonDefaultCheck(p, "this_.", field, std::move(emit_body),
5424+
/*with_enclosing_braces_always=*/true);
5425+
return;
5426+
}
5427+
5428+
if (field->options().weak()) {
5429+
p->Emit({{"emit_body", [&] { emit_body(); }}},
5430+
R"cc(
5431+
if (has_$name$()) {
5432+
$emit_body$;
5433+
}
5434+
)cc");
5435+
return;
5436+
}
5437+
5438+
int has_bit_index = has_bit_indices_[field->index()];
5439+
p->Emit({{"condition", GenerateConditionMaybeWithProbabilityForField(
5440+
has_bit_index, field, options_)},
5441+
{"check_nondefault_and_emit_body",
5442+
[&] {
5443+
// Note that it's possible that the field has explicit presence.
5444+
// In that case, nondefault check will not be emitted but
5445+
// emit_body will still be emitted.
5446+
MayEmitIfNonDefaultCheck(p, "this_.", field, std::move(emit_body),
5447+
/*with_enclosing_braces_always=*/false);
5448+
}}},
5449+
R"cc(
5450+
if ($condition$) {
5451+
$check_nondefault_and_emit_body$;
5452+
}
5453+
)cc");
5454+
}
5455+
5456+
void MessageGenerator::GenerateSerializeV2(io::Printer* p) {
5457+
}
5458+
54175459
bool MessageGenerator::NeedsIsInitialized() {
54185460
if (HasSimpleBaseClass(descriptor_, options_)) return false;
54195461
if (descriptor_->extension_range_count() != 0) return true;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class MessageGenerator {
128128
void GenerateSerializeWithCachedSizesBodyShuffled(io::Printer* p);
129129
void GenerateByteSize(io::Printer* p);
130130
void GenerateByteSizeV2(io::Printer* p);
131+
void GenerateSerializeV2(io::Printer* p);
131132
void GenerateClassData(io::Printer* p);
132133
void GenerateMapEntryClassDefinition(io::Printer* p);
133134
void GenerateAnyMethodDefinition(io::Printer* p);
@@ -208,7 +209,6 @@ class MessageGenerator {
208209
template <bool kIsV2 = false>
209210
void EmitCheckAndUpdateByteSizeForField(const FieldDescriptor* field,
210211
io::Printer* p) const;
211-
template <bool kIsV2 = false>
212212
void EmitUpdateByteSizeForField(const FieldDescriptor* field, io::Printer* p,
213213
int& cached_has_word_index) const;
214214

@@ -219,6 +219,8 @@ class MessageGenerator {
219219
void EmitUpdateByteSizeV2ForNumerics(
220220
size_t field_size, io::Printer* p, int& cached_has_word_index,
221221
std::vector<const FieldDescriptor*>&& fields) const;
222+
void EmitCheckAndSerializeField(const FieldDescriptor* field,
223+
io::Printer* p) const;
222224

223225
const Descriptor* descriptor_;
224226
int index_in_file_messages_;

src/google/protobuf/message_lite.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
#include <istream>
2020
#include <ostream>
2121
#include <string>
22-
#include <typeinfo>
2322
#include <utility>
2423

25-
#include "absl/base/config.h"
2624
#include "absl/base/optimization.h"
2725
#include "absl/log/absl_check.h"
2826
#include "absl/log/absl_log.h"
@@ -721,6 +719,7 @@ absl::Cord MessageLite::SerializePartialAsCord() const {
721719
return output;
722720
}
723721

722+
724723
namespace internal {
725724

726725
// Non-inline variants of std::string specializations for

0 commit comments

Comments
 (0)