blob: 76112641affcbbfbf0777729eb737766fcf852cf [file] [log] [blame]
Douglas Gregora9aafbe2008-11-25 04:08:05 +00001// RUN: clang -fsyntax-only -verify %s
2
Sebastian Redlb5ee8742008-12-03 20:26:15 +00003#include <stddef.h>
4
Sebastian Redl19fec9d2008-11-21 19:14:01 +00005struct S // expected-note {{candidate}}
6{
7 S(int, int, double); // expected-note {{candidate}}
Sebastian Redlebfa23a2009-02-07 19:52:04 +00008 S(double, int); // expected-note 2 {{candidate}}
9 S(float, int); // expected-note 2 {{candidate}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000010};
Douglas Gregor46fe06e2009-01-19 19:26:10 +000011struct T; // expected-note{{forward declaration of 'struct T'}}
Sebastian Redlfd98af22008-12-04 17:24:46 +000012struct U
13{
14 // A special new, to verify that the global version isn't used.
15 void* operator new(size_t, S*);
16};
Sebastian Redlec5f3262008-12-04 22:20:51 +000017struct V : U
18{
19};
Sebastian Redl19fec9d2008-11-21 19:14:01 +000020
Sebastian Redlebfa23a2009-02-07 19:52:04 +000021void* operator new(size_t); // expected-note 2 {{candidate}}
22void* operator new(size_t, int*); // expected-note 3 {{candidate}}
23void* operator new(size_t, float*); // expected-note 3 {{candidate}}
Sebastian Redlb5ee8742008-12-03 20:26:15 +000024
Sebastian Redl19fec9d2008-11-21 19:14:01 +000025void good_news()
26{
27 int *pi = new int;
28 float *pf = new (pi) float();
29 pi = new int(1);
30 pi = new int('c');
31 const int *pci = new const int();
32 S *ps = new S(1, 2, 3.4);
Sebastian Redl66df3ef2008-12-02 14:43:59 +000033 ps = new (pf) (S)(1, 2, 3.4);
Sebastian Redl19fec9d2008-11-21 19:14:01 +000034 S *(*paps)[2] = new S*[*pi][2];
35 ps = new (S[3])(1, 2, 3.4);
36 typedef int ia4[4];
37 ia4 *pai = new (int[3][4]);
Sebastian Redl7c5955a2008-12-02 16:35:44 +000038 pi = ::new int;
Sebastian Redlfd98af22008-12-04 17:24:46 +000039 U *pu = new (ps) U;
Sebastian Redlec5f3262008-12-04 22:20:51 +000040 // This is xfail. Inherited functions are not looked up currently.
41 //V *pv = new (ps) V;
Sebastian Redl19fec9d2008-11-21 19:14:01 +000042}
43
Douglas Gregora9aafbe2008-11-25 04:08:05 +000044void bad_news(int *ip)
Sebastian Redl19fec9d2008-11-21 19:14:01 +000045{
46 int i = 1;
47 (void)new; // expected-error {{missing type specifier}}
48 (void)new 4; // expected-error {{missing type specifier}}
49 (void)new () int; // expected-error {{expected expression}}
Sebastian Redl66df3ef2008-12-02 14:43:59 +000050 (void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000051 (void)new int[1][i]; // expected-error {{only the first dimension}}
52 (void)new (int[1][i]); // expected-error {{only the first dimension}}
53 (void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
54 (void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
55 (void)new S(1); // expected-error {{no matching constructor}}
Douglas Gregora9aafbe2008-11-25 04:08:05 +000056 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000057 (void)new const int; // expected-error {{must provide an initializer}}
Douglas Gregora9aafbe2008-11-25 04:08:05 +000058 (void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
Sebastian Redl66df3ef2008-12-02 14:43:59 +000059 // Undefined, but clang should reject it directly.
60 (void)new int[-1]; // expected-error {{array size is negative}}
61 (void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
Sebastian Redl7c5955a2008-12-02 16:35:44 +000062 (void)::S::new int; // expected-error {{expected unqualified-id}}
Sebastian Redlb5ee8742008-12-03 20:26:15 +000063 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redlec5f3262008-12-04 22:20:51 +000064 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
Sebastian Redlfd98af22008-12-04 17:24:46 +000065 // This must fail, because the member version shouldn't be found.
66 (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
Sebastian Redl3b7ec4b2009-02-09 18:24:27 +000067 (void)new (int[]); // expected-error {{array size must be specified in new expressions}}
68 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
69 (void)new (void ()); // expected-error {{cannot allocate function type 'void (void)' with new}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000070 // Some lacking cases due to lack of sema support.
71}
72
73void good_deletes()
74{
75 delete (int*)0;
76 delete [](int*)0;
77 delete (S*)0;
Sebastian Redl7c5955a2008-12-02 16:35:44 +000078 ::delete (int*)0;
Sebastian Redl19fec9d2008-11-21 19:14:01 +000079}
80
81void bad_deletes()
82{
83 delete 0; // expected-error {{cannot delete expression of type 'int'}}
84 delete [0] (int*)0; // expected-error {{expected ']'}} \
Chris Lattner921342c2008-11-23 23:17:07 +000085 // expected-note {{to match this '['}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000086 delete (void*)0; // expected-error {{cannot delete expression}}
87 delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
Sebastian Redl7c5955a2008-12-02 16:35:44 +000088 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
Sebastian Redl19fec9d2008-11-21 19:14:01 +000089}