Skip to content

Commit

Permalink
[ObjC] Breaking Change: Remove support for older generated code.
Browse files Browse the repository at this point in the history
Remove runtime methods that support the Objective-C gencode from before the 3.22.x release.

PiperOrigin-RevId: 684462445
  • Loading branch information
thomasvl authored and copybara-github committed Oct 10, 2024
1 parent 09ac8ca commit cffa590
Show file tree
Hide file tree
Showing 10 changed files with 5 additions and 487 deletions.
8 changes: 1 addition & 7 deletions objectivec/GPBBootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,4 @@

// Minimum runtime version supported for compiling/running against.
// - Gets changed when support for the older generated code is dropped.
#define GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION 30001

// This is a legacy constant now frozen in time for old generated code. If
// GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION ever gets moved above 30001 then
// this should also change to break code compiled with an old runtime that
// can't be supported any more.
#define GOOGLE_PROTOBUF_OBJC_GEN_VERSION 30001
#define GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION 30007
241 changes: 4 additions & 237 deletions objectivec/GPBDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ - (instancetype)initWithName:(NSString *)name
// The addresses of these variables are used as keys for objc_getAssociatedObject.
static const char kTextFormatExtraValueKey = 0;
static const char kParentClassValueKey = 0;
static const char kClassNameSuffixKey = 0;
static const char kFileDescriptorCacheKey = 0;

static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageFields)
Expand Down Expand Up @@ -156,114 +155,6 @@ + (instancetype)allocDescriptorForClass:(Class)messageClass
return descriptor;
}

+ (instancetype)allocDescriptorForClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30006,
time_to_remove_this_old_version_shim);

BOOL fixClassRefs = (flags & GPBDescriptorInitializationFlag_UsesClassRefs) == 0;
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30003,
time_to_remove_non_class_ref_support);

BOOL fixProto3Optional = (flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) == 0;
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30004,
time_to_remove_proto3_optional_fallback);

BOOL fixClosedEnums = (flags & GPBDescriptorInitializationFlag_ClosedEnumSupportKnown) == 0;
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30005,
time_to_remove_closed_enum_fallback);

if (fixClassRefs || fixProto3Optional || fixClosedEnums) {
BOOL fieldsIncludeDefault = (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
GPBFileSyntax fileSyntax = file.syntax;
#pragma clang diagnostic pop

for (uint32_t i = 0; i < fieldCount; ++i) {
GPBMessageFieldDescription *coreDesc;
if (fieldsIncludeDefault) {
coreDesc = &((((GPBMessageFieldDescriptionWithDefault *)fieldDescriptions)[i]).core);
} else {
coreDesc = &(((GPBMessageFieldDescription *)fieldDescriptions)[i]);
}

if (fixClassRefs && GPBDataTypeIsMessage(coreDesc->dataType)) {
const char *className = coreDesc->dataTypeSpecific.className;
Class msgClass = objc_getClass(className);
NSAssert(msgClass, @"Class %s not defined", className);
coreDesc->dataTypeSpecific.clazz = msgClass;
}

if (fixProto3Optional) {
// If it was...
// - proto3 syntax
// - not repeated/map
// - not in a oneof (negative has index)
// - not a message (the flag doesn't make sense for messages)
BOOL clearOnZero = ((fileSyntax == GPBFileSyntaxProto3) &&
((coreDesc->flags & (GPBFieldRepeated | GPBFieldMapKeyMask)) == 0) &&
(coreDesc->hasIndex >= 0) && !GPBDataTypeIsMessage(coreDesc->dataType));
if (clearOnZero) {
coreDesc->flags |= GPBFieldClearHasIvarOnZero;
}
}

if (fixClosedEnums) {
// NOTE: This isn't correct, it is using the syntax of the file that
// declared the field, not the syntax of the file that declared the
// enum; but for older generated code, that's all we have and that happens
// to be what the runtime was doing (even though it was wrong). This is
// only wrong in the rare cases an enum is declared in a proto3 syntax
// file but used for a field in the proto2 syntax file.
BOOL isClosedEnum =
(coreDesc->dataType == GPBDataTypeEnum && fileSyntax == GPBFileSyntaxProto2);
if (isClosedEnum) {
coreDesc->flags |= GPBFieldClosedEnum;
}
}
}
flags |= (GPBDescriptorInitializationFlag_UsesClassRefs |
GPBDescriptorInitializationFlag_Proto3OptionalKnown |
GPBDescriptorInitializationFlag_ClosedEnumSupportKnown);
}

GPBDescriptor *result = [self allocDescriptorForClass:messageClass
messageName:nil
fileDescription:NULL
fields:fieldDescriptions
fieldCount:fieldCount
storageSize:storageSize
flags:flags];
objc_setAssociatedObject(result, &kFileDescriptorCacheKey, file,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return result;
}

+ (instancetype)allocDescriptorForClass:(Class)messageClass
rootClass:(__unused Class)rootClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30006,
time_to_remove_this_old_version_shim);
// The rootClass is no longer used, but it is passed as [ROOT class] to
// ensure it was started up during initialization also when the message
// scopes extensions.
return [self allocDescriptorForClass:messageClass
file:file
fields:fieldDescriptions
fieldCount:fieldCount
storageSize:storageSize
flags:flags];
}

