Skip to content

Commit 51fda76

Browse files
author
Daniel Kroening
authored
Merge pull request #3577 from diffblue/smt2_error_ostream
smt2: error() can now act as ostream
2 parents af77440 + f5cb8b8 commit 51fda76

File tree

4 files changed

+69
-136
lines changed

4 files changed

+69
-136
lines changed

src/solvers/smt2/smt2_parser.cpp

Lines changed: 40 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,7 @@ exprt smt2_parsert::let_expression()
218218
exprt smt2_parsert::quantifier_expression(irep_idt id)
219219
{
220220
if(next_token()!=OPEN)
221-
{
222-
std::ostringstream msg;
223-
msg << "expected bindings after " << id;
224-
throw error(msg.str());
225-
}
221+
throw error() << "expected bindings after " << id;
226222

227223
std::vector<symbol_exprt> bindings;
228224

@@ -257,11 +253,7 @@ exprt smt2_parsert::quantifier_expression(irep_idt id)
257253
exprt expr=expression();
258254

259255
if(next_token()!=CLOSE)
260-
{
261-
std::ostringstream msg;
262-
msg << "expected ')' after " << id;
263-
throw error(msg.str());
264-
}
256+
throw error() << "expected ')' after " << id;
265257

266258
exprt result=expr;
267259

@@ -348,12 +340,10 @@ exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
348340
{
349341
if(op[i].type() != op[0].type())
350342
{
351-
std::ostringstream msg;
352-
msg << "expression must have operands with matching types,"
353-
" but got `"
354-
<< smt2_format(op[0].type()) << "' and `" << smt2_format(op[i].type())
355-
<< '\'';
356-
throw error(msg.str());
343+
throw error() << "expression must have operands with matching types,"
344+
" but got `"
345+
<< smt2_format(op[0].type()) << "' and `"
346+
<< smt2_format(op[i].type()) << '\'';
357347
}
358348
}
359349

@@ -369,12 +359,10 @@ exprt smt2_parsert::binary_predicate(irep_idt id, const exprt::operandst &op)
369359

370360
if(op[0].type() != op[1].type())
371361
{
372-
std::ostringstream msg;
373-
msg << "expression must have operands with matching types,"
374-
" but got `"
375-
<< smt2_format(op[0].type()) << "' and `" << smt2_format(op[1].type())
376-
<< '\'';
377-
throw error(msg.str());
362+
throw error() << "expression must have operands with matching types,"
363+
" but got `"
364+
<< smt2_format(op[0].type()) << "' and `"
365+
<< smt2_format(op[1].type()) << '\'';
378366
}
379367

380368
return binary_predicate_exprt(op[0], id, op[1]);
@@ -404,26 +392,16 @@ exprt smt2_parsert::function_application_ieee_float_op(
404392
const exprt::operandst &op)
405393
{
406394
if(op.size() != 3)
407-
{
408-
std::ostringstream message;
409-
message << id << " takes three operands";
410-
throw error(message);
411-
}
395+
throw error() << id << " takes three operands";
412396

413397
if(op[1].type().id() != ID_floatbv || op[2].type().id() != ID_floatbv)
414-
{
415-
std::ostringstream message;
416-
message << id << " takes FloatingPoint operands";
417-
throw error(message);
418-
}
398+
throw error() << id << " takes FloatingPoint operands";
419399

420400
if(op[1].type() != op[2].type())
421401
{
422-
std::ostringstream message;
423-
message << id << " takes FloatingPoint operands with matching sort, "
424-
<< "but got " << smt2_format(op[1].type()) << " vs "
425-
<< smt2_format(op[2].type());
426-
throw error(message);
402+
throw error() << id << " takes FloatingPoint operands with matching sort, "
403+
<< "but got " << smt2_format(op[1].type()) << " vs "
404+
<< smt2_format(op[2].type());
427405
}
428406

429407
// clang-format off
@@ -494,9 +472,7 @@ exprt smt2_parsert::function_application()
494472
}
495473
else
496474
{
497-
std::ostringstream msg;
498-
msg << "unknown indexed identifier " << buffer;
499-
throw error(msg.str());
475+
throw error() << "unknown indexed identifier " << buffer;
500476
}
501477
}
502478
else if(buffer == "!")
@@ -791,9 +767,7 @@ exprt smt2_parsert::function_application()
791767
return symbol_exprt(final_id, id_it->second.type);
792768
}
793769

