From cdd3cf7ed4a34d9b01065ffef41988c1aefc663c Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 27 Aug 2024 08:47:27 -0700 Subject: [PATCH] [ObjC] Make debug runtime tell developers to update their generated code. Migrate some internals of the library off the older apis. Also mark some of the old apis as deprecated, but the old generated code suppressed warnings broadly to support protobuf deprecations. PiperOrigin-RevId: 668003974 --- objectivec/GPBDescriptor_PackagePrivate.h | 20 ++++++++++++---- objectivec/GPBMessage.m | 23 ++++++++++--------- objectivec/GPBUtilities.m | 8 +++++++ objectivec/Tests/GPBMessageTests+ClassNames.m | 5 ++++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/objectivec/GPBDescriptor_PackagePrivate.h b/objectivec/GPBDescriptor_PackagePrivate.h index 07c83dc061e33..3c431db10b6d8 100644 --- a/objectivec/GPBDescriptor_PackagePrivate.h +++ b/objectivec/GPBDescriptor_PackagePrivate.h @@ -215,14 +215,18 @@ typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) { fields:(void *)fieldDescriptions fieldCount:(uint32_t)fieldCount storageSize:(uint32_t)storageSize - flags:(GPBDescriptorInitializationFlags)flags; + 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; + 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; - (void)setupMessageClassNameSuffix:(NSString *)suffix; @@ -282,13 +286,17 @@ typedef NS_OPTIONS(uint32_t, GPBEnumDescriptorInitializationFlags) { valueNames:(const char *)valueNames values:(const int32_t *)values count:(uint32_t)valueCount - enumVerifier:(GPBEnumValidationFunc)enumVerifier; + 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; + 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 () { @@ -307,7 +315,9 @@ typedef NS_OPTIONS(uint32_t, GPBEnumDescriptorInitializationFlags) { - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc usesClassRefs:(BOOL)usesClassRefs; // Deprecated. Calls above with `usesClassRefs = NO` -- (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc; +- (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 diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index 591ac47bd6884..4b2998fad8f9e 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -1196,18 +1196,19 @@ + (instancetype)alloc { + (GPBDescriptor *)descriptor { // This is thread safe because it is called from +initialize. static GPBDescriptor *descriptor = NULL; - static GPBFileDescriptor *fileDescriptor = NULL; + static GPBFileDescription fileDescription = { + .package = "internal", .prefix = "", .syntax = GPBFileSyntaxProto2}; if (!descriptor) { - fileDescriptor = [[GPBFileDescriptor alloc] initWithPackage:@"internal" - syntax:GPBFileSyntaxProto2]; - - descriptor = [GPBDescriptor allocDescriptorForClass:[GPBMessage class] - rootClass:Nil - file:fileDescriptor - fields:NULL - fieldCount:0 - storageSize:0 - flags:0]; + descriptor = [GPBDescriptor + allocDescriptorForClass:[GPBMessage class] + messageName:@"GPBMessage" + fileDescription:&fileDescription + fields:NULL + fieldCount:0 + storageSize:0 + flags:(GPBDescriptorInitializationFlag_UsesClassRefs | + GPBDescriptorInitializationFlag_Proto3OptionalKnown | + GPBDescriptorInitializationFlag_ClosedEnumSupportKnown)]; } return descriptor; } diff --git a/objectivec/GPBUtilities.m b/objectivec/GPBUtilities.m index 0764c96910595..dc6e871aa9908 100644 --- a/objectivec/GPBUtilities.m +++ b/objectivec/GPBUtilities.m @@ -206,6 +206,14 @@ void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion) { @" supports back to %d!", objcRuntimeVersion, GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION]; } +#if defined(DEBUG) && DEBUG + if (objcRuntimeVersion < GOOGLE_PROTOBUF_OBJC_VERSION) { + // This is a version we haven't generated for yet. + NSLog(@"WARNING: Code from generated Objective-C proto from an older version of the library is " + @"being used. Please regenerate with the current version as the code will stop working " + @"in a future release."); + } +#endif } void GPBRuntimeMatchFailure(void) { diff --git a/objectivec/Tests/GPBMessageTests+ClassNames.m b/objectivec/Tests/GPBMessageTests+ClassNames.m index 3ad5e7167f23a..010fd419eed38 100644 --- a/objectivec/Tests/GPBMessageTests+ClassNames.m +++ b/objectivec/Tests/GPBMessageTests+ClassNames.m @@ -14,6 +14,9 @@ #import "GPBMessage.h" #import "GPBRootObject_PackagePrivate.h" +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + // Support classes for tests using old class name (vs classrefs) interfaces. GPB_FINAL @interface MessageLackingClazzRoot : GPBRootObject @end @@ -107,6 +110,8 @@ + (GPBExtensionRegistry *)extensionRegistry { } @end +#pragma clang diagnostic pop + @interface MessageClassNameTests : GPBTestCase @end