- (instancetype)initWithClass:(Class)messageClass
messageName:(NSString *)messageName
fileDescription:(GPBFileDescription *)fileDescription
Expand Down Expand Up @@ -339,25 +230,6 @@ - (void)setupContainingMessageClass:(Class)messageClass {
objc_setAssociatedObject(self, &kParentClassValueKey, messageClass, OBJC_ASSOCIATION_ASSIGN);
}

- (void)setupContainingMessageClassName:(const char *)msgClassName {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30003,
time_to_remove_this_old_version_shim);
// Note: Only fetch the class here, can't send messages to it because
// that could cause cycles back to this class within +initialize if
// two messages have each other in fields (i.e. - they build a graph).
Class clazz = objc_getClass(msgClassName);
NSAssert(clazz, @"Class %s not defined", msgClassName);
[self setupContainingMessageClass:clazz];
}

- (void)setupMessageClassNameSuffix:(NSString *)suffix {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30007,
time_to_remove_this_old_version_shim);
if (suffix.length) {
objc_setAssociatedObject(self, &kClassNameSuffixKey, suffix, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}

- (NSString *)name {
return NSStringFromClass(messageClass_);
}
Expand Down Expand Up @@ -404,62 +276,10 @@ - (NSString *)fullName {
return messageName_;
}

GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30007,
time_to_remove_this_old_approach);
// NOTE: When this code path is removed, this also means this api can't return nil any more but
// that would be a breaking code change (not longer a Swift optional), so changing that will be
// harder.

NSString *className = NSStringFromClass(self.messageClass);
GPBFileDescriptor *file = self.file;
NSString *objcPrefix = file.objcPrefix;
if (objcPrefix && ![className hasPrefix:objcPrefix]) {
NSAssert(0, @"Class didn't have correct prefix? (%@ - %@)", className, objcPrefix);
return nil;
}

NSString *name = nil;
if (parent) {
NSString *parentClassName = NSStringFromClass(parent.messageClass);
// The generator will add _Class to avoid reserved words, drop it.
NSString *suffix = objc_getAssociatedObject(parent, &kClassNameSuffixKey);
if (suffix) {
if (![parentClassName hasSuffix:suffix]) {
NSAssert(0, @"ParentMessage class didn't have correct suffix? (%@ - %@)", className,
suffix);
return nil;
}
parentClassName = [parentClassName substringToIndex:(parentClassName.length - suffix.length)];
}
NSString *parentPrefix = [parentClassName stringByAppendingString:@"_"];
if (![className hasPrefix:parentPrefix]) {
NSAssert(0, @"Class didn't have the correct parent name prefix? (%@ - %@)", parentPrefix,
className);
return nil;
}
name = [className substringFromIndex:parentPrefix.length];
} else {
name = [className substringFromIndex:objcPrefix.length];
}

// The generator will add _Class to avoid reserved words, drop it.
NSString *suffix = objc_getAssociatedObject(self, &kClassNameSuffixKey);
if (suffix) {
if (![name hasSuffix:suffix]) {
NSAssert(0, @"Message class didn't have correct suffix? (%@ - %@)", name, suffix);
return nil;
}
name = [name substringToIndex:(name.length - suffix.length)];
}

NSString *prefix = (parent != nil ? parent.fullName : file.package);
NSString *result;
if (prefix.length > 0) {
result = [NSString stringWithFormat:@"%@.%@", prefix, name];
} else {
result = name;
}
return result;
#if defined(DEBUG) && DEBUG
NSAssert(NO, @"Missing messageName_");
#endif
return nil;
}

