Skip to content

Commit 83173d0

Browse files
committed
[analyzer][test] Add multiple owning arguments test case
Test multiple by-value arguments with smart pointer fields for comprehensive coverage.
1 parent 2dc6776 commit 83173d0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

clang/test/Analysis/NewDeleteLeaks-PR60896.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,57 @@ void test() {
125125
}
126126

127127
} // namespace base_class_smart_ptr_PR60896
128+
129+
//===----------------------------------------------------------------------===//
130+
// Check that we don't report leaks for multiple owning arguments
131+
//===----------------------------------------------------------------------===//
132+
namespace multiple_owning_args_PR60896 {
133+
134+
// Custom unique_ptr implementation for testing
135+
template <typename T>
136+
struct unique_ptr {
137+
T* ptr;
138+
unique_ptr(T* p) : ptr(p) {}
139+
~unique_ptr() { delete ptr; }
140+
unique_ptr(unique_ptr&& other) : ptr(other.ptr) { other.ptr = nullptr; }
141+
T* get() const { return ptr; }
142+
};
143+
144+
template <typename T, typename... Args>
145+
unique_ptr<T> make_unique(Args&&... args) {
146+
return unique_ptr<T>(new T(args...));
147+
}
148+
149+
// Struct with single smart pointer field
150+
struct SinglePtr {
151+
unique_ptr<int> ptr;
152+
SinglePtr(unique_ptr<int>&& p) : ptr(static_cast<unique_ptr<int>&&>(p)) {}
153+
};
154+
155+
// Struct with multiple smart pointer fields
156+
struct MultiPtr {
157+
unique_ptr<int> ptr1;
158+
unique_ptr<int> ptr2;
159+
unique_ptr<int> ptr3;
160+
161+
MultiPtr(unique_ptr<int>&& p1, unique_ptr<int>&& p2, unique_ptr<int>&& p3)
162+
: ptr1(static_cast<unique_ptr<int>&&>(p1))
163+
, ptr2(static_cast<unique_ptr<int>&&>(p2))
164+
, ptr3(static_cast<unique_ptr<int>&&>(p3)) {}
165+
};
166+
167+
void addMultiple(SinglePtr single, MultiPtr multi) {
168+
// All unique_ptr destructors will be called when the objects go out of scope
169+
// This tests handling of multiple by-value arguments with smart pointer fields
170+
}
171+
172+
void test() {
173+
// No warning should be emitted - all memory is properly managed by unique_ptr
174+
// in the temporary objects, which will properly clean up the memory
175+
addMultiple(
176+
SinglePtr(make_unique<int>(1)),
177+
MultiPtr(make_unique<int>(2), make_unique<int>(3), make_unique<int>(4))
178+
);
179+
}
180+
181+
} // namespace multiple_owning_args_PR60896

0 commit comments

Comments
 (0)