Skip to content

Commit aedbd75

Browse files
Merge pull request gcc-mirror#78 from NinaRanns/constify_non_autmatic
constify globals and non type template parameters of reference type
2 parents 8f27098 + 689397c commit aedbd75

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
@@ -2751,12 +2751,13 @@ constify_contract_access(tree decl)
27512751
* parameter of reference type
27522752
*/
27532753
if (!TREE_READONLY (decl)
2754-
&& ((VAR_P (decl) && decl_storage_duration (decl) == dk_auto)
2754+
&& (VAR_P (decl)
27552755
|| (TREE_CODE (decl) == PARM_DECL)
27562756
|| (REFERENCE_REF_P (decl)
2757-
&& ((VAR_P (TREE_OPERAND (decl, 0))
2758-
&& decl_storage_duration(TREE_OPERAND (decl, 0)) == dk_auto)
2759-
|| (TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL)))))
2757+
&& (VAR_P (TREE_OPERAND (decl, 0))
2758+
|| (TREE_CODE (TREE_OPERAND (decl, 0)) == PARM_DECL)
2759+
|| (TREE_CODE (TREE_OPERAND (decl, 0))
2760+
== TEMPLATE_PARM_INDEX)))))
27602761
{
27612762
decl = view_as_const (decl);
27622763
}

gcc/cp/semantics.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,6 +4802,7 @@ finish_id_expression_1 (tree id_expression,
48024802
| TYPE_QUAL_CONST));
48034803
r = build1 (VIEW_CONVERT_EXPR, ctype, r);
48044804
}
4805+
48054806
r = convert_from_reference (r);
48064807
if (integral_constant_expression_p
48074808
&& !dependent_type_p (TREE_TYPE (decl))
@@ -4813,6 +4814,11 @@ finish_id_expression_1 (tree id_expression,
48134814
"integral or enumeration type", decl, TREE_TYPE (decl));
48144815
*non_integral_constant_expression_p = true;
48154816
}
4817+
4818+
if (flag_contracts_nonattr && should_constify_contract
4819+
&& processing_contract_condition)
4820+
r = constify_contract_access(r);
4821+
48164822
return r;
48174823
}
48184824
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)