1 // RUN: %clang_cc1 -Wfree-nonheap-object -std=c++11 -x c++ -fsyntax-only -verify %s
3 extern "C" void free(void *) {}
6 using size_t = decltype(sizeof(0));
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
); }
19 operator char *() { return ptr1
; }
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'}}
25 free_reference(*ptr2
);
26 free_reference(static_cast<char&&>(*ptr3
));
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'}}
33 std_free_reference(*ptr2
);
34 std_free_reference(static_cast<char&&>(*ptr3
));
38 char *ptr1
= (char *)std::malloc(10);
39 char *ptr2
= (char *)std::malloc(10);
40 char *ptr3
= (char *)std::malloc(10);
48 free(&GI
); // expected-warning {{attempt to call free on non-heap object 'GI'}}
52 free(&SI
); // expected-warning {{attempt to call free on non-heap object 'SI'}}
56 free(&I
); // expected-warning {{attempt to call free on non-heap object 'I'}}
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'}}
78 free(&s
); // expected-warning {{attempt to call free on non-heap object 's'}}
88 std::free(&GI
); // expected-warning {{attempt to call std::free on non-heap object 'GI'}}
92 std::free(&SI
); // expected-warning {{attempt to call std::free on non-heap object 'SI'}}
96 std::free(&I
); // expected-warning {{attempt to call std::free on non-heap object 'I'}}
101 std::free(P
); // FIXME diagnosing this would require control flow analysis.
104 void *P
= std::malloc(8);
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'}}
126 std::free(&s
); // expected-warning {{attempt to call std::free on non-heap object 's'}}