|
| 1 | +// RUN: %clang_analyze_cc1 -verify -analyzer-output=text %s \ |
| 2 | +// RUN: -analyzer-checker=core \ |
| 3 | +// RUN: -analyzer-checker=cplusplus \ |
| 4 | +// RUN: -analyzer-checker=unix |
| 5 | +// expected-no-diagnostics |
| 6 | + |
| 7 | +#include "Inputs/system-header-simulator-for-malloc.h" |
| 8 | + |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +// Check that we don't report leaks for unique_ptr in temporary objects |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +namespace unique_ptr_temporary_PR60896 { |
| 13 | + |
| 14 | +// We use a custom implementation of unique_ptr for testing purposes |
| 15 | +template <typename T> |
| 16 | +struct unique_ptr { |
| 17 | + T* ptr; |
| 18 | + unique_ptr(T* p) : ptr(p) {} |
| 19 | + ~unique_ptr() { delete ptr; } |
| 20 | + unique_ptr(unique_ptr&& other) : ptr(other.ptr) { other.ptr = nullptr; } |
| 21 | + T* get() const { return ptr; } |
| 22 | +}; |
| 23 | + |
| 24 | +template <typename T, typename... Args> |
| 25 | +unique_ptr<T> make_unique(Args&&... args) { |
| 26 | + return unique_ptr<T>(new T(args...)); |
| 27 | +} |
| 28 | + |
| 29 | +// The test case that demonstrates the issue |
| 30 | +struct Foo { |
| 31 | + unique_ptr<int> i; |
| 32 | +}; |
| 33 | + |
| 34 | +void add(Foo foo) { |
| 35 | + // The unique_ptr destructor will be called when foo goes out of scope |
| 36 | +} |
| 37 | + |
| 38 | +void test() { |
| 39 | + // No warning should be emitted for this - the memory is managed by unique_ptr |
| 40 | + // in the temporary Foo object, which will properly clean up the memory |
| 41 | + add({make_unique<int>(1)}); |
| 42 | +} |
| 43 | + |
| 44 | +} // namespace unique_ptr_temporary_PR60896 |
0 commit comments