Artem Dergachev | a396df3 | 2018-01-24 20:59:40 +0000 | [diff] [blame] | 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 |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 5 | #include "Inputs/system-header-simulator-cxx.h" |
| 6 | |
Artem Dergachev | 868e9a1 | 2018-01-18 00:03:43 +0000 | [diff] [blame] | 7 | #if !(LEAKS && !ALLOCATOR_INLINING) |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 8 | // expected-no-diagnostics |
| 9 | #endif |
| 10 | |
| 11 | |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 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); } |
Artem Dergachev | 868e9a1 | 2018-01-18 00:03:43 +0000 | [diff] [blame] | 16 | void *operator new(std::size_t size, const std::nothrow_t ¬hrow) throw() { return allocator(size); } |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 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; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 30 | } |
Artem Dergachev | 13b2026 | 2018-01-17 23:46:13 +0000 | [diff] [blame] | 31 | #if LEAKS && !ALLOCATOR_INLINING |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 32 | // expected-warning@-2{{Potential leak of memory pointed to by 'c3'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 33 | #endif |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 34 | |
| 35 | void testOpNewArray() { |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 36 | void *p = operator new[](0); // call is inlined, no warn |
| 37 | } |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 38 | |
| 39 | void testNewExprArray() { |
| 40 | int *p = new int[0]; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 41 | } |
Artem Dergachev | 13b2026 | 2018-01-17 23:46:13 +0000 | [diff] [blame] | 42 | #if LEAKS && !ALLOCATOR_INLINING |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 43 | // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 46 | |
| 47 | //----- Custom non-placement operators |
| 48 | void testOpNew() { |
Anton Yartsev | 3dfc33e | 2013-03-30 00:50:37 +0000 | [diff] [blame] | 49 | void *p = operator new(0); // call is inlined, no warn |
| 50 | } |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 51 | |
| 52 | void testNewExpr() { |
| 53 | int *p = new int; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 54 | } |
Artem Dergachev | 13b2026 | 2018-01-17 23:46:13 +0000 | [diff] [blame] | 55 | #if LEAKS && !ALLOCATOR_INLINING |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 56 | // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 59 | |
| 60 | //----- Custom NoThrow placement operators |
| 61 | void testOpNewNoThrow() { |
Artem Dergachev | 868e9a1 | 2018-01-18 00:03:43 +0000 | [diff] [blame] | 62 | void *p = operator new(0, std::nothrow); // call is inlined, no warn |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 63 | } |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 64 | |
| 65 | void testNewExprNoThrow() { |
| 66 | int *p = new(std::nothrow) int; |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 67 | } |
Artem Dergachev | 868e9a1 | 2018-01-18 00:03:43 +0000 | [diff] [blame] | 68 | #if LEAKS && !ALLOCATOR_INLINING |
Anna Zaks | a1de856 | 2013-04-06 00:41:36 +0000 | [diff] [blame] | 69 | // expected-warning@-2{{Potential leak of memory pointed to by 'p'}} |
Jordan Rose | 2633056 | 2013-04-05 17:55:00 +0000 | [diff] [blame] | 70 | #endif |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 71 | |
| 72 | //----- Custom placement operators |
| 73 | void testOpNewPlacement() { |
| 74 | void *p = operator new(0, 0.1); // no warn |
Artem Dergachev | 13b2026 | 2018-01-17 23:46:13 +0000 | [diff] [blame] | 75 | } |
Anton Yartsev | 8b66270 | 2013-03-28 16:10:38 +0000 | [diff] [blame] | 76 | |
| 77 | void testNewExprPlacement() { |
| 78 | int *p = new(0.1) int; // no warn |
| 79 | } |