sema.cpp revision 762bb9d0ad20320b9f97a841dce57ba5e8e48b07
1// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -fms-extensions %s
2
3#define P(e) static_assert(noexcept(e), "expected nothrow")
4#define N(e) static_assert(!noexcept(e), "expected throw")
5#define B(b, e) static_assert(b == noexcept(e), "expectation failed")
6
7void simple() {
8  P(0);
9  P(0 + 0);
10  int i;
11  P(i);
12  P(sizeof(0));
13  P(static_cast<int>(0));
14  N(throw 0);
15  N((throw 0, 0));
16}
17
18void nospec();
19void allspec() throw(...);
20void intspec() throw(int);
21void emptyspec() throw();
22void nothrowattr() __attribute__((nothrow));
23void noexcept_true() noexcept;
24void noexcept_false() noexcept(false);
25
26void call() {
27  N(nospec());
28  N(allspec());
29  N(intspec());
30  P(emptyspec());
31  P(nothrowattr());
32  P(noexcept_true());
33  N(noexcept_false());
34}
35
36void (*pnospec)();
37void (*pallspec)() throw(...);
38void (*pintspec)() throw(int);
39void (*pemptyspec)() throw();
40
41void callptr() {
42  N(pnospec());
43  N((*pnospec)());
44  N(pallspec());
45  N((*pallspec)());
46  N(pintspec());
47  N((*pintspec)());
48  P(pemptyspec());
49  P((*pemptyspec)());
50}
51
52struct S1 {
53  void nospec();
54  void allspec() throw(...);
55  void intspec() throw(int);
56  void emptyspec() throw();
57};
58
59void callmem() {
60  S1 s;
61  N(s.nospec());
62  N(s.allspec());
63  N(s.intspec());
64  P(s.emptyspec());
65}
66
67void (S1::*mpnospec)();
68void (S1::*mpallspec)() throw(...);
69void (S1::*mpintspec)() throw(int);
70void (S1::*mpemptyspec)() throw();
71
72void callmemptr() {
73  S1 s;
74  N((s.*mpnospec)());
75  N((s.*mpallspec)());
76  N((s.*mpintspec)());
77  P((s.*mpemptyspec)());
78}
79
80struct S2 {
81  S2();
82  S2(int, int) throw();
83  void operator +();
84  void operator -() throw();
85  void operator +(int);
86  void operator -(int) throw();
87  operator int();
88  operator float() throw();
89};
90
91void *operator new(__typeof__(sizeof(int)) sz, int) throw();
92
93struct Bad1 {
94  ~Bad1() throw(int);
95};
96struct Bad2 {
97  void operator delete(void*) throw(int);
98};
99
100typedef int X;
101
102void implicits() {
103  N(new int);
104  P(new (0) int);
105  P(delete (int*)0);
106  N(delete (Bad1*)0);
107  N(delete (Bad2*)0);
108  N(S2());
109  P(S2(0, 0));
110  S2 s;
111  N(+s);
112  P(-s);
113  N(s + 0);
114  P(s - 0);
115  N(static_cast<int>(s));
116  P(static_cast<float>(s));
117  N(Bad1());
118  P(X().~X());
119}
120
121struct V {
122  virtual ~V() throw();
123};
124struct D : V {};
125
126void dyncast() {
127  V *pv = 0;
128  D *pd = 0;
129  P(dynamic_cast<V&>(*pd));
130  P(dynamic_cast<V*>(pd));
131  N(dynamic_cast<D&>(*pv));
132  P(dynamic_cast<D*>(pv));
133}
134
135namespace std {
136  struct type_info {};
137}
138
139void idtype() {
140  P(typeid(V));
141  P(typeid((V*)0));
142  P(typeid(*(S1*)0));
143  N(typeid(*(V*)0));
144}
145
146void uneval() {
147  P(sizeof(typeid(*(V*)0)));
148  P(typeid(typeid(*(V*)0)));
149}
150
151struct G1 {};
152struct G2 { int i; };
153struct G3 { S2 s; };
154
155void gencon() {
156  P(G1());
157  P(G2());
158  N(G3());
159}
160
161template <class T> void f(T&&) noexcept;
162template <typename T, bool b>
163void late() {
164  B(b, typeid(*(T*)0));
165  B(b, T(1));
166  B(b, static_cast<T>(S2(0, 0)));
167  B(b, S1() + T());
168  P(f(T()));
169  P(new (0) T);
170  P(delete (T*)0);
171}
172struct S3 {
173  virtual ~S3() throw();
174  S3() throw();
175  explicit S3(int);
176  S3(const S2&);
177};
178template <class T> T&& f2() noexcept;
179template <typename T>
180void late2() {
181  P(dynamic_cast<S3&>(f2<T&>()));
182}
183void operator +(const S1&, float) throw();
184void operator +(const S1&, const S3&);
185void tlate() {
186  late<float, true>();
187  late<S3, false>();
188  late2<S3>();
189}
190