Skip to content

Commit 0df5e13

Browse files
committed
csa/tests: add test suite for custom allocation classes
1 parent 4655163 commit 0df5e13

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

clang/test/Analysis/MismatchedDeallocator-checker-test.mm

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
void free(void *);
1515
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
1616

17+
void __attribute((ownership_returns(malloc1))) *my_malloc1(size_t);
18+
void __attribute((ownership_takes(malloc1, 1))) my_free1(void *);
19+
20+
void __attribute((ownership_returns(malloc2))) *my_malloc2(size_t);
21+
void __attribute((ownership_returns(malloc2))) *my_malloc3(size_t);
22+
void __attribute((ownership_takes(malloc2, 1))) __attribute((ownership_takes(malloc3, 1))) my_free23(void *);
23+
1724
//---------------------------------------------------------------
1825
// Test if an allocation function matches deallocation function
1926
//---------------------------------------------------------------
@@ -60,6 +67,41 @@ void testMalloc8() {
6067
operator delete[](p); // expected-warning{{Memory allocated by malloc() should be deallocated by free(), not operator delete[]}}
6168
}
6269

70+
void testMalloc9() {
71+
int *p = (int *)my_malloc(sizeof(int));
72+
my_free(p); // no warning
73+
}
74+
75+
void testMalloc10() {
76+
int *p = (int *)my_malloc1(sizeof(int));
77+
my_free1(p); // no warning
78+
}
79+
80+
void testMalloc11() {
81+
int *p = (int *)my_malloc2(sizeof(int));
82+
my_free23(p); // no warning
83+
}
84+
85+
void testMalloc12() {
86+
int *p = (int *)my_malloc1(sizeof(int));
87+
my_free(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not my_free(), which takes ownership of 'malloc'}}
88+
}
89+
90+
void testMalloc13() {
91+
int *p = (int *)my_malloc2(sizeof(int));
92+
my_free1(p); // expected-warning{{Memory allocated by my_malloc2() should be deallocated by function that takes ownership of 'malloc2', not my_free1(), which takes ownership of 'malloc1'}}
93+
}
94+
95+
void testMalloc14() {
96+
int *p = (int *)my_malloc1(sizeof(int));
97+
my_free23(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not my_free23(), which takes ownership of 'malloc2', 'malloc3'}}
98+
}
99+
100+
void testMalloc15() {
101+
int *p = (int *)my_malloc1(sizeof(int));
102+
free(p); // expected-warning{{Memory allocated by my_malloc1() should be deallocated by function that takes ownership of 'malloc1', not free()}}
103+
}
104+
63105
void testAlloca() {
64106
int *p = (int *)__builtin_alloca(sizeof(int));
65107
delete p; // expected-warning{{Memory allocated by alloca() should not be deallocated}}

0 commit comments

Comments
 (0)