PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/extern/llvm/tools/clang/test/SemaCXX/overloaded-operator.cpp

https://bitbucket.org/dwilliamson/clreflect/
C++ | 417 lines | 316 code | 85 blank | 16 comment | 12 complexity | 8bd9ee255036448e9f200193291300d4 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. class X { };
  3. X operator+(X, X);
  4. void f(X x) {
  5. x = x + x;
  6. }
  7. struct Y;
  8. struct Z;
  9. struct Y {
  10. Y(const Z&);
  11. };
  12. struct Z {
  13. Z(const Y&);
  14. };
  15. Y operator+(Y, Y);
  16. bool operator-(Y, Y); // expected-note{{candidate function}}
  17. bool operator-(Z, Z); // expected-note{{candidate function}}
  18. void g(Y y, Z z) {
  19. y = y + z;
  20. bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous}}
  21. }
  22. struct A {
  23. bool operator==(Z&); // expected-note 2{{candidate function}}
  24. };
  25. A make_A();
  26. bool operator==(A&, Z&); // expected-note 3{{candidate function}}
  27. void h(A a, const A ac, Z z) {
  28. make_A() == z; // expected-warning{{equality comparison result unused}}
  29. a == z; // expected-error{{use of overloaded operator '==' is ambiguous}}
  30. ac == z; // expected-error{{invalid operands to binary expression ('const A' and 'Z')}}
  31. }
  32. struct B {
  33. bool operator==(const B&) const;
  34. void test(Z z) {
  35. make_A() == z; // expected-warning{{equality comparison result unused}}
  36. }
  37. };
  38. // we shouldn't see warnings about self-comparison,
  39. // this is a member function, we dunno what it'll do
  40. bool i(B b)
  41. {
  42. return b == b;
  43. }
  44. enum Enum1 { };
  45. enum Enum2 { };
  46. struct E1 {
  47. E1(Enum1) { }
  48. };
  49. struct E2 {
  50. E2(Enum2);
  51. };
  52. // C++ [over.match.oper]p3 - enum restriction.
  53. float& operator==(E1, E2); // expected-note{{candidate function}}
  54. void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2, Enum1 next_enum1) {
  55. float &f1 = (e1 == e2);
  56. float &f2 = (enum1 == e2);
  57. float &f3 = (e1 == enum2);
  58. float &f4 = (enum1 == next_enum1); // expected-error{{non-const lvalue reference to type 'float' cannot bind to a temporary of type 'bool'}}
  59. }
  60. // PR5244 - Argument-dependent lookup would include the two operators below,
  61. // which would break later assumptions and lead to a crash.
  62. class pr5244_foo
  63. {
  64. pr5244_foo(int);
  65. pr5244_foo(char);
  66. };
  67. bool operator==(const pr5244_foo& s1, const pr5244_foo& s2); // expected-note{{candidate function}}
  68. bool operator==(char c, const pr5244_foo& s); // expected-note{{candidate function}}
  69. enum pr5244_bar
  70. {
  71. pr5244_BAR
  72. };
  73. class pr5244_baz
  74. {
  75. public:
  76. pr5244_bar quux;
  77. };
  78. void pr5244_barbaz()
  79. {
  80. pr5244_baz quuux;
  81. (void)(pr5244_BAR == quuux.quux);
  82. }
  83. struct PostInc {
  84. PostInc operator++(int);
  85. PostInc& operator++();
  86. };
  87. struct PostDec {
  88. PostDec operator--(int);
  89. PostDec& operator--();
  90. };
  91. void incdec_test(PostInc pi, PostDec pd) {
  92. const PostInc& pi1 = pi++;
  93. const PostDec& pd1 = pd--;
  94. PostInc &pi2 = ++pi;
  95. PostDec &pd2 = --pd;
  96. }
  97. struct SmartPtr {
  98. int& operator*();
  99. long& operator*() const volatile;
  100. };
  101. void test_smartptr(SmartPtr ptr, const SmartPtr cptr,
  102. const volatile SmartPtr cvptr) {
  103. int &ir = *ptr;
  104. long &lr = *cptr;
  105. long &lr2 = *cvptr;
  106. }
  107. struct ArrayLike {
  108. int& operator[](int);
  109. };
  110. void test_arraylike(ArrayLike a) {
  111. int& ir = a[17];
  112. }
  113. struct SmartRef {
  114. int* operator&();
  115. };
  116. void test_smartref(SmartRef r) {
  117. int* ip = &r;
  118. }
  119. bool& operator,(X, Y);
  120. void test_comma(X x, Y y) {
  121. bool& b1 = (x, y);
  122. X& xr = (x, x); // expected-warning {{expression result unused}}
  123. }
  124. struct Callable {
  125. int& operator()(int, double = 2.71828); // expected-note{{candidate function}}
  126. float& operator()(int, double, long, ...); // expected-note{{candidate function}}
  127. double& operator()(float); // expected-note{{candidate function}}
  128. };
  129. struct Callable2 {
  130. int& operator()(int i = 0);
  131. double& operator()(...) const;
  132. };
  133. struct DerivesCallable : public Callable {
  134. };
  135. void test_callable(Callable c, Callable2 c2, const Callable2& c2c,
  136. DerivesCallable dc) {
  137. int &ir = c(1);
  138. float &fr = c(1, 3.14159, 17, 42);
  139. c(); // expected-error{{no matching function for call to object of type 'Callable'}}
  140. double &dr = c(1.0f);
  141. int &ir2 = c2();
  142. int &ir3 = c2(1);
  143. double &fr2 = c2c();
  144. int &ir4 = dc(17);
  145. double &fr3 = dc(3.14159f);
  146. }
  147. typedef float FLOAT;
  148. typedef int& INTREF;
  149. typedef INTREF Func1(FLOAT, double);
  150. typedef float& Func2(int, double);
  151. struct ConvertToFunc {
  152. operator Func1*(); // expected-note 2{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}}
  153. operator Func2&(); // expected-note 2{{conversion candidate of type 'float &(&)(int, double)'}}
  154. void operator()();
  155. };
  156. struct ConvertToFuncDerived : ConvertToFunc { };
  157. void test_funcptr_call(ConvertToFunc ctf, ConvertToFuncDerived ctfd) {
  158. int &i1 = ctf(1.0f, 2.0);
  159. float &f1 = ctf((short int)1, 1.0f);
  160. ctf((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFunc' is ambiguous}}
  161. ctf();
  162. int &i2 = ctfd(1.0f, 2.0);
  163. float &f2 = ctfd((short int)1, 1.0f);
  164. ctfd((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFuncDerived' is ambiguous}}
  165. ctfd();
  166. }
  167. struct HasMember {
  168. int m;
  169. };
  170. struct Arrow1 {
  171. HasMember* operator->();
  172. };
  173. struct Arrow2 {
  174. Arrow1 operator->(); // expected-note{{candidate function}}
  175. };
  176. void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) {
  177. int &i1 = a1->m;
  178. int &i2 = a2->m;
  179. a3->m; // expected-error{{no viable overloaded 'operator->'}}
  180. }
  181. struct CopyConBase {
  182. };
  183. struct CopyCon : public CopyConBase {
  184. CopyCon(const CopyConBase &Base);
  185. CopyCon(const CopyConBase *Base) {
  186. *this = *Base;
  187. }
  188. };
  189. namespace N {
  190. struct X { };
  191. }
  192. namespace M {
  193. N::X operator+(N::X, N::X);
  194. }
  195. namespace M {
  196. void test_X(N::X x) {
  197. (void)(x + x);
  198. }
  199. }
  200. struct AA { bool operator!=(AA&); };
  201. struct BB : AA {};
  202. bool x(BB y, BB z) { return y != z; }
  203. struct AX {
  204. AX& operator ->(); // expected-note {{declared here}}
  205. int b;
  206. };
  207. void m() {
  208. AX a;
  209. a->b = 0; // expected-error {{circular pointer delegation detected}}
  210. }
  211. struct CircA {
  212. struct CircB& operator->(); // expected-note {{declared here}}
  213. int val;
  214. };
  215. struct CircB {
  216. struct CircC& operator->(); // expected-note {{declared here}}
  217. };
  218. struct CircC {
  219. struct CircA& operator->(); // expected-note {{declared here}}
  220. };
  221. void circ() {
  222. CircA a;
  223. a->val = 0; // expected-error {{circular pointer delegation detected}}
  224. }
  225. // PR5360: Arrays should lead to built-in candidates for subscript.
  226. typedef enum {
  227. LastReg = 23,
  228. } Register;
  229. class RegAlloc {
  230. int getPriority(Register r) {
  231. return usepri[r];
  232. }
  233. int usepri[LastReg + 1];
  234. };
  235. // PR5546: Don't generate incorrect and ambiguous overloads for multi-level
  236. // arrays.
  237. namespace pr5546
  238. {
  239. enum { X };
  240. extern const char *const sMoveCommands[][2][2];
  241. const char* a() { return sMoveCommands[X][0][0]; }
  242. const char* b() { return (*(sMoveCommands+X))[0][0]; }
  243. }
  244. // PR5512 and its discussion
  245. namespace pr5512 {
  246. struct Y {
  247. operator short();
  248. operator float();
  249. };
  250. void g_test(Y y) {
  251. short s = 0;
  252. // DR507, this should be ambiguous, but we special-case assignment
  253. s = y;
  254. // Note: DR507, this is ambiguous as specified
  255. //s += y;
  256. }
  257. struct S {};
  258. void operator +=(int&, S);
  259. void f(S s) {
  260. int i = 0;
  261. i += s;
  262. }
  263. struct A {operator int();};
  264. int a;
  265. void b(A x) {
  266. a += x;
  267. }
  268. }
  269. // PR5900
  270. namespace pr5900 {
  271. struct NotAnArray {};
  272. void test0() {
  273. NotAnArray x;
  274. x[0] = 0; // expected-error {{does not provide a subscript operator}}
  275. }
  276. struct NonConstArray {
  277. int operator[](unsigned); // expected-note {{candidate}}
  278. };
  279. int test1() {
  280. const NonConstArray x = NonConstArray();
  281. return x[0]; // expected-error {{no viable overloaded operator[] for type}}
  282. }
  283. // Not really part of this PR, but implemented at the same time.
  284. struct NotAFunction {};
  285. void test2() {
  286. NotAFunction x;
  287. x(); // expected-error {{does not provide a call operator}}
  288. }
  289. }
  290. // Operator lookup through using declarations.
  291. namespace N {
  292. struct X2 { };
  293. }
  294. namespace N2 {
  295. namespace M {
  296. namespace Inner {
  297. template<typename T>
  298. N::X2 &operator<<(N::X2&, const T&);
  299. }
  300. using Inner::operator<<;
  301. }
  302. }
  303. void test_lookup_through_using() {
  304. using namespace N2::M;
  305. N::X2 x;
  306. x << 17;
  307. }
  308. namespace rdar9136502 {
  309. struct X {
  310. int i();
  311. int i(int);
  312. };
  313. struct Y {
  314. Y &operator<<(int);
  315. };
  316. void f(X x, Y y) {
  317. y << x.i; // expected-error{{reference to non-static member function must be called}}
  318. }
  319. }
  320. namespace rdar9222009 {
  321. class StringRef {
  322. inline bool operator==(StringRef LHS, StringRef RHS) { // expected-error{{overloaded 'operator==' must be a binary operator (has 3 parameters)}}
  323. return !(LHS == RHS); // expected-error{{invalid operands to binary expression ('rdar9222009::StringRef' and 'rdar9222009::StringRef')}}
  324. }
  325. };
  326. }
  327. namespace PR11784 {
  328. struct A { A& operator=(void (*x)()); };
  329. void f();
  330. void f(int);
  331. void g() { A x; x = f; }
  332. }