11
11
12
12
#include " goto_convert.h"
13
13
14
- #include < cassert>
15
-
16
14
#include < util/arith_tools.h>
17
15
#include < util/cprover_prefix.h>
16
+ #include < util/exception_utils.h>
18
17
#include < util/expr_util.h>
19
18
#include < util/fresh_symbol.h>
20
19
#include < util/prefix.h>
@@ -116,10 +115,12 @@ void goto_convertt::finish_gotos(goto_programt &dest, const irep_idt &mode)
116
115
labelst::const_iterator l_it=
117
116
targets.labels .find (goto_label);
118
117
119
- DATA_INVARIANT (
120
- l_it != targets.labels .end (),
121
- i.code .find_source_location ().as_string () + " : goto label '" +
122
- id2string (goto_label) + " ' not found" );
118
+ if (l_it == targets.labels .end ())
119
+ {
120
+ throw incorrect_goto_program_exceptiont (
121
+ " goto label \' " + id2string (goto_label) + " \' not found" ,
122
+ i.code .find_source_location ());
123
+ }
123
124
124
125
i.targets .push_back (l_it->second .first );
125
126
}
@@ -129,10 +130,12 @@ void goto_convertt::finish_gotos(goto_programt &dest, const irep_idt &mode)
129
130
130
131
labelst::const_iterator l_it=targets.labels .find (goto_label);
131
132
132
- DATA_INVARIANT (
133
- l_it != targets.labels .end (),
134
- i.code .find_source_location ().as_string () + " : goto label '" +
135
- id2string (goto_label) + " ' not found" );
133
+ if (l_it == targets.labels .end ())
134
+ {
135
+ throw incorrect_goto_program_exceptiont (
136
+ " goto label \' " + id2string (goto_label) + " \' not found" ,
137
+ i.code .find_source_location ());
138
+ }
136
139
137
140
i.complete_goto (l_it->second .first );
138
141
@@ -192,13 +195,8 @@ void goto_convertt::finish_computed_gotos(goto_programt &goto_program)
192
195
for (auto &g_it : targets.computed_gotos )
193
196
{
194
197
goto_programt::instructiont &i=*g_it;
195
- exprt destination=i.code .op0 ();
196
-
197
- DATA_INVARIANT (
198
- destination.id () == ID_dereference, " dereference ID not allowed here" );
199
- DATA_INVARIANT (destination.operands ().size () == 1 , " expected 1 argument" );
200
-
201
- exprt pointer=destination.op0 ();
198
+ dereference_exprt destination = to_dereference_expr (i.code .op0 ());
199
+ exprt pointer = destination.op ();
202
200
203
201
// remember the expression for later checks
204
202
i.type =OTHER;
@@ -516,7 +514,7 @@ void goto_convertt::convert(
516
514
assertion.make_typecast (bool_typet ());
517
515
simplify (assertion, ns);
518
516
INVARIANT (
519
- assertion.is_false (),
517
+ ! assertion.is_false (),
520
518
code.op0 ().find_source_location ().as_string () + " : static assertion " +
521
519
id2string (get_string_constant (code.op1 ())));
522
520
}
@@ -580,12 +578,7 @@ void goto_convertt::convert_expression(
580
578
goto_programt &dest,
581
579
const irep_idt &mode)
582
580
{
583
- INVARIANT (
584
- code.operands ().size () == 1 ,
585
- code.find_source_location ().as_string () +
586
- " : expression statement takes one operand" );
587
-
588
- exprt expr=code.op0 ();
581
+ exprt expr = code.expression ();
589
582
590
583
if (expr.id ()==ID_if)
591
584
{
@@ -622,14 +615,7 @@ void goto_convertt::convert_decl(
622
615
goto_programt &dest,
623
616
const irep_idt &mode)
624
617
{
625
- const exprt &op = code.symbol ();
626
-
627
- INVARIANT (
628
- op.id () == ID_symbol,
629
- op.find_source_location ().as_string () +
630
- " : decl statement expects symbol as operand" );
631
-
632
- const irep_idt &identifier = to_symbol_expr (op).get_identifier ();
618
+ const irep_idt &identifier = to_symbol_expr (code.symbol ()).get_identifier ();
633
619
634
620
const symbolt &symbol = ns.lookup (identifier);
635
621
@@ -774,11 +760,8 @@ void goto_convertt::convert_assign(
774
760
775
761
if (lhs.id ()==ID_typecast)
776
762
{
777
- INVARIANT (lhs.operands ().size () == 1 , " typecast takes one operand" );
778
-
779
763
// add a typecast to the rhs
780
- exprt new_rhs=rhs;
781
- rhs.make_typecast (lhs.op0 ().type ());
764
+ rhs.make_typecast (to_typecast_expr (lhs).op ().type ());
782
765
783
766
// remove typecast from lhs
784
767
exprt tmp=lhs.op0 ();
@@ -1269,7 +1252,9 @@ void goto_convertt::convert_switch(
1269
1252
{
1270
1253
const caset &case_ops=case_pair.second ;
1271
1254
1272
- assert (!case_ops.empty ());
1255
+ if (case_ops.empty ())
1256
+ throw incorrect_goto_program_exceptiont (
1257
+ " switch case range cannot be empty" , code.find_source_location ());
1273
1258
1274
1259
exprt guard_expr=case_guard (argument, case_ops);
1275
1260
@@ -1325,9 +1310,8 @@ void goto_convertt::convert_return(
1325
1310
{
1326
1311
if (!targets.return_set )
1327
1312
{
1328
- error ().source_location =code.find_source_location ();
1329
- error () << " return without target" << eom;
1330
- throw 0 ;
1313
+ throw incorrect_goto_program_exceptiont (
1314
+ " return without target" , code.find_source_location ());
1331
1315
}
1332
1316
1333
1317
DATA_INVARIANT (
@@ -1763,10 +1747,14 @@ void goto_convertt::generate_conditional_branch(
1763
1747
{
1764
1748
if (guard.id ()==ID_not)
1765
1749
{
1766
- PRECONDITION (guard.operands ().size () == 1 );
1767
1750
// simply swap targets
1768
1751
generate_conditional_branch (
1769
- guard.op0 (), target_false, target_true, source_location, dest, mode);
1752
+ to_not_expr (guard).op (),
1753
+ target_false,
1754
+ target_true,
1755
+ source_location,
1756
+ dest,
1757
+ mode);
1770
1758
return ;
1771
1759
}
1772
1760
@@ -1877,7 +1865,7 @@ irep_idt goto_convertt::get_string_constant(const exprt &expr)
1877
1865
irep_idt result;
1878
1866
1879
1867
bool res = get_string_constant (expr, result);
1880
- INVARIANT (
1868
+ INVARIANT_WITH_DIAGNOSTICS (
1881
1869
!res,
1882
1870
expr.find_source_location ().as_string () + " : expected string constant" ,
1883
1871
expr.pretty ());
0 commit comments