Format the runtime sources.
diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m index c29b955..f6feaff 100644 --- a/objectivec/GPBDescriptor.m +++ b/objectivec/GPBDescriptor.m
@@ -32,9 +32,9 @@ #import <objc/runtime.h> +#import "GPBMessage_PackagePrivate.h" #import "GPBUtilities_PackagePrivate.h" #import "GPBWireFormat.h" -#import "GPBMessage_PackagePrivate.h" // Direct access is use for speed, to avoid even internally declaring things // read/write, etc. The warning is enabled in the project to ensure code calling @@ -48,16 +48,15 @@ static const char kClassNameSuffixKey = 0; // Utility function to generate selectors on the fly. -static SEL SelFromStrings(const char *prefix, const char *middle, - const char *suffix, BOOL takesArg) { +static SEL SelFromStrings(const char *prefix, const char *middle, const char *suffix, + BOOL takesArg) { if (prefix == NULL && suffix == NULL && !takesArg) { return sel_getUid(middle); } const size_t prefixLen = prefix != NULL ? strlen(prefix) : 0; const size_t middleLen = strlen(middle); const size_t suffixLen = suffix != NULL ? strlen(suffix) : 0; - size_t totalLen = - prefixLen + middleLen + suffixLen + 1; // include space for null on end. + size_t totalLen = prefixLen + middleLen + suffixLen + 1; // include space for null on end. if (takesArg) { totalLen += 1; } @@ -82,12 +81,10 @@ return result; } -static NSArray *NewFieldsArrayForHasIndex(int hasIndex, - NSArray *allMessageFields) +static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageFields) __attribute__((ns_returns_retained)); -static NSArray *NewFieldsArrayForHasIndex(int hasIndex, - NSArray *allMessageFields) { +static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageFields) { NSMutableArray *result = [[NSMutableArray alloc] init]; for (GPBFieldDescriptor *fieldDesc in allMessageFields) { if (fieldDesc->description_->hasIndex == hasIndex) { @@ -111,25 +108,21 @@ @synthesize file = file_; @synthesize wireFormat = wireFormat_; -+ (instancetype) - allocDescriptorForClass:(Class)messageClass - rootClass:(Class)rootClass - file:(GPBFileDescriptor *)file - fields:(void *)fieldDescriptions - fieldCount:(uint32_t)fieldCount - storageSize:(uint32_t)storageSize - flags:(GPBDescriptorInitializationFlags)flags { ++ (instancetype)allocDescriptorForClass:(Class)messageClass + rootClass:(Class)rootClass + file:(GPBFileDescriptor *)file + fields:(void *)fieldDescriptions + fieldCount:(uint32_t)fieldCount + storageSize:(uint32_t)storageSize + flags:(GPBDescriptorInitializationFlags)flags { // The rootClass is no longer used, but it is passed in to ensure it // was started up during initialization also. (void)rootClass; NSMutableArray *fields = nil; GPBFileSyntax syntax = file.syntax; - BOOL fieldsIncludeDefault = - (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0; - BOOL usesClassRefs = - (flags & GPBDescriptorInitializationFlag_UsesClassRefs) != 0; - BOOL proto3OptionalKnown = - (flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) != 0; + BOOL fieldsIncludeDefault = (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0; + BOOL usesClassRefs = (flags & GPBDescriptorInitializationFlag_UsesClassRefs) != 0; + BOOL proto3OptionalKnown = (flags & GPBDescriptorInitializationFlag_Proto3OptionalKnown) != 0; void *desc; for (uint32_t i = 0; i < fieldCount; ++i) { @@ -193,10 +186,9 @@ for (uint32_t i = 0, hasIndex = firstHasIndex; i < count; ++i, --hasIndex) { const char *name = oneofNames[i]; NSArray *fieldsForOneof = NewFieldsArrayForHasIndex(hasIndex, fields_); - NSCAssert(fieldsForOneof.count > 0, - @"No fields for this oneof? (%s:%d)", name, hasIndex); - GPBOneofDescriptor *oneofDescriptor = - [[GPBOneofDescriptor alloc] initWithName:name fields:fieldsForOneof]; + NSCAssert(fieldsForOneof.count > 0, @"No fields for this oneof? (%s:%d)", name, hasIndex); + GPBOneofDescriptor *oneofDescriptor = [[GPBOneofDescriptor alloc] initWithName:name + fields:fieldsForOneof]; [oneofs addObject:oneofDescriptor]; [oneofDescriptor release]; [fieldsForOneof release]; @@ -210,8 +202,7 @@ NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo]; for (GPBFieldDescriptor *fieldDescriptor in fields_) { if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) { - objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey, - extraInfoValue, + objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey, extraInfoValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } } @@ -224,9 +215,7 @@ } - (void)setupContainingMessageClass:(Class)messageClass { - objc_setAssociatedObject(self, &kParentClassValueKey, - messageClass, - OBJC_ASSOCIATION_ASSIGN); + objc_setAssociatedObject(self, &kParentClassValueKey, messageClass, OBJC_ASSOCIATION_ASSIGN); } - (void)setupContainingMessageClassName:(const char *)msgClassName { @@ -240,9 +229,7 @@ - (void)setupMessageClassNameSuffix:(NSString *)suffix { if (suffix.length) { - objc_setAssociatedObject(self, &kClassNameSuffixKey, - suffix, - OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, &kClassNameSuffixKey, suffix, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } } @@ -260,9 +247,7 @@ GPBFileDescriptor *file = self.file; NSString *objcPrefix = file.objcPrefix; if (objcPrefix && ![className hasPrefix:objcPrefix]) { - NSAssert(0, - @"Class didn't have correct prefix? (%@ - %@)", - className, objcPrefix); + NSAssert(0, @"Class didn't have correct prefix? (%@ - %@)", className, objcPrefix); return nil; } GPBDescriptor *parent = self.containingType; @@ -274,19 +259,16 @@ NSString *suffix = objc_getAssociatedObject(parent, &kClassNameSuffixKey); if (suffix) { if (![parentClassName hasSuffix:suffix]) { - NSAssert(0, - @"ParentMessage class didn't have correct suffix? (%@ - %@)", - className, suffix); + NSAssert(0, @"ParentMessage class didn't have correct suffix? (%@ - %@)", className, + suffix); return nil; } - parentClassName = - [parentClassName substringToIndex:(parentClassName.length - suffix.length)]; + 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); + NSAssert(0, @"Class didn't have the correct parent name prefix? (%@ - %@)", parentPrefix, + className); return nil; } name = [className substringFromIndex:parentPrefix.length]; @@ -298,9 +280,7 @@ NSString *suffix = objc_getAssociatedObject(self, &kClassNameSuffixKey); if (suffix) { if (![name hasSuffix:suffix]) { - NSAssert(0, - @"Message class didn't have correct suffix? (%@ - %@)", - name, suffix); + NSAssert(0, @"Message class didn't have correct suffix? (%@ - %@)", name, suffix); return nil; } name = [name substringToIndex:(name.length - suffix.length)]; @@ -372,8 +352,7 @@ return self; } -- (instancetype)initWithPackage:(NSString *)package - syntax:(GPBFileSyntax)syntax { +- (instancetype)initWithPackage:(NSString *)package syntax:(GPBFileSyntax)syntax { self = [super init]; if (self) { package_ = [package copy]; @@ -414,7 +393,7 @@ } - (NSString *)name { - return (NSString * _Nonnull)@(name_); + return (NSString *_Nonnull)@(name_); } - (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber { @@ -444,19 +423,17 @@ // Maps are repeated messages on the wire. format = GPBWireFormatForType(GPBDataTypeMessage, NO); } else { - format = GPBWireFormatForType(description->dataType, - ((description->flags & GPBFieldPacked) != 0)); + format = + GPBWireFormatForType(description->dataType, ((description->flags & GPBFieldPacked) != 0)); } return GPBWireFormatMakeTag(description->number, format); } uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { GPBMessageFieldDescription *description = self->description_; - NSCAssert((description->flags & GPBFieldRepeated) != 0, - @"Only valid on repeated fields"); + NSCAssert((description->flags & GPBFieldRepeated) != 0, @"Only valid on repeated fields"); GPBWireFormat format = - GPBWireFormatForType(description->dataType, - ((description->flags & GPBFieldPacked) == 0)); + GPBWireFormatForType(description->dataType, ((description->flags & GPBFieldPacked) == 0)); return GPBWireFormatMakeTag(description->number, format); } @@ -516,10 +493,8 @@ // - not repeated/map // - not in a oneof (negative has index) // - not a message (the flag doesn't make sense for messages) - BOOL clearOnZero = ((syntax == GPBFileSyntaxProto3) && - !isMapOrArray && - (coreDesc->hasIndex >= 0) && - !isMessage); + BOOL clearOnZero = ((syntax == GPBFileSyntaxProto3) && !isMapOrArray && + (coreDesc->hasIndex >= 0) && !isMessage); if (clearOnZero) { coreDesc->flags |= GPBFieldClearHasIvarOnZero; } @@ -534,8 +509,7 @@ // It is a single field; it gets has/setHas selectors if... // - not in a oneof (negative has index) // - not clearing on zero - if ((coreDesc->hasIndex >= 0) && - ((coreDesc->flags & GPBFieldClearHasIvarOnZero) == 0)) { + if ((coreDesc->hasIndex >= 0) && ((coreDesc->flags & GPBFieldClearHasIvarOnZero) == 0)) { hasOrCountSel_ = SelFromStrings("has", coreDesc->name, NULL, NO); setHasSel_ = SelFromStrings("setHas", coreDesc->name, NULL, YES); } @@ -556,11 +530,9 @@ } } else if (dataType == GPBDataTypeEnum) { if ((coreDesc->flags & GPBFieldHasEnumDescriptor) != 0) { - enumHandling_.enumDescriptor_ = - coreDesc->dataTypeSpecific.enumDescFunc(); + enumHandling_.enumDescriptor_ = coreDesc->dataTypeSpecific.enumDescFunc(); } else { - enumHandling_.enumVerifier_ = - coreDesc->dataTypeSpecific.enumVerifier; + enumHandling_.enumVerifier_ = coreDesc->dataTypeSpecific.enumVerifier; } } @@ -576,8 +548,7 @@ memcpy(&length, bytes, sizeof(length)); length = ntohl(length); bytes += sizeof(length); - defaultValue_.valueData = - [[NSData alloc] initWithBytes:bytes length:length]; + defaultValue_.valueData = [[NSData alloc] initWithBytes:bytes length:length]; } } } @@ -586,8 +557,7 @@ } - (void)dealloc { - if (description_->dataType == GPBDataTypeBytes && - !(description_->flags & GPBFieldRepeated)) { + if (description_->dataType == GPBDataTypeBytes && !(description_->flags & GPBFieldRepeated)) { [defaultValue_.valueData release]; } [super dealloc]; @@ -606,7 +576,7 @@ } - (NSString *)name { - return (NSString * _Nonnull)@(description_->name); + return (NSString *_Nonnull)@(description_->name); } - (BOOL)isRequired { @@ -666,8 +636,7 @@ } - (BOOL)isValidEnumValue:(int32_t)value { - NSAssert(description_->dataType == GPBDataTypeEnum, - @"Field Must be of type GPBDataTypeEnum"); + NSAssert(description_->dataType == GPBDataTypeEnum, @"Field Must be of type GPBDataTypeEnum"); if (description_->flags & GPBFieldHasEnumDescriptor) { return enumHandling_.enumDescriptor_.enumVerifier(value); } else { @@ -703,15 +672,13 @@ - (NSString *)textFormatName { if ((description_->flags & GPBFieldTextFormatNameCustom) != 0) { - NSValue *extraInfoValue = - objc_getAssociatedObject(self, &kTextFormatExtraValueKey); + NSValue *extraInfoValue = objc_getAssociatedObject(self, &kTextFormatExtraValueKey); // Support can be left out at generation time. if (!extraInfoValue) { return nil; } const uint8_t *extraTextFormatInfo = [extraInfoValue pointerValue]; - return GPBDecodeTextFormatName(extraTextFormatInfo, GPBFieldNumber(self), - self.name); + return GPBDecodeTextFormatName(extraTextFormatInfo, GPBFieldNumber(self), self.name); } // The logic here has to match SetCommonFieldVariables() from @@ -726,8 +693,7 @@ } // Remove "Array" from the end for repeated fields. - if (((description_->flags & GPBFieldRepeated) != 0) && - [name hasSuffix:@"Array"]) { + if (((description_->flags & GPBFieldRepeated) != 0) && [name hasSuffix:@"Array"]) { name = [name substringToIndex:(len - 5)]; len = [name length]; } @@ -739,9 +705,8 @@ if (firstChar >= 'a' && firstChar <= 'z') { NSString *firstCharString = [NSString stringWithFormat:@"%C", (unichar)(firstChar - 'a' + 'A')]; - NSString *result = - [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) - withString:firstCharString]; + NSString *result = [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) + withString:firstCharString]; return result; } return name; @@ -784,12 +749,11 @@ @synthesize name = name_; @synthesize enumVerifier = enumVerifier_; -+ (instancetype) - allocDescriptorForName:(NSString *)name - valueNames:(const char *)valueNames - values:(const int32_t *)values - count:(uint32_t)valueCount - enumVerifier:(GPBEnumValidationFunc)enumVerifier { ++ (instancetype)allocDescriptorForName:(NSString *)name + valueNames:(const char *)valueNames + values:(const int32_t *)values + count:(uint32_t)valueCount + enumVerifier:(GPBEnumValidationFunc)enumVerifier { GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name valueNames:valueNames values:values @@ -798,13 +762,12 @@ return descriptor; } -+ (instancetype) - allocDescriptorForName:(NSString *)name - valueNames:(const char *)valueNames - values:(const int32_t *)values - count:(uint32_t)valueCount - enumVerifier:(GPBEnumValidationFunc)enumVerifier - extraTextFormatInfo:(const char *)extraTextFormatInfo { ++ (instancetype)allocDescriptorForName:(NSString *)name + valueNames:(const char *)valueNames + values:(const int32_t *)values + count:(uint32_t)valueCount + enumVerifier:(GPBEnumValidationFunc)enumVerifier + extraTextFormatInfo:(const char *)extraTextFormatInfo { // Call the common case. GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name valueNames:valueNames @@ -892,27 +855,26 @@ } - (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName { - if (nameOffsets_ == NULL) [self calcValueNameOffsets]; - if (nameOffsets_ == NULL) return NO; + if (nameOffsets_ == NULL) [self calcValueNameOffsets]; + if (nameOffsets_ == NULL) return NO; - for (uint32_t i = 0; i < valueCount_; ++i) { - NSString *valueTextFormatName = [self getEnumTextFormatNameForIndex:i]; - if ([valueTextFormatName isEqual:textFormatName]) { - if (outValue) { - *outValue = values_[i]; - } - return YES; - } + for (uint32_t i = 0; i < valueCount_; ++i) { + NSString *valueTextFormatName = [self getEnumTextFormatNameForIndex:i]; + if ([valueTextFormatName isEqual:textFormatName]) { + if (outValue) { + *outValue = values_[i]; + } + return YES; } - return NO; + } + return NO; } - (NSString *)textFormatNameForValue:(int32_t)number { // Find the EnumValue descriptor and its index. BOOL foundIt = NO; uint32_t valueDescriptorIndex; - for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_; - ++valueDescriptorIndex) { + for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_; ++valueDescriptorIndex) { if (values_[valueDescriptorIndex] == number) { foundIt = YES; break; @@ -955,8 +917,7 @@ // See if it is in the map of special format handling. if (extraTextFormatInfo_) { - result = GPBDecodeTextFormatName(extraTextFormatInfo_, - (int32_t)index, shortName); + result = GPBDecodeTextFormatName(extraTextFormatInfo_, (int32_t)index, shortName); } // Logic here needs to match what objectivec_enum.cc does in the proto // compiler. @@ -1005,16 +966,14 @@ GPBDataType type = description_->dataType; if (type == GPBDataTypeBytes) { // Data stored as a length prefixed c-string in descriptor records. - const uint8_t *bytes = - (const uint8_t *)description_->defaultValue.valueData; + const uint8_t *bytes = (const uint8_t *)description_->defaultValue.valueData; if (bytes) { uint32_t length; memcpy(&length, bytes, sizeof(length)); // The length is stored in network byte order. length = ntohl(length); bytes += sizeof(length); - defaultValue_.valueData = - [[NSData alloc] initWithBytes:bytes length:length]; + defaultValue_.valueData = [[NSData alloc] initWithBytes:bytes length:length]; } } else if (type == GPBDataTypeMessage || type == GPBDataTypeGroup) { // The default is looked up in -defaultValue instead since extensions @@ -1032,8 +991,7 @@ } - (void)dealloc { - if ((description_->dataType == GPBDataTypeBytes) && - !GPBExtensionIsRepeated(description_)) { + if ((description_->dataType == GPBDataTypeBytes) && !GPBExtensionIsRepeated(description_)) { [defaultValue_.valueData release]; } [super dealloc]; @@ -1046,7 +1004,7 @@ } - (NSString *)singletonName { - return (NSString * _Nonnull)@(description_->singletonName); + return (NSString *_Nonnull)@(description_->singletonName); } - (const char *)singletonNameC { @@ -1062,15 +1020,12 @@ } - (GPBWireFormat)wireType { - return GPBWireFormatForType(description_->dataType, - GPBExtensionIsPacked(description_)); + return GPBWireFormatForType(description_->dataType, GPBExtensionIsPacked(description_)); } - (GPBWireFormat)alternateWireType { - NSAssert(GPBExtensionIsRepeated(description_), - @"Only valid on repeated extensions"); - return GPBWireFormatForType(description_->dataType, - !GPBExtensionIsPacked(description_)); + NSAssert(GPBExtensionIsRepeated(description_), @"Only valid on repeated extensions"); + return GPBWireFormatForType(description_->dataType, !GPBExtensionIsPacked(description_)); } - (BOOL)isRepeated { @@ -1126,8 +1081,7 @@ return @(defaultValue_.valueUInt64); case GPBDataTypeBytes: // Like message fields, the default is zero length data. - return (defaultValue_.valueData ? defaultValue_.valueData - : GPBEmptyNSData()); + return (defaultValue_.valueData ? defaultValue_.valueData : GPBEmptyNSData()); case GPBDataTypeString: // Like message fields, the default is zero length string. return (defaultValue_.valueString ? defaultValue_.valueString : @"");