Remove codegen references to unused_field_stripping.

PiperOrigin-RevId: 499585080
pull/11460/head
Chris Kennelly 2 years ago committed by Copybara-Service
parent d5fd41d2d4
commit 46058d2c2d
  1. 2
      src/google/protobuf/compiler/cpp/field_generators/map_field.cc
  2. 80
      src/google/protobuf/compiler/cpp/field_generators/message_field.cc
  3. 6
      src/google/protobuf/compiler/cpp/helpers.h
  4. 90
      src/google/protobuf/compiler/cpp/message.cc
  5. 1
      src/google/protobuf/compiler/cpp/options.h
  6. 2
      src/google/protobuf/compiler/cpp/parse_function_generator.cc

@ -331,8 +331,6 @@ void MapFieldGenerator::GenerateAggregateInitializer(
}
void MapFieldGenerator::GenerateDestructorCode(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (ShouldSplit(descriptor_, options_)) {
format("$cached_split_ptr$->$name$_.Destruct();\n");

@ -199,41 +199,18 @@ void MessageFieldGenerator::GeneratePrivateMembers(io::Printer* printer) const {
void MessageFieldGenerator::GenerateAccessorDeclarations(
io::Printer* printer) const {
Formatter format(printer, variables_);
if (IsFieldStripped(descriptor_, options_)) {
format(
"$deprecated_attr$const $type$& ${1$$name$$}$() const { "
"__builtin_trap(); }\n"
"PROTOBUF_NODISCARD $deprecated_attr$$type$* "
"${1$$release_name$$}$() { "
"__builtin_trap(); }\n"
"$deprecated_attr$$type$* ${1$mutable_$name$$}$() { "
"__builtin_trap(); }\n"
"$deprecated_attr$void ${1$set_allocated_$name$$}$"
"($type$* $name$) { __builtin_trap(); }\n"
"$deprecated_attr$void "
"${1$unsafe_arena_set_allocated_$name$$}$(\n"
" $type$* $name$) { __builtin_trap(); }\n"
"$deprecated_attr$$type$* ${1$unsafe_arena_release_$name$$}$() { "
"__builtin_trap(); }\n",
descriptor_);
return;
}
format(
"$deprecated_attr$const $type$& ${1$$name$$}$() const;\n"
"PROTOBUF_NODISCARD $deprecated_attr$$type$* "
"${1$$release_name$$}$();\n"
"$deprecated_attr$$type$* ${1$mutable_$name$$}$();\n"
"$deprecated_attr$void ${1$set_allocated_$name$$}$"
"($type$* $name$);\n",
descriptor_);
if (!IsFieldStripped(descriptor_, options_)) {
format(
"($type$* $name$);\n"
"private:\n"
"const $type$& ${1$_internal_$name$$}$() const;\n"
"$type$* ${1$_internal_mutable_$name$$}$();\n"
"public:\n",
descriptor_);
}
format(
"$deprecated_attr$void "
"${1$unsafe_arena_set_allocated_$name$$}$(\n"
@ -461,8 +438,6 @@ void MessageFieldGenerator::GenerateInternalAccessorDefinitions(
}
void MessageFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (!internal::cpp::HasHasbit(descriptor_)) {
// If we don't have has-bits, message presence is indicated only by ptr !=
@ -479,8 +454,6 @@ void MessageFieldGenerator::GenerateClearingCode(io::Printer* printer) const {
void MessageFieldGenerator::GenerateMessageClearingCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (!internal::cpp::HasHasbit(descriptor_)) {
// If we don't have has-bits, message presence is indicated only by ptr !=
@ -498,8 +471,6 @@ void MessageFieldGenerator::GenerateMessageClearingCode(
}
void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (implicit_weak_field_) {
format(
@ -513,15 +484,11 @@ void MessageFieldGenerator::GenerateMergingCode(io::Printer* printer) const {
}
void MessageFieldGenerator::GenerateSwappingCode(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format("swap($field$, other->$field$);\n");
}
void MessageFieldGenerator::GenerateDestructorCode(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (options_.opensource_runtime) {
// TODO(gerbens) Remove this when we don't need to destruct default
@ -542,8 +509,6 @@ using internal::cpp::HasHasbit;
void MessageFieldGenerator::GenerateCopyConstructorCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (HasHasbit(descriptor_)) {
format(
@ -560,8 +525,6 @@ void MessageFieldGenerator::GenerateCopyConstructorCode(
void MessageFieldGenerator::GenerateSerializeWithCachedSizesToArray(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
format(
@ -578,8 +541,6 @@ void MessageFieldGenerator::GenerateSerializeWithCachedSizesToArray(
}
void MessageFieldGenerator::GenerateByteSize(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format(
"total_size += $tag_size$ +\n"
@ -588,8 +549,6 @@ void MessageFieldGenerator::GenerateByteSize(io::Printer* printer) const {
}
void MessageFieldGenerator::GenerateIsInitialized(io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
if (!has_required_fields_) return;
Formatter format(printer, variables_);
@ -768,8 +727,6 @@ void MessageOneofFieldGenerator::GenerateInlineAccessorDefinitions(
void MessageOneofFieldGenerator::GenerateClearingCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format(
"if (GetArenaForAllocation() == nullptr) {\n"
@ -836,34 +793,15 @@ void RepeatedMessageFieldGenerator::GeneratePrivateMembers(
void RepeatedMessageFieldGenerator::GenerateAccessorDeclarations(
io::Printer* printer) const {
Formatter format(printer, variables_);
if (IsFieldStripped(descriptor_, options_)) {
format(
"$deprecated_attr$$type$* ${1$mutable_$name$$}$(int index) { "
"__builtin_trap(); }\n"
"$deprecated_attr$::$proto_ns$::RepeatedPtrField< $type$ >*\n"
" ${1$mutable_$name$$}$() { __builtin_trap(); }\n"
"$deprecated_attr$const $type$& ${1$$name$$}$(int index) const { "
"__builtin_trap(); }\n"
"$deprecated_attr$$type$* ${1$add_$name$$}$() { "
"__builtin_trap(); }\n"
"$deprecated_attr$const ::$proto_ns$::RepeatedPtrField< $type$ >&\n"
" ${1$$name$$}$() const { __builtin_trap(); }\n",
descriptor_);
return;
}
format(
"$deprecated_attr$$type$* ${1$mutable_$name$$}$(int index);\n"
"$deprecated_attr$::$proto_ns$::RepeatedPtrField< $type$ >*\n"
" ${1$mutable_$name$$}$();\n",
descriptor_);
if (!IsFieldStripped(descriptor_, options_)) {
format(
" ${1$mutable_$name$$}$();\n"
"private:\n"
"const $type$& ${1$_internal_$name$$}$(int index) const;\n"
"$type$* ${1$_internal_add_$name$$}$();\n"
"public:\n",
descriptor_);
}
format(
"$deprecated_attr$const $type$& ${1$$name$$}$(int index) const;\n"
"$deprecated_attr$$type$* ${1$add_$name$$}$();\n"
@ -937,24 +875,18 @@ void RepeatedMessageFieldGenerator::GenerateInlineAccessorDefinitions(
void RepeatedMessageFieldGenerator::GenerateClearingCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format("$field$.Clear();\n");
}
void RepeatedMessageFieldGenerator::GenerateMergingCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format("_this->$field$.MergeFrom(from.$field$);\n");
}
void RepeatedMessageFieldGenerator::GenerateSwappingCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format("$field$.InternalSwap(&other->$field$);\n");
}
@ -966,8 +898,6 @@ void RepeatedMessageFieldGenerator::GenerateConstructorCode(
void RepeatedMessageFieldGenerator::GenerateDestructorCode(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (implicit_weak_field_) {
format("$field$.~WeakRepeatedPtrField();\n");
@ -978,8 +908,6 @@ void RepeatedMessageFieldGenerator::GenerateDestructorCode(
void RepeatedMessageFieldGenerator::GenerateSerializeWithCachedSizesToArray(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
if (implicit_weak_field_) {
format(
@ -1023,8 +951,6 @@ void RepeatedMessageFieldGenerator::GenerateSerializeWithCachedSizesToArray(
void RepeatedMessageFieldGenerator::GenerateByteSize(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
Formatter format(printer, variables_);
format(
"total_size += $tag_size$UL * this->_internal_$name$_size();\n"
@ -1036,8 +962,6 @@ void RepeatedMessageFieldGenerator::GenerateByteSize(
void RepeatedMessageFieldGenerator::GenerateIsInitialized(
io::Printer* printer) const {
GOOGLE_ABSL_CHECK(!IsFieldStripped(descriptor_, options_));
if (!has_required_fields_) return;
Formatter format(printer, variables_);

@ -408,12 +408,6 @@ inline bool IsFieldUsed(const FieldDescriptor* /* field */,
return true;
}
// Returns true if "field" is stripped.
inline bool IsFieldStripped(const FieldDescriptor* /*field*/,
const Options& /*options*/) {
return false;
}
// Does the file contain any definitions that need extension_set.h?
bool HasExtensionsOrExtendableMessage(const FileDescriptor* file);

@ -548,10 +548,6 @@ MessageGenerator::MessageGenerator(
// Compute optimized field order to be used for layout and initialization
// purposes.
for (auto field : FieldRange(descriptor_)) {
if (IsFieldStripped(field, options_)) {
continue;
}
if (IsWeak(field, options_)) {
++num_weak_fields_;
continue;
@ -653,8 +649,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
optimized_order_.end());
for (auto field : FieldRange(descriptor_)) {
if (!field->real_containing_oneof() && !field->options().weak() &&
!IsFieldStripped(field, options_)) {
if (!field->real_containing_oneof() && !field->options().weak()) {
continue;
}
ordered_fields.push_back(field);
@ -681,17 +676,14 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
)cc");
}
for (auto field : ordered_fields) {
bool stripped = IsFieldStripped(field, options_);
auto name = FieldName(field);
auto v = p->WithVars(FieldVars(field, options_));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
p->Emit(
{{"field_comment", FieldComment(field)},
Sub("const_impl", !stripped ? "const;" : "const { __builtin_trap(); }")
.WithSuffix(";"),
Sub("impl", !stripped ? ";" : " { __builtin_trap(); }")
.WithSuffix(";"),
Sub("const_impl", "const;").WithSuffix(";"),
Sub("impl", ";").WithSuffix(";"),
{"sizer",
[&] {
if (!field->is_repeated()) return;
@ -701,7 +693,6 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
$deprecated_attr $int $name_size$() $const_impl$;
)cc");
if (stripped) return;
p->Emit({Sub("_internal_name_size",
absl::StrCat("_internal_", name, "_size"))
.AnnotatedAs(field)},
@ -723,8 +714,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) {
}},
{"internal_hazzer",
[&] {
if (field->is_repeated() || !HasInternalHasMethod(field) ||
stripped) {
if (field->is_repeated() || !HasInternalHasMethod(field)) {
return;
}
p->Emit(
@ -969,12 +959,6 @@ void MessageGenerator::GenerateSingularFieldHasBits(
const FieldDescriptor* field, io::Printer* p) {
auto t = p->WithVars(MakeTrackerCalls(field, options_));
Formatter format(p);
if (IsFieldStripped(field, options_)) {
format(
"inline bool $classname$::has_$name$() const { "
"__builtin_trap(); }\n");
return;
}
if (field->options().weak()) {
format(
"inline bool $classname$::has_$name$() const {\n"
@ -1049,17 +1033,6 @@ void MessageGenerator::GenerateOneofMemberHasBits(const FieldDescriptor* field,
auto v = p->WithVars(OneofFieldVars(field));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
Formatter format(p);
if (IsFieldStripped(field, options_)) {
if (HasHasMethod(field)) {
format(
"inline bool $classname$::has_$name$() const { "
"__builtin_trap(); }\n");
}
format(
"inline void $classname$::set_has_$name$() { __builtin_trap(); "
"}\n");
return;
}
// Singular field in a oneof
// N.B.: Without field presence, we do not use has-bits or generate
// has_$name$() methods, but oneofs still have set_has_$name$().
@ -1089,10 +1062,6 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field,
bool is_inline, io::Printer* p) {
auto t = p->WithVars(MakeTrackerCalls(field, options_));
Formatter format(p);
if (IsFieldStripped(field, options_)) {
format("void $classname$::clear_$name$() { __builtin_trap(); }\n");
return;
}
// Generate clear_$name$().
if (is_inline) {
@ -1136,19 +1105,10 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
for (auto field : FieldRange(descriptor_)) {
PrintFieldComment(format, field);
if (IsFieldStripped(field, options_)) {
continue;
}
auto v = p->WithVars(FieldVars(field, options_));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
// Generate has_$name$() or $name$_size().
if (field->is_repeated()) {
if (IsFieldStripped(field, options_)) {
format(
"inline int $classname$::$name$_size() const { "
"__builtin_trap(); }\n");
} else {
format(
"inline int $classname$::_internal_$name$_size() const {\n"
" return $field$$1$.size();\n"
@ -1161,7 +1121,6 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
field->message_type()
? ".weak"
: "");
}
} else if (field->real_containing_oneof()) {
GenerateOneofMemberHasBits(field, p);
} else {
@ -1173,9 +1132,7 @@ void MessageGenerator::GenerateFieldAccessorDefinitions(io::Printer* p) {
GenerateFieldClear(field, true, p);
}
// Generate type-specific accessors.
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateInlineAccessorDefinitions(p);
}
format("\n");
}
@ -1827,18 +1784,14 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
" ::$proto_ns$::internal::ConstantInitialized _constinit_;\n",
camel_oneof_name);
for (auto field : FieldRange(oneof)) {
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GeneratePrivateMembers(p);
}
}
format.Outdent();
format("} $1$_;\n", oneof->name());
for (auto field : FieldRange(oneof)) {
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateStaticMembers(p);
}
}
}
// Members assumed to align to 4 bytes:
@ -2003,9 +1956,6 @@ void MessageGenerator::GenerateClassMethods(io::Printer* p) {
for (auto field : FieldRange(descriptor_)) {
auto t = p->WithVars(MakeTrackerCalls(field, options_));
field_generators_.get(field).GenerateInternalAccessorDeclarations(p);
if (IsFieldStripped(field, options_)) {
continue;
}
if (HasHasbit(field)) {
int has_bit_index = HasBitIndex(field);
GOOGLE_ABSL_CHECK_NE(has_bit_index, kNoHasbit) << field->full_name();
@ -2029,17 +1979,11 @@ void MessageGenerator::GenerateClassMethods(io::Printer* p) {
format.Outdent();
format("};\n\n");
for (auto field : FieldRange(descriptor_)) {
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateInternalAccessorDefinitions(p);
}
}
// Generate non-inline field definitions.
for (auto field : FieldRange(descriptor_)) {
if (IsFieldStripped(field, options_)) {
continue;
}
auto v = p->WithVars(FieldVars(field, options_));
auto t = p->WithVars(MakeTrackerCalls(field, options_));
field_generators_.get(field).GenerateNonInlineAccessorDefinitions(p);
@ -2188,10 +2132,6 @@ std::pair<size_t, size_t> MessageGenerator::GenerateOffsets(io::Printer* p) {
descriptor_->real_oneof_decl_count();
size_t entries = offsets;
for (auto field : FieldRange(descriptor_)) {
if (IsFieldStripped(field, options_)) {
format("~0u, // stripped\n");
continue;
}
// TODO(sbenza): We should not have an entry in the offset table for fields
// that do not use them.
if (field->options().weak() || field->real_containing_oneof()) {
@ -2301,7 +2241,6 @@ void MessageGenerator::GenerateSharedConstructorCode(io::Printer* p) {
// Initialize member variables with arena constructor.
for (auto field : optimized_order_) {
GOOGLE_ABSL_DCHECK(!IsFieldStripped(field, options_));
if (ShouldSplit(field, options_)) {
continue;
}
@ -2468,7 +2407,6 @@ ArenaDtorNeeds MessageGenerator::NeedsArenaDestructor() const {
if (HasSimpleBaseClass(descriptor_, options_)) return ArenaDtorNeeds::kNone;
ArenaDtorNeeds needs = ArenaDtorNeeds::kNone;
for (const auto* field : FieldRange(descriptor_)) {
if (IsFieldStripped(field, options_)) continue;
needs =
std::max(needs, field_generators_.get(field).NeedsArenaDestructor());
}
@ -2493,16 +2431,14 @@ void MessageGenerator::GenerateArenaDestructorCode(io::Printer* p) {
// Process non-oneof fields first.
for (auto field : optimized_order_) {
if (IsFieldStripped(field, options_) || ShouldSplit(field, options_))
continue;
if (ShouldSplit(field, options_)) continue;
field_generators_.get(field).GenerateArenaDestructorCode(p);
}
if (ShouldSplit(descriptor_, options_)) {
format("if (!_this->IsSplitMessageDefault()) {\n");
format.Indent();
for (auto field : optimized_order_) {
if (IsFieldStripped(field, options_) || !ShouldSplit(field, options_))
continue;
if (!ShouldSplit(field, options_)) continue;
field_generators_.get(field).GenerateArenaDestructorCode(p);
}
format.Outdent();
@ -2512,7 +2448,6 @@ void MessageGenerator::GenerateArenaDestructorCode(io::Printer* p) {
// Process oneof fields.
for (auto oneof : OneOfRange(descriptor_)) {
for (auto field : FieldRange(oneof)) {
if (IsFieldStripped(field, options_)) continue;
field_generators_.get(field).GenerateArenaDestructorCode(p);
}
}
@ -2841,9 +2776,7 @@ void MessageGenerator::GenerateStructors(io::Printer* p) {
for (auto field : FieldRange(oneof)) {
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateMergingCode(p);
}
format("break;\n");
format.Outdent();
format("}\n");
@ -3133,7 +3066,7 @@ void MessageGenerator::GenerateOneofClear(io::Printer* p) {
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
// We clear only allocated objects in oneofs
if (!IsStringOrMessage(field) || IsFieldStripped(field, options_)) {
if (!IsStringOrMessage(field)) {
format("// No need to clear\n");
} else {
field_generators_.get(field).GenerateClearingCode(p);
@ -3468,9 +3401,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {
for (auto field : FieldRange(oneof)) {
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateMergingCode(p);
}
format("break;\n");
format.Outdent();
format("}\n");
@ -3875,9 +3806,6 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {
(i < descriptor_->field_count() &&
ordered_fields[i]->number() < sorted_extensions[j]->start)) {
const FieldDescriptor* field = ordered_fields[i++];
if (IsFieldStripped(field, options_)) {
continue;
}
re.Flush();
if (field->options().weak()) {
largest_weak_field.ReplaceIfLarger(field);
@ -4217,9 +4145,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
PrintFieldComment(format, field);
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateByteSize(p);
}
format("break;\n");
format.Outdent();
format("}\n");
@ -4316,9 +4242,7 @@ void MessageGenerator::GenerateIsInitialized(io::Printer* p) {
for (auto field : FieldRange(oneof)) {
format("case k$1$: {\n", UnderscoresToCamelCase(field->name(), true));
format.Indent();
if (!IsFieldStripped(field, options_)) {
field_generators_.get(field).GenerateIsInitialized(p);
}
format("break;\n");
format.Outdent();
format("}\n");

@ -81,7 +81,6 @@ struct Options {
bool bootstrap = false;
bool opensource_runtime = false;
bool annotate_accessor = false;
bool unused_field_stripping = false;
bool unverified_lazy_message_sets = false;
bool profile_driven_inline_string = true;
bool force_split = false;

@ -69,10 +69,8 @@ std::vector<const FieldDescriptor*> GetOrderedFields(
const Descriptor* descriptor, const Options& options) {
std::vector<const FieldDescriptor*> ordered_fields;
for (auto field : FieldRange(descriptor)) {
if (!IsFieldStripped(field, options)) {
ordered_fields.push_back(field);
}
}
std::sort(ordered_fields.begin(), ordered_fields.end(),
[](const FieldDescriptor* a, const FieldDescriptor* b) {
return a->number() < b->number();

Loading…
Cancel
Save