xref: /aosp_15_r20/external/clang/test/SemaObjC/objc-class-property.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -fsyntax-only -verify %s
2*67e74705SXin Li
3*67e74705SXin Li#if !__has_feature(objc_class_property)
4*67e74705SXin Li#error does not support class property
5*67e74705SXin Li#endif
6*67e74705SXin Li
7*67e74705SXin Li@interface Root
8*67e74705SXin Li-(id) alloc;
9*67e74705SXin Li-(id) init;
10*67e74705SXin Li@end
11*67e74705SXin Li
12*67e74705SXin Li@interface A : Root {
13*67e74705SXin Li  int x;
14*67e74705SXin Li  int z;
15*67e74705SXin Li}
16*67e74705SXin Li@property int x;
17*67e74705SXin Li@property int y;
18*67e74705SXin Li@property int z;
19*67e74705SXin Li@property(readonly) int ro, ro2;
20*67e74705SXin Li@property (class) int c;
21*67e74705SXin Li@property (class) int c2; // expected-note {{property declared here}} \
22*67e74705SXin Li                          // expected-note {{property declared here}}
23*67e74705SXin Li@property (class) int x;
24*67e74705SXin Li@end
25*67e74705SXin Li
26*67e74705SXin Li@implementation A // expected-warning {{class property 'c2' requires method 'c2' to be defined}} \
27*67e74705SXin Li                  // expected-warning {{class property 'c2' requires method 'setC2:' to be defined}}
28*67e74705SXin Li@dynamic x; // refers to the instance property
29*67e74705SXin Li@dynamic (class) x; // refers to the class property
30*67e74705SXin Li@synthesize z, c2; // expected-error {{@synthesize not allowed on a class property 'c2'}}
31*67e74705SXin Li@dynamic c; // refers to the class property
32*67e74705SXin Li@end
33*67e74705SXin Li
34*67e74705SXin Liint test() {
35*67e74705SXin Li  A *a = [[A alloc] init];
36*67e74705SXin Li  a.c; // expected-error {{property 'c' is a class property; did you mean to access it with class 'A'}}
37*67e74705SXin Li  return a.x + A.c;
38*67e74705SXin Li}
39*67e74705SXin Li
40*67e74705SXin Livoid message_id(id me) {
41*67e74705SXin Li  [me y];
42*67e74705SXin Li}
43*67e74705SXin Li
44*67e74705SXin Livoid message_class(Class me) {
45*67e74705SXin Li  [me c2];
46*67e74705SXin Li}
47*67e74705SXin Li
48*67e74705SXin Li@interface NSObject
49*67e74705SXin Li@end
50*67e74705SXin Li
51*67e74705SXin Li@interface MyClass : NSObject
52*67e74705SXin Li@property(class, readonly) int classProp; // expected-note {{property declared here}}
53*67e74705SXin Li@end
54*67e74705SXin Li
55*67e74705SXin Li@implementation MyClass // expected-warning {{class property 'classProp' requires method 'classProp' to be defined}}
56*67e74705SXin Li- (int)classProp { // Oops, mistakenly made this an instance method.
57*67e74705SXin Li  return 8;
58*67e74705SXin Li}
59*67e74705SXin Li@end
60