Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++98
      2 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
      3 
      4 typedef __SIZE_TYPE__ size_t;
      5 
      6 #if __cplusplus >= 201103L
      7 struct S1 {
      8    void *operator new(size_t n) {
      9      return nullptr; // expected-warning {{'operator new' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
     10    }
     11    void *operator new[](size_t n) noexcept {
     12      return __null;
     13    }
     14 };
     15 #endif
     16 
     17 struct S2 {
     18    static size_t x;
     19    void *operator new(size_t n) throw() {
     20      return 0;
     21    }
     22    void *operator new[](size_t n) {
     23      return (void*)0;
     24 #if __cplusplus >= 201103L
     25      // expected-warning@-2 {{'operator new[]' should not return a null pointer unless it is declared 'throw()' or 'noexcept'}}
     26 #else
     27      // expected-warning-re@-4 {{'operator new[]' should not return a null pointer unless it is declared 'throw()'{{$}}}}
     28 #endif
     29    }
     30 };
     31 
     32 struct S3 {
     33    void *operator new(size_t n) {
     34      return 1-1;
     35 #if __cplusplus >= 201103L
     36      // expected-error@-2 {{cannot initialize return object of type 'void *' with an rvalue of type 'int'}}
     37 #else
     38      // expected-warning@-4 {{expression which evaluates to zero treated as a null pointer constant of type 'void *'}}
     39      // expected-warning@-5 {{'operator new' should not return a null pointer unless it is declared 'throw()'}}
     40 #endif
     41    }
     42    void *operator new[](size_t n) {
     43      return (void*)(1-1); // expected-warning {{'operator new[]' should not return a null pointer unless it is declared 'throw()'}}
     44    }
     45 };
     46 
     47 #if __cplusplus >= 201103L
     48 template<bool B> struct S4 {
     49   void *operator new(size_t n) noexcept(B) {
     50     return 0; // expected-warning {{'operator new' should not return a null pointer}}
     51   }
     52 };
     53 template struct S4<true>;
     54 template struct S4<false>; // expected-note {{in instantiation of}}
     55 #endif
     56 
     57 template<typename ...T> struct S5 { // expected-warning 0-1{{extension}}
     58   void *operator new(size_t n) throw(T...) {
     59     return 0; // expected-warning {{'operator new' should not return a null pointer}}
     60   }
     61 };
     62 template struct S5<>;
     63 template struct S5<int>; // expected-note {{in instantiation of}}
     64