Skip to content

Commit 689397c

Browse files
committed
adding constification of globals and non type template parameters of reference type
1 parent 37b0ff4 commit 689397c

File tree

6 files changed

+30
-18
lines changed

6 files changed

+30
-18
lines changed

gcc/cp/contracts.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,12 +2742,13 @@ constify_contract_access(tree decl)
27422742
* parameter of reference type
27432743
*/
27442744
if (!TREE_READONLY (decl)
2745-
&& ((VAR_P (decl) && decl_storage_duration (decl) == dk_auto)
2745+
&& (VAR_P (decl)
27462746
|| (TREE_CODE (decl) == PARM_DECL)
27472747
|| (REFERENCE_REF_P (decl)
2748-
&& ((VAR_P (TREE_OPERAND (decl, 0))
2749-
&& decl_storage_duration(TREE_OPERAND (decl, 0)) == dk_auto)
2750-
|| (TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL)))))
2748+
&& (VAR_P (TREE_OPERAND (decl, 0))
2749+
|| (TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL)
2750+
|| (TREE_CODE (TREE_OPERAND (decl, 0))
2751+
== TEMPLATE_PARM_INDEX)))))
27512752
{
27522753
decl = view_as_const (decl);
27532754
}

gcc/cp/semantics.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,6 +4720,7 @@ finish_id_expression_1 (tree id_expression,
47204720
| TYPE_QUAL_CONST));
47214721
r = build1 (VIEW_CONVERT_EXPR, ctype, r);
47224722
}
4723+
47234724
r = convert_from_reference (r);
47244725
if (integral_constant_expression_p
47254726
&& !dependent_type_p (TREE_TYPE (decl))
@@ -4731,6 +4732,11 @@ finish_id_expression_1 (tree id_expression,
47314732
"integral or enumeration type", decl, TREE_TYPE (decl));
47324733
*non_integral_constant_expression_p = true;
47334734
}
4735+
4736+
if (flag_contracts_nonattr && should_constify_contract
4737+
&& processing_contract_condition)
4738+
r = constify_contract_access(r);
4739+
47344740
return r;
47354741
}
47364742
else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)

gcc/testsuite/g++.dg/contracts/cpp26/constification.C

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ static_assert (__cpp_contracts_roles >= 201906);
1818
int gi = 7;
1919
int &gri = gi;
2020

21+
bool f(int&&){ return true;}
22+
int temporary(){ return 4;}
2123

2224
void f1(int i) pre(i++); // { dg-error "increment of read-only location" }
2325
void f2(int &i) pre(i++); // { dg-error "increment of read-only location" }
2426
void f3(int *i) pre(i++); // { dg-error "increment of read-only location" }
2527
void f4(int *i) pre((*i)++); // ok, not deep const
26-
void f5(int *i) pre(gi++); // ok, non automatic storage
28+
void f5(int *i) pre(gi++); // { dg-error "increment of read-only location" }
29+
void f6() pre(f(temporary())); // ok, lifetime started within pre condition
2730

2831
// todo structured binding test
2932
// lambda tests
@@ -97,8 +100,8 @@ int main()
97100
contract_assert(ri++); // { dg-error "increment of read-only location" }
98101
ri = 6;
99102

100-
contract_assert(gi++); // ok, not automatic storage
101-
contract_assert(gri++); // ok, not automatic storage
103+
contract_assert(gi++); // { dg-error "increment of read-only location" }
104+
contract_assert(gri++); // { dg-error "increment of read-only location" }
102105

103106
int& rgi = gi;
104107
contract_assert(rgi++); // { dg-error "increment of read-only location" }

gcc/testsuite/g++.dg/contracts/cpp26/constification_const.C

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void f1(int i) pre const(i++); // { dg-error "increment of read-only location" }
2323
void f2(int &i) pre const(i++); // { dg-error "increment of read-only location" }
2424
void f3(int *i) pre const(i++); // { dg-error "increment of read-only location" }
2525
void f4(int *i) pre const((*i)++); // ok, not deep const
26-
void f5(int *i) pre const(gi++); // ok, non automatic storage
26+
void f5(int *i) pre const(gi++); // { dg-error "increment of read-only location" }
2727

