8sa1-gcc/gcc/testsuite/objc.dg/gnu-api-2-method.m
Iain Sandoe d35fbf7f76 Objective-C [2/3] : Implement Wobjc-root-class [PR77404].
Add "Wno-objc-root-class" to tests in the Objective-C suite where
needed.

gcc/testsuite/ChangeLog:

	PR objc/77404
	* objc.dg/anon-1.m: Add Wno-objc-root-class.
	* objc.dg/attributes/class-attribute-1.m: Likewise.
	* objc.dg/attributes/class-attribute-2.m: Likewise.
	* objc.dg/attributes/class-attribute-3.m: Likewise.
	* objc.dg/attributes/method-deprecated-1.m: Likewise.
	* objc.dg/attributes/method-deprecated-2.m: Likewise.
	* objc.dg/attributes/method-deprecated-3.m: Likewise.
	* objc.dg/attributes/method-format-1.m: Likewise.
	* objc.dg/attributes/method-nonnull-1.m: Likewise.
	* objc.dg/attributes/method-noreturn-1.m: Likewise.
	* objc.dg/attributes/method-sentinel-1.m: Likewise.
	* objc.dg/attributes/nsobject-01.m: Likewise.
	* objc.dg/attributes/objc-exception-1.m: Likewise.
	* objc.dg/attributes/parameter-attribute-1.m: Likewise.
	* objc.dg/attributes/parameter-attribute-2.m: Likewise.
	* objc.dg/attributes/proto-attribute-1.m: Likewise.
	* objc.dg/attributes/proto-attribute-2.m: Likewise.
	* objc.dg/attributes/proto-attribute-3.m: Likewise.
	* objc.dg/attributes/proto-attribute-4.m: Likewise.
	* objc.dg/bitfield-2.m: Likewise.
	* objc.dg/break-in-ifstmt.m: Likewise.
	* objc.dg/class-1.m: Likewise.
	* objc.dg/class-extension-1.m: Likewise.
	* objc.dg/class-extension-2.m: Likewise.
	* objc.dg/class-extension-3.m: Likewise.
	* objc.dg/class-extension-4.m: Likewise.
	* objc.dg/class-protocol-1.m: Likewise.
	* objc.dg/comp-types-7.m: Likewise.
	* objc.dg/demangle-1.m: Likewise.
	* objc.dg/duplicate-class-1.m: Likewise.
	* objc.dg/encode-6-next.m: Likewise.
	* objc.dg/encode-6.m: Likewise.
	* objc.dg/enhanced-proto-2.m: Likewise.
	* objc.dg/exceptions-1.m: Likewise.
	* objc.dg/exceptions-3.m: Likewise.
	* objc.dg/exceptions-4.m: Likewise.
	* objc.dg/exceptions-5.m: Likewise.
	* objc.dg/fobjc-std-1.m: Likewise.
	* objc.dg/foreach-2.m: Likewise.
	* objc.dg/foreach-4.m: Likewise.
	* objc.dg/foreach-5.m: Likewise.
	* objc.dg/fsyntax-only.m: Likewise.
	* objc.dg/gnu-api-2-class-meta.m: Likewise.
	* objc.dg/gnu-api-2-class.m: Likewise.
	* objc.dg/gnu-api-2-ivar.m: Likewise.
	* objc.dg/gnu-api-2-method.m: Likewise.
	* objc.dg/gnu-api-2-objc.m: Likewise.
	* objc.dg/gnu-api-2-objc_msg_lookup.m: Likewise.
	* objc.dg/gnu-api-2-object.m: Likewise.
	* objc.dg/gnu-api-2-property.m: Likewise.
	* objc.dg/gnu-api-2-protocol.m: Likewise.
	* objc.dg/gnu-api-2-resolve-method.m: Likewise.
	* objc.dg/gnu-api-2-sel.m: Likewise.
	* objc.dg/incomplete-type-1.m: Likewise.
	* objc.dg/instancetype-0.m: Likewise.
	* objc.dg/invalid-method-2.m: Likewise.
	* objc.dg/ivar-invalid-type-1.m: Likewise.
	* objc.dg/ivar-problem-1.m: Likewise.
	* objc.dg/ivar-scope-1.m: Likewise.
	* objc.dg/ivar-scope-2.m: Likewise.
	* objc.dg/ivar-scope-4.m: Likewise.
	* objc.dg/ivar-visibility-1.m: Likewise.
	* objc.dg/ivar-visibility-2.m: Likewise.
	* objc.dg/ivar-visibility-3.m: Likewise.
	* objc.dg/ivar-visibility-4.m: Likewise.
	* objc.dg/local-decl-1.m: Likewise.
	* objc.dg/lto/lto.exp: Likewise.
	* objc.dg/lto/trivial-1_0.m: Likewise.
	* objc.dg/method-1.m: Likewise.
	* objc.dg/method-12.m: Likewise.
	* objc.dg/method-13.m: Likewise.
	* objc.dg/method-14.m: Likewise.
	* objc.dg/missing-proto-3.m: Likewise.
	* objc.dg/next-runtime-1.m: Likewise.
	* objc.dg/objc-foreach-1.m: Likewise.
	* objc.dg/objc-foreach-2.m: Likewise.
	* objc.dg/objc-foreach-3.m: Likewise.
	* objc.dg/objc-nofilename-1.m: Likewise.
	* objc.dg/param-1.m: Likewise.
	* objc.dg/pch/pch.exp: Likewise.
	* objc.dg/plugin/diagnostic-test-expressions-1.m: Likewise.
	* objc.dg/pr23709.m: Likewise.
	* objc.dg/private-1.m: Likewise.
	* objc.dg/private-2.m: Likewise.
	* objc.dg/property/property.exp: Likewise.
	* objc.dg/proto-hier-1.m: Likewise.
	* objc.dg/proto-hier-2.m: Likewise.
	* objc.dg/proto-lossage-1.m: Likewise.
	* objc.dg/proto-lossage-5.m: Likewise.
	* objc.dg/proto-qual-1.m: Likewise.
	* objc.dg/protocol-inheritance-1.m: Likewise.
	* objc.dg/protocol-inheritance-2.m: Likewise.
	* objc.dg/protocol-optional-1.m: Likewise.
	* objc.dg/selector-1.m: Likewise.
	* objc.dg/selector-2.m: Likewise.
	* objc.dg/selector-3.m: Likewise.
	* objc.dg/selector-4.m: Likewise.
	* objc.dg/shadow-1.m: Likewise.
	* objc.dg/shadow-2.m: Likewise.
	* objc.dg/special/load-category-1.m: Likewise.
	* objc.dg/special/load-category-2.m: Likewise.
	* objc.dg/special/load-category-3.m: Likewise.
	* objc.dg/special/special.exp: Likewise.
	* objc.dg/special/unclaimed-category-1.h: Likewise.
	* objc.dg/special/unclaimed-category-1.m: Likewise.
	* objc.dg/stabs-1.m: Likewise.
	* objc.dg/strings/strings.exp: Likewise.
	* objc.dg/stubify-1.m: Likewise.
	* objc.dg/stubify-2.m: Likewise.
	* objc.dg/super-class-2.m: Likewise.
	* objc.dg/super-dealloc-1.m: Likewise.
	* objc.dg/super-dealloc-2.m: Likewise.
	* objc.dg/sync-3.m: Likewise.
	* objc.dg/threedotthree-abi-1.m: Likewise.
	* objc.dg/torture/dg-torture.exp: Likewise.
	* objc.dg/torture/strings/strings.exp: Likewise.
	* objc.dg/try-catch-11.m: Likewise.
	* objc.dg/try-catch-12.m: Likewise.
	* objc.dg/type-size-2.m: Likewise.
	* objc.dg/type-size-3.m: Likewise.
	* objc.dg/type-size-4.m: Likewise.
	* objc.dg/type-size-5.m: Likewise.
	* objc.dg/undeclared-selector.m: Likewise.
	* objc.dg/volatile-1.m: Likewise.
