blob: f5a2952699ff21b4a72dd625673fd9d912b93a94 [file] [log] [blame]
Artem Dergacheva396df32018-01-24 20:59:40 +00001// 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
Anton Yartsev8b662702013-03-28 16:10:38 +00005#include "Inputs/system-header-simulator-cxx.h"
6
Artem Dergachev868e9a12018-01-18 00:03:43 +00007#if !(LEAKS && !ALLOCATOR_INLINING)
Jordan Rose26330562013-04-05 17:55:00 +00008// expected-no-diagnostics
9#endif
10
11
Anton Yartsev8b662702013-03-28 16:10:38 +000012void *allocator(std::size_t size);
13
14void *operator new[](std::size_t size) throw() { return allocator(size); }
15void *operator new(std::size_t size) throw() { return allocator(size); }
Artem Dergachev868e9a12018-01-18 00:03:43 +000016void *operator new(std::size_t size, const std::nothrow_t &nothrow) throw() { return allocator(size); }
Anton Yartsev8b662702013-03-28 16:10:38 +000017void *operator new(std::size_t, double d);
18
19class C {
20public:
21 void *operator new(std::size_t);
22};
23
24void testNewMethod() {
25 void *p1 = C::operator new(0); // no warn
26
27 C *p2 = new C; // no warn
28
29 C *c3 = ::new C;
Jordan Rose26330562013-04-05 17:55:00 +000030}
Artem Dergachev13b20262018-01-17 23:46:13 +000031#if LEAKS && !ALLOCATOR_INLINING
Anna Zaksa1de8562013-04-06 00:41:36 +000032// expected-warning@-2{{Potential leak of memory pointed to by 'c3'}}
Jordan Rose26330562013-04-05 17:55:00 +000033#endif
Anton Yartsev8b662702013-03-28 16:10:38 +000034
35void testOpNewArray() {
Anton Yartsev3dfc33e2013-03-30 00:50:37 +000036 void *p = operator new[](0); // call is inlined, no warn
37}
Anton Yartsev8b662702013-03-28 16:10:38 +000038
39void testNewExprArray() {
40 int *p = new int[0];
Jordan Rose26330562013-04-05 17:55:00 +000041}
Artem Dergachev13b20262018-01-17 23:46:13 +000042#if LEAKS && !ALLOCATOR_INLINING
Anna Zaksa1de8562013-04-06 00:41:36 +000043// expected-warning@-2{{Potential leak of memory pointed to by 'p'}}
Jordan Rose26330562013-04-05 17:55:00 +000044#endif
45
Anton Yartsev8b662702013-03-28 16:10:38 +000046
47//----- Custom non-placement operators
48void testOpNew() {
Anton Yartsev3dfc33e2013-03-30 00:50:37 +000049 void *p = operator new(0); // call is inlined, no warn
50}
Anton Yartsev8b662702013-03-28 16:10:38 +000051
52void testNewExpr() {
53 int *p = new int;
Jordan Rose26330562013-04-05 17:55:00 +000054}
Artem Dergachev13b20262018-01-17 23:46:13 +000055#if LEAKS && !ALLOCATOR_INLINING
Anna Zaksa1de8562013-04-06 00:41:36 +000056// expected-warning@-2{{Potential leak of memory pointed to by 'p'}}
Jordan Rose26330562013-04-05 17:55:00 +000057#endif
58
Anton Yartsev8b662702013-03-28 16:10:38 +000059
60//----- Custom NoThrow placement operators
61void testOpNewNoThrow() {
Artem Dergachev868e9a12018-01-18 00:03:43 +000062 void *p = operator new(0, std::nothrow); // call is inlined, no warn
Jordan Rose26330562013-04-05 17:55:00 +000063}
Anton Yartsev8b662702013-03-28 16:10:38 +000064
65void testNewExprNoThrow() {
66 int *p = new(std::nothrow) int;
Jordan Rose26330562013-04-05 17:55:00 +000067}
Artem Dergachev868e9a12018-01-18 00:03:43 +000068#if LEAKS && !ALLOCATOR_INLINING
Anna Zaksa1de8562013-04-06 00:41:36 +000069// expected-warning@-2{{Potential leak of memory pointed to by 'p'}}
Jordan Rose26330562013-04-05 17:55:00 +000070#endif
Anton Yartsev8b662702013-03-28 16:10:38 +000071
72//----- Custom placement operators
73void testOpNewPlacement() {
74 void *p = operator new(0, 0.1); // no warn
Artem Dergachev13b20262018-01-17 23:46:13 +000075}
Anton Yartsev8b662702013-03-28 16:10:38 +000076
77void testNewExprPlacement() {
78 int *p = new(0.1) int; // no warn
79}