1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,unix.Malloc -std=c++11 -analyzer-config c++-allocator-inlining=false -fblocks -verify %s 2 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,unix.Malloc -std=c++11 -analyzer-config c++-allocator-inlining=false -DLEAKS=1 -fblocks -verify %s 3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,unix.Malloc -std=c++11 -DALLOCATOR_INLINING=1 -fblocks -verify %s 4 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,unix.Malloc -std=c++11 -DLEAKS=1 -DALLOCATOR_INLINING=1 -fblocks -verify %s 5 #include "Inputs/system-header-simulator-cxx.h" 6 7 #if !(LEAKS && !ALLOCATOR_INLINING) 8 // expected-no-diagnostics 9 #endif 10 11 12 void *allocator(std::size_t size); 13 14 void *operator new[](std::size_t size) throw() { return allocator(size); } 15 void *operator new(std::size_t size) throw() { return allocator(size); } 16 void *operator new(std::size_t size, const std::nothrow_t ¬hrow) throw() { return allocator(size); } 17 void *operator new(std::size_t, double d); 18 19 class C { 20 public: 21 void *operator new(std::size_t); 22 }; 23 24 void testNewMethod() { 25 void *p1 = C::operator new(0); // no warn 26 27 C *p2 = new C; // no warn 28 29 C *c3 = ::new C; 30 } 31 #if LEAKS && !ALLOCATOR_INLINING 32 // expected-warning@-2{{Potential leak of memory pointed to by 'c3'}} 33 #endif 34 35 void testOpNewArray() { 36 void *p = operator new[](0); // call is inlined, no warn 37 } 38 39 void testNewExprArray() { 40 int *p = new int[0]; 41 } 42 #if LEAKS && !ALLOCATOR_INLINING 43 // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} 44 #endif 45 46 47 //----- Custom non-placement operators 48 void testOpNew() { 49 void *p = operator new(0); // call is inlined, no warn 50 } 51 52 void testNewExpr() { 53 int *p = new int; 54 } 55 #if LEAKS && !ALLOCATOR_INLINING 56 // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} 57 #endif 58 59 60 //----- Custom NoThrow placement operators 61 void testOpNewNoThrow() { 62 void *p = operator new(0, std::nothrow); // call is inlined, no warn 63 } 64 65 void testNewExprNoThrow() { 66 int *p = new(std::nothrow) int; 67 } 68 #if LEAKS && !ALLOCATOR_INLINING 69 // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} 70 #endif 71 72 //----- Custom placement operators 73 void testOpNewPlacement() { 74 void *p = operator new(0, 0.1); // no warn 75 } 76 77 void testNewExprPlacement() { 78 int *p = new(0.1) int; // no warn 79 } 80