Skip to content

Commit

Permalink
Automated rollback of commit 589cf71.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 669198477
  • Loading branch information
mkruskal-google authored and copybara-github committed Aug 30, 2024
1 parent 890dd25 commit 363c0e1
Show file tree
Hide file tree
Showing 17 changed files with 1,380 additions and 1,887 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,9 @@ void CordFieldGenerator::GenerateInlineAccessorDefinitions(
void CordFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
Formatter format(printer, variables_);
if (field_->default_value_string().empty()) {
format("this_.$field$.Clear();\n");
format("$field$.Clear();\n");
} else {
format(
"this_.$field$ = ::absl::string_view($default$, $default_length$);\n");
format("$field$ = ::absl::string_view($default$, $default_length$);\n");
}
}

Expand Down
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(
this_.$field_$ = $kDefault$;
$field_$ = $kDefault$;
)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("this_.$field_$.ClearIfNotDefault();\n");
p->Emit("$field_$.ClearIfNotDefault();\n");
} else {
p->Emit("this_.$field_$.Clear();\n");
p->Emit("$field_$.Clear();\n");
}
}

Expand Down
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(
this_.$field_$.Clear();
$field_$.Clear();
)cc");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ void SingularMessage::GenerateMessageClearingCode(io::Printer* p) const {
ABSL_CHECK(has_hasbit_);
p->Emit(
R"cc(
$DCHK$(this_.$field_$ != nullptr);
this_.$field_$->Clear();
$DCHK$($field_$ != nullptr);
$field_$->Clear();
)cc");
}

