From e2941fa4e973bfa93049ec8f5d7c9767238f6136 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Wed, 6 Sep 2023 16:34:56 -0400 Subject: [PATCH] Annotate deprecated Objects. --- Tools/ServiceGenerator/SGGenerator.m | 51 ++++++++++++++++++++++++++-- Tools/ServiceGenerator/SGUtils.h | 2 ++ Tools/ServiceGenerator/SGUtils.m | 14 ++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Tools/ServiceGenerator/SGGenerator.m b/Tools/ServiceGenerator/SGGenerator.m index d174ce0fa..bf11d6a3f 100644 --- a/Tools/ServiceGenerator/SGGenerator.m +++ b/Tools/ServiceGenerator/SGGenerator.m @@ -52,6 +52,7 @@ static NSString *kReturnsSchemaParameterKey = @"returnsSchema"; static NSString *kAllMethodObjectParametersKey = @"allMethodObjectParameters"; static NSString *kAllMethodObjectParameterRefsKey = @"allMethodObjectParameterRefs"; +static NSString *kHasDeprecatedSchemaKey = @"hasDeprecatedSchemaKey"; static NSString *kCleanedRootURLStringKey = @"cleanedRootURLString"; static NSString *kResumableUploadPathKey = @"resumableUploadPath"; static NSString *kSimpleUploadPathKey = @"simpleUploadPath"; @@ -96,6 +97,7 @@ @interface GTLRDiscovery_RestDescription (SGGeneratorAdditions) @property(readonly) NSDictionary *sg_objectEnumsMap; @property(readonly) NSArray *sg_allSchemas; @property(readonly) NSArray *sg_topLevelObjectSchemas; +@property(readonly) BOOL sg_hasDeprecatedSchema; @property(readonly) NSArray *sg_allMethodObjectParameterReferences; @property(readonly) NSString *sg_resumableUploadPath; @property(readonly) NSString *sg_simpleUploadPath; @@ -1287,6 +1289,7 @@ - (NSString *)objectsHeader { } SGClangDirectives *clangDirectives = [SGClangDirectives disabledDocumentation]; + clangDirectives.disableDeprecatedDeclarations = self.api.sg_hasDeprecatedSchema; [parts addObject:clangDirectives.start]; [parts addObject:@"NS_ASSUME_NONNULL_BEGIN\n"]; @@ -1339,6 +1342,14 @@ - (NSString *)objectsSource { [parts addObjectsFromArray:blocks]; } + SGClangDirectives *clangDirectives = [[SGClangDirectives alloc] init]; + BOOL hasDeprecatedSchema = self.api.sg_hasDeprecatedSchema; + clangDirectives.disableDeprecatedImplementations = hasDeprecatedSchema; + clangDirectives.disableDeprecatedDeclarations = hasDeprecatedSchema; + if (clangDirectives.hasDirectives) { + [parts addObject:clangDirectives.start]; + } + NSMutableArray *classParts = [NSMutableArray array]; for (GTLRDiscovery_JsonSchema *schema in self.api.sg_topLevelObjectSchemas) { NSString *objectClassStr = [self generateObjectClassForSchema:schema @@ -1354,6 +1365,10 @@ - (NSString *)objectsSource { // Two blank lines between classes. [parts addObject:[classParts componentsJoinedByString:@"\n\n"]]; + if (clangDirectives.hasDirectives) { + [parts addObject:clangDirectives.end]; + } + return [parts componentsJoinedByString:@"\n"]; } @@ -2403,8 +2418,9 @@ - (NSString *)generateObjectClassForSchema:(GTLRDiscovery_JsonSchema *)schema } else if (isTopLevelArrayResult) { baseClass = kResultArrayClass; } - atBlock = [NSString stringWithFormat:@"@interface %@ : %@\n", - schemaClassName, baseClass]; + NSString *maybeDeprecated = schema.deprecated.boolValue ? kDeprecatedWithNewline : @""; + atBlock = [NSString stringWithFormat:@"%@@interface %@ : %@\n", + maybeDeprecated, schemaClassName, baseClass]; } else { atBlock = [NSString stringWithFormat:@"@implementation %@\n", schemaClassName]; @@ -3313,6 +3329,37 @@ - (NSArray *)sg_topLevelObjectSchemas { return result; } +- (BOOL)sg_hasDeprecatedSchema { + // This could be expanded to deal with if there are referenced as types + // on other schema vs. queries to only add the guards when needed, but + // for now just generally insert them when any schema was deprecated. + NSNumber *result = [self sg_propertyForKey:kHasDeprecatedSchemaKey]; + if (result == nil) { + BOOL hasDeprecated = NO; + + for (GTLRDiscovery_JsonSchema *schema in self.sg_topLevelObjectSchemas) { + if (schema.deprecated.boolValue) { + hasDeprecated = YES; + break; + } + + for (GTLRDiscovery_JsonSchema *subSchema in schema.sg_childObjectSchemas) { + if (subSchema.deprecated.boolValue) { + hasDeprecated = YES; + break; + } + } + if (hasDeprecated) { + break; + } + } + + result = [NSNumber numberWithBool:hasDeprecated]; + [self sg_setProperty:result forKey:kHasDeprecatedSchemaKey]; + } + return [result boolValue]; +} + // These are resolved schema references in the method parameters (refs or // inline). - (NSArray *)sg_allMethodObjectParameterReferences { diff --git a/Tools/ServiceGenerator/SGUtils.h b/Tools/ServiceGenerator/SGUtils.h index 50424c101..7159d6b89 100644 --- a/Tools/ServiceGenerator/SGUtils.h +++ b/Tools/ServiceGenerator/SGUtils.h @@ -73,6 +73,8 @@ // -Wdeprecated-implementations @property BOOL disableDeprecatedImplementations; +// -Wdeprecated-declarations +@property BOOL disableDeprecatedDeclarations; // -Wdocumentation @property BOOL disableDocumentation; diff --git a/Tools/ServiceGenerator/SGUtils.m b/Tools/ServiceGenerator/SGUtils.m index 3aa64fc0e..9b3bc6ef6 100644 --- a/Tools/ServiceGenerator/SGUtils.m +++ b/Tools/ServiceGenerator/SGUtils.m @@ -681,6 +681,7 @@ typedef NS_OPTIONS(NSInteger, SGCDFlags) { SGCDFlagsNone = 0, SGCDFlagsDocumentation = 1 << 0, SGCDFlagsDeprecatedImplementations = 1 << 1, + SGCDFlagsDeprecatedDeclarations = 1 << 2, }; @implementation SGClangDirectives { @@ -700,6 +701,14 @@ - (void)setDisableDeprecatedImplementations:(BOOL)value { SET_FLAG(SGCDFlagsDeprecatedImplementations, value); } +- (BOOL)disableDeprecatedDeclarations { + return IS_FLAG_SET(SGCDFlagsDeprecatedDeclarations); +} + +- (void)setDisableDeprecatedDeclarations:(BOOL)value { + SET_FLAG(SGCDFlagsDeprecatedDeclarations, value); +} + - (BOOL)disableDocumentation { return IS_FLAG_SET(SGCDFlagsDocumentation); } @@ -740,6 +749,11 @@ - (NSString *)start { [result appendString:@"#pragma clang diagnostic push\n"]; } + if (self.disableDeprecatedDeclarations) { + [result appendString: + @"#pragma clang diagnostic ignored \"-Wdeprecated-declarations\"\n"]; + } + if (self.disableDeprecatedImplementations) { [result appendString: @"#pragma clang diagnostic ignored \"-Wdeprecated-implementations\"\n"];