sema.cpp revision be57cf41fb55b48e3f889787960b3ac2eb5e4dbd
1// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++0x -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 100void implicits() { 101 N(new int); 102 P(new (0) int); 103 P(delete (int*)0); 104 N(delete (Bad1*)0); 105 N(delete (Bad2*)0); 106 N(S2()); 107 P(S2(0, 0)); 108 S2 s; 109 N(+s); 110 P(-s); 111 N(s + 0); 112 P(s - 0); 113 N(static_cast<int>(s)); 114 P(static_cast<float>(s)); 115 N(Bad1()); 116} 117 118struct V { 119 virtual ~V() throw(); 120}; 121struct D : V {}; 122 123void dyncast() { 124 V *pv = 0; 125 D *pd = 0; 126 P(dynamic_cast<V&>(*pd)); 127 P(dynamic_cast<V*>(pd)); 128 N(dynamic_cast<D&>(*pv)); 129 P(dynamic_cast<D*>(pv)); 130} 131 132namespace std { 133 struct type_info {}; 134} 135 136void idtype() { 137 P(typeid(V)); 138 P(typeid((V*)0)); 139 P(typeid(*(S1*)0)); 140 N(typeid(*(V*)0)); 141} 142 143void uneval() { 144 P(sizeof(typeid(*(V*)0))); 145 P(typeid(typeid(*(V*)0))); 146} 147 148struct G1 {}; 149struct G2 { int i; }; 150struct G3 { S2 s; }; 151 152void gencon() { 153 P(G1()); 154 P(G2()); 155 N(G3()); 156} 157 158template <class T> void f(T&&) noexcept; 159template <typename T, bool b> 160void late() { 161 B(b, typeid(*(T*)0)); 162 B(b, T(1)); 163 B(b, static_cast<T>(S2(0, 0))); 164 B(b, S1() + T()); 165 P(f(T())); 166 P(new (0) T); 167 P(delete (T*)0); 168} 169struct S3 { 170 virtual ~S3() throw(); 171 S3() throw(); 172 explicit S3(int); 173 S3(const S2&); 174}; 175template <class T> T&& f2() noexcept; 176template <typename T> 177void late2() { 178 P(dynamic_cast<S3&>(f2<T&>())); 179} 180void operator +(const S1&, float) throw(); 181void operator +(const S1&, const S3&); 182void tlate() { 183 late<float, true>(); 184 late<S3, false>(); 185 late2<S3>(); 186} 187