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