Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2 | @interface Foo |
| 3 | @end |
| 4 | |
| 5 | @implementation Foo |
| 6 | |
| 7 | void func(id); |
| 8 | |
| 9 | + zone { |
| 10 | func(self); |
| 11 | return self; |
| 12 | } |
Douglas Gregor | a46969d | 2008-12-19 19:16:37 +0000 | [diff] [blame] | 13 | @end |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 14 | |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 15 | @protocol P0 |
| 16 | @end |
| 17 | |
| 18 | @protocol P1 |
| 19 | @end |
| 20 | |
| 21 | @interface A <P0> |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 22 | @end |
| 23 | |
| 24 | @interface B : A |
| 25 | @end |
| 26 | |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 27 | @interface C <P1> |
| 28 | @end |
| 29 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 30 | int& f(A*); // expected-note {{candidate}} |
| 31 | float& f(B*); // expected-note {{candidate}} |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 32 | void g(A*); |
| 33 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 34 | int& h(A*); |
| 35 | float& h(id); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 36 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 37 | void test0(A* a, B* b, id val) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 38 | int& i1 = f(a); |
| 39 | float& f1 = f(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 40 | |
| 41 | // GCC succeeds here, which is clearly ridiculous. |
| 42 | float& f2 = f(val); // expected-error {{ambiguous}} |
| 43 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 44 | g(a); |
| 45 | g(b); |
| 46 | g(val); |
| 47 | int& i2 = h(a); |
| 48 | float& f3 = h(val); |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 50 | int& i3 = h(b); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 51 | } |
| 52 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 53 | void test1(A* a) { |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 54 | B* b = a; // expected-warning{{incompatible pointer types initializing 'B *' with an expression of type 'A *'}} |
Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 55 | B *c; c = a; // expected-warning{{incompatible pointer types assigning to 'B *' from 'A *'}} |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 56 | } |
| 57 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 58 | void test2(A** ap) { |
Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 59 | B** bp = ap; // expected-warning{{incompatible pointer types initializing 'B **' with an expression of type 'A **'}} |
Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 60 | bp = ap; // expected-warning{{incompatible pointer types assigning to 'B **' from 'A **'}} |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // FIXME: we should either allow overloading here or give a better diagnostic |
| 64 | int& cv(A*); // expected-note {{previous declaration}} expected-note 2 {{not viable}} |
| 65 | float& cv(const A*); // expected-error {{cannot be overloaded}} |
| 66 | |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 67 | int& cv2(void*); |
| 68 | float& cv2(const void*); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 69 | |
| 70 | void cv_test(A* a, B* b, const A* ac, const B* bc) { |
| 71 | int &i1 = cv(a); |
| 72 | int &i2 = cv(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 73 | float &f1 = cv(ac); // expected-error {{no matching function}} |
| 74 | float &f2 = cv(bc); // expected-error {{no matching function}} |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 75 | int& i3 = cv2(a); |
| 76 | float& f3 = cv2(ac); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 77 | } |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 78 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 79 | // We agree with GCC that these can't be overloaded. |
| 80 | int& qualid(id<P0>); // expected-note {{previous declaration}} expected-note {{not viable}} |
| 81 | float& qualid(id<P1>); // expected-error {{cannot be overloaded}} |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 82 | |
| 83 | void qualid_test(A *a, B *b, C *c) { |
| 84 | int& i1 = qualid(a); |
| 85 | int& i2 = qualid(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 86 | |
| 87 | // This doesn't work only because the overload was rejected above. |
| 88 | float& f1 = qualid(c); // expected-error {{no matching function}} |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 89 | |
| 90 | id<P0> p1 = 0; |
| 91 | p1 = 0; |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 92 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | @class NSException; |
| 96 | typedef struct { |
| 97 | void (*throw_exc)(id); |
| 98 | } |
| 99 | objc_exception_functions_t; |
| 100 | |
| 101 | void (*_NSExceptionRaiser(void))(NSException *) { |
| 102 | objc_exception_functions_t exc_funcs; |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 103 | return exc_funcs.throw_exc; // expected-warning{{incompatible pointer types returning 'void (*)(id)' from a function with result type 'void (*)(NSException *)'}} |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 104 | } |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 105 | |
| 106 | namespace test5 { |
| 107 | void foo(bool); |
| 108 | void foo(void *); |
| 109 | |
| 110 | void test(id p) { |
| 111 | foo(p); |
| 112 | } |
| 113 | } |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 114 | |
| 115 | // rdar://problem/8592139 |
| 116 | namespace test6 { |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 117 | void foo(id); // expected-note{{candidate function}} |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 118 | void foo(A*) __attribute__((unavailable)); // expected-note {{explicitly made unavailable}} |
| 119 | |
| 120 | void test(B *b) { |
Douglas Gregor | 395cc37 | 2011-01-31 18:51:41 +0000 | [diff] [blame] | 121 | foo(b); // expected-error {{call to unavailable function 'foo'}} |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
Douglas Gregor | da80f74 | 2010-12-01 21:43:58 +0000 | [diff] [blame] | 124 | |
| 125 | namespace rdar8714395 { |
| 126 | int &f(const void*); |
| 127 | float &f(const Foo*); |
| 128 | |
| 129 | int &f2(const void*); |
| 130 | float &f2(Foo const* const *); |
| 131 | |
| 132 | int &f3(const void*); |
| 133 | float &f3(Foo const**); |
| 134 | |
| 135 | void g(Foo *p) { |
| 136 | float &fr = f(p); |
| 137 | float &fr2 = f2(&p); |
| 138 | int &ir = f3(&p); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | } |
Douglas Gregor | 143c7ac | 2010-12-06 22:09:19 +0000 | [diff] [blame] | 143 | |
| 144 | namespace rdar8734046 { |
| 145 | void f1(id); |
| 146 | void f2(id<P0>); |
| 147 | void g(const A *a) { |
| 148 | f1(a); |
| 149 | f2(a); |
| 150 | } |
| 151 | } |
Douglas Gregor | f9af524 | 2011-04-15 20:45:44 +0000 | [diff] [blame] | 152 | |
| 153 | namespace PR9735 { |
| 154 | int &f3(const A*); |
| 155 | float &f3(const void*); |
| 156 | |
| 157 | void test_f(B* b, const B* bc) { |
| 158 | int &ir1 = f3(b); |
| 159 | int &ir2 = f3(bc); |
| 160 | } |
| 161 | } |
Douglas Gregor | 0f7b3dc | 2011-04-27 00:01:52 +0000 | [diff] [blame] | 162 | |
| 163 | @interface D : B |
| 164 | @end |
| 165 | |
| 166 | namespace rdar9327203 { |
| 167 | int &f(void* const&, int); |
| 168 | float &f(void* const&, long); |
| 169 | |
| 170 | void g(id x) { |
| 171 | int &fr = (f)(x, 0); |
| 172 | } |
| 173 | } |