Home | History | Annotate | Download | only in expr.unary.noexcept
      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 
      7 void 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 
     18 void nospec();
     19 void allspec() throw(...);
     20 void intspec() throw(int);
     21 void emptyspec() throw();
     22 void nothrowattr() __attribute__((nothrow));
     23 void noexcept_true() noexcept;
     24 void noexcept_false() noexcept(false);
     25 
     26 void 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 
     36 void (*pnospec)();
     37 void (*pallspec)() throw(...);
     38 void (*pintspec)() throw(int);
     39 void (*pemptyspec)() throw();
     40 
     41 void 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 
     52 struct S1 {
     53   void nospec();
     54   void allspec() throw(...);
     55   void intspec() throw(int);
     56   void emptyspec() throw();
     57 };
     58 
     59 void callmem() {
     60   S1 s;
     61   N(s.nospec());
     62   N(s.allspec());
     63   N(s.intspec());
     64   P(s.emptyspec());
     65 }
     66 
     67 void (S1::*mpnospec)();
     68 void (S1::*mpallspec)() throw(...);
     69 void (S1::*mpintspec)() throw(int);
     70 void (S1::*mpemptyspec)() throw();
     71 
     72 void callmemptr() {
     73   S1 s;
     74   N((s.*mpnospec)());
     75   N((s.*mpallspec)());
     76   N((s.*mpintspec)());
     77   P((s.*mpemptyspec)());
     78 }
     79 
     80 struct 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 
     91 void *operator new(__typeof__(sizeof(int)) sz, int) throw();
     92 
     93 struct Bad1 {
     94   ~Bad1() throw(int);
     95 };
     96 struct Bad2 {
     97   void operator delete(void*) throw(int);
     98 };
     99 
    100 typedef int X;
    101 
    102 void 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 
    121 struct V {
    122   virtual ~V() throw();
    123 };
    124 struct D : V {};
    125 
    126 void 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 
    135 namespace std {
    136   struct type_info {};
    137 }
    138 
    139 void idtype() {
    140   P(typeid(V));
    141   P(typeid((V*)0));
    142   P(typeid(*(S1*)0));
    143   N(typeid(*(V*)0));
    144 }
    145 
    146 void uneval() {
    147   P(sizeof(typeid(*(V*)0)));
    148   P(typeid(typeid(*(V*)0)));
    149 }
    150 
    151 struct G1 {};
    152 struct G2 { int i; };
    153 struct G3 { S2 s; };
    154 
    155 void gencon() {
    156   P(G1());
    157   P(G2());
    158   N(G3());
    159 }
    160 
    161 template <class T> void f(T&&) noexcept;
    162 template <typename T, bool b>
    163 void 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 }
    172 struct S3 {
    173   virtual ~S3() throw();
    174   S3() throw();
    175   explicit S3(int);
    176   S3(const S2&);
    177 };
    178 template <class T> T&& f2() noexcept;
    179 template <typename T>
    180 void late2() {
    181   P(dynamic_cast<S3&>(f2<T&>()));
    182 }
    183 void operator +(const S1&, float) throw();
    184 void operator +(const S1&, const S3&);
    185 void tlate() {
    186   late<float, true>();
    187   late<S3, false>();
    188   late2<S3>();
    189 }
    190