2020-11-13 10:40:54 +00:00

232 lines
6.6 KiB
Objective-C

/* Test the Modern GNU Objective-C Runtime API.
This is test 'method', covering all functions starting with 'method'. */
/* { dg-do run } */
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
/* { dg-additional-options "-Wno-objc-root-class" } */
/* To get the modern GNU Objective-C Runtime API, you include
objc/runtime.h. */
#include <objc/runtime.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@interface MyRootClass
{ Class isa; }
+ alloc;
- init;
+ initialize;
@end
@implementation MyRootClass
+ alloc { return class_createInstance (self, 0); }
- init { return self; }
+ initialize { return self; }
@end
@protocol MyProtocol
- (id) variable;
@end
@protocol MySecondProtocol
- (id) setVariable: (id)value;
@end
@interface MySubClass : MyRootClass <MyProtocol>
{ id variable_ivar; }
- (void) setVariable: (id)value;
- (id) variable;
- (id) constant;
@end
@implementation MySubClass
- (void) setVariable: (id)value { variable_ivar = value; }
- (id) variable { return variable_ivar; }
- (id) constant { return nil; }
@end
int main(int argc, void **args)
{
/* Functions are tested in alphabetical order. */
printf ("Testing method_copyArgumentType () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
char *type = method_copyArgumentType (method, 2);
if (type == NULL || type[0] != '@')
abort ();
}
printf ("Testing method_copyReturnType () ...\n");
{
Method method = class_getClassMethod (objc_getClass ("MyRootClass"),
@selector (alloc));
char *type = method_copyReturnType (method);
/* Check that it returns an object. */
if (type == NULL || type[0] != '@')
abort ();
}
printf ("Testing method_exchangeImplementations () ...\n");
{
Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (variable));
Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (constant));
MySubClass *object = [[MySubClass alloc] init];
/* Check that things work as expected before the swap. */
[object setVariable: object];
if ([object variable] != object || [object constant] != nil)
abort ();
/* Swap the methods. */
method_exchangeImplementations (method_a, method_b);
/* Check that behavior has changed. */
if ([object variable] != nil || [object constant] != object)
abort ();
/* Swap the methods again. */
method_exchangeImplementations (method_a, method_b);
/* Check that behavior is back to normal. */
if ([object variable] != object || [object constant] != nil)
abort ();
}
printf ("Testing method_getArgumentType () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MyRootClass"),
@selector (init));
char type[16];
method_getArgumentType (method, 1, type, 16);
/* Check the second argument (_cmd), which should be a SEL. */
if (type[0] != ':')
abort ();
}
printf ("Testing method_getDescription () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (variable));
struct objc_method_description *description = method_getDescription (method);
if (strcmp (sel_getName (description->name), "variable") != 0)
abort ();
if (method_getDescription (NULL) != NULL)
abort ();
}
printf ("Testing method_getImplementation () ...\n");
{
typedef void (*set_variable_function) (id receiver, SEL _cmd, id variable);
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
set_variable_function imp;
MySubClass *object = [[MySubClass alloc] init];
imp = (set_variable_function)(method_getImplementation (method));
(*imp)(object, @selector (setVariable:), object);
if ([object variable] != object)
abort ();
}
printf ("Testing method_getName () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
if (strcmp (sel_getName (method_getName (method)), "setVariable:") != 0)
abort ();
}
printf ("Testing method_getNumberOfArguments () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
if (method_getNumberOfArguments (method) != 3)
abort ();
method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (variable));
if (method_getNumberOfArguments (method) != 2)
abort ();
}
printf ("Testing method_getTypeEncoding () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
const char *types = method_getTypeEncoding (method);
/* Check that method type string starts with 'v' (void) */
if (types == NULL || types[0] != 'v')
abort ();
}
printf ("Testing method_getReturnType () ...\n");
{
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (setVariable:));
char type[16];
method_getReturnType (method, type, 16);
if (type[0] != 'v')
abort ();
method_getReturnType (NULL, type, 16);
if (type[0] != 0)
abort ();
}
printf ("Testing method_setImplementation () ...\n");
{
Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (variable));
Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"),
@selector (constant));
IMP original_imp_a = method_getImplementation (method_a);
IMP original_imp_b = method_getImplementation (method_b);
MySubClass *object = [[MySubClass alloc] init];
/* Check that things work as expected before the swap. */
[object setVariable: object];
if ([object variable] != object || [object constant] != nil)
abort ();
/* Have 'variable' use the same implementation as 'constant'. */
if (method_setImplementation (method_a, original_imp_b) != original_imp_a)
abort ();
/* Check that behavior has changed. */
if ([object variable] != nil || [object constant] != nil)
abort ();
/* Put the original method back. */
if (method_setImplementation (method_a, original_imp_a) != original_imp_b)
abort ();
/* Check that behavior is back to normal. */
if ([object variable] != object || [object constant] != nil)
abort ();
}
return 0;
}