Refactor implicit field presence code generation in GenerateByteSize.

ShouldEmitIfNonDefaultCheck is mostly called in MayEmitIfNonDefaultCheck,
except in one location in GenerateByteSize.

Making MayEmitIfNonDefaultCheck the only caller of ShouldEmitIfNonDefaultCheck
seems like a good application of DRY and reduces the possibility of unintended
divergence in the future. It also makes future changes to serialization logic
easier.

There should be no changes to the resulting generated code.

PiperOrigin-RevId: 654789393
pull/17533/head
Tony Liao 4 months ago committed by Copybara-Service
parent 6d25846f47
commit d29246f390
  1. 29
      src/google/protobuf/compiler/cpp/message.cc

@ -227,16 +227,16 @@ bool MayEmitIfNonDefaultCheck(io::Printer* p, const std::string& prefix,
ABSL_CHECK(!HasHasbit(field)); ABSL_CHECK(!HasHasbit(field));
if (!ShouldEmitNonDefaultCheck(field)) return false; if (!ShouldEmitNonDefaultCheck(field)) return false;
// SUBTLE: format_string must be a raw string without newline. // SUBTLE: |format| must be a raw string without newline.
// io::Printer::Emit treats the format string as a "raw string" if it doesn't // io::Printer::Emit treats the format string as a "raw string" if it doesn't
// contain multiple lines. Note that a string of the form "\n($condition$)\n" // contain multiple lines. Note that a string of the form "\n($condition$)\n"
// (i.e. newline characters are present; there is only one non-empty line) // (i.e. newline characters are present; there is only one non-empty line)
// will be treated as a multi-line string. // will still be treated as a multi-line string.
// //
// io::Printer::Emit will print a newline if the input is a multi-line string. // io::Printer::Emit will print a newline if the input is a multi-line string.
absl::string_view format_string = "if ($condition$) "; // In this case, we prefer to let the caller handle if-statement braces.
p->Emit({{"condition", [&] { EmitNonDefaultCheck(p, prefix, field); }}}, p->Emit({{"condition", [&] { EmitNonDefaultCheck(p, prefix, field); }}},
format_string); /*format=*/"if ($condition$)");
return true; return true;
} }
@ -4778,7 +4778,11 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
}}, }},
{"check_if_field_present", {"check_if_field_present",
[&] { [&] {
if (HasHasbit(field)) { if (!HasHasbit(field)) {
MayEmitIfNonDefaultCheck(p, "this_.", field);
return;
}
if (field->options().weak()) { if (field->options().weak()) {
p->Emit("if (has_$name$())"); p->Emit("if (has_$name$())");
return; return;
@ -4786,22 +4790,11 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
int has_bit_index = int has_bit_index =
has_bit_indices_[field->index()]; has_bit_indices_[field->index()];
p->Emit({{"mask", p->Emit(
absl::StrFormat( {{"mask", absl::StrFormat(
"0x%08xu", "0x%08xu",
1u << (has_bit_index % 32))}}, 1u << (has_bit_index % 32))}},
"if (cached_has_bits & $mask$)"); "if (cached_has_bits & $mask$)");
} else if (ShouldEmitNonDefaultCheck(field)) {
// Without field presence: field is
// serialized only if it has a non-default
// value.
p->Emit({{"non_default_check",
[&] {
EmitNonDefaultCheck(p, "this_.",
field);
}}},
"if ($non_default_check$)");
}
}}}, }}},
R"cc( R"cc(
$comment$; $comment$;

Loading…
Cancel
Save