Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wfree-nonheap-object-3.C

raw
1 /* PR c++/57111 - Generalize -Wfree-nonheap-object to delete
2 Verify that even without -Wsystem-headers the warning is issued
3 for pairs of library functions defined in system headers.
4 { dg-do compile { target c++11 } }
5 { dg-options "-O2 -Wall" }
6 { dg-skip-if "requires hosted libstdc++ for string" { ! hostedlib } } */
7
8 #include <memory>
9 #include <string>
10
11 void test_string ()
12 {
13 std::string str ("abc"); // { dg-message "declared here" }
14
15 const char *s = str.c_str ();
16 __builtin_printf ("%s\n", s);
17
18 /* Because the delete call is made directly in the function this
19 does not exercise the same thing as test_unique_ptr. */
20 delete s; // { dg-warning "'void operator delete\\(void\\*\[^\\)\]*\\)' called on unallocated object 'str'" }
21 }
22
23 void test_unique_ptr ()
24 {
25 int arr[]= { 1, 2 }; // { dg-message "declared here" }
26
27 std::unique_ptr<int[]> up (arr);
28 __builtin_printf ("%i %i\n", up[0], up[1]);
29
30 /* TO DO: verify that the warning is printed, including its inlining
31 context (the directive below doesn't work):
32 { Xdg-message "In member function.*inlined from 'void test_unique_ptr\\(\\)'.*warning: 'void operator delete \\\[]\\(void\\*\\)' called on unallocated object 'arr'" "" { target *-*-* } 0 } */
33
34 /* Here, the delete call is made indirectly from std::unique_ptr
35 dtor. */
36 }
37
38 /* Prune out the warning from test_unique_ptr().
39 { dg-prune-output "-Wfree-nonheap-object" } */