Skip to content

Commit

Permalink
In the custom vtable mode invert how the static/non-static overloads …
Browse files Browse the repository at this point in the history
…versions work. That way the public entry point and the function pointer both end up being the same. Currently, the static function is a separate function than the one called externally, forcing two functions to exist.

It has no effect in normal mode.

PiperOrigin-RevId: 640173494
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jul 2, 2024
1 parent a9bc366 commit 8fbedea
Show file tree
Hide file tree
Showing 24 changed files with 6,776 additions and 5,008 deletions.
28 changes: 14 additions & 14 deletions editions/golden/compare_cpp_codegen_failure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
(0 | ::_fl::kFcOptional | ::_fl::kInt32)},
}},
@@ @@
(void)cached_has_bits;
(void)cached_has_bits;

cached_has_bits = _impl_._has_bits_[0];
- // optional int32 int32_field = 1;
+ // int32 int32_field = 1;
if (cached_has_bits & 0x00000001u) {
target = ::proto2::internal::WireFormatLite::
WriteInt32ToArrayWithField<1>(
cached_has_bits = this_._impl_._has_bits_[0];
- // optional int32 int32_field = 1;
+ // int32 int32_field = 1;
if (cached_has_bits & 0x00000001u) {
target = ::proto2::internal::WireFormatLite::
WriteInt32ToArrayWithField<1>(
@@ @@
(void)cached_has_bits;
(void)cached_has_bits;

{
- // optional int32 int32_field = 1;
+ // int32 int32_field = 1;
cached_has_bits = _impl_._has_bits_[0];
if (cached_has_bits & 0x00000001u) {
total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(
{
- // optional int32 int32_field = 1;
+ // int32 int32_field = 1;
cached_has_bits =
this_._impl_._has_bits_[0];
if (cached_has_bits & 0x00000001u) {
[ FAILED ] third_party/protobuf/editions/golden/simple_proto3.pb.cc
[ RUN ] third_party/protobuf/editions/golden/simple_proto3.pb.h
@@ @@
Expand Down
2 changes: 1 addition & 1 deletion editions/golden/compare_cpp_codegen_failure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<testsuites tests="1" name="AllTests">
<testsuite name="EditionsCodegenTests">
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.cc" status="run" result="completed" classname="DiffTest">
<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;// Protobuf C++ Version: 0.20240628.0&#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/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.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,&#x0A; (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#x0A;@@ @@&#x0A; (void)cached_has_bits;&#x0A; &#x0A; cached_has_bits = _impl_._has_bits_[0];&#x0A;- // optional int32 int32_field = 1;&#x0A;+ // int32 int32_field = 1;&#x0A; if (cached_has_bits &amp; 0x00000001u) {&#x0A; target = ::proto2::internal::WireFormatLite::&#x0A; 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 = _impl_._has_bits_[0];&#x0A; if (cached_has_bits &amp; 0x00000001u) {&#x0A; total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(" type=""></failure>
<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;// Protobuf C++ Version: 0.20240628.0&#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/wire_format_lite.h&quot;&#x0A;#include &quot;third_party/protobuf/io/zero_copy_stream_impl_lite.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,&#x0A; (0 | ::_fl::kFcOptional | ::_fl::kInt32)},&#x0A; }},&#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) {&#x0A; target = ::proto2::internal::WireFormatLite::&#x0A; 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 =&#x0A; this_._impl_._has_bits_[0];&#x0A; if (cached_has_bits &amp; 0x00000001u) {" type=""></failure>
</testcase>
<testcase name="third_party/protobuf/editions/golden/simple_proto3.pb.h" status="run" result="completed" classname="DiffTest">
<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;// Protobuf C++ Version: 0.20240628.0&#x0A;&#x0A;#ifndef GOOGLE_PROTOBUF_INCLUDED_third_5fparty_2fprotobuf_2feditions_2fgolden_2fsimple_5fproto3_2eproto_2epb_2eh&#x0A;#define GOOGLE_PROTOBUF_INCLUDED_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;#if PROTOBUF_VERSION != 20240628&#x0A;#error &quot;Protobuf C++ gencode is built with an incompatible version of&quot;&#x0A;#error &quot;Protobuf C++ headers/runtime. See&quot;&#x0A;#error &quot;https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp&quot;&#x0A;#endif&#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/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 // GOOGLE_PROTOBUF_INCLUDED_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>
Expand Down
13 changes: 7 additions & 6 deletions src/google/protobuf/compiler/cpp/field_generators/cord_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
void CordFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
Formatter format(printer, variables_);
if (field_->default_value_string().empty()) {
format("$field$.Clear();\n");
format("this_.$field$.Clear();\n");
} else {
format("$field$ = ::absl::string_view($default$, $default_length$);\n");
format(
"this_.$field$ = ::absl::string_view($default$, $default_length$);\n");
}
}

Expand Down Expand Up @@ -262,12 +263,12 @@ void CordFieldGenerator::GenerateSerializeWithCachedSizesToArray(
if (field_->type() == FieldDescriptor::TYPE_STRING) {
GenerateUtf8CheckCodeForCord(
field_, options_, false,
absl::Substitute("this->_internal_$0(), ", printer->LookupVar("name")),
absl::Substitute("this_._internal_$0(), ", printer->LookupVar("name")),
format);
}
format(
"target = stream->Write$declared_type$($number$, "
"this->_internal_$name$(), "
"this_._internal_$name$(), "
"target);\n");
}

Expand All @@ -276,7 +277,7 @@ void CordFieldGenerator::GenerateByteSize(io::Printer* printer) const {
format(
"total_size += $tag_size$ +\n"
" ::$proto_ns$::internal::WireFormatLite::$declared_type$Size(\n"
" this->_internal_$name$());\n");
" this_._internal_$name$());\n");
}

void CordFieldGenerator::GenerateConstexprAggregateInitializer(
Expand Down Expand Up @@ -428,7 +429,7 @@ void CordOneofFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
Formatter format(printer, variables_);
format(
"if (GetArena() == nullptr) {\n"
" delete $field$;\n"
" delete this_.$field$;\n"
"}\n");
}

Expand Down
32 changes: 16 additions & 16 deletions src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SingularEnum : public FieldGeneratorBase {

void GenerateClearingCode(io::Printer* p) const override {
p->Emit(R"cc(
$field_$ = $kDefault$;
this_.$field_$ = $kDefault$;
)cc");
}

Expand Down Expand Up @@ -103,14 +103,14 @@ class SingularEnum : public FieldGeneratorBase {
p->Emit(R"cc(
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteEnumToArray(
$number$, this->_internal_$name$(), target);
$number$, this_._internal_$name$(), target);
)cc");
}

void GenerateByteSize(io::Printer* p) const override {
p->Emit(R"cc(
total_size += $kTagBytes$ +
::_pbi::WireFormatLite::EnumSize(this->_internal_$name$());
::_pbi::WireFormatLite::EnumSize(this_._internal_$name$());
)cc");
}

Expand Down Expand Up @@ -248,9 +248,9 @@ class RepeatedEnum : public FieldGeneratorBase {

void GenerateClearingCode(io::Printer* p) const override {
if (should_split()) {
p->Emit("$field_$.ClearIfNotDefault();\n");
p->Emit("this_.$field_$.ClearIfNotDefault();\n");
} else {
p->Emit("$field_$.Clear();\n");
p->Emit("this_.$field_$.Clear();\n");
}
}

Expand Down Expand Up @@ -481,16 +481,16 @@ void RepeatedEnum::GenerateSerializeWithCachedSizesToArray(
{"byte_size",
[&] {
if (has_cached_size_) {
p->Emit(
R"cc(std::size_t byte_size = $cached_size_$.Get();)cc");
p->Emit(R"cc(std::size_t byte_size =
this_.$cached_size_$.Get();)cc");
} else {
p->Emit(R"cc(
std::size_t byte_size = 0;
auto count = static_cast<std::size_t>(this->_internal_$name$_size());
auto count = static_cast<std::size_t>(this_._internal_$name$_size());
for (std::size_t i = 0; i < count; ++i) {
byte_size += ::_pbi::WireFormatLite::EnumSize(
this->_internal_$name$().Get(static_cast<int>(i)));
this_._internal_$name$().Get(static_cast<int>(i)));
}
)cc");
}
Expand All @@ -500,18 +500,18 @@ void RepeatedEnum::GenerateSerializeWithCachedSizesToArray(
{
$byte_size$;
if (byte_size > 0) {
target = stream->WriteEnumPacked($number$, _internal_$name$(),
byte_size, target);
target = stream->WriteEnumPacked(
$number$, this_._internal_$name$(), byte_size, target);
}
}
)cc");
return;
}
p->Emit(R"cc(
for (int i = 0, n = this->_internal_$name$_size(); i < n; ++i) {
for (int i = 0, n = this_._internal_$name$_size(); i < n; ++i) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteEnumToArray(
$number$, static_cast<$Enum$>(this->_internal_$name$().Get(i)),
$number$, static_cast<$Enum$>(this_._internal_$name$().Get(i)),
target);
}
)cc");
Expand All @@ -538,18 +538,18 @@ void RepeatedEnum::GenerateByteSize(io::Printer* p) const {
)cc");
if (has_cached_size_) {
p->Emit(R"cc(
$cached_size_$.Set(::_pbi::ToCachedSize(data_size));
this_.$cached_size_$.Set(::_pbi::ToCachedSize(data_size));
)cc");
}
}},
},
R"cc(
std::size_t data_size = 0;
auto count = static_cast<std::size_t>(this->_internal_$name$_size());
auto count = static_cast<std::size_t>(this_._internal_$name$_size());
for (std::size_t i = 0; i < count; ++i) {
data_size += ::_pbi::WireFormatLite::EnumSize(
this->_internal_$name$().Get(static_cast<int>(i)));
this_._internal_$name$().Get(static_cast<int>(i)));
}
total_size += data_size;
$add_to_size$;
Expand Down
11 changes: 6 additions & 5 deletions src/google/protobuf/compiler/cpp/field_generators/map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Map : public FieldGeneratorBase {

void GenerateClearingCode(io::Printer* p) const override {
p->Emit(R"cc(
$field_$.Clear();
this_.$field_$.Clear();
)cc");
}

Expand Down Expand Up @@ -276,10 +276,10 @@ void Map::GenerateSerializeWithCachedSizesToArray(io::Printer* p) const {
}},
},
R"cc(
if (!_internal_$name$().empty()) {
if (!this_._internal_$name$().empty()) {
using MapType = $Map$;
using WireHelper = $Funcs$;
const auto& field = _internal_$name$();
const auto& field = this_._internal_$name$();
if (stream->IsSerializationDeterministic() && field.size() > 1) {
for (const auto& entry : $pbi$::$Sorter$<MapType>(field)) {
Expand All @@ -304,8 +304,9 @@ void Map::GenerateByteSize(io::Printer* p) const {
{"Funcs", [&] { EmitFuncs(field_, p); }},
},
R"cc(
total_size += $kTagBytes$ * $pbi$::FromIntSize(_internal_$name$_size());
for (const auto& entry : _internal_$name$()) {
total_size +=
$kTagBytes$ * $pbi$::FromIntSize(this_._internal_$name$_size());
for (const auto& entry : this_._internal_$name$()) {
total_size += $Funcs$::ByteSizeLong(entry.first, entry.second);
}
)cc");
Expand Down
Loading

0 comments on commit 8fbedea

Please sign in to comment.