xref: /llvm-project/clang/test/SemaObjC/class-message-protocol-lookup.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1  -fsyntax-only -verify -Wno-objc-root-class %s
2
3@interface RandomObject {
4@private
5}
6+ (id)alloc;
7@end
8
9@protocol TestProtocol
10- (void)nothingInteresting;
11@end
12
13@protocol Test2Protocol
14+ (id)alloc;
15- (id)alloc2; // expected-note 2 {{method 'alloc2' declared here}}
16@end
17
18@implementation RandomObject
19- (void) Meth {
20    Class<TestProtocol> c = [c alloc]; //  expected-warning {{class method '+alloc' not found (return type defaults to 'id')}}
21    Class<Test2Protocol> c1 = [c1 alloc2]; //  expected-warning {{instance method 'alloc2' found instead of class method 'alloc2'}}
22    Class<Test2Protocol> c2 = [c2 alloc]; //  ok
23}
24+ (id)alloc { return 0; }
25@end
26
27int main (void)
28{
29    Class<TestProtocol> c = [c alloc]; //  expected-warning {{class method '+alloc' not found (return type defaults to 'id')}}
30    Class<Test2Protocol> c1 = [c1 alloc2]; //  expected-warning {{instance method 'alloc2' found instead of class method 'alloc2'}}
31    Class<Test2Protocol> c2 = [c2 alloc]; //  ok
32    return 0;
33}
34
35@protocol NSObject
36
37- (int)respondsToSelector:(SEL)aSelector;
38
39@end
40
41__attribute__((objc_root_class))
42@interface NSObject <NSObject>
43
44@end
45
46@protocol OtherProto
47
48- (void)otherInstanceMethod; // expected-note {{method 'otherInstanceMethod' declared here}}
49
50@end
51
52@protocol MyProto <NSObject, OtherProto>
53@end
54
55void allowInstanceMethodsFromRootProtocols(Class<MyProto> c) {
56  [c respondsToSelector: @selector(instanceMethod)]; // no warning
57  [c otherInstanceMethod]; //  expected-warning {{instance method 'otherInstanceMethod' found instead of class method 'otherInstanceMethod'}}
58}
59