- (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber {
Expand Down Expand Up @@ -927,38 +747,6 @@ + (instancetype)allocDescriptorForName:(NSString *)name
return descriptor;
}

+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30005,
time_to_remove_this_old_version_shim);
return [self allocDescriptorForName:name
valueNames:valueNames
values:values
count:valueCount
enumVerifier:enumVerifier
flags:GPBEnumDescriptorInitializationFlag_None];
}

+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30005,
time_to_remove_this_old_version_shim);
return [self allocDescriptorForName:name
valueNames:valueNames
values:values
count:valueCount
enumVerifier:enumVerifier
flags:GPBEnumDescriptorInitializationFlag_None
extraTextFormatInfo:extraTextFormatInfo];
}

- (instancetype)initWithName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
Expand Down Expand Up @@ -1186,27 +974,6 @@ - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
return self;
}

- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc {
GPBInternalCompileAssert(GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION <= 30003,
time_to_remove_this_old_version_shim);

const char *className = desc->messageOrGroupClass.name;
if (className) {
Class clazz = objc_lookUpClass(className);
NSAssert(clazz != Nil, @"Class %s not defined", className);
desc->messageOrGroupClass.clazz = clazz;
}

const char *extendedClassName = desc->extendedClass.name;
if (extendedClassName) {
Class clazz = objc_lookUpClass(extendedClassName);
NSAssert(clazz, @"Class %s not defined", extendedClassName);
desc->extendedClass.clazz = clazz;
}

return [self initWithExtensionDescription:desc usesClassRefs:YES];
}

- (void)dealloc {
if ((description_->dataType == GPBDataTypeBytes) && !GPBExtensionIsRepeated(description_)) {
[defaultValue_.valueData release];
Expand Down
45 changes: 0 additions & 45 deletions objectivec/GPBDescriptor_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,6 @@ typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
- (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
- (void)setupContainingMessageClass:(Class)msgClass;

// Deprecated, these remain to support older versions of source generation.
+ (instancetype)allocDescriptorForClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));
+ (instancetype)allocDescriptorForClass:(Class)messageClass
rootClass:(Class)rootClass
file:(GPBFileDescriptor *)file
fields:(void *)fieldDescriptions
fieldCount:(uint32_t)fieldCount
storageSize:(uint32_t)storageSize
flags:(GPBDescriptorInitializationFlags)flags
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));
- (void)setupContainingMessageClassName:(const char *)msgClassName
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));
- (void)setupMessageClassNameSuffix:(NSString *)suffix
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));

@end

@interface GPBFileDescriptor ()
Expand Down Expand Up @@ -285,22 +260,6 @@ typedef NS_OPTIONS(uint32_t, GPBEnumDescriptorInitializationFlags) {
flags:(GPBEnumDescriptorInitializationFlags)flags
extraTextFormatInfo:(const char *)extraTextFormatInfo;

// Deprecated, these remain to support older versions of source generation.
+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));
+ (instancetype)allocDescriptorForName:(NSString *)name
valueNames:(const char *)valueNames
values:(const int32_t *)values
count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));
@end

@interface GPBExtensionDescriptor () {
Expand All @@ -318,10 +277,6 @@ typedef NS_OPTIONS(uint32_t, GPBEnumDescriptorInitializationFlags) {
// description has to be long lived, it is held as a raw pointer.
- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
usesClassRefs:(BOOL)usesClassRefs;
// Deprecated. Calls above with `usesClassRefs = NO`
- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
__attribute__((deprecated("Please use a newer version of protoc to regenerate your sources. "
"Support for this version will go away in the future.")));

- (NSComparisonResult)compareByFieldNumber:(GPBExtensionDescriptor *)other;
@end
Expand Down
6 changes: 0 additions & 6 deletions objectivec/GPBMessage_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@

#import "GPBMessage.h"

// TODO: Remove this import. Older generated code use the OSAtomic* apis,
// so anyone that hasn't regenerated says building by having this. After
// enough time has passed, this likely can be removed as folks should have
// regenerated.
#import <libkern/OSAtomic.h>

#import "GPBBootstrap.h"

typedef struct GPBMessage_Storage {
Expand Down
Loading

0 comments on commit cffa590

Please sign in to comment.