Permalink
Browse files

scrooge-generator: reduce warnings and support modules for Cocoa

Summary: Problem

The Objective C code generated from the Scrooge Cocoa generator results
in compiler warnings with certain inputs. In addition, the public header
files do not support putting the code in a modular framework.

Solution

1. Loop iterators and parameters to initWithCapacity now explicitly
convert from a signed integer to an unsigned one. This prevents warnings
and integer overflows.

2. The NSDecoder methods now explicitly cast the result to the expected
type. This is largely unnecessary, though not harmful, except in the
case of the Thrift type i16, where an implicit cast from a 32-bit
integer to the 16-bit member var results in a warning. NSDecoder only
provides a decodeInt32ForKey: method, and not a decodeInt16ForKey:.

3. The generated header files that need to reference other generated
header files now use a modular import syntax. That is, instead of:

#import "Filename.h"

it now generates:

#import <Namespace/Filename.h>

4. ApacheThrift is now expected to be a module and uses the "@import
ApacheThrift" syntax.

JIRA Issues: FAB-1590

Differential Revision: https://phabricator.twitter.biz/D74200
  • Loading branch information...
kgoodier authored and jenkins committed Jul 28, 2017
1 parent 8aefcd6 commit f48cd56e9398305ee7e280e161b0423da4131845
Showing with 171 additions and 158 deletions.
  1. +2 −0 CHANGES
  2. +10 −5 scrooge-generator-tests/src/test/resources/cocoa_output/TFNTwitterThriftScribeAnotherTestStruct.h
  3. +84 −25 scrooge-generator-tests/src/test/resources/cocoa_output/TFNTwitterThriftScribeAnotherTestStruct.m
  4. +1 −2 scrooge-generator-tests/src/test/resources/cocoa_output/TFNTwitterThriftScribeTestStruct.h
  5. +7 −13 scrooge-generator-tests/src/test/resources/cocoa_output/TFNTwitterThriftScribeTestStruct.m
  6. +1 −2 ...-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldAnotherException.h
  7. +3 −10 ...-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldAnotherException.m
  8. +1 −2 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldCollectionId.h
  9. +3 −10 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldCollectionId.m
  10. +1 −2 ...rator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldOverCapacityException.h
  11. +3 −10 ...rator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldOverCapacityException.m
  12. +2 −3 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldRequest.h
  13. +23 −26 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldRequest.m
  14. +2 −3 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldResponse.h
  15. +4 −11 scrooge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldResponse.m
  16. +1 −2 ...oge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldResponseUnion.h
  17. +4 −11 ...oge-generator-tests/src/test/resources/gold_file_output_cocoa/TFNTwitterThriftGoldResponseUnion.m
  18. +2 −0 scrooge-generator-tests/src/test/resources/test_thrift/cocoa.thrift
  19. +1 −0 scrooge-generator/src/main/resources/cocoagen/OWNERS
  20. +3 −2 scrooge-generator/src/main/resources/cocoagen/readList.m
  21. +2 −1 scrooge-generator/src/main/resources/cocoagen/readMap.m
  22. +3 −2 scrooge-generator/src/main/resources/cocoagen/readSet.m
  23. +2 −3 scrooge-generator/src/main/resources/cocoagen/struct.h
  24. +3 −10 scrooge-generator/src/main/resources/cocoagen/struct.m
  25. +2 −2 scrooge-generator/src/main/resources/cocoagen/writeList.m
  26. +1 −1 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/CocoaGenerator.scala
