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