NewDelete-custom.cpp revision 176edba5311f6eff0cad2631449885ddf4fbc9ea
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
10void *allocator(std::size_t size);
11
12void *operator new[](std::size_t size) throw() { return allocator(size); }
13void *operator new(std::size_t size) throw() { return allocator(size); }
14void *operator new(std::size_t size, std::nothrow_t& nothrow) throw() { return allocator(size); }
15void *operator new(std::size_t, double d);
16
17class C {
18public:
19  void *operator new(std::size_t);
20};
21
22void 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
33void testOpNewArray() {
34  void *p = operator new[](0); // call is inlined, no warn
35}
36
37void 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
46void testOpNew() {
47  void *p = operator new(0); // call is inlined, no warn
48}
49
50void 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
59void 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
66void 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
74void testOpNewPlacement() {
75  void *p = operator new(0, 0.1); // no warn
76}
77
78void testNewExprPlacement() {
79  int *p = new(0.1) int; // no warn
80}
81