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