Skip to content

Commit c594d07

Browse files
committed
SMT2 backend: move quoting into convert_identifier
The quotes |...| are now added in one place by convert_identifier, instead of asking all consumers of identifiers to do this.
1 parent 0deef16 commit c594d07

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

src/solvers/smt2/smt2_conv.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void smt2_convt::write_footer()
218218
if(solver!=solvert::BOOLECTOR)
219219
{
220220
for(const auto &id : smt2_identifiers)
221-
out << "(get-value (|" << id << "|))"
221+
out << "(get-value (" << id << "))"
222222
<< "\n";
223223
}
224224

@@ -260,7 +260,7 @@ void smt2_convt::define_object_size(
260260
<< "((_ extract " << h << " " << l << ") ";
261261
convert_expr(ptr);
262262
out << ") (_ bv" << number << " " << config.bv_encoding.object_bits << "))"
263-
<< "(= |" << id << "| (_ bv" << *object_size << " " << size_width
263+
<< "(= " << id << " (_ bv" << *object_size << " " << size_width
264264
<< "))))\n";
265265

266266
++number;
@@ -837,16 +837,17 @@ literalt smt2_convt::convert(const exprt &expr)
837837
out << " () Bool)\n";
838838
out << "(assert (= ";
839839
convert_literal(l);
840+
out << ' ';
840841
convert_expr(prepared_expr);
841842
out << "))\n";
842843
}
843844
else
844845
{
845-
defined_expressions[expr] =
846-
std::string{"|B"} + std::to_string(l.var_no()) + "|";
847-
out << "(define-fun ";
848-
convert_literal(l);
849-
out << " () Bool ";
846+
auto identifier =
847+
convert_identifier(std::string{"B"} + std::to_string(l.var_no()));
848+
defined_expressions[expr] = identifier;
849+
smt2_identifiers.insert(identifier);
850+
out << "(define-fun " << identifier << " () Bool ";
850851
convert_expr(prepared_expr);
851852
out << ")\n";
852853
}
@@ -874,12 +875,15 @@ void smt2_convt::convert_literal(const literalt l)
874875
if(l.sign())
875876
out << "(not ";
876877

877-
out << "|B" << l.var_no() << "|";
878+
const auto identifier =
879+
convert_identifier("B" + std::to_string(l.var_no()));
880+
881+
out << identifier;
878882

879883
if(l.sign())
880884
out << ")";
881885

882-
smt2_identifiers.insert("B"+std::to_string(l.var_no()));
886+
smt2_identifiers.insert(identifier);
883887
}
884888
}
885889

@@ -906,7 +910,7 @@ std::string smt2_convt::convert_identifier(const irep_idt &identifier)
906910
// Otherwise, for Common Lisp compatibility they would have to be treated
907911
// as escaping symbols.
908912

909-
std::string result;
913+
std::string result = "|";
910914

911915
for(std::size_t i=0; i<identifier.size(); i++)
912916
{
@@ -928,6 +932,8 @@ std::string smt2_convt::convert_identifier(const irep_idt &identifier)
928932
}
929933
}
930934

935+
result += '|';
936+
931937
return result;
932938
}
933939

@@ -989,7 +995,7 @@ void smt2_convt::convert_floatbv(const exprt &expr)
989995
if(expr.id()==ID_symbol)
990996
{
991997
const irep_idt &id = to_symbol_expr(expr).get_identifier();
992-
out << '|' << convert_identifier(id) << '|';
998+
out << convert_identifier(id);
993999
return;
9941000
}
9951001

@@ -1003,9 +1009,9 @@ void smt2_convt::convert_floatbv(const exprt &expr)
10031009
INVARIANT(
10041010
!expr.operands().empty(), "non-symbol expressions shall have operands");
10051011

1006-
out << "(|float_bv." << expr.id()
1007-
<< floatbv_suffix(expr)
1008-
<< '|';
1012+
out << '('
1013+
<< convert_identifier(
1014+
"float_bv." + expr.id_string() + floatbv_suffix(expr));
10091015

