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