Skip to content

Commit 13b3360

Browse files
committed
csa/tests: add test suite for custom allocation classes
1 parent 4fa8b72 commit 13b3360

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
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+
22+
// The order of these declarations are important to verify that analisys still works even
23+
// if there are less specific declarations of the same functions
24+
void __attribute((ownership_returns(malloc3))) *my_malloc3(size_t);
25+
void *my_malloc3(size_t);
26+
27+
void *my_malloc4(size_t);
28+
void __attribute((ownership_returns(malloc4))) *my_malloc4(size_t);
29+
1730
//---------------------------------------------------------------
1831
// Test if an allocation function matches deallocation function
1932
//---------------------------------------------------------------
@@ -60,6 +73,41 @@ void testMalloc8() {
6073
operator delete[](p); // expected-warning{{Memory allocated by 'malloc()' should be deallocated by 'free()', not 'operator delete[]'}}
6174
}
6275

76+
void testMalloc9() {
77+
int *p = (int *)my_malloc(sizeof(int));
78+
my_free(p); // no warning
79+
}
80+
81+
void testMalloc10() {
82+
int *p = (int *)my_malloc1(sizeof(int));
83+
my_free1(p); // no warning
84+
}
85+
86+
void testMalloc11() {
87+
int *p = (int *)my_malloc1(sizeof(int));
88+
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'}}
89+
}
90+
91+
void testMalloc12() {
92+
int *p = (int *)my_malloc2(sizeof(int));
93+
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'}}
94+
}
95+
96+
void testMalloc13() {
97+
int *p = (int *)my_malloc1(sizeof(int));
98+
free(p); // expected-warning{{Memory allocated by 'my_malloc1()' should be deallocated by function that takes ownership of 'malloc1', not 'free()'}}
99+
}
100+
101+
void testMalloc14() {
102+
int *p = (int *)my_malloc3(sizeof(int));
103+
free(p); // expected-warning{{Memory allocated by 'my_malloc3()' should be deallocated by function that takes ownership of 'malloc3', not 'free()'}}
104+
}
105+
106+
void testMalloc15() {
107+
int *p = (int *)my_malloc4(sizeof(int));
108+
free(p); // expected-warning{{Memory allocated by 'my_malloc4()' should be deallocated by function that takes ownership of 'malloc4', not 'free()'}}
109+
}
110+
63111
void testAlloca() {
64112
int *p = (int *)__builtin_alloca(sizeof(int));
65113
delete p; // expected-warning{{Memory allocated by 'alloca()' should not be deallocated}}

0 commit comments

Comments
 (0)