Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wnonnull9.C

raw
1 /* PR c++/98646 - spurious -Wnonnull calling a member on the result
2 of static_cast
3 { dg-do compile }
4 { dg-options "-O2 -Wall" } */
5
6 struct A { virtual ~A (); };
7 struct B
8 {
9 virtual ~B ();
10 B* bptr ();
11 B& bref ();
12 };
13
14 struct C: A, B { virtual ~C (); void g () const; };
15
16
17 void c_cast_C_ptr (B *p)
18 {
19 ((C*)p->bptr ())->g ();
20 }
21
22 void c_cast_const_C_ptr (B *p)
23 {
24 ((const C*)p->bptr ())->g ();
25 }
26
27 void static_cast_C_ptr (B *p)
28 {
29 static_cast<C*>(p->bptr ())->g ();
30 }
31
32 void static_cast_const_C_ptr (B *p)
33 {
34 /* The static_cast can't fail so verify that no warning is issued
35 here, even though GCC emits a null check for its argument. */
36 static_cast<const C*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
37 }
38
39 void dynamic_cast_C_ptr (B *p)
40 {
41 /* Unlike static_cast, dynamic cast may return null even for a nonnull
42 operand but detecting assumptions to the contrary isn't -Wnonnull's
43 purpose. Verify -Wnonnull isn't issued, either for the implicitly
44 emitted null check or for other reasons (the latter may be worth
45 warning for by some other warning). See also pr99251. */
46 dynamic_cast<C*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
47 }
48
49 void dynamic_cast_const_C_ptr (B *p)
50 {
51 dynamic_cast<const C*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
52 }
53
54
55 void c_cast_C_ref (B *p)
56 {
57 ((C&)p->bref ()).g ();
58 }
59
60 void c_cast_const_C_ref (B *p)
61 {
62 ((const C&)p->bref ()).g ();
63 }
64
65 void static_cast_C_ref (B *p)
66 {
67 static_cast<C&>(p->bref ()).g ();
68 }
69
70 void static_cast_const_C_ref (B *p)
71 {
72 static_cast<const C&>(p->bref ()).g ();
73 }
74
75 void dynamic_cast_C_ref (B *p)
76 {
77 /* The dynamic_cast fails by throwing an exception so verify that
78 no warning is issued. */
79 dynamic_cast<C&>(p->bref ()).g ();
80 }
81
82 void dynamic_cast_const_C_ref (B *p)
83 {
84 dynamic_cast<const C&>(p->bref ()).g ();
85 }
86
87
88 struct D: B, A { virtual ~D (); void g () const; };
89
90 void c_cast_D_ptr (B *p)
91 {
92 ((D*)p->bptr ())->g ();
93 }
94
95 void c_cast_const_D_ptr (B *p)
96 {
97 ((const D*)p->bptr ())->g ();
98 }
99
100 void static_cast_D_ptr (B *p)
101 {
102 static_cast<D*>(p->bptr ())->g ();
103 }
104
105 void static_cast_const_D_ptr (B *p)
106 {
107 /* The static_cast can't fail so verify that no warning is issued
108 here, even though GCC emits a null check for its argument. */
109 static_cast<const D*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
110 }
111
112 void dynamic_cast_D_ptr (B *p)
113 {
114 /* Unlike static_cast, dynamic cast may return null even for a nonnull
115 operand but detecting assumptions to the contrary isn't -Wnonnull's
116 purpose. Verify -Wnonnull isn't issued, either for the implicitly
117 emitted null check or for other reasons (the latter may be worth
118 warning for by some other warning). See also pr99251. */
119 dynamic_cast<D*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
120 }
121
122 void dynamic_cast_const_D_ptr (B *p)
123 {
124 dynamic_cast<const D*>(p->bptr ())->g (); // { dg-bogus "\\\[-Wnonnull" }
125 }