Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / free.cpp
bloba812a22c47d396bd71ee0e9e1ad8b29f549cbe83
1 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.Malloc
4 //
5 // RUN: %clang_analyze_cc1 -fblocks -verify %s \
6 // RUN: -analyzer-checker=core \
7 // RUN: -analyzer-checker=unix.Malloc \
8 // RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true
9 namespace std {
10 using size_t = decltype(sizeof(int));
11 void free(void *);
14 extern "C" void free(void *);
15 extern "C" void *alloca(std::size_t);
17 void t1a () {
18 int a[] = { 1 };
19 free(a);
20 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
21 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
24 void t1b () {
25 int a[] = { 1 };
26 std::free(a);
27 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
28 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
31 void t2a () {
32 int a = 1;
33 free(&a);
34 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
35 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
38 void t2b () {
39 int a = 1;
40 std::free(&a);
41 // expected-warning@-1{{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
42 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
45 void t3a () {
46 static int a[] = { 1 };
47 free(a);
48 // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
49 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
52 void t3b () {
53 static int a[] = { 1 };
54 std::free(a);
55 // expected-warning@-1{{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
56 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
59 void t4a (char *x) {
60 free(x); // no-warning
63 void t4b (char *x) {
64 std::free(x); // no-warning
67 void t5a () {
68 extern char *ptr();
69 free(ptr()); // no-warning
72 void t5b () {
73 extern char *ptr();
74 std::free(ptr()); // no-warning
77 void t6a () {
78 free((void*)1000);
79 // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
80 // expected-warning@-2{{attempt to call free on non-heap object '(void *)1000'}}
83 void t6b () {
84 std::free((void*)1000);
85 // expected-warning@-1{{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
86 // expected-warning@-2{{attempt to call std::free on non-heap object '(void *)1000'}}
89 void t7a (char **x) {
90 free(*x); // no-warning
93 void t7b (char **x) {
94 std::free(*x); // no-warning
97 void t8a (char **x) {
98 // ugh
99 free((*x)+8); // no-warning
102 void t8b (char **x) {
103 // ugh
104 std::free((*x)+8); // no-warning
107 void t9a () {
108 label:
109 free(&&label);
110 // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
111 // expected-warning@-2{{attempt to call free on non-heap object 'label'}}
114 void t9b () {
115 label:
116 std::free(&&label);
117 // expected-warning@-1{{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
118 // expected-warning@-2{{attempt to call std::free on non-heap object 'label'}}
121 void t10a () {
122 free((void*)&t10a);
123 // expected-warning@-1{{Argument to free() is the address of the function 't10a', which is not memory allocated by malloc()}}
124 // expected-warning@-2{{attempt to call free on non-heap object 't10a'}}
127 void t10b () {
128 std::free((void*)&t10b);
129 // expected-warning@-1{{Argument to free() is the address of the function 't10b', which is not memory allocated by malloc()}}
130 // expected-warning@-2{{attempt to call std::free on non-heap object 't10b'}}
133 void t11a () {
134 char *p = (char*)alloca(2);
135 free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
138 void t11b () {
139 char *p = (char*)alloca(2);
140 std::free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
143 void t12a () {
144 char *p = (char*)__builtin_alloca(2);
145 free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
148 void t12b () {
149 char *p = (char*)__builtin_alloca(2);
150 std::free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}}
153 void t13a () {
154 free(^{return;});
155 // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
156 // expected-warning@-2{{attempt to call free on non-heap object: block expression}}
159 void t13b () {
160 std::free(^{return;});
161 // expected-warning@-1{{Argument to free() is a block, which is not memory allocated by malloc()}}
162 // expected-warning@-2{{attempt to call std::free on non-heap object: block expression}}
165 void t14a () {
166 free((void *)+[]{ return; });
167 // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
168 // expected-warning@-2{{attempt to call free on non-heap object: lambda-to-function-pointer conversion}}
171 void t14b () {
172 std::free((void *)+[]{ return; });
173 // expected-warning@-1{{Argument to free() is the address of the function '__invoke', which is not memory allocated by malloc()}}
174 // expected-warning@-2{{attempt to call std::free on non-heap object: lambda-to-function-pointer conversion}}
177 void t15a (char a) {
178 free(&a);
179 // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
180 // expected-warning@-2{{attempt to call free on non-heap object 'a'}}
183 void t15b (char a) {
184 std::free(&a);
185 // expected-warning@-1{{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
186 // expected-warning@-2{{attempt to call std::free on non-heap object 'a'}}
189 static int someGlobal[2];
190 void t16a () {
191 free(someGlobal);
192 // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
193 // expected-warning@-2{{attempt to call free on non-heap object 'someGlobal'}}
196 void t16b () {
197 std::free(someGlobal);
198 // expected-warning@-1{{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
199 // expected-warning@-2{{attempt to call std::free on non-heap object 'someGlobal'}}
202 void t17a (char **x, int offset) {
203 // Unknown value
204 free(x[offset]); // no-warning
207 void t17b (char **x, int offset) {
208 // Unknown value
209 std::free(x[offset]); // no-warning
212 struct S {
213 const char* p;
216 void t18_C_style_C_style_free (S s) {
217 free((void*)(unsigned long long)s.p); // no warning
220 void t18_C_style_C_style_std_free (S s) {
221 std::free((void*)(unsigned long long)s.p); // no warning
224 void t18_C_style_reinterpret_free (S s) {
225 free((void*)reinterpret_cast<unsigned long long>(s.p)); // no warning
228 void t18_C_style_reinterpret_std_free (S s) {
229 std::free((void*)reinterpret_cast<unsigned long long>(s.p)); // no warning
232 void t18_reinterpret_C_style_free (S s) {
233 free(reinterpret_cast<void*>((unsigned long long)(s.p))); // no warning
236 void t18_reinterpret_C_style_std_free (S s) {
237 std::free(reinterpret_cast<void*>((unsigned long long)(s.p))); // no warning
240 void t18_reinterpret_reinterpret_free (S s) {
241 free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s.p))); // no warning
244 void t18_reinterpret_reinterpret_std_free (S s) {
245 std::free(reinterpret_cast<void*>(reinterpret_cast<unsigned long long>(s.p))); // no warning