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