Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wdangling-pointer.C

raw
1 /* Exercise basic C++-only cases of -Wdangling-pointer.
2 { dg-do compile }
3 { dg-options "-Wall -Wno-class-memaccess" } */
4
5 extern "C" void* memset (void*, int, __SIZE_TYPE__);
6
7 void sink (const void*, ...);
8
9 struct S { S (); };
10
11 void nowarn_int_ref (int i)
12 {
13 const S &sref = S ();
14 const int &iref = 1 + i;
15 sink (&sref, &iref);
16 }
17
18 void warn_init_ref_member ()
19 {
20 struct AS
21 {
22 const S &sref;
23 AS ():
24 // The temporary S object is destroyed when AS::AS() returns.
25 sref (S ()) // { dg-warning "storing the address" }
26 { }
27 } as;
28
29 struct Ai
30 {
31 const int &iref;
32 Ai ():
33 // The temporary int is destroyed when Ai::Ai() returns.
34 iref (1 + 1) // { dg-warning "storing the address" }
35 { }
36 } ai;
37
38 struct Al
39 {
40 const S &sref;
41 Al ():
42 // The temporary S object is destroyed when Al::Al() returns.
43 sref (S ()) // { dg-warning "storing the address" }
44 {
45 // Copying this to an SSA_NAME used to disable the warning:
46 Al *ptr = this;
47 asm ("" : "+r" (ptr));
48 }
49 } al;
50
51 struct An
52 {
53 An *next;
54 const S &sref;
55 An ():
56 next (0),
57 // The temporary S object is destroyed when An::An() returns.
58 sref (S ()) // { dg-warning "storing the address" "" { xfail *-*-* } }
59 {
60 // ??? Writing to another part of *this disables the warning:
61 next = 0;
62 }
63 } an;
64
65 sink (&as, &ai, &al, &an);
66 }
67
68
69 void default_ref_arg (const S& = S ());
70
71 void nowarn_call_default_ref_arg ()
72 {
73 default_ref_arg ();
74 }
75
76
77 void nowarn_array_access ()
78 {
79 /* Verify that the clobber in the exceptional basic block doesn't
80 cause bogus warnings. */
81 S a[1];
82 memset (a, 0, sizeof a);
83 sink (a);
84 }
85
86
87 void nowarn_array_access_cond (int i)
88 {
89 if (i)
90 {
91 S a1[1];
92 memset (a1, 0, sizeof a1);
93 sink (a1);
94 }
95 else
96 {
97 S a2[2];
98 memset (a2, 0, sizeof a2);
99 sink (a2);
100 }
101 }