10101016
forall_operands(it, expr)
10111017
{
@@ -1023,13 +1029,13 @@ void smt2_convt::convert_expr(const exprt &expr)
10231029
{
10241030
const irep_idt &id = to_symbol_expr(expr).get_identifier();
10251031
DATA_INVARIANT(!id.empty(), "symbol must have identifier");
1026-
out << '|' << convert_identifier(id) << '|';
1032+
out << convert_identifier(id);
10271033
}
10281034
else if(expr.id()==ID_nondet_symbol)
10291035
{
10301036
const irep_idt &id = to_nondet_symbol_expr(expr).get_identifier();
10311037
DATA_INVARIANT(!id.empty(), "nondet symbol must have identifier");
1032-
out << '|' << convert_identifier("nondet_"+id2string(id)) << '|';
1038+
out << convert_identifier("nondet_" + id2string(id));
10331039
}
10341040
else if(expr.id()==ID_smt2_symbol)
10351041
{
@@ -2149,7 +2155,7 @@ void smt2_convt::convert_expr(const exprt &expr)
21492155
else if(
21502156
const auto object_size = expr_try_dynamic_cast<object_size_exprt>(expr))
21512157
{
2152-
out << "|" << object_sizes[*object_size] << "|";
2158+
out << object_sizes[*object_size];
21532159
}
21542160
else if(expr.id()==ID_let)
21552161
{
@@ -4619,7 +4625,7 @@ void smt2_convt::set_to(const exprt &expr, bool value)
46194625
smt2_identifiers.insert(smt2_identifier);
46204626

46214627
out << "; set_to true (equal)\n";
4622-
out << "(define-fun |" << smt2_identifier << '|';
4628+
out << "(define-fun " << smt2_identifier;
46234629

46244630
if(equal_expr.lhs().type().id() == ID_mathematical_function)
46254631
{
@@ -4803,7 +4809,7 @@ void smt2_convt::find_symbols(const exprt &expr)
48034809
smt2_identifiers.insert(smt2_identifier);
48044810

48054811
out << "; find_symbols\n";
4806-
out << "(declare-fun |" << smt2_identifier << '|';
4812+
out << "(declare-fun " << smt2_identifier;
48074813

48084814
if(expr.type().id() == ID_mathematical_function)
48094815
{
@@ -4982,8 +4988,9 @@ void smt2_convt::find_symbols(const exprt &expr)
49824988
{
49834989
if(object_sizes.find(*object_size) == object_sizes.end())
49844990
{
4985-
const irep_idt id = "object_size." + std::to_string(object_sizes.size());
4986-
out << "(declare-fun |" << id << "| () ";
4991+
const irep_idt id = convert_identifier(
4992+
"object_size." + std::to_string(object_sizes.size()));
4993+
out << "(declare-fun " << id << " () ";
49874994
convert_type(object_size->type());
49884995
out << ")"
49894996
<< "\n";
@@ -5016,8 +5023,8 @@ void smt2_convt::find_symbols(const exprt &expr)
50165023
to_multi_ary_expr(expr).op0().type().id() == ID_floatbv)))
50175024
// clang-format on
50185025
{
5019-
irep_idt function=
5020-
"|float_bv."+expr.id_string()+floatbv_suffix(expr)+"|";
5026+
irep_idt function =
5027+
convert_identifier("float_bv." + expr.id_string() + floatbv_suffix(expr));
50215028

50225029
if(bvfp_set.insert(function).second)
50235030
{

src/solvers/smt2/smt2_dec.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ decision_proceduret::resultt smt2_dect::dec_solve()
130130
return read_result(in);
131131
}
132132

133+
static std::string drop_quotes(std::string src)
134+
{
135+
if(src.size() >= 2 && src.front() == '|' && src.back() == '|')
136+
return std::string(src, 1, src.size() - 2);
137+
else
138+
return src;
139+
}
140+
133141
decision_proceduret::resultt smt2_dect::read_result(std::istream &in)
134142
{
135143
std::string line;
@@ -199,22 +207,25 @@ decision_proceduret::resultt smt2_dect::read_result(std::istream &in)
199207

200208
for(auto &assignment : identifier_map)
201209
{
202-
std::string conv_id = convert_identifier(assignment.first);
210+
std::string conv_id = drop_quotes(convert_identifier(assignment.first));
203211
const irept &value = parsed_values[conv_id];
204212
assignment.second.value = parse_rec(value, assignment.second.type);
205213
}
206214

207215
// Booleans
208216
for(unsigned v=0; v<no_boolean_variables; v++)
209217
{
210-
const std::string boolean_identifier = "B" + std::to_string(v);
218+
const std::string boolean_identifier =
219+
convert_identifier("B" + std::to_string(v));
211220
boolean_assignment[v] = [&]() {
212-
const auto found_parsed_value = parsed_values.find(boolean_identifier);
221+
const auto found_parsed_value =
222+
parsed_values.find(drop_quotes(boolean_identifier));
213223
if(found_parsed_value != parsed_values.end())
224+
{
214225
return found_parsed_value->second.id() == ID_true;
226+
}
215227
// Work out the value based on what set_to was called with.
216-
const auto found_set_value =
217-
set_values.find('|' + boolean_identifier + '|');
228+
const auto found_set_value = set_values.find(boolean_identifier);
218229
if(found_set_value != set_values.end())
219230
return found_set_value->second;
220231
// Old code used the computation

src/solvers/smt2_incremental/smt_to_smt2_string.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
static std::string escape_identifier(const irep_idt &identifier)
2121
{
22-
return std::string{"|"} + smt2_convt::convert_identifier(identifier) + "|";
22+
return smt2_convt::convert_identifier(identifier);
2323
}
2424

2525
class smt_index_output_visitort : public smt_index_const_downcast_visitort

unit/solvers/smt2/smt2_conv.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ TEST_CASE(
88
"smt2_convt::convert_identifier character escaping.",
99
"[core][solvers][smt2]")
1010
{
11-
const auto no_escaping_characters = "abcdefghijklmnopqrstuvwxyz0123456789$";
11+
const std::string no_escaping_characters =
12+
"abcdefghijklmnopqrstuvwxyz0123456789$";
1213
CHECK(
1314
smt2_convt::convert_identifier(no_escaping_characters) ==
14-
no_escaping_characters);
15-
CHECK(smt2_convt::convert_identifier("\\") == "&92;");
16-
CHECK(smt2_convt::convert_identifier("|") == "&124;");
17-
CHECK(smt2_convt::convert_identifier("&") == "&38;");
15+
"|" + no_escaping_characters + "|");
16+
CHECK(smt2_convt::convert_identifier("\\") == "|&92;|");
17+
CHECK(smt2_convt::convert_identifier("|") == "|&124;|");
18+
CHECK(smt2_convt::convert_identifier("&") == "|&38;|");
1819
}

0 commit comments

Comments
 (0)