1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// Copyright (C) 2021-2025 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-readonly-check.h"
#include "rust-tree.h"
#include "rust-gcc.h"
#include "print-tree.h"
namespace Rust {
namespace Analysis {
static std::map<tree, int> assignment_map = {};
// ported over from c-family/c-warn.cc
void
readonly_error (location_t loc, tree arg, enum lvalue_use use)
{
gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
|| use == lv_asm);
STRIP_ANY_LOCATION_WRAPPER (arg);
/* Using this macro rather than (for example) arrays of messages
ensures that all the format strings are checked at compile
time. */
#define READONLY_MSG(A, I, D, AS) \
(use == lv_assign \
? (A) \
: (use == lv_increment ? (I) : (use == lv_decrement ? (D) : (AS))))
if (TREE_CODE (arg) == COMPONENT_REF)
{
if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
error_at (loc,
READONLY_MSG (G_ ("assignment of member "
"%qD in read-only object"),
G_ ("increment of member "
"%qD in read-only object"),
G_ ("decrement of member "
"%qD in read-only object"),
G_ ("member %qD in read-only object "
"used as %<asm%> output")),
TREE_OPERAND (arg, 1));
else
error_at (
loc,
READONLY_MSG (G_ ("assignment of read-only member %qD"),
G_ ("increment of read-only member %qD"),
G_ ("decrement of read-only member %qD"),
G_ ("read-only member %qD used as %<asm%> output")),
TREE_OPERAND (arg, 1));
}
else if (VAR_P (arg))
error_at (loc,
READONLY_MSG (G_ ("assignment of read-only variable %qD"),
G_ ("increment of read-only variable %qD"),
G_ ("decrement of read-only variable %qD"),
G_ (
"read-only variable %qD used as %<asm%> output")),
arg);
else if (TREE_CODE (arg) == PARM_DECL)
error_at (loc,
READONLY_MSG (G_ ("assignment of read-only parameter %qD"),
G_ ("increment of read-only parameter %qD"),
G_ ("decrement of read-only parameter %qD"),
G_ (
"read-only parameter %qD use as %<asm%> output")),
arg);
else if (TREE_CODE (arg) == RESULT_DECL)
{
error_at (loc,
READONLY_MSG (G_ ("assignment of "
"read-only named return value %qD"),
G_ ("increment of "
"read-only named return value %qD"),
G_ ("decrement of "
"read-only named return value %qD"),
G_ ("read-only named return value %qD "
"used as %<asm%>output")),
arg);
}
else if (TREE_CODE (arg) == FUNCTION_DECL)
error_at (loc,
READONLY_MSG (G_ ("assignment of function %qD"),
G_ ("increment of function %qD"),
G_ ("decrement of function %qD"),
G_ ("function %qD used as %<asm%> output")),
arg);
else
error_at (loc,
READONLY_MSG (G_ ("assignment of read-only location %qE"),
G_ ("increment of read-only location %qE"),
G_ ("decrement of read-only location %qE"),
G_ (
"read-only location %qE used as %<asm%> output")),
arg);
}
static void
emit_error (tree *t, tree lhs, enum lvalue_use use)
{
readonly_error (EXPR_LOCATION (*t), lhs, use);
TREE_OPERAND (*t, 0) = error_mark_node;
}
static void
check_modify_expr (tree *t)
{
tree lhs = TREE_OPERAND (*t, 0);
if (TREE_CODE (lhs) == ARRAY_REF || TREE_CODE (lhs) == COMPONENT_REF)
lhs = TREE_OPERAND (lhs, 0);
tree lhs_type = TREE_TYPE (lhs);
if (TYPE_READONLY (lhs_type) || TREE_READONLY (lhs) || TREE_CONSTANT (lhs))
{
if (TREE_CODE (lhs) != VAR_DECL)
emit_error (t, lhs, lv_assign);
else if (!DECL_ARTIFICIAL (lhs))
{
if (DECL_INITIAL (lhs) != NULL)
emit_error (t, lhs, lv_assign);
else
{
if (assignment_map.find (lhs) == assignment_map.end ())
{
assignment_map.insert ({lhs, 0});
}
assignment_map[lhs]++;
if (assignment_map[lhs] > 1)
emit_error (t, lhs, lv_assign);
}
}
}
}
static void
check_decl (tree *t)
{
switch (TREE_CODE (*t))
{
case MODIFY_EXPR:
check_modify_expr (t);
break;
default:
break;
}
}
static tree
readonly_walk_fn (tree *t, int *, void *)
{
check_decl (t);
return NULL_TREE;
}
void
ReadonlyCheck::Lint (Compile::Context &ctx)
{
assignment_map.clear ();
for (auto &fndecl : ctx.get_func_decls ())
{
for (tree p = DECL_ARGUMENTS (fndecl); p != NULL_TREE; p = DECL_CHAIN (p))
{
check_decl (&p);
}
walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
&readonly_walk_fn, &ctx);
}
assignment_map.clear ();
for (auto &var : ctx.get_var_decls ())
{
tree decl = var->get_decl ();
check_decl (&decl);
}
assignment_map.clear ();
for (auto &const_decl : ctx.get_const_decls ())
{
check_decl (&const_decl);
}
}
} // namespace Analysis
} // namespace Rust
|