Skip to content

Commit 11158c9

Browse files
authored
Merge pull request #988 from diffblue/smv_enum_type
SMV: type check SMV enum literals
2 parents 082eb98 + 21c37ed commit 11158c9

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

regression/smv/enums/enum3.desc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
KNOWNBUG
1+
CORE
22
enum3.smv
33

4-
^EXIT=0$
4+
^file .* line 7: enum c not a member of \{ a, b \}$
5+
^EXIT=2$
56
^SIGNAL=0$
67
--
78
^warning: ignoring
89
--
9-
This should be a type error.

src/smvlang/smv_typecheck.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class smv_typecheckt:public typecheckt
7979
bool do_spec;
8080

8181
smv_ranget convert_type(const typet &);
82+
static bool is_contained_in(irep_idt, const enumeration_typet &);
83+
8284
void convert(smv_parse_treet::modulet::itemt &);
8385
void typecheck(smv_parse_treet::modulet::itemt &);
8486
void typecheck_expr_rec(exprt &, const typet &, modet);
@@ -137,6 +139,26 @@ class smv_typecheckt:public typecheckt
137139

138140
/*******************************************************************\
139141
142+
Function: smv_typecheckt::is_contained_in
143+
144+
Inputs:
145+
146+
Outputs:
147+
148+
Purpose:
149+
150+
\*******************************************************************/
151+
152+
bool smv_typecheckt::is_contained_in(irep_idt id, const enumeration_typet &type)
153+
{
154+
for(auto &element : type.elements())
155+
if(element.id() == id)
156+
return true;
157+
return false;
158+
}
159+
160+
/*******************************************************************\
161+
140162
Function: smv_typecheckt::convert_ports
141163
142164
Inputs:
@@ -749,10 +771,11 @@ void smv_typecheckt::typecheck_expr_rec(
749771
}
750772
else if(expr.id()==ID_constant)
751773
{
774+
const auto value = to_constant_expr(expr).get_value();
775+
752776
if(expr.type().id()==ID_integer)
753777
{
754-
const std::string &value=expr.get_string(ID_value);
755-
mp_integer int_value=string2integer(value);
778+
mp_integer int_value = string2integer(id2string(value));
756779

757780
if(dest_type.is_nil())
758781
{
@@ -796,6 +819,12 @@ void smv_typecheckt::typecheck_expr_rec(
796819
{
797820
if(dest_type.id() == ID_enumeration)
798821
{
822+
if(!is_contained_in(value, to_enumeration_type(dest_type)))
823+
{
824+
throw errort().with_location(expr.find_source_location())
825+
<< "enum " << value << " not a member of " << to_string(dest_type);
826+
}
827+
799828
if(to_enumeration_type(expr.type()).elements().empty())
800829
expr.type() = dest_type;
801830
}

0 commit comments

Comments
 (0)