#include "Test.h" #include #include #include #include #include #pragma GCC diagnostic ignored "-Wobjc-property-no-attribute" // Clang < 3 doesn't exist usefully, so we can skip tests for it. Clang 3.5 // adds proper metadata for weak properties, earlier ones don't, so don't fail // the tests because of known compiler bugs. #ifndef __clang_minor__ #define WEAK_ATTR ATTR("W", ""), #define WEAK_STR "W," #elif (__clang_major__ < 4) && (__clang_minor__ < 5) #define WEAK_ATTR #define WEAK_STR #else #define WEAK_ATTR ATTR("W", ""), #define WEAK_STR "W," #endif enum FooManChu { FOO, MAN, CHU }; struct YorkshireTeaStruct { int pot; signed char lady; }; typedef struct YorkshireTeaStruct YorkshireTeaStructType; union MoneyUnion { float alone; double down; }; #ifndef __has_attribute #define __has_attribute(x) 0 #endif #if __has_attribute(objc_root_class) __attribute__((objc_root_class)) #endif @interface PropertyTest { @public Class isa; atomic_bool atomicBoolDefault; signed char charDefault; double doubleDefault; enum FooManChu enumDefault; float floatDefault; int intDefault; long longDefault; short shortDefault; signed signedDefault; struct YorkshireTeaStruct structDefault; YorkshireTeaStructType typedefDefault; union MoneyUnion unionDefault; unsigned unsignedDefault; int (*functionPointerDefault)(char *); int *intPointer; void *voidPointerDefault; int intSynthEquals; int intSetterGetter; int intReadonly; int intReadonlyGetter; int intReadwrite; int intAssign; __unsafe_unretained id idDefault; id idRetain; id idCopy; __weak id idWeak; id idStrong; int intNonatomic; id idReadonlyCopyNonatomic; id idReadonlyRetainNonatomic; __weak id idReadonlyWeakNonatomic; id _idOther; } @property atomic_bool atomicBoolDefault; @property signed char charDefault; @property double doubleDefault; @property enum FooManChu enumDefault; @property float floatDefault; @property int intDefault; @property long longDefault; @property short shortDefault; @property signed signedDefault; @property struct YorkshireTeaStruct structDefault; @property YorkshireTeaStructType typedefDefault; @property union MoneyUnion unionDefault; @property unsigned unsignedDefault; @property int (*functionPointerDefault)(char *); @property int *intPointer; @property void *voidPointerDefault; @property(getter=intGetFoo, setter=intSetFoo:) int intSetterGetter; @property(readonly) int intReadonly; @property(getter=isIntReadOnlyGetter, readonly) int intReadonlyGetter; @property(readwrite) int intReadwrite; @property(assign) int intAssign; @property(unsafe_unretained) id idDefault; @property(retain) id idRetain; @property(copy) id idCopy; @property(weak) id idWeak; @property(strong) id idStrong; @property(nonatomic) int intNonatomic; @property(nonatomic, readonly, copy) id idReadonlyCopyNonatomic; @property(nonatomic, readonly, retain) id idReadonlyRetainNonatomic; @property(nonatomic, readonly, weak) id idReadonlyWeakNonatomic; @property(retain) id idOther; @property(retain) id idDynamic; @property(retain, nonatomic, getter=dynamicGetterSetter, setter=setDynamicGetterSetter:) id idDynamicGetterSetter; @end @interface PropertyTest (Informal) - (void)setStructDefault2: (struct YorkshireTeaStruct)tp; - (void)setIntDefault2: (int)i; - (struct YorkshireTeaStruct)structDefault2; - (int)intDefault2; @end @implementation PropertyTest @synthesize atomicBoolDefault; @synthesize charDefault; @synthesize doubleDefault; @synthesize enumDefault; @synthesize floatDefault; @synthesize intDefault; @synthesize longDefault; @synthesize shortDefault; @synthesize signedDefault; @synthesize structDefault; @synthesize typedefDefault; @synthesize unionDefault; @synthesize unsignedDefault; @synthesize functionPointerDefault; @synthesize intPointer; @synthesize voidPointerDefault; @synthesize intSetterGetter; @synthesize intReadonly; @synthesize intReadonlyGetter; @synthesize intReadwrite; @synthesize intAssign; @synthesize idDefault; @synthesize idRetain; @synthesize idCopy; @synthesize idWeak; @synthesize idStrong; @synthesize intNonatomic; @synthesize idReadonlyCopyNonatomic; @synthesize idReadonlyRetainNonatomic; @synthesize idReadonlyWeakNonatomic; @synthesize idOther = _idOther; @dynamic idDynamic; @dynamic idDynamicGetterSetter; - (void)_ARCCompliantRetainRelease {} @end @protocol ProtocolTest @property atomic_bool atomicBoolDefault; @property signed char charDefault; @property double doubleDefault; @property enum FooManChu enumDefault; @property float floatDefault; @property int intDefault; @property long longDefault; @property short shortDefault; @property signed signedDefault; @property struct YorkshireTeaStruct structDefault; @property YorkshireTeaStructType typedefDefault; @property union MoneyUnion unionDefault; @property unsigned unsignedDefault; @property int (*functionPointerDefault)(char *); @property int *intPointer; @property void *voidPointerDefault; @property(getter=intGetFoo, setter=intSetFoo:) int intSetterGetter; @property(readonly) int intReadonly; @property(getter=isIntReadOnlyGetter, readonly) int intReadonlyGetter; @property(readwrite) int intReadwrite; @property(assign) int intAssign; @property(unsafe_unretained) id idDefault; @property(retain) id idRetain; @property(copy) id idCopy; @property(weak) id idWeak; @property(strong) id idStrong; @property(nonatomic) int intNonatomic; @property(nonatomic, readonly, copy) id idReadonlyCopyNonatomic; @property(nonatomic, readonly, retain) id idReadonlyRetainNonatomic; @property(nonatomic, readonly, weak) id idReadonlyWeakNonatomic; @property(retain) id idOther; @property(retain) id idDynamic; @property(retain, nonatomic, getter=dynamicGetterSetter, setter=setDynamicGetterSetter:) id idDynamicGetterSetter; @end #if __has_attribute(objc_root_class) __attribute__((objc_root_class)) #endif @interface PropertyProtocolTest { Class isa; atomic_bool atomicBoolDefault; signed char charDefault; double doubleDefault; enum FooManChu enumDefault; float floatDefault; int intDefault; long longDefault; short shortDefault; signed signedDefault; struct YorkshireTeaStruct structDefault; YorkshireTeaStructType typedefDefault; union MoneyUnion unionDefault; unsigned unsignedDefault; int (*functionPointerDefault)(char *); int *intPointer; void *voidPointerDefault; int intSynthEquals; int intSetterGetter; int intReadonly; int intReadonlyGetter; int intReadwrite; int intAssign; __unsafe_unretained id idDefault; id idRetain; id idCopy; __weak id idWeak; id idStrong; int intNonatomic; id idReadonlyCopyNonatomic; id idReadonlyRetainNonatomic; __weak id idReadonlyWeakNonatomic; id _idOther; } @end @implementation PropertyProtocolTest @synthesize atomicBoolDefault; @synthesize charDefault; @synthesize doubleDefault; @synthesize enumDefault; @synthesize floatDefault; @synthesize intDefault; @synthesize longDefault; @synthesize shortDefault; @synthesize signedDefault; @synthesize structDefault; @synthesize typedefDefault; @synthesize unionDefault; @synthesize unsignedDefault; @synthesize functionPointerDefault; @synthesize intPointer; @synthesize voidPointerDefault; @synthesize intSetterGetter; @synthesize intReadonly; @synthesize intReadonlyGetter; @synthesize intReadwrite; @synthesize intAssign; @synthesize idDefault; @synthesize idRetain; @synthesize idCopy; @synthesize idWeak; @synthesize idStrong; @synthesize intNonatomic; @synthesize idReadonlyCopyNonatomic; @synthesize idReadonlyRetainNonatomic; @synthesize idReadonlyWeakNonatomic; @synthesize idOther = _idOther; @dynamic idDynamic; @dynamic idDynamicGetterSetter; - (void)_ARCCompliantRetainRelease {} @end #define ATTR(n, v) (objc_property_attribute_t){(n), (v)} #define ATTRS(...) (objc_property_attribute_t[]){ __VA_ARGS__ }, \ sizeof((objc_property_attribute_t[]){ __VA_ARGS__ }) / sizeof(objc_property_attribute_t) #define OPT_ASSERT(stmt) if (abort) { \ assert(stmt);\ } else { \ if (!(stmt)) { return NO; } \ } static BOOL testPropertyForProperty_alt(objc_property_t p, const char *name, const char *types, objc_property_attribute_t* list, unsigned int size, BOOL abort) { OPT_ASSERT(0 != p); OPT_ASSERT(strcmp(name, property_getName(p)) == 0); const char *attrs = property_getAttributes(p); OPT_ASSERT(0 != attrs); OPT_ASSERT(strcmp(types, attrs) == 0); unsigned int attrsCount = 0; objc_property_attribute_t *attrsList = property_copyAttributeList(p, &attrsCount); OPT_ASSERT(0 != attrsList); OPT_ASSERT(attrsCount == size); for (unsigned int index=0; indexname != NULL) && (attrsCount < size); attrsCount++, ra++) {} OPT_ASSERT(attrsCount == size); free(attrsList); for (unsigned int index=0; indexidRetain == testValue); assert(t->_idOther == nil); Method idRetainSetter = class_getInstanceMethod(testClass, @selector(setIdRetain:)); Method idOtherSetter = class_getInstanceMethod(testClass, @selector(setIdOther:)); method_setImplementation(idRetainSetter, method_getImplementation(idOtherSetter)); idRetainSetter = class_getInstanceMethod(testClass, @selector(setIdRetain:)); id testValue2 = [Test new]; t.idRetain = testValue2; assert(t->idRetain == testValue); assert(t->_idOther == testValue2); return 0; }