[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / warn-free-nonheap-object.cpp
blob37dc0fdaad935dbd794a7b9e4bb7446992c5dd24
1 // RUN: %clang_cc1 -Wfree-nonheap-object -std=c++11 -x c++ -fsyntax-only -verify %s
3 extern "C" void free(void *) {}
5 namespace std {
6 using size_t = decltype(sizeof(0));
7 void *malloc(size_t);
8 void free(void *p);
9 } // namespace std
11 int GI;
13 void free_reference(char &x) { ::free(&x); }
14 void free_reference(char &&x) { ::free(&x); }
15 void std_free_reference(char &x) { std::free(&x); }
16 void std_free_reference(char &&x) { std::free(&x); }
18 struct S {
19 operator char *() { return ptr1; }
21 void CFree() {
22 ::free(&ptr1); // expected-warning {{attempt to call free on non-heap object 'ptr1'}}
23 ::free(&I); // expected-warning {{attempt to call free on non-heap object 'I'}}
24 ::free(ptr1);
25 free_reference(*ptr2);
26 free_reference(static_cast<char&&>(*ptr3));
29 void CXXFree() {
30 std::free(&ptr1); // expected-warning {{attempt to call std::free on non-heap object 'ptr1'}}
31 std::free(&I); // expected-warning {{attempt to call std::free on non-heap object 'I'}}
32 std::free(ptr1);
33 std_free_reference(*ptr2);
34 std_free_reference(static_cast<char&&>(*ptr3));
37 private:
38 char *ptr1 = (char *)std::malloc(10);
39 char *ptr2 = (char *)std::malloc(10);
40 char *ptr3 = (char *)std::malloc(10);
41 static int I;
44 int S::I = 0;
46 void test1() {
48 free(&GI); // expected-warning {{attempt to call free on non-heap object 'GI'}}
51 static int SI = 0;
52 free(&SI); // expected-warning {{attempt to call free on non-heap object 'SI'}}
55 int I = 0;
56 free(&I); // expected-warning {{attempt to call free on non-heap object 'I'}}
59 int I = 0;
60 int *P = &I;
61 free(P);
64 void *P = std::malloc(8);
65 free(P); // FIXME diagnosing this would require control flow analysis.
68 int A[] = {0, 1, 2, 3};
69 free(A); // expected-warning {{attempt to call free on non-heap object 'A'}}
72 int A[] = {0, 1, 2, 3};
73 free(&A); // expected-warning {{attempt to call free on non-heap object 'A'}}
76 S s;
77 free(s);
78 free(&s); // expected-warning {{attempt to call free on non-heap object 's'}}
81 S s;
82 s.CFree();
86 void test2() {
88 std::free(&GI); // expected-warning {{attempt to call std::free on non-heap object 'GI'}}
91 static int SI = 0;
92 std::free(&SI); // expected-warning {{attempt to call std::free on non-heap object 'SI'}}
95 int I = 0;
96 std::free(&I); // expected-warning {{attempt to call std::free on non-heap object 'I'}}
99 int I = 0;
100 int *P = &I;
101 std::free(P); // FIXME diagnosing this would require control flow analysis.
104 void *P = std::malloc(8);
105 std::free(P);
108 char* P = (char *)std::malloc(2);
109 std_free_reference(*P);
112 char* P = (char *)std::malloc(2);
113 std_free_reference(static_cast<char&&>(*P));
116 int A[] = {0, 1, 2, 3};
117 std::free(A); // expected-warning {{attempt to call std::free on non-heap object 'A'}}
120 int A[] = {0, 1, 2, 3};
121 std::free(&A); // expected-warning {{attempt to call std::free on non-heap object 'A'}}
124 S s;
125 std::free(s);
126 std::free(&s); // expected-warning {{attempt to call std::free on non-heap object 's'}}
129 S s;
130 s.CXXFree();