View
@@ -15,6 +15,8 @@
`FutureIface`. It already "was" that type in practice but did not implement
that trait. ``PHAB_ID=D67289``
- scrooge-generator: Generated Cocoa code now supports modular frameworks and
removes some compiler warnings about implicit casts. ``PHAB_ID=D74200``
4.18.0
------
@@ -4,11 +4,10 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import <TFNTwitterThriftScribe/TFNTwitterThriftScribeTestEnum.h>
#import <TFNTwitterThriftScribe/TFNTwitterThriftScribeTestStruct.h>
#import "ApacheThrift/TBase.h"
#import "TFNTwitterThriftScribeTestEnum.h"
#import "TFNTwitterThriftScribeTestStruct.h"
@import ApacheThrift.TBase;
@interface TFNTwitterThriftScribeAnotherTestStruct : NSObject <TBase, NSCoding>
@@ -39,8 +38,14 @@
@property (nonatomic) TFNTwitterThriftScribeTestEnum anEnum;
@property (nonatomic, readonly) BOOL anEnumIsSet;
@property (nonatomic) int16_t shortNum;
@property (nonatomic, readonly) BOOL shortNumIsSet;
- (instancetype)initWithStructs:(NSArray *)structs stringStructs:(NSArray *)stringStructs aSet:(NSSet *)aSet aMap:(NSDictionary *)aMap id_:(int32_t)id_ protocol_:(NSString *)protocol_ sel_:(TFNTwitterThriftScribeTestStruct*)sel_ notACamel:(int32_t)notACamel anEnum:(TFNTwitterThriftScribeTestEnum)anEnum;
@property (nonatomic) int64_t longLongNum;
@property (nonatomic, readonly) BOOL longLongNumIsSet;
- (instancetype)initWithStructs:(NSArray *)structs stringStructs:(NSArray *)stringStructs aSet:(NSSet *)aSet aMap:(NSDictionary *)aMap id_:(int32_t)id_ protocol_:(NSString *)protocol_ sel_:(TFNTwitterThriftScribeTestStruct*)sel_ notACamel:(int32_t)notACamel anEnum:(TFNTwitterThriftScribeTestEnum)anEnum shortNum:(int16_t)shortNum longLongNum:(int64_t)longLongNum;
- (void)read:(id<TProtocol>)inProtocol;
- (void)write:(id<TProtocol>)outProtocol;
@@ -4,17 +4,10 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TApplicationException.h"
#import "ApacheThrift/TBase.h"
#import "ApacheThrift/TObjective-C.h"
#import "ApacheThrift/TProcessor.h"
#import "ApacheThrift/TProtocol.h"
#import "ApacheThrift/TProtocolException.h"
#import "ApacheThrift/TProtocolUtil.h"
#import "TFNTwitterThriftScribeAnotherTestStruct.h"
@import ApacheThrift;
@implementation TFNTwitterThriftScribeAnotherTestStruct
- (NSString*)description
@@ -38,11 +31,15 @@ - (NSString*)description
[ms appendFormat:@"%@ ", @(_notACamel)];
[ms appendString:@"anEnum:"];
[ms appendFormat:@"%@ ", @(_anEnum)];
[ms appendString:@"shortNum:"];
[ms appendFormat:@"%@ ", @(_shortNum)];
[ms appendString:@"longLongNum:"];
[ms appendFormat:@"%@ ", @(_longLongNum)];
[ms appendString:@")"];
return [NSString stringWithString:ms];
}
- (instancetype)initWithStructs:(NSArray *)structs stringStructs:(NSArray *)stringStructs aSet:(NSSet *)aSet aMap:(NSDictionary *)aMap id_:(int32_t)id_ protocol_:(NSString *)protocol_ sel_:(TFNTwitterThriftScribeTestStruct*)sel_ notACamel:(int32_t)notACamel anEnum:(TFNTwitterThriftScribeTestEnum)anEnum
- (instancetype)initWithStructs:(NSArray *)structs stringStructs:(NSArray *)stringStructs aSet:(NSSet *)aSet aMap:(NSDictionary *)aMap id_:(int32_t)id_ protocol_:(NSString *)protocol_ sel_:(TFNTwitterThriftScribeTestStruct*)sel_ notACamel:(int32_t)notACamel anEnum:(TFNTwitterThriftScribeTestEnum)anEnum shortNum:(int16_t)shortNum longLongNum:(int64_t)longLongNum
{
if (self = [super init]) {
[self setStructs:structs];
@@ -54,6 +51,8 @@ - (instancetype)initWithStructs:(NSArray *)structs stringStructs:(NSArray *)stri
[self setSel_:sel_];
[self setNotACamel:notACamel];
[self setAnEnum:anEnum];
[self setShortNum:shortNum];
[self setLongLongNum:longLongNum];
}
return self;
@@ -63,31 +62,37 @@ - (instancetype)initWithCoder:(NSCoder*)decoder
{
if (self = [super init]) {
if ([decoder containsValueForKey:@"1"]) {
[self setStructs:[decoder decodeObjectForKey:@"1"]];
[self setStructs:(NSArray *)[decoder decodeObjectForKey:@"1"]];
}
if ([decoder containsValueForKey:@"2"]) {
[self setStringStructs:[decoder decodeObjectForKey:@"2"]];
[self setStringStructs:(NSArray *)[decoder decodeObjectForKey:@"2"]];
}
if ([decoder containsValueForKey:@"3"]) {
[self setASet:[decoder decodeObjectForKey:@"3"]];
[self setASet:(NSSet *)[decoder decodeObjectForKey:@"3"]];
}
if ([decoder containsValueForKey:@"4"]) {
[self setAMap:[decoder decodeObjectForKey:@"4"]];
[self setAMap:(NSDictionary *)[decoder decodeObjectForKey:@"4"]];
}
if ([decoder containsValueForKey:@"5"]) {
[self setId_:[decoder decodeInt32ForKey:@"5"]];
[self setId_:(int32_t)[decoder decodeInt32ForKey:@"5"]];
}
if ([decoder containsValueForKey:@"6"]) {
[self setProtocol_:[decoder decodeObjectForKey:@"6"]];
[self setProtocol_:(NSString *)[decoder decodeObjectForKey:@"6"]];
}
if ([decoder containsValueForKey:@"7"]) {
[self setSel_:[decoder decodeObjectForKey:@"7"]];
[self setSel_:(TFNTwitterThriftScribeTestStruct*)[decoder decodeObjectForKey:@"7"]];
}
if ([decoder containsValueForKey:@"8"]) {
[self setNotACamel:[decoder decodeInt32ForKey:@"8"]];
[self setNotACamel:(int32_t)[decoder decodeInt32ForKey:@"8"]];
}
if ([decoder containsValueForKey:@"9"]) {
[self setAnEnum:[decoder decodeInt32ForKey:@"9"]];
[self setAnEnum:(TFNTwitterThriftScribeTestEnum)[decoder decodeInt32ForKey:@"9"]];
}
if ([decoder containsValueForKey:@"10"]) {
[self setShortNum:(int16_t)[decoder decodeInt32ForKey:@"10"]];
}
if ([decoder containsValueForKey:@"11"]) {
[self setLongLongNum:(int64_t)[decoder decodeInt64ForKey:@"11"]];
}
}
return self;
@@ -122,6 +127,12 @@ - (void)encodeWithCoder:(NSCoder*)encoder
if (_anEnumIsSet) {
[encoder encodeInt32:_anEnum forKey:@"9"];
}
if (_shortNumIsSet) {
[encoder encodeInt32:_shortNum forKey:@"10"];
}
if (_longLongNumIsSet) {
[encoder encodeInt64:_longLongNum forKey:@"11"];
}
}
- (void)setStructs:(NSArray *)structs
@@ -178,6 +189,18 @@ - (void)setAnEnum:(TFNTwitterThriftScribeTestEnum)anEnum
_anEnumIsSet = YES;
}
- (void)setShortNum:(int16_t)shortNum
{
_shortNum = shortNum;
_shortNumIsSet = YES;
}
- (void)setLongLongNum:(int64_t)longLongNum
{
_longLongNum = longLongNum;
_longLongNumIsSet = YES;
}
- (void)read:(id <TProtocol>)inProtocol
{
NSString* fieldName;
@@ -196,7 +219,8 @@ - (void)read:(id <TProtocol>)inProtocol
NSArray * structs_item;
int _structs_item_size;
[inProtocol readListBeginReturningElementType:NULL size:&_structs_item_size];
NSMutableArray *structs_item_mutable = [[NSMutableArray alloc] initWithCapacity:_structs_item_size];
NSUInteger _structs_item_capacity = _structs_item_size < 0 ? 0 : (NSUInteger)_structs_item_size;
NSMutableArray *structs_item_mutable = [[NSMutableArray alloc] initWithCapacity:_structs_item_capacity];
for (int _structs_item_i = 0; _structs_item_i < _structs_item_size; ++_structs_item_i) {
TFNTwitterThriftScribeTestStruct * structs_item_element;
structs_item_element = [[TFNTwitterThriftScribeTestStruct alloc] init];
@@ -216,7 +240,8 @@ - (void)read:(id <TProtocol>)inProtocol
NSArray * stringStructs_item;
int _stringStructs_item_size;
[inProtocol readListBeginReturningElementType:NULL size:&_stringStructs_item_size];
NSMutableArray *stringStructs_item_mutable = [[NSMutableArray alloc] initWithCapacity:_stringStructs_item_size];
NSUInteger _stringStructs_item_capacity = _stringStructs_item_size < 0 ? 0 : (NSUInteger)_stringStructs_item_size;
NSMutableArray *stringStructs_item_mutable = [[NSMutableArray alloc] initWithCapacity:_stringStructs_item_capacity];
for (int _stringStructs_item_i = 0; _stringStructs_item_i < _stringStructs_item_size; ++_stringStructs_item_i) {
NSString * stringStructs_item_element;
stringStructs_item_element = [inProtocol readString];
@@ -235,7 +260,8 @@ - (void)read:(id <TProtocol>)inProtocol
NSSet * aSet_item;
int _aSet_item_size;
[inProtocol readSetBeginReturningElementType: NULL size: &_aSet_item_size];
NSMutableSet *aSet_item_mutable = [[NSMutableSet alloc] initWithCapacity:_aSet_item_size];
NSUInteger _aSet_item_capacity= _aSet_item_size < 0 ? 0 : (NSUInteger)_aSet_item_size;
NSMutableSet *aSet_item_mutable = [[NSMutableSet alloc] initWithCapacity:_aSet_item_capacity];
for (int _aSet_item_i = 0; _aSet_item_i < _aSet_item_size; ++_aSet_item_i) {
TFNTwitterThriftScribeTestStruct * aSet_item_element;
aSet_item_element = [[TFNTwitterThriftScribeTestStruct alloc] init];
@@ -255,7 +281,8 @@ - (void)read:(id <TProtocol>)inProtocol
NSDictionary * aMap_item;
int _aMap_item_size;
[inProtocol readMapBeginReturningKeyType: NULL valueType: NULL size: &_aMap_item_size];
NSMutableDictionary *aMap_item_mutable = [[NSMutableDictionary alloc] initWithCapacity: _aMap_item_size];
NSUInteger _aMap_item_capacity= _aMap_item_size < 0 ? 0 : (NSUInteger)_aMap_item_size;
NSMutableDictionary *aMap_item_mutable = [[NSMutableDictionary alloc] initWithCapacity: _aMap_item_capacity];
for (int _aMap_item_i = 0; _aMap_item_i < _aMap_item_size; ++_aMap_item_i) {
NSString * aMap_item_key;
TFNTwitterThriftScribeTestStruct * aMap_item_value;
@@ -323,6 +350,26 @@ - (void)read:(id <TProtocol>)inProtocol
[TProtocolUtil skipType:fieldType onProtocol:inProtocol];
}
break;
case 10:
if (fieldType == TType_I16) {
int16_t shortNum_item;
shortNum_item = [inProtocol readI16];
[self setShortNum:shortNum_item];
} else {
NSLog(@"%s: field ID %i has unexpected type %i. Skipping.", __PRETTY_FUNCTION__, fieldID, fieldType);
[TProtocolUtil skipType:fieldType onProtocol:inProtocol];
}
break;
case 11:
if (fieldType == TType_I64) {
int64_t longLongNum_item;
longLongNum_item = [inProtocol readI64];
[self setLongLongNum:longLongNum_item];
} else {
NSLog(@"%s: field ID %i has unexpected type %i. Skipping.", __PRETTY_FUNCTION__, fieldID, fieldType);
[TProtocolUtil skipType:fieldType onProtocol:inProtocol];
}
break;
default:
NSLog(@"%s: unexpected field ID %i with type %i. Skipping.", __PRETTY_FUNCTION__, fieldID, fieldType);
[TProtocolUtil skipType:fieldType onProtocol:inProtocol];
@@ -342,7 +389,7 @@ - (void)write:(id <TProtocol>)outProtocol
[outProtocol writeFieldBeginWithName:@"structs" type:TType_LIST fieldID:1];
NSArray * structs_item = _structs;
[outProtocol writeListBeginWithElementType:TType_STRUCT size:(int)[structs_item count]];
for (int _structs_item_i = 0; _structs_item_i < [structs_item count]; _structs_item_i++) {
for (NSUInteger _structs_item_i = 0; _structs_item_i < [structs_item count]; _structs_item_i++) {
TFNTwitterThriftScribeTestStruct * structs_item_element = structs_item[_structs_item_i];
[structs_item_element write: outProtocol];
}
@@ -353,7 +400,7 @@ - (void)write:(id <TProtocol>)outProtocol
[outProtocol writeFieldBeginWithName:@"stringStructs" type:TType_LIST fieldID:2];
NSArray * stringStructs_item = _stringStructs;
[outProtocol writeListBeginWithElementType:TType_STRING size:(int)[stringStructs_item count]];
for (int _stringStructs_item_i = 0; _stringStructs_item_i < [stringStructs_item count]; _stringStructs_item_i++) {
for (NSUInteger _stringStructs_item_i = 0; _stringStructs_item_i < [stringStructs_item count]; _stringStructs_item_i++) {
NSString * stringStructs_item_element = stringStructs_item[_stringStructs_item_i];
[outProtocol writeString:stringStructs_item_element];
}
@@ -416,6 +463,18 @@ - (void)write:(id <TProtocol>)outProtocol
[outProtocol writeI32:anEnum_item];
[outProtocol writeFieldEnd];
}
if (_shortNumIsSet) {
[outProtocol writeFieldBeginWithName:@"shortNum" type:TType_I16 fieldID:10];
int16_t shortNum_item = _shortNum;
[outProtocol writeI16:shortNum_item];
[outProtocol writeFieldEnd];
}
if (_longLongNumIsSet) {
[outProtocol writeFieldBeginWithName:@"longLongNum" type:TType_I64 fieldID:11];
int64_t longLongNum_item = _longLongNum;
[outProtocol writeI64:longLongNum_item];
[outProtocol writeFieldEnd];
}
[outProtocol writeFieldStop];
[outProtocol writeStructEnd];
}
@@ -4,10 +4,9 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TBase.h"
@import ApacheThrift.TBase;
@interface TFNTwitterThriftScribeTestStruct : NSObject <TBase, NSCoding>
@@ -4,17 +4,10 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TApplicationException.h"
#import "ApacheThrift/TBase.h"
#import "ApacheThrift/TObjective-C.h"
#import "ApacheThrift/TProcessor.h"
#import "ApacheThrift/TProtocol.h"
#import "ApacheThrift/TProtocolException.h"
#import "ApacheThrift/TProtocolUtil.h"
#import "TFNTwitterThriftScribeTestStruct.h"
@import ApacheThrift;
@implementation TFNTwitterThriftScribeTestStruct
- (NSString*)description
@@ -45,13 +38,13 @@ - (instancetype)initWithCoder:(NSCoder*)decoder
{
if (self = [super init]) {
if ([decoder containsValueForKey:@"1"]) {
[self setName:[decoder decodeObjectForKey:@"1"]];
[self setName:(NSString *)[decoder decodeObjectForKey:@"1"]];
}
if ([decoder containsValueForKey:@"2"]) {
[self setValues:[decoder decodeObjectForKey:@"2"]];
[self setValues:(NSSet *)[decoder decodeObjectForKey:@"2"]];
}
if ([decoder containsValueForKey:@"3"]) {
[self setPrice:[decoder decodeInt32ForKey:@"3"]];
[self setPrice:(int32_t)[decoder decodeInt32ForKey:@"3"]];
}
}
return self;
@@ -116,7 +109,8 @@ - (void)read:(id <TProtocol>)inProtocol
NSSet * values_item;
int _values_item_size;
[inProtocol readSetBeginReturningElementType: NULL size: &_values_item_size];
NSMutableSet *values_item_mutable = [[NSMutableSet alloc] initWithCapacity:_values_item_size];
NSUInteger _values_item_capacity= _values_item_size < 0 ? 0 : (NSUInteger)_values_item_size;
NSMutableSet *values_item_mutable = [[NSMutableSet alloc] initWithCapacity:_values_item_capacity];
for (int _values_item_i = 0; _values_item_i < _values_item_size; ++_values_item_i) {
NSString * values_item_element;
values_item_element = [inProtocol readString];
@@ -4,10 +4,9 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TBase.h"
@import ApacheThrift.TBase;
@interface TFNTwitterThriftGoldAnotherException : NSObject <TBase, NSCoding>
@@ -4,17 +4,10 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TApplicationException.h"
#import "ApacheThrift/TBase.h"
#import "ApacheThrift/TObjective-C.h"
#import "ApacheThrift/TProcessor.h"
#import "ApacheThrift/TProtocol.h"
#import "ApacheThrift/TProtocolException.h"
#import "ApacheThrift/TProtocolUtil.h"
#import "TFNTwitterThriftGoldAnotherException.h"
@import ApacheThrift;
@implementation TFNTwitterThriftGoldAnotherException
- (NSString*)description
@@ -39,7 +32,7 @@ - (instancetype)initWithCoder:(NSCoder*)decoder
{
if (self = [super init]) {
if ([decoder containsValueForKey:@"1"]) {
[self setErrorCode:[decoder decodeInt32ForKey:@"1"]];
[self setErrorCode:(int32_t)[decoder decodeInt32ForKey:@"1"]];
}
}
return self;
@@ -4,10 +4,9 @@
* rev: ?
* built at: ?
*/
#import <Foundation/Foundation.h>
#import "ApacheThrift/TBase.h"
@import ApacheThrift.TBase;
@interface TFNTwitterThriftGoldCollectionId : NSObject <TBase, NSCoding>
Oops, something went wrong.

0 comments on commit f48cd56

Please sign in to comment.