xref: /llvm-project/clang/test/SemaObjCXX/overload.mm (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s
2@interface Foo
3@end
4
5@implementation Foo
6
7void func(id);
8
9+ zone {
10 func(self);
11 return self;
12}
13@end
14
15@protocol P0
16@end
17
18@protocol P1
19@end
20
21@interface A <P0>
22@end
23
24@interface B : A
25@end
26
27@interface C <P1>
28@end
29
30int& f(A*); // expected-note {{candidate}}
31float& f(B*); // expected-note {{candidate}}
32void g(A*);
33
34int& h(A*);
35float& h(id);
36
37void test0(A* a, B* b, id val) {
38  int& i1 = f(a);
39  float& f1 = f(b);
40
41  // GCC succeeds here, which is clearly ridiculous.
42  float& f2 = f(val); // expected-error {{ambiguous}}
43
44  g(a);
45  g(b);
46  g(val);
47  int& i2 = h(a);
48  float& f3 = h(val);
49
50  int& i3 = h(b);
51}
52
53void test1(A* a) {
54  B* b = a; // expected-warning{{incompatible pointer types initializing 'B *' with an expression of type 'A *'}}
55  B *c; c = a; // expected-warning{{incompatible pointer types assigning to 'B *' from 'A *'}}
56}
57
58void test2(A** ap) {
59  B** bp = ap; // expected-warning{{incompatible pointer types initializing 'B **' with an expression of type 'A **'}}
60  bp = ap; // expected-warning{{incompatible pointer types assigning to 'B **' from 'A **'}}
61}
62
63int& cv(A*);
64float& cv(const A*);
65
66int& cv2(void*);
67float& cv2(const void*);
68
69void cv_test(A* a, B* b, const A* ac, const B* bc) {
70  int &i1 = cv(a);
71  int &i2 = cv(b);
72  float &f1 = cv(ac);
73  float &f2 = cv(bc);
74  int& i3 = cv2(a);
75  float& f3 = cv2(ac);
76}
77
78int& qualid(id<P0>);
79float& qualid(id<P1>);
80
81void qualid_test(A *a, B *b, C *c) {
82  int& i1 = qualid(a);
83  int& i2 = qualid(b);
84
85  float& f1 = qualid(c);
86
87  id<P0> p1 = 0;
88  p1 = 0;
89}
90
91
92@class NSException;
93typedef struct {
94    void (*throw_exc)(id);
95}
96objc_exception_functions_t;
97
98void (*_NSExceptionRaiser(void))(NSException *) {
99    objc_exception_functions_t exc_funcs;
100    return exc_funcs.throw_exc; // expected-warning{{incompatible pointer types returning 'void (*)(id)' from a function with result type 'void (*)(NSException *)'}}
101}
102
103namespace test5 {
104  void foo(bool);
105  void foo(void *);
106
107  void test(id p) {
108    foo(p);
109  }
110}
111
112namespace test6 {
113  void foo(id);
114  void foo(A*) __attribute__((unavailable)); // expected-note {{marked unavailable here}}
115
116  void test(B *b) {
117    foo(b); // expected-error {{'foo' is unavailable}}
118  }
119}
120
121namespace rdar8714395 {
122  int &f(const void*);
123  float &f(const Foo*);
124
125  int &f2(const void*);
126  float &f2(Foo const* const *);
127
128  int &f3(const void*);
129  float &f3(Foo const**);
130
131  void g(Foo *p) {
132    float &fr = f(p);
133    float &fr2 = f2(&p);
134    int &ir = f3(&p);
135  }
136
137
138}
139
140namespace rdar8734046 {
141  void f1(id);
142  void f2(id<P0>);
143  void g(const A *a) {
144    f1(a);
145    f2(a);
146  }
147}
148
149namespace PR9735 {
150  int &f3(const A*);
151  float &f3(const void*);
152
153  void test_f(B* b, const B* bc) {
154    int &ir1 = f3(b);
155    int &ir2 = f3(bc);
156  }
157}
158
159@interface D : B
160@end
161
162namespace rdar9327203 {
163  int &f(void* const&, int);
164  float &f(void* const&, long);
165
166  void g(id x) {
167    int &fr = (f)(x, 0);
168  }
169}
170
171namespace class_id {
172  // it's okay to overload Class with id.
173  void f(Class) { }
174  void f(id) { }
175}
176
177@interface NSDictionary<__covariant KeyType, __covariant ObjectType> : A
178@end
179
180@interface NSMutableDictionary<KeyType, ObjectType> : NSDictionary<KeyType, ObjectType>
181@end
182
183namespace rdar20124827 {
184
185int overload(NSDictionary *) { return 1; }
186
187__attribute__((deprecated))  // expected-note {{'overload' has been explicitly marked deprecated here}}
188int overload(NSMutableDictionary *) { return 0; }
189
190__attribute__((deprecated))
191void overload2(NSDictionary *); // expected-note {{candidate function}}
192void overload2(NSDictionary<A *, A *> *); // expected-note {{candidate function}}
193
194void test(NSDictionary *d1, NSDictionary<A *, A *> *d2, NSMutableDictionary<A *, A *> *m1) {
195  overload(d1);
196  overload(d2); // no warning
197  overload(m1); // expected-warning {{'overload' is deprecated}}
198  overload2(d2); // no warning
199  overload2(m1); // expected-error {{call to 'overload2' is ambiguous}}
200}
201
202}
203
204namespace StringLiterals {
205void f(const char(&&)[5]);
206void f(const wchar_t(&&)[5]);
207void f(const char16_t(&&)[5]);
208void f(const char32_t(&&)[5]);
209void g() {
210  f({"abc"});
211  f({(((@encode(int))))});
212  f({L"abc"});
213  f({uR"(abc)"});
214  f({(UR"(abc)")});
215}
216} // namespace StringLiterals
217