794-
std::ostringstream msg;
795-
msg << "unknown function symbol " << id;
796-
throw error(msg.str());
770+
throw error() << "unknown function symbol `" << id << '\'';
797771
}
798772
}
799773
break;
@@ -847,29 +821,17 @@ exprt smt2_parsert::function_application()
847821
id=="zero_extend")
848822
{
849823
if(next_token()!=NUMERAL)
850-
{
851-
std::ostringstream msg;
852-
msg << "expected numeral after " << id;
853-
throw error(msg.str());
854-
}
824+
throw error() << "expected numeral after " << id;
855825

856826
auto index=std::stoll(buffer);
857827

858828
if(next_token()!=CLOSE)
859-
{
860-
std::ostringstream msg;
861-
msg << "expected ')' after " << id << " index";
862-
throw error(msg.str());
863-
}
829+
throw error() << "expected ')' after " << id << " index";
864830

865831
auto op=operands();
866832

867833
if(op.size()!=1)
868-
{
869-
std::ostringstream msg;
870-
msg << id << " takes one operand";
871-
throw error(msg.str());
872-
}
834+
throw error() << id << " takes one operand";
873835

874836
if(id=="rotate_left")
875837
{
@@ -911,9 +873,7 @@ exprt smt2_parsert::function_application()
911873
}
912874
else
913875
{
914-
std::ostringstream msg;
915-
msg << "unknown indexed identifier " << buffer;
916-
throw error(msg.str());
876+
throw error() << "unknown indexed identifier `" << buffer << '\'';
917877
}
918878
}
919879
else
@@ -1001,9 +961,7 @@ exprt smt2_parsert::expression()
1001961
return std::move(symbol_expr);
1002962
}
1003963

1004-
std::ostringstream msg;
1005-
msg << "unknown expression " << identifier;
1006-
throw error(msg.str());
964+
throw error() << "unknown expression `" << identifier << '\'';
1007965
}
1008966
}
1009967

@@ -1054,11 +1012,7 @@ typet smt2_parsert::sort()
10541012
else if(buffer=="Real")
10551013
return real_typet();
10561014
else
1057-
{
1058-
std::ostringstream msg;
1059-
msg << "unexpected sort: `" << buffer << '\'';
1060-
throw error(msg.str());
1061-
}
1015+
throw error() << "unexpected sort: `" << buffer << '\'';
10621016

10631017
case OPEN:
10641018
if(next_token()!=SYMBOL)
@@ -1102,11 +1056,7 @@ typet smt2_parsert::sort()
11021056
return ieee_float_spect(width_f - 1, width_e).to_type();
11031057
}
11041058
else
1105-
{
1106-
std::ostringstream msg;
1107-
msg << "unexpected sort: `" << buffer << '\'';
1108-
throw error(msg.str());
1109-
}
1059+
throw error() << "unexpected sort: `" << buffer << '\'';
11101060
}
11111061
else if(buffer == "Array")
11121062
{
@@ -1126,16 +1076,10 @@ typet smt2_parsert::sort()
11261076
throw error("unsupported array sort");
11271077
}
11281078
else
1129-
{
1130-
std::ostringstream msg;
1131-
msg << "unexpected sort: `" << buffer << '\'';
1132-
throw error(msg.str());
1133-
}
1079+
throw error() << "unexpected sort: `" << buffer << '\'';
11341080

11351081
default:
1136-
std::ostringstream msg;
1137-
msg << "unexpected token in a sort: `" << buffer << '\'';
1138-
throw error(msg.str());
1082+
throw error() << "unexpected token in a sort: `" << buffer << '\'';
11391083
}
11401084
}
11411085

@@ -1224,21 +1168,13 @@ void smt2_parsert::command(const std::string &c)
12241168
// declare-var appears to be a synonym for declare-const that is
12251169
// accepted by Z3 and CVC4
12261170
if(next_token()!=SYMBOL)
1227-
{
1228-
std::ostringstream msg;
1229-
msg << "expected a symbol after `" << c << '\'';
1230-
throw error(msg.str());
1231-
}
1171+
throw error() << "expected a symbol after `" << c << '\'';
12321172

12331173
irep_idt id = buffer;
12341174
auto type = sort();
12351175

12361176
if(id_map.find(id)!=id_map.end())
1237-
{
1238-
std::ostringstream msg;
1239-
msg << "identifier `" << id << "' defined twice";
1240-
throw error(msg.str());
1241-
}
1177+
throw error() << "identifier `" << id << "' defined twice";
12421178

