Assume HasHasbit() for SingularMessage and !HasHasbit() for OneofMessage.

PiperOrigin-RevId: 612347702
pull/16028/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent f7317e02e6
commit 024d90983e
  1. 77
      src/google/protobuf/compiler/cpp/field_generators/message_field.cc

@ -309,42 +309,20 @@ void SingularMessage::GenerateInlineAccessorDefinitions(io::Printer* p) const {
}
void SingularMessage::GenerateClearingCode(io::Printer* p) const {
if (!has_hasbit_) {
// If we don't have has-bits, message presence is indicated only by ptr !=
// nullptr. Thus on clear, we need to delete the object.
p->Emit(
R"cc(
if (GetArena() == nullptr && $field_$ != nullptr) {
delete $field_$;
}
$field_$ = nullptr;
)cc");
} else {
p->Emit(
R"cc(
if ($field_$ != nullptr) $field_$->Clear();
)cc");
}
ABSL_CHECK(has_hasbit_);
p->Emit(
R"cc(
if ($field_$ != nullptr) $field_$->Clear();
)cc");
}
void SingularMessage::GenerateMessageClearingCode(io::Printer* p) const {
if (!has_hasbit_) {
// If we don't have has-bits, message presence is indicated only by ptr !=
// nullptr. Thus on clear, we need to delete the object.
p->Emit(
R"cc(
if (GetArena() == nullptr && $field_$ != nullptr) {
delete $field_$;
}
$field_$ = nullptr;
)cc");
} else {
p->Emit(
R"cc(
$DCHK$($field_$ != nullptr);
$field_$->Clear();
)cc");
}
ABSL_CHECK(has_hasbit_);
p->Emit(
R"cc(
$DCHK$($field_$ != nullptr);
$field_$->Clear();
)cc");
}
bool SingularMessage::RequiresArena(GeneratorFunction function) const {
@ -407,21 +385,13 @@ void SingularMessage::GenerateDestructorCode(io::Printer* p) const {
using internal::cpp::HasHasbit;
void SingularMessage::GenerateCopyConstructorCode(io::Printer* p) const {
if (has_hasbit_) {
p->Emit(R"cc(
if ((from.$has_hasbit$) != 0) {
_this->$field_$ =
$superclass$::CopyConstruct<$Submsg$>(arena, *from.$field_$);
}
)cc");
} else {
p->Emit(R"cc(
if (from._internal_has_$name$()) {
_this->$field_$ =
$superclass$::CopyConstruct<$Submsg$>(arena, *from.$field_$);
}
)cc");
}
ABSL_CHECK(has_hasbit_);
p->Emit(R"cc(
if ((from.$has_hasbit$) != 0) {
_this->$field_$ =
$superclass$::CopyConstruct<$Submsg$>(arena, *from.$field_$);
}
)cc");
}
void SingularMessage::GenerateSerializeWithCachedSizesToArray(
@ -505,6 +475,7 @@ class OneofMessage : public SingularMessage {
void GenerateSwappingCode(io::Printer* p) const override;
void GenerateDestructorCode(io::Printer* p) const override;
void GenerateConstructorCode(io::Printer* p) const override;
void GenerateCopyConstructorCode(io::Printer* p) const override;
void GenerateIsInitialized(io::Printer* p) const override;
void GenerateMergingCode(io::Printer* p) const override;
bool RequiresArena(GeneratorFunction func) const override;
@ -646,6 +617,16 @@ void OneofMessage::GenerateConstructorCode(io::Printer* p) const {
// space only when this field is used.
}
void OneofMessage::GenerateCopyConstructorCode(io::Printer* p) const {
ABSL_CHECK(!has_hasbit_);
p->Emit(R"cc(
if (from._internal_has_$name$()) {
_this->$field_$ =
$superclass$::CopyConstruct<$Submsg$>(arena, *from.$field_$);
}
)cc");
}
void OneofMessage::GenerateIsInitialized(io::Printer* p) const {
if (!has_required_) return;

Loading…
Cancel
Save