Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/compound-assignment-1.c

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4
5 struct ptr_wrapper
6 {
7 int *ptr;
8 };
9
10 struct ptr_wrapper
11 test_1 (void)
12 {
13 struct ptr_wrapper r;
14 r.ptr = (int *) malloc (sizeof (int));
15 return r;
16 }
17
18 struct ptr_wrapper
19 test_2 (void)
20 {
21 struct ptr_wrapper r, s;
22 r.ptr = (int *) malloc (sizeof (int));
23 s = r;
24 return s;
25 }
26
27 struct nested
28 {
29 struct ptr_wrapper w;
30 };
31
32 struct nested
33 test_3 (void)
34 {
35 struct nested n;
36 n.w.ptr = (int *) malloc (sizeof (int));
37 return n;
38 }
39
40 void test_4 (void)
41 {
42 struct ptr_wrapper r;
43 r.ptr = (int *) malloc (sizeof (int)); /* { dg-message "allocated here" } */
44 } /* { dg-warning "leak of 'r.ptr'" "" { target c } } */
45 /* { dg-warning "leak of 'r.ptr_wrapper::ptr'" "" { target c++ } .-1 } */
46 /* { dg-bogus "leak of '<unknown>'" "unknown leak" { target *-*-* } .-1 } */
47
48 static struct ptr_wrapper __attribute__((noinline))
49 called_by_test_5a (void)
50 {
51 struct ptr_wrapper r;
52 r.ptr = (int *) malloc (sizeof (int)); /* { dg-message "allocated here" } */
53 return r;
54 }
55
56 void test_5a (void)
57 {
58 struct ptr_wrapper q = called_by_test_5a ();
59 } /* { dg-warning "leak of 'q.ptr'" "" { target c } } */
60 /* { dg-warning "leak of 'q.ptr_wrapper::ptr'" "" { target c++ } .-1 } */
61
62 static struct ptr_wrapper __attribute__((noinline))
63 called_by_test_5b (void)
64 {
65 struct ptr_wrapper r;
66 r.ptr = (int *) malloc (sizeof (int));
67 return r; /* { dg-warning "leak of '<return-value>.ptr'" "" { target c } } */
68 /* TODO: show the allocation point; improve above messages. C++ does show it. */
69 }
70
71 void test_5b (void)
72 {
73 called_by_test_5b (); }
74 /* { dg-warning "leak of '<anonymous>.ptr_wrapper::ptr'" "" { target c++ } .-1 } */
75 /* The closing } above is intentionally on the same line as the call, because
76 otherwise the exact line of the diagnostics depends on whether the
77 called_by_test_5b () call satisfies aggregate_value_p or not. */