Skip to content

Commit e1fc49f

Browse files
authored
Merge pull request #2720 from tautschnig/replace_symbol-cleanup1
replace_symbolt: hide {expr,type}_map
2 parents 3f487c3 + e327ef2 commit e1fc49f

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

src/analyses/constant_propagator.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ bool constant_propagator_domaint::valuest::is_constant(const exprt &expr) const
298298
return false;
299299

300300
if(expr.id()==ID_symbol)
301-
if(replace_const.expr_map.find(to_symbol_expr(expr).get_identifier())==
302-
replace_const.expr_map.end())
301+
if(!replace_const.replaces_symbol(to_symbol_expr(expr).get_identifier()))
303302
return false;
304303

305304
if(expr.id()==ID_index)
@@ -337,8 +336,7 @@ bool constant_propagator_domaint::valuest::is_constant_address_of(
337336
/// Do not call this when iterating over replace_const.expr_map!
338337
bool constant_propagator_domaint::valuest::set_to_top(const irep_idt &id)
339338
{
340-
replace_symbolt::expr_mapt::size_type n_erased=
341-
replace_const.expr_map.erase(id);
339+
const auto n_erased = replace_const.erase(id);
342340

343341
INVARIANT(n_erased==0 || !is_bottom, "bottom should have no elements at all");
344342

@@ -351,7 +349,7 @@ void constant_propagator_domaint::valuest::set_dirty_to_top(
351349
const namespacet &ns)
352350
{
353351
typedef replace_symbolt::expr_mapt expr_mapt;
354-
expr_mapt &expr_map=replace_const.expr_map;
352+
expr_mapt &expr_map = replace_const.get_expr_map();
355353

356354
for(expr_mapt::iterator it=expr_map.begin();
357355
it!=expr_map.end();)
@@ -377,19 +375,19 @@ void constant_propagator_domaint::valuest::output(
377375
if(is_bottom)
378376
{
379377
out << " bottom\n";
380-
DATA_INVARIANT(replace_const.expr_map.empty(),
378+
DATA_INVARIANT(is_empty(),
381379
"If the domain is bottom, the map must be empty");
382380
return;
383381
}
384382

385383
INVARIANT(!is_bottom, "Have handled bottom");
386-
if(replace_const.expr_map.empty())
384+
if(is_empty())
387385
{
388386
out << "top\n";
389387
return;
390388
}
391389

392-
for(const auto &p : replace_const.expr_map)
390+
for(const auto &p : replace_const.get_expr_map())
393391
{
394392
out << ' ' << p.first << "=" << from_expr(ns, p.first, p.second) << '\n';
395393
}
@@ -424,20 +422,21 @@ bool constant_propagator_domaint::valuest::merge(const valuest &src)
424422

425423
bool changed=false;
426424

427-
replace_symbolt::expr_mapt &expr_map=replace_const.expr_map;
428-
const replace_symbolt::expr_mapt &src_expr_map=src.replace_const.expr_map;
429-
430425
// handle top
431-
if(src_expr_map.empty())
426+
if(src.is_empty())
432427
{
433428
// change if it was not top
434-
changed=!expr_map.empty();
429+
changed = !is_empty();
435430

436431
set_to_top();
437432

438433
return changed;
439434
}
440435

436+
replace_symbolt::expr_mapt &expr_map = replace_const.get_expr_map();
437+
const replace_symbolt::expr_mapt &src_expr_map =
438+
src.replace_const.get_expr_map();
439+
441440
// remove those that are
442441
// - different in src
443442
// - do not exist in src
@@ -484,12 +483,12 @@ bool constant_propagator_domaint::valuest::meet(
484483

485484
bool changed=false;
486485

487-
for(const auto &m : src.replace_const.expr_map)
486+
for(const auto &m : src.replace_const.get_expr_map())
488487
{
489-
replace_symbolt::expr_mapt::iterator
490-
c_it=replace_const.expr_map.find(m.first);
488+
replace_symbolt::expr_mapt::const_iterator c_it =
489+
replace_const.get_expr_map().find(m.first);
491490

492-
if(c_it!=replace_const.expr_map.end())
491+
if(c_it != replace_const.get_expr_map().end())
493492
{
494493
if(c_it->second!=m.second)
495494
{

src/analyses/constant_propagator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class constant_propagator_domaint:public ai_domain_baset
105105

106106
void set_to(const irep_idt &lhs, const exprt &rhs)
107107
{
108-
replace_const.expr_map[lhs]=rhs;
108+
replace_const.get_expr_map()[lhs] = rhs;
109109
is_bottom=false;
110110
}
111111

@@ -129,7 +129,7 @@ class constant_propagator_domaint:public ai_domain_baset
129129

130130
bool is_empty() const
131131
{
132-
return replace_const.expr_map.empty();
132+
return replace_const.empty();
133133
}
134134

135135
void output(std::ostream &out, const namespacet &ns) const;

src/goto-programs/link_goto_model.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static bool link_functions(
140140
rename_symbols_in_function(dest_it->second, final_id, macro_application);
141141
}
142142

143-
if(!object_type_updates.expr_map.empty())
143+
if(!object_type_updates.empty())
144144
{
145145
Forall_goto_functions(dest_it, dest_functions)
146146
Forall_goto_program_instructions(iit, dest_it->second.body)

src/util/replace_symbol.h

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Author: Daniel Kroening, [email protected]
1919

2020
#include <unordered_map>
2121

22+
/// Replace expression or type symbols by an expression or type, respectively.
2223
class replace_symbolt
2324
{
2425
public:
@@ -79,9 +80,31 @@ class replace_symbolt
7980
return expr_map.empty() && type_map.empty();
8081
}
8182

83+
std::size_t erase(const irep_idt &id)
84+
{
85+
return expr_map.erase(id) + type_map.erase(id);
86+
}
87+
88+
bool replaces_symbol(const irep_idt &id) const
89+
{
90+
return expr_map.find(id) != expr_map.end() ||
91+
type_map.find(id) != type_map.end();
92+
}
93+
8294
replace_symbolt();
8395
virtual ~replace_symbolt();
8496

97+
const expr_mapt &get_expr_map() const
98+
{
99+
return expr_map;
100+
}
101+
102+
expr_mapt &get_expr_map()
103+
{
104+
return expr_map;
105+
}
106+
107+
protected:
85108
expr_mapt expr_map;
86109
type_mapt type_map;
87110

unit/util/replace_symbol.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ TEST_CASE("Replace all symbols in expression", "[core][util][replace_symbol]")
2626
exprt other_expr("other");
2727

2828
replace_symbolt r;
29+
REQUIRE(r.empty());
30+
2931
r.insert("a", other_expr);
32+
REQUIRE(r.replaces_symbol("a"));
33+
REQUIRE(r.get_expr_map().size() == 1);
3034

3135
REQUIRE(r.replace(binary) == false);
3236
REQUIRE(binary.op0() == other_expr);
@@ -37,8 +41,12 @@ TEST_CASE("Replace all symbols in expression", "[core][util][replace_symbol]")
3741
REQUIRE(r.replace(s2) == true);
3842
REQUIRE(s2 == symbol_exprt("b", typet("some_type")));
3943

44+
4045
REQUIRE(r.replace(array_type) == false);
4146
REQUIRE(array_type.size() == other_expr);
47+
48+
REQUIRE(r.erase("a") == 1);
49+
REQUIRE(r.empty());
4250
}
4351

4452
TEST_CASE("Lvalue only", "[core][util][replace_symbol]")

0 commit comments

Comments
 (0)