Expand Down Expand Up @@ -872,9 +872,9 @@ void RepeatedMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class SingularPrimitive final : public FieldGeneratorBase {

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

Expand Down Expand Up @@ -297,9 +297,9 @@ class RepeatedPrimitive final : public FieldGeneratorBase {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void SingularString::GenerateClearingCode(io::Printer* p) const {
void SingularString::GenerateMessageClearingCode(io::Printer* p) const {
if (is_oneof()) {
p->Emit(R"cc(
this_.$field_$.Destroy();
$field_$.Destroy();
)cc");
return;
}
Expand All @@ -563,23 +563,23 @@ void SingularString::GenerateMessageClearingCode(io::Printer* p) const {
// For non-inlined strings, we distinguish from non-default by comparing
// instances, rather than contents.
p->Emit(R"cc(
$DCHK$(!this_.$field_$.IsDefault());
$DCHK$(!$field_$.IsDefault());
)cc");
}

if (!EmptyDefault()) {
// Clear to a non-empty default is more involved, as we try to use the
// Arena if one is present and may need to reallocate the string.
p->Emit(R"cc(
this_.$field_$.ClearToDefault($lazy_var$, this_.GetArena());
$field_$.ClearToDefault($lazy_var$, GetArena());
)cc");
return;
}

p->Emit({{"Clear",
HasHasbit(field_) ? "ClearNonDefaultToEmpty" : "ClearToEmpty"}},
R"cc(
this_.$field_$.$Clear$();
$field_$.$Clear$();
)cc");
}

Expand Down Expand Up @@ -750,9 +750,9 @@ class RepeatedString : public FieldGeneratorBase {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ void SingularStringView::GenerateClearingCode(io::Printer* p) const {
void SingularStringView::GenerateMessageClearingCode(io::Printer* p) const {
if (is_oneof()) {
p->Emit(R"cc(
this_.$field_$.Destroy();
$field_$.Destroy();
)cc");
return;
}
Expand All @@ -390,23 +390,23 @@ void SingularStringView::GenerateMessageClearingCode(io::Printer* p) const {

if (is_inlined() && HasHasbit(field_)) {
p->Emit(R"cc(
$DCHK$(!this_.$field_$.IsDefault());
$DCHK$(!$field_$.IsDefault());
)cc");
}

if (!EmptyDefault()) {
// Clear to a non-empty default is more involved, as we try to use the
// Arena if one is present and may need to reallocate the string.
p->Emit(R"cc(
this_.$field_$.ClearToDefault($lazy_var$, this_.GetArena());
$field_$.ClearToDefault($lazy_var$, GetArena());
)cc");
return;
}

p->Emit({{"Clear",
HasHasbit(field_) ? "ClearNonDefaultToEmpty" : "ClearToEmpty"}},
R"cc(
this_.$field_$.$Clear$();
$field_$.$Clear$();
)cc");
}

Expand Down Expand Up @@ -577,9 +577,9 @@ class RepeatedStringView : public FieldGeneratorBase {

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

Expand Down
56 changes: 22 additions & 34 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ std::string ConditionalToCheckBitmasks(
return result + (return_success ? " == 0" : " != 0");
}

void PrintPresenceCheck(const FieldDescriptor* field, absl::string_view prefix,
void PrintPresenceCheck(const FieldDescriptor* field,
const std::vector<int>& has_bit_indices, io::Printer* p,
int* cached_has_word_index) {
if (!field->options().weak()) {
int has_bit_index = has_bit_indices[field->index()];
if (*cached_has_word_index != (has_bit_index / 32)) {
*cached_has_word_index = (has_bit_index / 32);
p->Emit({{"prefix", prefix}, {"index", *cached_has_word_index}},
p->Emit({{"index", *cached_has_word_index}},
R"cc(
cached_has_bits = $prefix$$has_bits$[$index$];
cached_has_bits = $has_bits$[$index$];
)cc");
}
p->Emit({{"mask", absl::StrFormat("0x%08xu", 1u << (has_bit_index % 32))}},
Expand Down Expand Up @@ -1171,7 +1171,6 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field,
}}},
R"cc(
$inline $void $classname$::clear_$name$() {
$classname$& this_ PROTOBUF_UNUSED = *this;
$pbi$::TSanWrite(&_impl_);
$WeakDescriptorSelfPin$;
$body$;
Expand Down Expand Up @@ -1860,24 +1859,22 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
// virtual overrides. This reduces the number of functions in the
// binary in both modes.
p->Emit(R"cc(
ABSL_ATTRIBUTE_REINITIALIZES void Clear() PROTOBUF_FINAL;
#if defined(PROTOBUF_CUSTOM_VTABLE)
private:
static void Clear(MessageLite& msg);
static ::size_t ByteSizeLong(const ::$proto_ns$::MessageLite& msg);
static $uint8$* _InternalSerialize(
const MessageLite& msg, $uint8$* target,
::$proto_ns$::io::EpsCopyOutputStream* stream);
public:
ABSL_ATTRIBUTE_REINITIALIZES void Clear() { Clear(*this); };
::size_t ByteSizeLong() const { return ByteSizeLong(*this); }
$uint8$* _InternalSerialize(
$uint8$* target,
::$proto_ns$::io::EpsCopyOutputStream* stream) const {
return _InternalSerialize(*this, target, stream);
}
#else // PROTOBUF_CUSTOM_VTABLE
ABSL_ATTRIBUTE_REINITIALIZES void Clear() final;
::size_t ByteSizeLong() const final;
$uint8$* _InternalSerialize(
$uint8$* target,
Expand Down Expand Up @@ -3448,19 +3445,12 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
// hasbit to see if a zero-init is necessary.
const int kMaxUnconditionalPrimitiveBytesClear = 4;

p->Emit(R"cc(
#if defined(PROTOBUF_CUSTOM_VTABLE)
void $classname$::Clear(MessageLite& base) {
$classname$& this_ = static_cast<$classname$&>(base);
#else // PROTOBUF_CUSTOM_VTABLE
void $classname$::Clear() {
$classname$& this_ = *this;
#endif // PROTOBUF_CUSTOM_VTABLE
// @@protoc_insertion_point(message_clear_start:$full_name$)
)cc");
format(
"PROTOBUF_NOINLINE void $classname$::Clear() {\n"
"// @@protoc_insertion_point(message_clear_start:$full_name$)\n");
format.Indent();

format("$pbi$::TSanWrite(&this_._impl_);\n");
format("$pbi$::TSanWrite(&_impl_);\n");

format(
// TODO: It would be better to avoid emitting this if it is not used,
Expand All @@ -3470,7 +3460,7 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
"(void) cached_has_bits;\n\n");

if (descriptor_->extension_range_count() > 0) {
format("this_.$extensions$.Clear();\n");
format("$extensions$.Clear();\n");
}

// Collect fields into chunks. Each chunk may have an if() condition that
Expand Down Expand Up @@ -3506,14 +3496,13 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
int cached_has_word_index = -1;
while (it != end) {
auto next = FindNextUnequalChunk(it, end, MayGroupChunksForHaswordsCheck);
bool has_haswords_check =
MaybeEmitHaswordsCheck(it, next, options_, has_bit_indices_,
cached_has_word_index, "this_.", p);
bool has_haswords_check = MaybeEmitHaswordsCheck(
it, next, options_, has_bit_indices_, cached_has_word_index, "", p);
bool has_default_split_check = !it->fields.empty() && it->should_split;
if (has_default_split_check) {
// Some fields are cleared without checking has_bit. So we add the
// condition here to avoid writing to the default split instance.
format("if (!this_.IsSplitMessageDefault()) {\n");
format("if (!IsSplitMessageDefault()) {\n");
format.Indent();
}
while (it != next) {
Expand Down Expand Up @@ -3559,8 +3548,7 @@ void MessageGenerator::GenerateClear(io::Printer* p) {

if (cached_has_word_index != HasWordIndex(fields.front())) {
cached_has_word_index = HasWordIndex(fields.front());
format("cached_has_bits = this_.$has_bits$[$1$];\n",
cached_has_word_index);
format("cached_has_bits = $has_bits$[$1$];\n", cached_has_word_index);
}
format("if (cached_has_bits & 0x$1$u) {\n", chunk_mask_str);
format.Indent();
Expand All @@ -3574,9 +3562,9 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
ABSL_CHECK_EQ(chunk_is_split, ShouldSplit(memset_start, options_));
ABSL_CHECK_EQ(chunk_is_split, ShouldSplit(memset_end, options_));
format(
"::memset(&this_.$1$, 0, static_cast<::size_t>(\n"
" reinterpret_cast<char*>(&this_.$2$) -\n"
" reinterpret_cast<char*>(&this_.$1$)) + sizeof($2$));\n",
"::memset(&$1$, 0, static_cast<::size_t>(\n"
" reinterpret_cast<char*>(&$2$) -\n"
" reinterpret_cast<char*>(&$1$)) + sizeof($2$));\n",
FieldMemberName(memset_start, chunk_is_split),
FieldMemberName(memset_end, chunk_is_split));
}
Expand All @@ -3595,7 +3583,7 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
field->cpp_type() == FieldDescriptor::CPPTYPE_STRING);

if (have_enclosing_if) {
PrintPresenceCheck(field, "this_.", has_bit_indices_, p,
PrintPresenceCheck(field, has_bit_indices_, p,
&cached_has_word_index);
format.Indent();
}
Expand Down Expand Up @@ -3633,21 +3621,21 @@ void MessageGenerator::GenerateClear(io::Printer* p) {
}
// Step 4: Unions.
for (auto oneof : OneOfRange(descriptor_)) {
format("this_.clear_$1$();\n", oneof->name());
format("clear_$1$();\n", oneof->name());
}

if (num_weak_fields_) {
format("this_.$weak_field_map$.ClearAll();\n");
format("$weak_field_map$.ClearAll();\n");
}

// We don't clear donated status.

if (!has_bit_indices_.empty()) {
// Step 5: Everything else.
format("this_.$has_bits$.Clear();\n");
format("$has_bits$.Clear();\n");
}

format("this_._internal_metadata_.Clear<$unknown_fields_type$>();\n");
format("_internal_metadata_.Clear<$unknown_fields_type$>();\n");

format.Outdent();
format("}\n");
Expand Down Expand Up @@ -4004,7 +3992,7 @@ void MessageGenerator::GenerateClassData(io::Printer* p) {
if (HasGeneratedMethods(descriptor_->file(), options_) &&
!IsMapEntryMessage(descriptor_)) {
p->Emit(R"cc(
&$classname$::Clear, &$classname$::ByteSizeLong,
$superclass$::GetClearImpl<$classname$>(), &$classname$::ByteSizeLong,
&$classname$::_InternalSerialize,
)cc");
} else {
Expand Down
Loading

0 comments on commit 363c0e1

Please sign in to comment.