Debian Codegraph @

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

raw
1 // basic test to ensure contracts generalized redecl works
2 // { dg-do run }
3 // { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" }
4 #include <cstdio>
5
6 namespace defining
7 {
8 int x = 10;
9 double y = 10.5;
10
11 struct S
12 {
13 bool z;
14 };
15
16 struct T1
17 {
18 void vfun(int m, double n) const;
19 int fun(int m, double n) volatile;
20 double fun2(int m, double n) const volatile;
21 static S funend(int m, double n);
22 };
23
24 void T1::vfun(int m, double n) const
25 [[ pre: x < 0 ]]
26 [[ pre: m < 0 ]]
27 {
28 printf("vfun::x: %d\n", x);
29 }
30
31 int T1::fun(int m, double n) volatile
32 [[ pre: x < 0 ]]
33 {
34 printf("fun::x: %d\n", x);
35 return x;
36 }
37
38 double T1::fun2(int m, double n) const volatile
39 [[ pre: x < 0 ]]
40 [[ pre: y < 0 ]]
41 [[ pre: m < 0 ]]
42 [[ pre: n < 0 ]]
43 {
44 printf("fun2::x: %d fun2::y: %f\n", x, y);
45 return y;
46 }
47 }
48
49 namespace nondefining
50 {
51 int x = 10;
52 double y = 10.5;
53
54 struct S
55 {
56 bool z;
57 };
58
59 struct T1
60 {
61 void vfun(int m, double n) const;
62 int fun(int m, double n) volatile;
63 double fun2(int m, double n) const volatile;
64 static S funend(int m, double n);
65 };
66
67 void T1::vfun(int m, double n) const
68 [[ pre: x < 0 ]]
69 [[ pre: m < 0 ]];
70 int T1::fun(int m, double n) volatile
71 [[ pre: x < 0 ]];
72 double T1::fun2(int m, double n) const volatile
73 [[ pre: x < 0 ]]
74 [[ pre: y < 0 ]]
75 [[ pre: m < 0 ]]
76 [[ pre: n < 0 ]];
77
78 void T1::vfun(int m, double n) const
79 {
80 printf("vfun::x: %d\n", x);
81 }
82
83 int T1::fun(int m, double n) volatile
84 {
85 printf("fun::x: %d\n", x);
86 return x;
87 }
88
89 double T1::fun2(int m, double n) const volatile
90 {
91 printf("fun2::x: %d fun2::y: %f\n", x, y);
92 return y;
93 }
94 }
95
96 int main(int, char**) {
97 // defining redecl
98 {
99 int x = 11;
100 double y = 11.5;
101
102 defining::T1 t1;
103 t1.vfun(x, y);
104
105 int f = 13;
106 f = t1.fun(x, y);
107 printf("main::f: %d\n", f);
108 double d = 13.37;
109 d = t1.fun2(x, y);
110 printf("main::d: %f\n", d);
111 defining::S s = defining::T1::funend(x, y);
112 printf("main::s.z: %d\n", s.z ? 1 : 0);
113 }
114
115 // nondefining redecl
116 {
117 int x = 12;
118 double y = 12.5;
119
120 nondefining::T1 t1;
121 t1.vfun(x, y);
122
123 int f = 13;
124 f = t1.fun(x, y);
125 printf("main::f: %d\n", f);
126 double d = 13.37;
127 d = t1.fun2(x, y);
128 printf("main::d: %f\n", d);
129 nondefining::S s = nondefining::T1::funend(x, y);
130 printf("main::s.z: %d\n", s.z ? 1 : 0);
131 }
132 return 0;
133 }
134
135 namespace defining
136 {
137 S T1::funend(int m, double n)
138 [[ pre: x < 0 ]]
139 [[ pre: m < 0 ]]
140 {
141 printf("funend::x: %d\n", x);
142 S s;
143 s.z = true;
144 return s;
145 }
146 }
147
148 namespace nondefining
149 {
150 S T1::funend(int m, double n)
151 [[ pre: x < 0 ]]
152 [[ pre: m < 0 ]];
153
154 S T1::funend(int m, double n)
155 {
156 printf("funend::x: %d\n", x);
157 S s;
158 s.z = true;
159 return s;
160 }
161 }
162
163 // { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
164 // { dg-output "contract violation in function defining::T1::vfun at .*.C:25: .*(\n|\r\n|\r)" }
165 // { dg-output "contract violation in function defining::T1::vfun at .*.C:26: .*(\n|\r\n|\r)" }
166 // { dg-output "vfun::x: 10(\n|\r\n|\r)" }
167 // { dg-output "contract violation in function defining::T1::fun at .*.C:32: .*(\n|\r\n|\r)" }
168 // { dg-output "fun::x: 10(\n|\r\n|\r)" }
169 // { dg-output "main::f: 10(\n|\r\n|\r)" }
170 // { dg-output "contract violation in function defining::T1::fun2 at .*.C:39: .*(\n|\r\n|\r)" }
171 // { dg-output "contract violation in function defining::T1::fun2 at .*.C:40: .*(\n|\r\n|\r)" }
172 // { dg-output "contract violation in function defining::T1::fun2 at .*.C:41: .*(\n|\r\n|\r)" }
173 // { dg-output "contract violation in function defining::T1::fun2 at .*.C:42: .*(\n|\r\n|\r)" }
174 // { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
175 // { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
176 // { dg-output "contract violation in function defining::T1::funend at .*.C:138: .*(\n|\r\n|\r)" }
177 // { dg-output "contract violation in function defining::T1::funend at .*.C:139: .*(\n|\r\n|\r)" }
178 // { dg-output "funend::x: 10(\n|\r\n|\r)" }
179 // { dg-output "main::s.z: 1(\n|\r\n|\r)" }
180 // { dg-output "contract violation in function nondefining::T1::vfun at .*.C:68: .*(\n|\r\n|\r)" }
181 // { dg-output "contract violation in function nondefining::T1::vfun at .*.C:69: .*(\n|\r\n|\r)" }
182 // { dg-output "vfun::x: 10(\n|\r\n|\r)" }
183 // { dg-output "contract violation in function nondefining::T1::fun at .*.C:71: .*(\n|\r\n|\r)" }
184 // { dg-output "fun::x: 10(\n|\r\n|\r)" }
185 // { dg-output "main::f: 10(\n|\r\n|\r)" }
186 // { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:73: .*(\n|\r\n|\r)" }
187 // { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:74: .*(\n|\r\n|\r)" }
188 // { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:75: .*(\n|\r\n|\r)" }
189 // { dg-output "contract violation in function nondefining::T1::fun2 at .*.C:76: .*(\n|\r\n|\r)" }
190 // { dg-output "fun2::x: 10 fun2::y: 10.500000(\n|\r\n|\r)" }
191 // { dg-output "main::d: 10.500000(\n|\r\n|\r)" }
192 // { dg-output "contract violation in function nondefining::T1::funend at .*.C:151: .*(\n|\r\n|\r)" }
193 // { dg-output "contract violation in function nondefining::T1::funend at .*.C:152: .*(\n|\r\n|\r)" }
194 // { dg-output "funend::x: 10(\n|\r\n|\r)" }
195 // { dg-output "main::s.z: 1(\n|\r\n|\r)" }
196