Debian Codegraph @

gcc/testsuite/g++.dg/contracts/contracts-access1.C

raw
1 // ensure that that preconditions can access public, protected, and private
2 // members of the current and base classes
3 // { dg-do run }
4 // { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
5
6 struct Base
7 {
8 int pub{-1};
9
10 virtual int b()
11 [[ pre: pub > 0 ]]
12 [[ pre: pro > 0 ]]
13 [[ pre: pri > 0 ]]
14 {
15 return pub * pro * pri;
16 }
17
18 protected:
19 int pro{-1};
20 int pri{-1};
21 };
22
23 struct Child : Base
24 {
25 int fun()
26 [[ pre: pub > 0 ]]
27 [[ pre: pro > 0 ]]
28 [[ pre: pri > 0 ]]
29 {
30 return pub * pro;
31 }
32 };
33
34 struct VChild : Base
35 {
36 int b()
37 [[ pre: pub > 0 ]]
38 [[ pre: pro > 0 ]]
39 [[ pre: pri > 0 ]]
40 {
41 return pub * pro;
42 }
43 };
44
45 template<typename B>
46 struct TChild : B
47 {
48 int fun()
49 [[ pre: B::pub > 0 ]]
50 [[ pre: B::pro > 0 ]]
51 [[ pre: B::pri > 0 ]]
52 {
53 return B::pub * B::pro;
54 }
55 };
56
57 struct PubBase
58 {
59 int pub{-1};
60 int pro{-1};
61 int pri{-1};
62 };
63
64 struct PubChild : PubBase
65 {
66 int fun()
67 [[ pre: pub > 0 ]]
68 [[ pre: pro > 0 ]]
69 [[ pre: pri > 0 ]]
70 {
71 return pub * pro;
72 }
73 };
74
75 template<typename B>
76 struct TPubChild : B
77 {
78 int fun()
79 [[ pre: B::pub > 0 ]]
80 [[ pre: B::pro > 0 ]]
81 [[ pre: B::pri > 0 ]]
82 {
83 return B::pub * B::pro;
84 }
85 };
86
87 int main()
88 {
89 Base base{};
90 base.b();
91
92 Child child{};
93 child.fun();
94
95 VChild vchild{};
96 vchild.b();
97
98 TChild<Base> tchild{};
99 tchild.fun();
100
101 PubChild pubchild{};
102 pubchild.fun();
103
104 TPubChild<PubBase> tpubchild;
105 tpubchild.fun();
106
107 return 0;
108 }
109
110 // { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } }
111 // { dg-output "contract violation in function Base::b at .*.C:11: .*(\n|\r\n|\r)" }
112 // { dg-output "contract violation in function Base::b at .*.C:12: .*(\n|\r\n|\r)" }
113 // { dg-output "contract violation in function Base::b at .*.C:13: .*(\n|\r\n|\r)" }
114 // { dg-output "contract violation in function Child::fun at .*.C:26: .*(\n|\r\n|\r)" }
115 // { dg-output "contract violation in function Child::fun at .*.C:27: .*(\n|\r\n|\r)" }
116 // { dg-output "contract violation in function Child::fun at .*.C:28: .*(\n|\r\n|\r)" }
117 // { dg-output "contract violation in function VChild::b at .*.C:37: .*(\n|\r\n|\r)" }
118 // { dg-output "contract violation in function VChild::b at .*.C:38: .*(\n|\r\n|\r)" }
119 // { dg-output "contract violation in function VChild::b at .*.C:39: .*(\n|\r\n|\r)" }
120 // { dg-output "contract violation in function TChild<Base>::fun at .*.C:49: .*(\n|\r\n|\r)" }
121 // { dg-output "contract violation in function TChild<Base>::fun at .*.C:50: .*(\n|\r\n|\r)" }
122 // { dg-output "contract violation in function TChild<Base>::fun at .*.C:51: .*(\n|\r\n|\r)" }
123 // { dg-output "contract violation in function PubChild::fun at .*.C:67: .*(\n|\r\n|\r)" }
124 // { dg-output "contract violation in function PubChild::fun at .*.C:68: .*(\n|\r\n|\r)" }
125 // { dg-output "contract violation in function PubChild::fun at .*.C:69: .*(\n|\r\n|\r)" }
126 // { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:79: .*(\n|\r\n|\r)" }
127 // { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:80: .*(\n|\r\n|\r)" }
128 // { dg-output "contract violation in function TPubChild<PubBase>::fun at .*.C:81: .*(\n|\r\n|\r)" }
129