xref: /llvm-project/clang/test/CXX/expr/expr.unary/expr.unary.noexcept/sema.cpp (revision 6c93b3e29c56d14eab570ce62cd646a95f0c1403)
1*6c93b3e2SAaron Ballman // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -fms-extensions -Wno-delete-incomplete -Wno-unused-value %s
2c6e68daaSAndy Gibbs // expected-no-diagnostics
35f0180d8SSebastian Redl 
45f0180d8SSebastian Redl #define P(e) static_assert(noexcept(e), "expected nothrow")
55f0180d8SSebastian Redl #define N(e) static_assert(!noexcept(e), "expected throw")
6dbd14bdeSSebastian Redl #define B(b, e) static_assert(b == noexcept(e), "expectation failed")
75f0180d8SSebastian Redl 
simple()85f0180d8SSebastian Redl void simple() {
95f0180d8SSebastian Redl   P(0);
105f0180d8SSebastian Redl   P(0 + 0);
115f0180d8SSebastian Redl   int i;
125f0180d8SSebastian Redl   P(i);
135f0180d8SSebastian Redl   P(sizeof(0));
145f0180d8SSebastian Redl   P(static_cast<int>(0));
155f0180d8SSebastian Redl   N(throw 0);
164fa4a6b8SSebastian Redl   N((throw 0, 0));
175f0180d8SSebastian Redl }
185f0180d8SSebastian Redl 
195f0180d8SSebastian Redl void nospec();
205f0180d8SSebastian Redl void allspec() throw(...);
215f0180d8SSebastian Redl void intspec() throw(int);
225f0180d8SSebastian Redl void emptyspec() throw();
23b8a76c42SSebastian Redl void nothrowattr() __attribute__((nothrow));
2452ddca23SSebastian Redl void noexcept_true() noexcept;
2552ddca23SSebastian Redl void noexcept_false() noexcept(false);
265f0180d8SSebastian Redl 
call()275f0180d8SSebastian Redl void call() {
285f0180d8SSebastian Redl   N(nospec());
295f0180d8SSebastian Redl   N(allspec());
305f0180d8SSebastian Redl   N(intspec());
315f0180d8SSebastian Redl   P(emptyspec());
32b8a76c42SSebastian Redl   P(nothrowattr());
3352ddca23SSebastian Redl   P(noexcept_true());
3452ddca23SSebastian Redl   N(noexcept_false());
355f0180d8SSebastian Redl }
365f0180d8SSebastian Redl 
375f0180d8SSebastian Redl void (*pnospec)();
385f0180d8SSebastian Redl void (*pallspec)() throw(...);
395f0180d8SSebastian Redl void (*pintspec)() throw(int);
405f0180d8SSebastian Redl void (*pemptyspec)() throw();
415f0180d8SSebastian Redl 
425a8738ffSEli Friedman typedef void (*funcptr)();
435a8738ffSEli Friedman funcptr returnsptr() throw();
445a8738ffSEli Friedman 
callptr()455f0180d8SSebastian Redl void callptr() {
465f0180d8SSebastian Redl   N(pnospec());
475f0180d8SSebastian Redl   N((*pnospec)());
485f0180d8SSebastian Redl   N(pallspec());
495f0180d8SSebastian Redl   N((*pallspec)());
505f0180d8SSebastian Redl   N(pintspec());
515f0180d8SSebastian Redl   N((*pintspec)());
525f0180d8SSebastian Redl   P(pemptyspec());
535f0180d8SSebastian Redl   P((*pemptyspec)());
545a8738ffSEli Friedman   N(returnsptr()());
555f0180d8SSebastian Redl }
565f0180d8SSebastian Redl 
575f0180d8SSebastian Redl struct S1 {
585f0180d8SSebastian Redl   void nospec();
595f0180d8SSebastian Redl   void allspec() throw(...);
605f0180d8SSebastian Redl   void intspec() throw(int);
615f0180d8SSebastian Redl   void emptyspec() throw();
625f0180d8SSebastian Redl };
635f0180d8SSebastian Redl 
callmem()645f0180d8SSebastian Redl void callmem() {
655f0180d8SSebastian Redl   S1 s;
665f0180d8SSebastian Redl   N(s.nospec());
675f0180d8SSebastian Redl   N(s.allspec());
685f0180d8SSebastian Redl   N(s.intspec());
695f0180d8SSebastian Redl   P(s.emptyspec());
705f0180d8SSebastian Redl }
715f0180d8SSebastian Redl 
725f0180d8SSebastian Redl void (S1::*mpnospec)();
735f0180d8SSebastian Redl void (S1::*mpallspec)() throw(...);
745f0180d8SSebastian Redl void (S1::*mpintspec)() throw(int);
755f0180d8SSebastian Redl void (S1::*mpemptyspec)() throw();
765f0180d8SSebastian Redl 
callmemptr()775f0180d8SSebastian Redl void callmemptr() {
785f0180d8SSebastian Redl   S1 s;
795f0180d8SSebastian Redl   N((s.*mpnospec)());
805f0180d8SSebastian Redl   N((s.*mpallspec)());
815f0180d8SSebastian Redl   N((s.*mpintspec)());
825f0180d8SSebastian Redl   P((s.*mpemptyspec)());
835f0180d8SSebastian Redl }
845f0180d8SSebastian Redl 
855f0180d8SSebastian Redl struct S2 {
865f0180d8SSebastian Redl   S2();
875f0180d8SSebastian Redl   S2(int, int) throw();
885f0180d8SSebastian Redl   void operator +();
895f0180d8SSebastian Redl   void operator -() throw();
905f0180d8SSebastian Redl   void operator +(int);
915f0180d8SSebastian Redl   void operator -(int) throw();
925f0180d8SSebastian Redl   operator int();
935f0180d8SSebastian Redl   operator float() throw();
945f0180d8SSebastian Redl };
955f0180d8SSebastian Redl 
965f0180d8SSebastian Redl void *operator new(__typeof__(sizeof(int)) sz, int) throw();
975f0180d8SSebastian Redl 
980423b76bSEli Friedman struct IncompleteStruct;
990423b76bSEli Friedman 
100a8bac37bSSebastian Redl struct Bad1 {
101a8bac37bSSebastian Redl   ~Bad1() throw(int);
102a8bac37bSSebastian Redl };
103a8bac37bSSebastian Redl struct Bad2 {
104a8bac37bSSebastian Redl   void operator delete(void*) throw(int);
105a8bac37bSSebastian Redl };
106a8bac37bSSebastian Redl 
107622e4fcaSEli Friedman typedef int X;
108622e4fcaSEli Friedman 
implicits()1095f0180d8SSebastian Redl void implicits() {
1105f0180d8SSebastian Redl   N(new int);
1115f0180d8SSebastian Redl   P(new (0) int);
112a8bac37bSSebastian Redl   P(delete (int*)0);
1130423b76bSEli Friedman   P(delete (IncompleteStruct*)0);
114a8bac37bSSebastian Redl   N(delete (Bad1*)0);
115a8bac37bSSebastian Redl   N(delete (Bad2*)0);
1165f0180d8SSebastian Redl   N(S2());
1175f0180d8SSebastian Redl   P(S2(0, 0));
1185f0180d8SSebastian Redl   S2 s;
1195f0180d8SSebastian Redl   N(+s);
1205f0180d8SSebastian Redl   P(-s);
1215f0180d8SSebastian Redl   N(s + 0);
1225f0180d8SSebastian Redl   P(s - 0);
1235f0180d8SSebastian Redl   N(static_cast<int>(s));
1245f0180d8SSebastian Redl   P(static_cast<float>(s));
125a8bac37bSSebastian Redl   N(Bad1());
126622e4fcaSEli Friedman   P(X().~X());
1275f0180d8SSebastian Redl }
1285f0180d8SSebastian Redl 
1295f0180d8SSebastian Redl struct V {
1305f0180d8SSebastian Redl   virtual ~V() throw();
1315f0180d8SSebastian Redl };
1325f0180d8SSebastian Redl struct D : V {};
1335f0180d8SSebastian Redl 
dyncast()1345f0180d8SSebastian Redl void dyncast() {
1355f0180d8SSebastian Redl   V *pv = 0;
1365f0180d8SSebastian Redl   D *pd = 0;
1375f0180d8SSebastian Redl   P(dynamic_cast<V&>(*pd));
1385f0180d8SSebastian Redl   P(dynamic_cast<V*>(pd));
1395f0180d8SSebastian Redl   N(dynamic_cast<D&>(*pv));
1405f0180d8SSebastian Redl   P(dynamic_cast<D*>(pv));
1415f0180d8SSebastian Redl }
1425f0180d8SSebastian Redl 
1435f0180d8SSebastian Redl namespace std {
1445f0180d8SSebastian Redl   struct type_info {};
1455f0180d8SSebastian Redl }
1465f0180d8SSebastian Redl 
idtype()1475f0180d8SSebastian Redl void idtype() {
1485f0180d8SSebastian Redl   P(typeid(V));
1495f0180d8SSebastian Redl   P(typeid((V*)0));
1505f0180d8SSebastian Redl   P(typeid(*(S1*)0));
1515f0180d8SSebastian Redl   N(typeid(*(V*)0));
1525f0180d8SSebastian Redl }
1535f0180d8SSebastian Redl 
uneval()1545f0180d8SSebastian Redl void uneval() {
1555f0180d8SSebastian Redl   P(sizeof(typeid(*(V*)0)));
1565f0180d8SSebastian Redl   P(typeid(typeid(*(V*)0)));
1575f0180d8SSebastian Redl }
158dbd14bdeSSebastian Redl 
159dbd14bdeSSebastian Redl struct G1 {};
160dbd14bdeSSebastian Redl struct G2 { int i; };
161dbd14bdeSSebastian Redl struct G3 { S2 s; };
162dbd14bdeSSebastian Redl 
gencon()163dbd14bdeSSebastian Redl void gencon() {
164dbd14bdeSSebastian Redl   P(G1());
165dbd14bdeSSebastian Redl   P(G2());
166dbd14bdeSSebastian Redl   N(G3());
167dbd14bdeSSebastian Redl }
168dbd14bdeSSebastian Redl 
169c6587cc5SEli Friedman template <class T> void f(T&&) noexcept;
170dbd14bdeSSebastian Redl template <typename T, bool b>
late()171dbd14bdeSSebastian Redl void late() {
172dbd14bdeSSebastian Redl   B(b, typeid(*(T*)0));
173dbd14bdeSSebastian Redl   B(b, T(1));
174dbd14bdeSSebastian Redl   B(b, static_cast<T>(S2(0, 0)));
175dbd14bdeSSebastian Redl   B(b, S1() + T());
176c6587cc5SEli Friedman   P(f(T()));
177c6587cc5SEli Friedman   P(new (0) T);
178c6587cc5SEli Friedman   P(delete (T*)0);
179dbd14bdeSSebastian Redl }
180dbd14bdeSSebastian Redl struct S3 {
181dbd14bdeSSebastian Redl   virtual ~S3() throw();
182dbd14bdeSSebastian Redl   S3() throw();
183dbd14bdeSSebastian Redl   explicit S3(int);
184dbd14bdeSSebastian Redl   S3(const S2&);
185dbd14bdeSSebastian Redl };
186c6587cc5SEli Friedman template <class T> T&& f2() noexcept;
187c6587cc5SEli Friedman template <typename T>
late2()188c6587cc5SEli Friedman void late2() {
189c6587cc5SEli Friedman   P(dynamic_cast<S3&>(f2<T&>()));
190c6587cc5SEli Friedman }
191dbd14bdeSSebastian Redl void operator +(const S1&, float) throw();
192dbd14bdeSSebastian Redl void operator +(const S1&, const S3&);
tlate()193dbd14bdeSSebastian Redl void tlate() {
194dbd14bdeSSebastian Redl   late<float, true>();
195dbd14bdeSSebastian Redl   late<S3, false>();
196c6587cc5SEli Friedman   late2<S3>();
197dbd14bdeSSebastian Redl }
198