12431179
auto &entry = id_map[id];
12441180
entry.type = type;
@@ -1253,11 +1189,7 @@ void smt2_parsert::command(const std::string &c)
12531189
auto type = function_signature_declaration();
12541190

12551191
if(id_map.find(id)!=id_map.end())
1256-
{
1257-
std::ostringstream msg;
1258-
msg << "identifier `" << id << "' defined twice";
1259-
throw error(msg.str());
1260-
}
1192+
throw error() << "identifier `" << id << "' defined twice";
12611193

12621194
auto &entry = id_map[id];
12631195
entry.type = type;
@@ -1271,23 +1203,17 @@ void smt2_parsert::command(const std::string &c)
12711203
const irep_idt id = buffer;
12721204

12731205
if(id_map.find(id) != id_map.end())
1274-
{
1275-
std::ostringstream msg;
1276-
msg << "identifier `" << id << "' defined twice";
1277-
throw error(msg.str());
1278-
}
1206+
throw error() << "identifier `" << id << "' defined twice";
12791207

12801208
const auto type = sort();
12811209
const auto value = expression();
12821210

12831211
// check type of value
12841212
if(value.type() != type)
12851213
{
1286-
std::ostringstream msg;
1287-
msg << "type mismatch in constant definition: expected `"
1288-
<< smt2_format(type) << "' but got `" << smt2_format(value.type())
1289-
<< '\'';
1290-
throw error(msg.str());
1214+
throw error() << "type mismatch in constant definition: expected `"
1215+
<< smt2_format(type) << "' but got `"
1216+
<< smt2_format(value.type()) << '\'';
12911217
}
12921218

12931219
// create the entry
@@ -1303,11 +1229,7 @@ void smt2_parsert::command(const std::string &c)
13031229
const irep_idt id=buffer;
13041230

13051231
if(id_map.find(id)!=id_map.end())
1306-
{
1307-
std::ostringstream msg;
1308-
msg << "identifier `" << id << "' defined twice";
1309-
throw error(msg.str());
1310-
}
1232+
throw error() << "identifier `" << id << "' defined twice";
13111233

13121234
const auto signature = function_signature_definition();
13131235
const auto body = expression();
@@ -1318,20 +1240,16 @@ void smt2_parsert::command(const std::string &c)
13181240
const auto &f_signature = to_mathematical_function_type(signature.type);
13191241
if(body.type() != f_signature.codomain())
13201242
{
1321-
std::ostringstream msg;
1322-
msg << "type mismatch in function definition: expected `"
1323-
<< smt2_format(f_signature.codomain()) << "' but got `"
1324-
<< smt2_format(body.type()) << '\'';
1325-
throw error(msg.str());
1243+
throw error() << "type mismatch in function definition: expected `"
1244+
<< smt2_format(f_signature.codomain()) << "' but got `"
1245+
<< smt2_format(body.type()) << '\'';
13261246
}
13271247
}
13281248
else if(body.type() != signature.type)
13291249
{
1330-
std::ostringstream msg;
1331-
msg << "type mismatch in function definition: expected `"
1332-
<< smt2_format(signature.type) << "' but got `"
1333-
<< smt2_format(body.type()) << '\'';
1334-
throw error(msg.str());
1250+
throw error() << "type mismatch in function definition: expected `"
1251+
<< smt2_format(signature.type) << "' but got `"
1252+
<< smt2_format(body.type()) << '\'';
13351253
}
13361254

13371255
// create the entry

src/solvers/smt2/smt2_solver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ void smt2_solvert::command(const std::string &c)
192192
const auto id_map_it = id_map.find(identifier);
193193

194194
if(id_map_it == id_map.end())
195-
throw error("unexpected symbol " + id2string(identifier));
195+
throw error() << "unexpected symbol `" << identifier << '\'';
196196

197197
const exprt value = solver.get(op);
198198

199199
if(value.is_nil())
200-
throw error("no value for " + id2string(identifier));
200+
throw error() << "no value for `" << identifier << '\'';
201201

202202
values.push_back(value);
203203
}

src/solvers/smt2/smt2_tokenizer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,7 @@ void smt2_tokenizert::get_token_from_stream()
305305
else
306306
{
307307
// illegal character, error
308-
std::ostringstream msg;
309-
msg << "unexpected character `" << ch << '\'';
310-
throw error(msg.str());
308+
throw error() << "unexpected character `" << ch << '\'';
311309
}
312310
}
313311
}

0 commit comments

Comments
 (0)