@@ -40,83 +40,95 @@ bool has_symbol(
40
40
return has_symbol (src, symbols, true , true );
41
41
}
42
42
43
- void find_symbols (
44
- const exprt &src,
45
- std::set<symbol_exprt> &dest)
46
- {
47
- src.visit_pre ([&dest](const exprt &e) {
48
- if (e.id () == ID_symbol)
49
- dest.insert (to_symbol_expr (e));
50
- });
51
- }
52
-
53
- std::set<symbol_exprt> find_symbols (const exprt &src)
54
- {
55
- return make_range (src.depth_begin (), src.depth_end ())
56
- .filter ([](const exprt &e) { return e.id () == ID_symbol; })
57
- .map ([](const exprt &e) { return to_symbol_expr (e); });
58
- }
43
+ static bool find_symbols (
44
+ kindt,
45
+ const typet &,
46
+ std::function<bool (const symbol_exprt &)>);
59
47
60
- std::unordered_set<irep_idt> find_symbol_identifiers (const exprt &src)
61
- {
62
- std::unordered_set<irep_idt> result;
63
- src.visit_pre ([&](const exprt &e) {
64
- if (e.id () == ID_symbol)
65
- result.insert (to_symbol_expr (e).get_identifier ());
66
- });
67
- return result;
68
- }
69
-
70
- void find_symbols (kindt kind, const typet &src, find_symbols_sett &dest);
71
-
72
- void find_symbols (kindt kind, const exprt &src, find_symbols_sett &dest)
48
+ static bool find_symbols (
49
+ kindt kind,
50
+ const exprt &src,
51
+ std::function<bool (const symbol_exprt &)> op)
73
52
{
74
53
forall_operands (it, src)
75
- find_symbols (kind, *it, dest);
54
+ {
55
+ if (!find_symbols (kind, *it, op))
56
+ return false ;
57
+ }
76
58
77
- find_symbols (kind, src.type (), dest);
59
+ if (!find_symbols (kind, src.type (), op))
60
+ return false ;
78
61
79
62
if (
80
63
kind == kindt::F_ALL || kind == kindt::F_EXPR_CURRENT ||
81
64
kind == kindt::F_EXPR_BOTH)
82
65
{
83
- if (src.id () == ID_symbol)
84
- dest. insert ( to_symbol_expr (src). get_identifier ()) ;
66
+ if (src.id () == ID_symbol && ! op ( to_symbol_expr (src)) )
67
+ return false ;
85
68
}
86
69
87
70
if (
88
71
kind == kindt::F_ALL || kind == kindt::F_EXPR_NEXT ||
89
72
kind == kindt::F_EXPR_BOTH)
90
73
{
91
- if (src.id () == ID_next_symbol)
92
- dest.insert (src.get (ID_identifier));
74
+ if (
75
+ src.id () == ID_next_symbol &&
76
+ !op (symbol_exprt{src.get (ID_identifier), typet{}}))
77
+ {
78
+ return false ;
79
+ }
93
80
}
94
81
95
82
const irept &c_sizeof_type=src.find (ID_C_c_sizeof_type);
96
83
97
- if (c_sizeof_type.is_not_nil ())
98
- find_symbols (kind, static_cast <const typet &>(c_sizeof_type), dest);
84
+ if (
85
+ c_sizeof_type.is_not_nil () &&
86
+ !find_symbols (kind, static_cast <const typet &>(c_sizeof_type), op))
87
+ {
88
+ return false ;
89
+ }
99
90
100
91
const irept &va_arg_type=src.find (ID_C_va_arg_type);
101
92
102
- if (va_arg_type.is_not_nil ())
103
- find_symbols (kind, static_cast <const typet &>(va_arg_type), dest);
93
+ if (
94
+ va_arg_type.is_not_nil () &&
95
+ !find_symbols (kind, static_cast <const typet &>(va_arg_type), op))
96
+ {
97
+ return false ;
98
+ }
99
+
100
+ return true ;
104
101
}
105
102
106
- void find_symbols (kindt kind, const typet &src, find_symbols_sett &dest)
103
+ static bool find_symbols (
104
+ kindt kind,
105
+ const typet &src,
106
+ std::function<bool (const symbol_exprt &)> op)
107
107
{
108
108
if (kind!=kindt::F_TYPE_NON_PTR ||
109
109
src.id ()!=ID_pointer)
110
110
{
111
- if (src.has_subtype ())
112
- find_symbols (kind, to_type_with_subtype (src).subtype (), dest);
111
+ if (
112
+ src.has_subtype () &&
113
+ !find_symbols (kind, to_type_with_subtype (src).subtype (), op))
114
+ {
115
+ return false ;
116
+ }
113
117
114
118
for (const typet &subtype : to_type_with_subtypes (src).subtypes ())
115
- find_symbols (kind, subtype, dest);
119
+ {
120
+ if (!find_symbols (kind, subtype, op))
121
+ return false ;
122
+ }
116
123
117
- const irep_idt &typedef_name=src.get (ID_C_typedef);
118
- if (!typedef_name.empty ())
119
- dest.insert (typedef_name);
124
+ if (
125
+ kind == kindt::F_TYPE || kind == kindt::F_TYPE_NON_PTR ||
126
+ kind == kindt::F_ALL)
127
+ {
128
+ const irep_idt &typedef_name = src.get (ID_C_typedef);
129
+ if (!typedef_name.empty () && !op (symbol_exprt{typedef_name, typet{}}))
130
+ return false ;
131
+ }
120
132
}
121
133
122
134
if (src.id ()==ID_struct ||
@@ -125,81 +137,126 @@ void find_symbols(kindt kind, const typet &src, find_symbols_sett &dest)
125
137
const struct_union_typet &struct_union_type=to_struct_union_type (src);
126
138
127
139
for (const auto &c : struct_union_type.components ())
128
- find_symbols (kind, c, dest);
140
+ {
141
+ if (!find_symbols (kind, c, op))
142
+ return false ;
143
+ }
129
144
}
130
145
else if (src.id ()==ID_code)
131
146
{
132
147
const code_typet &code_type=to_code_type (src);
133
- find_symbols (kind, code_type.return_type (), dest);
148
+ if (!find_symbols (kind, code_type.return_type (), op))
149
+ return false ;
134
150
135
151
for (const auto &p : code_type.parameters ())
136
152
{
137
- find_symbols (kind, p, dest);
153
+ if (!find_symbols (kind, p, op))
154
+ return false ;
138
155
}
139
156
}
140
157
else if (src.id ()==ID_array)
141
158
{
142
159
// do the size -- the subtype is already done
143
- find_symbols (kind, to_array_type (src).size (), dest);
144
- }
145
- else if (src.id ()==ID_c_enum_tag)
146
- {
147
- dest.insert (to_c_enum_tag_type (src).get_identifier ());
160
+ if (!find_symbols (kind, to_array_type (src).size (), op))
161
+ return false ;
148
162
}
149
- else if (src.id ()==ID_struct_tag)
163
+ else if (
164
+ kind == kindt::F_TYPE || kind == kindt::F_TYPE_NON_PTR ||
165
+ kind == kindt::F_ALL)
150
166
{
151
- dest.insert (to_struct_tag_type (src).get_identifier ());
152
- }
153
- else if (src.id ()==ID_union_tag)
154
- {
155
- dest.insert (to_union_tag_type (src).get_identifier ());
167
+ if (src.id () == ID_c_enum_tag)
168
+ {
169
+ if (!op (symbol_exprt{to_c_enum_tag_type (src).get_identifier (), typet{}}))
170
+ return false ;
171
+ }
172
+ else if (src.id () == ID_struct_tag)
173
+ {
174
+ if (!op (symbol_exprt{to_struct_tag_type (src).get_identifier (), typet{}}))
175
+ return false ;
176
+ }
177
+ else if (src.id () == ID_union_tag)
178
+ {
179
+ if (!op (symbol_exprt{to_union_tag_type (src).get_identifier (), typet{}}))
180
+ return false ;
181
+ }
156
182
}
183
+
184
+ return true ;
185
+ }
186
+
187
+ void find_symbols (const exprt &src, std::set<symbol_exprt> &dest)
188
+ {
189
+ find_symbols (kindt::F_EXPR_CURRENT, src, [&dest](const symbol_exprt &e) {
190
+ dest.insert (e);
191
+ return true ;
192
+ });
157
193
}
158
194
159
195
bool has_symbol (const exprt &src, const irep_idt &identifier, kindt kind)
160
196
{
161
- find_symbols_sett tmp_dest;
162
- find_symbols (kind, src, tmp_dest) ;
163
- return tmp_dest. find (identifier) != tmp_dest. end ( );
197
+ return ! find_symbols (kind, src, [&identifier]( const symbol_exprt &e) {
198
+ return e. get_identifier () != identifier ;
199
+ } );
164
200
}
165
201
166
202
void find_type_symbols (const exprt &src, find_symbols_sett &dest)
167
203
{
168
- find_symbols (kindt::F_TYPE, src, dest);
204
+ find_symbols (kindt::F_TYPE, src, [&dest](const symbol_exprt &e) {
205
+ dest.insert (e.get_identifier ());
206
+ return true ;
207
+ });
169
208
}
170
209
171
210
void find_type_symbols (const typet &src, find_symbols_sett &dest)
172
211
{
173
- find_symbols (kindt::F_TYPE, src, dest);
212
+ find_symbols (kindt::F_TYPE, src, [&dest](const symbol_exprt &e) {
213
+ dest.insert (e.get_identifier ());
214
+ return true ;
215
+ });
174
216
}
175
217
176
218
void find_non_pointer_type_symbols (
177
219
const exprt &src,
178
220
find_symbols_sett &dest)
179
221
{
180
- find_symbols (kindt::F_TYPE_NON_PTR, src, dest);
222
+ find_symbols (kindt::F_TYPE_NON_PTR, src, [&dest](const symbol_exprt &e) {
223
+ dest.insert (e.get_identifier ());
224
+ return true ;
225
+ });
181
226
}
182
227
183
228
void find_non_pointer_type_symbols (
184
229
const typet &src,
185
230
find_symbols_sett &dest)
186
231
{
187
- find_symbols (kindt::F_TYPE_NON_PTR, src, dest);
232
+ find_symbols (kindt::F_TYPE_NON_PTR, src, [&dest](const symbol_exprt &e) {
233
+ dest.insert (e.get_identifier ());
234
+ return true ;
235
+ });
188
236
}
189
237
190
238
void find_type_and_expr_symbols (const exprt &src, find_symbols_sett &dest)
191
239
{
192
- find_symbols (kindt::F_ALL, src, dest);
240
+ find_symbols (kindt::F_ALL, src, [&dest](const symbol_exprt &e) {
241
+ dest.insert (e.get_identifier ());
242
+ return true ;
243
+ });
193
244
}
194
245
195
246
void find_type_and_expr_symbols (const typet &src, find_symbols_sett &dest)
196
247
{
197
- find_symbols (kindt::F_ALL, src, dest);
248
+ find_symbols (kindt::F_ALL, src, [&dest](const symbol_exprt &e) {
249
+ dest.insert (e.get_identifier ());
250
+ return true ;
251
+ });
198
252
}
199
253
200
254
void find_symbols_or_nexts (const exprt &src, find_symbols_sett &dest)
201
255
{
202
- find_symbols (kindt::F_EXPR_BOTH, src, dest);
256
+ find_symbols (kindt::F_EXPR_BOTH, src, [&dest](const symbol_exprt &e) {
257
+ dest.insert (e.get_identifier ());
258
+ return true ;
259
+ });
203
260
}
204
261
205
262
void find_symbols (
@@ -208,13 +265,17 @@ void find_symbols(
208
265
bool current,
209
266
bool next)
210
267
{
268
+ auto store = [&dest](const symbol_exprt &e) {
269
+ dest.insert (e.get_identifier ());
270
+ return true ;
271
+ };
211
272
if (current)
212
273
{
213
274
if (next)
214
- find_symbols (kindt::F_EXPR_BOTH, src, dest );
275
+ find_symbols (kindt::F_EXPR_BOTH, src, store );
215
276
else
216
- find_symbols (kindt::F_EXPR_CURRENT, src, dest );
277
+ find_symbols (kindt::F_EXPR_CURRENT, src, store );
217
278
}
218
279
else if (next)
219
- find_symbols (kindt::F_EXPR_NEXT, src, dest );
280
+ find_symbols (kindt::F_EXPR_NEXT, src, store );
220
281
}
0 commit comments