2828
// todo structured binding test
2929
// lambda tests
@@ -97,8 +97,8 @@ int main()
9797
contract_assert const(ri++); // { dg-error "increment of read-only location" }
9898
ri = 6;
9999

100-
contract_assert const(gi++); // ok, not automatic storage
101-
contract_assert const(gri++); // ok, not automatic storage
100+
contract_assert const(gi++); // { dg-error "increment of read-only location" }
101+
contract_assert const(gri++); // { dg-error "increment of read-only location" }
102102

103103
int& rgi = gi;
104104
contract_assert const(rgi++); // { dg-error "increment of read-only location" }

gcc/testsuite/g++.dg/contracts/cpp26/constification_const_mutable.C

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ void f1(int i) pre const(i++); // { dg-error "increment of read-only location" }
2323
void f2(int &i) pre const(i++); // { dg-error "increment of read-only location" }
2424
void f3(int *i) pre const(i++); // { dg-error "increment of read-only location" }
2525
void f4(int *i) pre const((*i)++); // ok, not deep const
26-
void f5(int *i) pre const(gi++); // ok, non automatic storage
26+
void f5(int *i) pre const(gi++); // { dg-error "increment of read-only location" }
2727
void f6(int *i) pre const mutable(gi++); // { dg-error {cannot be both 'const' and 'mutable'} }
28+
// { dg-error {increment of read-only location} "" { target *-*-* } .-1 }
2829
void f7(int *i) pre const((*i)++) pre mutable((*i)++); // ok, not deep const
29-
void f8(int *i) pre const(gi++) pre mutable(gi++); // ok, non automatic storage
30+
void f8(int *i) pre const(gi++) pre mutable(gi++); // { dg-error "increment of read-only location" }
3031

3132
// todo structured binding test
3233
// lambda tests
@@ -81,7 +82,7 @@ void template_related_tests()
8182
perfect_forward(ci);
8283
perfect_forward((const int&&) ci);
8384
perfect_forward2(666);
84-
// { dg-error {increment of read-only location} "" { target *-*-* } 66 }
85+
// { dg-error {increment of read-only location} "" { target *-*-* } 67 }
8586
S2 s;
8687
const S2 cs;
8788
s.perfect_forward();
@@ -109,8 +110,8 @@ int main()
109110
contract_assert const(ri++); // { dg-error "increment of read-only location" }
110111
ri = 6;
111112

112-
contract_assert const(gi++); // ok, not automatic storage
113-
contract_assert const(gri++); // ok, not automatic storage
113+
contract_assert const(gi++); // { dg-error "increment of read-only location" }
114+
contract_assert const(gri++); // { dg-error "increment of read-only location" }
114115

115116
int& rgi = gi;
116117
contract_assert const(rgi++); // { dg-error "increment of read-only location" }

gcc/testsuite/g++.dg/contracts/cpp26/constification_mutable.C

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ void f1(int i) pre(i++); // { dg-error "increment of read-only location" }
2323
void f2(int &i) pre(i++); // { dg-error "increment of read-only location" }
2424
void f3(int *i) pre(i++); // { dg-error "increment of read-only location" }
2525
void f4(int *i) pre((*i)++); // ok, not deep const
26-
void f5(int *i) pre(gi++); // ok, non automatic storage
26+
void f5(int *i) pre(gi++); // { dg-error "increment of read-only location" }
2727
void f6(int i) pre mutable(i++);
2828
void f7(int &i) pre mutable(i++);
2929
void f8(int *i) pre mutable(i++);
30+
void f9(int *i) pre mutable(gi++);
3031

3132
// todo structured binding test
3233
// lambda tests
@@ -114,8 +115,8 @@ int main()
114115
contract_assert mutable(ri++);
115116
ri = 6;
116117

117-
contract_assert(gi++); // ok, not automatic storage
118-
contract_assert(gri++); // ok, not automatic storage
118+
contract_assert(gi++); // { dg-error "increment of read-only location" }
119+
contract_assert(gri++); // { dg-error "increment of read-only location" }
119120

120121
int& rgi = gi;
121122
contract_assert(rgi++); // { dg-error "increment of read-only location" }

0 commit comments

Comments
 (0)