[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / new-null.cpp
blobb2be0c5b382f2bc0eb82c65e2e46ec962157e532
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++98
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
4 typedef __SIZE_TYPE__ size_t;
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'}}
11 void *operator new[](size_t n) noexcept {
12 return __null;
15 #endif
17 struct S2 {
18 static size_t x;
19 void *operator new(size_t n) throw() {
20 return 0;
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
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
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()'}}
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}}
53 template struct S4<true>;
54 template struct S4<false>; // expected-note {{in instantiation of}}
55 #endif
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}}
62 template struct S5<>;
63 template struct S5<int>; // expected-note {{in instantiation of}}