Skip to content

Commit 676018e

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Expand retention stripping API, and improve test coverage
PiperOrigin-RevId: 520526112
1 parent 8c8b2be commit 676018e

File tree

5 files changed

+638
-81
lines changed

5 files changed

+638
-81
lines changed

src/google/protobuf/compiler/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,20 @@ cc_library(
333333
],
334334
)
335335

336+
337+
cc_test(
338+
name = "retention_unittest",
339+
srcs = ["retention_unittest.cc"],
340+
deps = [
341+
":importer",
342+
":retention",
343+
"//src/google/protobuf/io",
344+
"@com_google_googletest//:gtest",
345+
"@com_google_googletest//:gtest_main",
346+
"@com_google_absl//absl/log:die_if_null",
347+
],
348+
)
349+
336350
################################################################################
337351
# Generates protoc release artifacts.
338352
################################################################################

src/google/protobuf/compiler/retention.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,43 @@ FileDescriptorProto StripSourceRetentionOptions(const FileDescriptor& file,
210210
return file_proto;
211211
}
212212

213+
DescriptorProto StripSourceRetentionOptions(const Descriptor& message) {
214+
DescriptorProto message_proto;
215+
message.CopyTo(&message_proto);
216+
ConvertToDynamicMessageAndStripOptions(message_proto,
217+
*message.file()->pool());
218+
return message_proto;
219+
}
220+
221+
DescriptorProto::ExtensionRange StripSourceRetentionOptions(
222+
const Descriptor& message, const Descriptor::ExtensionRange& range) {
223+
DescriptorProto::ExtensionRange range_proto;
224+
range.CopyTo(&range_proto);
225+
ConvertToDynamicMessageAndStripOptions(range_proto, *message.file()->pool());
226+
return range_proto;
227+
}
228+
229+
EnumDescriptorProto StripSourceRetentionOptions(const EnumDescriptor& enm) {
230+
EnumDescriptorProto enm_proto;
231+
enm.CopyTo(&enm_proto);
232+
ConvertToDynamicMessageAndStripOptions(enm_proto, *enm.file()->pool());
233+
return enm_proto;
234+
}
235+
236+
FieldDescriptorProto StripSourceRetentionOptions(const FieldDescriptor& field) {
237+
FieldDescriptorProto field_proto;
238+
field.CopyTo(&field_proto);
239+
ConvertToDynamicMessageAndStripOptions(field_proto, *field.file()->pool());
240+
return field_proto;
241+
}
242+
243+
OneofDescriptorProto StripSourceRetentionOptions(const OneofDescriptor& oneof) {
244+
OneofDescriptorProto oneof_proto;
245+
oneof.CopyTo(&oneof_proto);
246+
ConvertToDynamicMessageAndStripOptions(oneof_proto, *oneof.file()->pool());
247+
return oneof_proto;
248+
}
249+
213250
EnumOptions StripLocalSourceRetentionOptions(const EnumDescriptor& descriptor) {
214251
return StripLocalOptions(descriptor);
215252
}
@@ -232,6 +269,14 @@ MessageOptions StripLocalSourceRetentionOptions(const Descriptor& descriptor) {
232269
return StripLocalOptions(descriptor);
233270
}
234271

272+
ExtensionRangeOptions StripLocalSourceRetentionOptions(
273+
const Descriptor& descriptor, const Descriptor::ExtensionRange& range) {
274+
if (range.options_ == nullptr) return ExtensionRangeOptions{};
275+
ExtensionRangeOptions options = *range.options_;
276+
ConvertToDynamicMessageAndStripOptions(options, GetPool(descriptor));
277+
return options;
278+
}
279+
235280
MethodOptions StripLocalSourceRetentionOptions(
236281
const MethodDescriptor& descriptor) {
237282
return StripLocalOptions(descriptor);

src/google/protobuf/compiler/retention.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ namespace compiler {
4747
// corresponding to source-retention options.
4848
PROTOC_EXPORT FileDescriptorProto StripSourceRetentionOptions(
4949
const FileDescriptor& file, bool include_source_code_info = false);
50+
PROTOC_EXPORT DescriptorProto
51+
StripSourceRetentionOptions(const Descriptor& message);
52+
PROTOC_EXPORT DescriptorProto::ExtensionRange StripSourceRetentionOptions(
53+
const Descriptor& message, const Descriptor::ExtensionRange& range);
54+
PROTOC_EXPORT EnumDescriptorProto
55+
StripSourceRetentionOptions(const EnumDescriptor& enm);
56+
PROTOC_EXPORT FieldDescriptorProto
57+
StripSourceRetentionOptions(const FieldDescriptor& field);
58+
PROTOC_EXPORT OneofDescriptorProto
59+
StripSourceRetentionOptions(const OneofDescriptor& oneof);
5060

5161
// The following functions take a descriptor and strip all source-retention
5262
// options from just the local entity (e.g. message, enum, field). Most code
@@ -63,6 +73,8 @@ PROTOC_EXPORT FileOptions
6373
StripLocalSourceRetentionOptions(const FileDescriptor& descriptor);
6474
PROTOC_EXPORT MessageOptions
6575
StripLocalSourceRetentionOptions(const Descriptor& descriptor);
76+
PROTOC_EXPORT ExtensionRangeOptions StripLocalSourceRetentionOptions(
77+
const Descriptor& descriptor, const Descriptor::ExtensionRange& range);
6678
PROTOC_EXPORT MethodOptions
6779
StripLocalSourceRetentionOptions(const MethodDescriptor& descriptor);
6880
PROTOC_EXPORT OneofOptions

0 commit comments

Comments
 (0)