Skip to content

Commit efeff92

Browse files
author
Daniel Kroening
committed
smt2irep now returns optional
This is follow-up from a discussion in #4395 and clarifies the contract of this function.
1 parent 3155cbc commit efeff92

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

src/solvers/smt2/smt2_dec.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ decision_proceduret::resultt smt2_dect::read_result(std::istream &in)
143143

144144
while(in)
145145
{
146-
irept parsed = smt2irep(in, get_message_handler());
146+
auto parsed_opt = smt2irep(in, get_message_handler());
147+
148+
if(!parsed_opt.has_value())
149+
break;
150+
151+
const auto &parsed = parsed_opt.value();
147152

148153
if(parsed.id()=="sat")
149154
res=resultt::D_SATISFIABLE;

src/solvers/smt2/smt2irep.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,33 @@ class smt2irept:public smt2_tokenizert
2222
{
2323
}
2424

25-
inline irept operator()()
26-
{
27-
parse();
28-
return result;
29-
}
30-
31-
bool parse();
25+
optionalt<irept> operator()();
3226

3327
protected:
3428
messaget log;
35-
irept result;
3629
};
3730

38-
bool smt2irept::parse()
31+
optionalt<irept> smt2irept::operator()()
3932
{
4033
try
4134
{
4235
std::stack<irept> stack;
43-
result.clear();
4436

4537
while(true)
4638
{
4739
switch(next_token())
4840
{
4941
case END_OF_FILE:
5042
if(stack.empty())
51-
{
52-
result = irept(irep_idt());
53-
return false;
54-
}
43+
return {};
5544
else
5645
throw error("unexpected end of file");
5746

5847
case STRING_LITERAL:
5948
case NUMERAL:
6049
case SYMBOL:
6150
if(stack.empty())
62-
{
63-
result = irept(buffer);
64-
return false; // all done!
65-
}
51+
return irept(buffer); // all done!
6652
else
6753
stack.top().get_sub().push_back(irept(buffer));
6854
break;
@@ -82,10 +68,7 @@ bool smt2irept::parse()
8268
stack.pop();
8369

8470
if(stack.empty())
85-
{
86-
result = tmp;
87-
return false; // all done!
88-
}
71+
return tmp; // all done!
8972

9073
stack.top().get_sub().push_back(tmp);
9174
break;
@@ -100,11 +83,11 @@ bool smt2irept::parse()
10083
{
10184
log.error().source_location.set_line(e.get_line_no());
10285
log.error() << e.what() << messaget::eom;
103-
return true;
86+
return {};
10487
}
10588
}
10689

107-
irept smt2irep(std::istream &in, message_handlert &message_handler)
90+
optionalt<irept> smt2irep(std::istream &in, message_handlert &message_handler)
10891
{
10992
return smt2irept(in, message_handler)();
11093
}

src/solvers/smt2/smt2irep.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ Author: Daniel Kroening, kroening@kroening.com
1313
#include <iosfwd>
1414

1515
#include <util/irep.h>
16+
#include <util/optional.h>
1617

1718
class message_handlert;
1819

19-
irept smt2irep(std::istream &, message_handlert &);
20+
/// returns an irep for an SMT-LIB2 expression read from a given stream
21+
/// returns {} when EOF is encountered before reading non-whitespace input
22+
optionalt<irept> smt2irep(std::istream &, message_handlert &);
2023

2124
#endif // CPROVER_SOLVERS_SMT2_SMT2IREP_H

0 commit comments

Comments
 (0)