ivar.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. TEST_BUILD_OUTPUT
  3. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  4. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  5. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  6. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  7. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  8. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\]
  9. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  10. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  11. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  12. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  13. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  14. .*ivar.m:\d+:\d+: warning: null passed to a callee that requires a non-null argument \[-Wnonnull\](\n.* note: expanded from macro 'testassert')?
  15. END
  16. */
  17. #include "test.h"
  18. #include "testroot.i"
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <objc/objc-runtime.h>
  22. @interface Super : TestRoot {
  23. @public
  24. char superIvar;
  25. }
  26. @end
  27. @interface Sub : Super {
  28. @public
  29. id subIvar;
  30. }
  31. @end
  32. @implementation Super @end
  33. @implementation Sub @end
  34. int main()
  35. {
  36. /*
  37. Runtime layout of Sub:
  38. [0] isa
  39. [1] superIvar
  40. [2] subIvar
  41. */
  42. Ivar ivar;
  43. Sub *sub = [Sub new];
  44. sub->subIvar = [Sub class];
  45. testassert(((Class *)(__bridge void *)sub)[2] == [Sub class]);
  46. ivar = class_getInstanceVariable([Sub class], "subIvar");
  47. testassert(ivar);
  48. testassert(2*sizeof(intptr_t) == (size_t)ivar_getOffset(ivar));
  49. testassert(0 == strcmp(ivar_getName(ivar), "subIvar"));
  50. testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "@"));
  51. ivar = class_getInstanceVariable([Super class], "superIvar");
  52. testassert(ivar);
  53. testassert(sizeof(intptr_t) == (size_t)ivar_getOffset(ivar));
  54. testassert(0 == strcmp(ivar_getName(ivar), "superIvar"));
  55. testassert(0 == strcmp(ivar_getTypeEncoding(ivar), "c"));
  56. testassert(ivar == class_getInstanceVariable([Sub class], "superIvar"));
  57. ivar = class_getInstanceVariable([Super class], "subIvar");
  58. testassert(!ivar);
  59. ivar = class_getInstanceVariable(object_getClass([Sub class]), "subIvar");
  60. testassert(!ivar);
  61. ivar = class_getInstanceVariable([Sub class], "subIvar");
  62. object_setIvar(sub, ivar, sub);
  63. testassert(sub->subIvar == sub);
  64. testassert(sub == object_getIvar(sub, ivar));
  65. testassert(NULL == class_getInstanceVariable(NULL, "foo"));
  66. testassert(NULL == class_getInstanceVariable([Sub class], NULL));
  67. testassert(NULL == class_getInstanceVariable(NULL, NULL));
  68. testassert(NULL == object_getIvar(sub, NULL));
  69. testassert(NULL == object_getIvar(NULL, ivar));
  70. testassert(NULL == object_getIvar(NULL, NULL));
  71. object_setIvar(sub, NULL, NULL);
  72. object_setIvar(NULL, ivar, NULL);
  73. object_setIvar(NULL, NULL, NULL);
  74. #if !__has_feature(objc_arc)
  75. uintptr_t value;
  76. sub->subIvar = (id)10;
  77. value = 0;
  78. object_getInstanceVariable(sub, "subIvar", (void **)&value);
  79. testassert(value == 10);
  80. object_setInstanceVariable(sub, "subIvar", (id)11);
  81. testassert(sub->subIvar == (id)11);
  82. ivar = class_getInstanceVariable([Sub class], "subIvar");
  83. testassert(ivar == object_getInstanceVariable(sub, "subIvar", NULL));
  84. testassert(NULL == object_getInstanceVariable(sub, NULL, NULL));
  85. testassert(NULL == object_getInstanceVariable(NULL, "foo", NULL));
  86. testassert(NULL == object_getInstanceVariable(NULL, NULL, NULL));
  87. value = 10;
  88. testassert(NULL == object_getInstanceVariable(sub, NULL, (void **)&value));
  89. testassert(value == 0);
  90. value = 10;
  91. testassert(NULL == object_getInstanceVariable(NULL, "foo", (void **)&value));
  92. testassert(value == 0);
  93. value = 10;
  94. testassert(NULL == object_getInstanceVariable(NULL, NULL, (void **)&value));
  95. testassert(value == 0);
  96. testassert(NULL == object_setInstanceVariable(sub, NULL, NULL));
  97. testassert(NULL == object_setInstanceVariable(NULL, "foo", NULL));
  98. testassert(NULL == object_setInstanceVariable(NULL, NULL, NULL));
  99. #else
  100. // provoke the same nullability warnings as the real test
  101. objc_getClass(nil);
  102. objc_getClass(nil);
  103. objc_getClass(nil);
  104. objc_getClass(nil);
  105. objc_getClass(nil);
  106. objc_getClass(nil);
  107. #endif
  108. succeed(__FILE__);
  109. return 0;
  110. }