Skip to content

Commit

Permalink
[ObjC] Deprecate GPBUnknownFieldSet and related apis.
Browse files Browse the repository at this point in the history
GPBUnknownFields will be going away in the next major release of the ObjC
Protobuf runtime. Code should be updated to make use of GPBUnknownFields
instead.

PiperOrigin-RevId: 659963056
  • Loading branch information
thomasvl committed Aug 12, 2024
1 parent e822dce commit 9f6ee72
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 33 deletions.
3 changes: 3 additions & 0 deletions objectivec/GPBCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ - (void)readGroup:(int32_t)fieldNumber
--state_.recursionDepth;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)readUnknownGroup:(int32_t)fieldNumber message:(GPBUnknownFieldSet *)message {
CheckRecursionLimit(&state_);
++state_.recursionDepth;
Expand All @@ -502,6 +504,7 @@ - (void)readUnknownGroup:(int32_t)fieldNumber message:(GPBUnknownFieldSet *)mess
GPBWireFormatMakeTag(fieldNumber, GPBWireFormatEndGroup));
--state_.recursionDepth;
}
#pragma clang diagnostic pop

- (void)readMessage:(GPBMessage *)message
extensionRegistry:(id<GPBExtensionRegistry>)extensionRegistry {
Expand Down
3 changes: 3 additions & 0 deletions objectivec/GPBCodedInputStream_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ typedef struct GPBCodedInputStreamState {

// Reads a group field value from the stream and merges it into the given
// UnknownFieldSet.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)readUnknownGroup:(int32_t)fieldNumber message:(GPBUnknownFieldSet *)message;
#pragma clang diagnostic pop

// Reads a map entry.
- (void)readMapEntry:(id)mapDictionary
Expand Down
20 changes: 12 additions & 8 deletions objectivec/GPBCodedOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,34 +589,39 @@ __attribute__((objc_subclassing_restricted))
- (void)writeGroupNoTag:(int32_t)fieldNumber
value:(GPBMessage *)value;

//%PDDM-EXPAND-END _WRITE_DECLS()

// clang-format on

/**
* Write a GPBUnknownFieldSet for the given field number.
*
* @param fieldNumber The field number assigned to the value.
* @param value The value to write out.
**/
- (void)writeUnknownGroup:(int32_t)fieldNumber
value:(GPBUnknownFieldSet *)value;
value:(GPBUnknownFieldSet *)value
__attribute__((deprecated("GPBUnknownFieldSet is going away.")));

/**
* Write an array of GPBUnknownFieldSet for the given field number.
*
* @param fieldNumber The field number assigned to the values.
* @param values The values to write out.
**/
- (void)writeUnknownGroupArray:(int32_t)fieldNumber
values:(NSArray<GPBUnknownFieldSet*> *)values;
values:(NSArray<GPBUnknownFieldSet *> *)values
__attribute__((deprecated("GPBUnknownFieldSet is going away.")));

/**
* Write a GPBUnknownFieldSet without any tag (but does write the endGroup tag).
*
* @param fieldNumber The field number assigned to the value.
* @param value The value to write out.
**/
- (void)writeUnknownGroupNoTag:(int32_t)fieldNumber
value:(GPBUnknownFieldSet *)value;

//%PDDM-EXPAND-END _WRITE_DECLS()

// clang-format on
value:(GPBUnknownFieldSet *)value
__attribute__((deprecated("GPBUnknownFieldSet is going away.")));

/**
Write a MessageSet extension field to the stream. For historical reasons,
Expand Down Expand Up @@ -741,6 +746,5 @@ NS_ASSUME_NONNULL_END
//%_WRITE_UNPACKABLE_DECLS(Message, GPBMessage)
//%_WRITE_UNPACKABLE_DECLS(Bytes, NSData)
//%_WRITE_GROUP_DECLS(Group, GPBMessage)
//%_WRITE_GROUP_DECLS(UnknownGroup, GPBUnknownFieldSet)

// clang-format on
23 changes: 18 additions & 5 deletions objectivec/GPBCodedOutputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ - (void)writeGroup:(int32_t)fieldNumber value:(GPBMessage *)value {
[self writeGroupNoTag:fieldNumber value:value];
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

- (void)writeUnknownGroupNoTag:(int32_t)fieldNumber value:(const GPBUnknownFieldSet *)value {
[value writeToCodedOutputStream:self];
GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatEndGroup);
Expand All @@ -362,6 +365,8 @@ - (void)writeUnknownGroup:(int32_t)fieldNumber value:(GPBUnknownFieldSet *)value
[self writeUnknownGroupNoTag:fieldNumber value:value];
}

#pragma clang diagnostic pop

- (void)writeMessageNoTag:(GPBMessage *)value {
GPBWriteRawVarint32(&state_, (int32_t)[value serializedSize]);
[value writeToCodedOutputStream:self];
Expand Down Expand Up @@ -838,18 +843,20 @@ - (void)writeGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
}
}

//%PDDM-EXPAND WRITE_UNPACKABLE_DEFNS(UnknownGroup, GPBUnknownFieldSet)
// This block of code is generated, do not edit it directly.
//%PDDM-EXPAND-END (19 expansions)

// clang-format on

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

- (void)writeUnknownGroupArray:(int32_t)fieldNumber values:(NSArray *)values {
for (GPBUnknownFieldSet *value in values) {
[self writeUnknownGroup:fieldNumber value:value];
}
}

//%PDDM-EXPAND-END (19 expansions)

// clang-format on
#pragma clang diagnostic pop

- (void)writeMessageSetExtension:(int32_t)fieldNumber value:(GPBMessage *)value {
GPBWriteTagWithFormat(&state_, GPBWireFormatMessageSetItem, GPBWireFormatStartGroup);
Expand Down Expand Up @@ -982,7 +989,10 @@ size_t GPBComputeStringSizeNoTag(NSString *value) {

size_t GPBComputeGroupSizeNoTag(GPBMessage *value) { return [value serializedSize]; }

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) { return value.serializedSize; }
#pragma clang diagnostic pop

size_t GPBComputeMessageSizeNoTag(GPBMessage *value) {
size_t size = [value serializedSize];
Expand Down Expand Up @@ -1050,9 +1060,12 @@ size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) {
return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeGroupSizeNoTag(value);
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size_t GPBComputeUnknownGroupSize(int32_t fieldNumber, GPBUnknownFieldSet *value) {
return GPBComputeTagSize(fieldNumber) * 2 + GPBComputeUnknownGroupSizeNoTag(value);
}
#pragma clang diagnostic pop

size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) {
return GPBComputeTagSize(fieldNumber) + GPBComputeMessageSizeNoTag(value);
Expand Down
6 changes: 6 additions & 0 deletions objectivec/GPBCodedOutputStream_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ size_t GPBComputeFixed32Size(int32_t fieldNumber, uint32_t value) __attribute__(
size_t GPBComputeBoolSize(int32_t fieldNumber, BOOL value) __attribute__((const));
size_t GPBComputeStringSize(int32_t fieldNumber, NSString *value) __attribute__((const));
size_t GPBComputeGroupSize(int32_t fieldNumber, GPBMessage *value) __attribute__((const));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size_t GPBComputeUnknownGroupSize(int32_t fieldNumber, GPBUnknownFieldSet *value)
__attribute__((const));
#pragma clang diagnostic pop
size_t GPBComputeMessageSize(int32_t fieldNumber, GPBMessage *value) __attribute__((const));
size_t GPBComputeBytesSize(int32_t fieldNumber, NSData *value) __attribute__((const));
size_t GPBComputeUInt32Size(int32_t fieldNumber, uint32_t value) __attribute__((const));
Expand All @@ -43,7 +46,10 @@ size_t GPBComputeFixed32SizeNoTag(uint32_t value) __attribute__((const));
size_t GPBComputeBoolSizeNoTag(BOOL value) __attribute__((const));
size_t GPBComputeStringSizeNoTag(NSString *value) __attribute__((const));
size_t GPBComputeGroupSizeNoTag(GPBMessage *value) __attribute__((const));
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) __attribute__((const));
#pragma clang diagnostic pop
size_t GPBComputeMessageSizeNoTag(GPBMessage *value) __attribute__((const));
size_t GPBComputeBytesSizeNoTag(NSData *value) __attribute__((const));
size_t GPBComputeUInt32SizeNoTag(int32_t value) __attribute__((const));
Expand Down
8 changes: 5 additions & 3 deletions objectivec/GPBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ CF_EXTERN_C_END
* exist in two places, you don't want a sub message to be a property
* property of two other messages.
*
* @note While the class support NSSecureCoding, if the message has any
* extensions, they will end up reloaded in @c unknownFields as there is
* @note While the class supports NSSecureCoding, if the message has any
* extensions, they will end up reloaded in the unknown fields as there is
* no way for the @c NSCoding plumbing to pass through a
* @c GPBExtensionRegistry. To support extensions, instead of passing the
* calls off to the Message, simple store the result of @c data, and then
Expand All @@ -73,7 +73,9 @@ CF_EXTERN_C_END
/**
* The set of unknown fields for this message.
**/
@property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields;
@property(nonatomic, copy, nullable) GPBUnknownFieldSet *unknownFields __attribute__((
deprecated("Use GPBUnknownFields and the -initFromMessage: initializer and "
"mergeUnknownFields:extensionRegistry:error: to add the data back to a message.")));

/**
* Whether the message, along with all submessages, have the required fields
Expand Down
24 changes: 23 additions & 1 deletion objectivec/GPBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ @interface GPBMessage () {
// be protected with an @synchronized(self) block (that way the code also doesn't have to
// worry about throws).
NSMutableData *unknownFieldData_;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPBUnknownFieldSet *unknownFields_;
#pragma clang diagnostic pop

NSMutableDictionary *extensionMap_;
// Readonly access to autocreatedExtensionMap_ is protected via readOnlyLock_.
Expand Down Expand Up @@ -1106,6 +1109,8 @@ void GPBClearMessageAutocreator(GPBMessage *self) {
self->autocreatorExtension_ = nil;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPB_NOINLINE
static void MergeUnknownFieldDataIntoFieldSet(GPBMessage *self, NSData *data,
GPBUnknownFieldSet *targetSet) {
Expand Down Expand Up @@ -1151,6 +1156,7 @@ static void MergeUnknownFieldDataIntoFieldSet(GPBMessage *self, NSData *data,
[decodeInto release];
}
}
#pragma clang diagnostic pop

@implementation GPBMessage

Expand Down Expand Up @@ -1481,7 +1487,14 @@ - (void)internalClear:(BOOL)zeroStorage {
}

- (void)clearUnknownFields {
self.unknownFields = nil;
[unknownFieldData_ release];
unknownFieldData_ = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[unknownFields_ release];
unknownFields_ = nil;
#pragma clang diagnostic pop
GPBBecomeVisibleToAutocreator(self);
}

- (BOOL)mergeUnknownFields:(GPBUnknownFields *)unknownFields
Expand Down Expand Up @@ -3149,6 +3162,8 @@ - (BOOL)isEqual:(id)other {
// need to guard against is concurrent r/o access, so we can grab the values (and retain them)
// so we have a version to compare against safely incase the second access causes the transform
// between internal states.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPBUnknownFieldSet *selfUnknownFields;
NSData *selfUnknownFieldData;
@synchronized(self) {
Expand All @@ -3161,6 +3176,7 @@ - (BOOL)isEqual:(id)other {
otherUnknownFields = [otherMsg->unknownFields_ retain];
otherUnknownFieldData = [otherMsg->unknownFieldData_ retain];
}
#pragma clang diagnostic pop
#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
if (selfUnknownFields) {
NSAssert(selfUnknownFieldData == nil, @"Internal error both unknown states were set");
Expand Down Expand Up @@ -3189,6 +3205,8 @@ - (BOOL)isEqual:(id)other {
result = [selfUnknownFields isEqual:otherUnknownFields];
} else {
// At this point, we're done to one have a set/nothing, and the other having data/nothing.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPBUnknownFieldSet *theSet = selfUnknownFields ? selfUnknownFields : otherUnknownFields;
NSData *theData = selfUnknownFieldData ? selfUnknownFieldData : otherUnknownFieldData;
if (theSet) {
Expand All @@ -3204,6 +3222,7 @@ - (BOOL)isEqual:(id)other {
// It was a data/nothing and nothing, so they equal if the other didn't have data.
result = theData == nil;
}
#pragma clang diagnostic pop
}

[selfUnknownFields release];
Expand Down Expand Up @@ -4015,7 +4034,10 @@ id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
NSData *GPBMessageUnknownFieldsData(GPBMessage *self) {
NSData *result = nil;
@synchronized(self) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPBUnknownFieldSet *unknownFields = self->unknownFields_;
#pragma clang diagnostic pop
if (unknownFields) {
#if defined(DEBUG) && DEBUG
NSCAssert(self->unknownFieldData_ == nil, @"Internal error both unknown states were set");
Expand Down
Loading

0 comments on commit 9f6ee72

Please sign in to comment.