Skip to content

Commit 5e7e55a

Browse files
committed
Change data_invariants to invariants_with_diagnostics
Change some data_invariants to invariants_with_diagnostics where they are more appropriate, and remove some duplicate invariants, where they are guaranteed by construction of the types themselves.
1 parent d1b0ddc commit 5e7e55a

File tree

1 file changed

+54
-64
lines changed

1 file changed

+54
-64
lines changed

src/goto-programs/goto_convert.cpp

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void goto_convertt::finish_computed_gotos(goto_programt &goto_program)
196196
{
197197
goto_programt::instructiont &i=*g_it;
198198
dereference_exprt destination = to_dereference_expr(i.code.op0());
199-
exprt pointer = destination.op();
199+
const exprt pointer = destination.pointer();
200200

201201
// remember the expression for later checks
202202
i.type=OTHER;
@@ -303,11 +303,6 @@ void goto_convertt::convert_label(
303303
goto_programt &dest,
304304
const irep_idt &mode)
305305
{
306-
DATA_INVARIANT(
307-
code.operands().size() == 1,
308-
code.find_source_location().as_string() +
309-
": label statement expected to have one operand");
310-
311306
// grab the label
312307
const irep_idt &label=code.get_label();
313308

@@ -320,8 +315,8 @@ void goto_convertt::convert_label(
320315
{
321316
// the body of the thread is expected to be
322317
// in the operand.
323-
INVARIANT(code.op0().is_not_nil(),
324-
"op0 in magic thread creation label is null");
318+
DATA_INVARIANT(
319+
code.op0().is_not_nil(), "op0 in magic thread creation label is null");
325320

326321
// replace the magic thread creation label with a
327322
// thread block (START_THREAD...END_THREAD).
@@ -386,18 +381,18 @@ void goto_convertt::convert_gcc_switch_case_range(
386381
goto_programt &dest,
387382
const irep_idt &mode)
388383
{
389-
DATA_INVARIANT(
384+
INVARIANT_WITH_DIAGNOSTICS(
390385
code.operands().size() == 3,
391-
code.find_source_location().as_string() +
392-
": GCC's switch-case-range statement expected to have three operands");
386+
"GCC's switch-case-range statement expected to have three operands",
387+
code.find_source_location());
393388

394389
const auto lb = numeric_cast<mp_integer>(code.op0());
395390
const auto ub = numeric_cast<mp_integer>(code.op1());
396391

397-
DATA_INVARIANT(
392+
INVARIANT_WITH_DIAGNOSTICS(
398393
lb.has_value() && ub.has_value(),
399-
code.find_source_location().as_string() +
400-
": GCC's switch-case-range statement requires constant bounds");
394+
"GCC's switch-case-range statement requires constant bounds",
395+
code.find_source_location());
401396

402397
if(*lb > *ub)
403398
{
@@ -513,10 +508,10 @@ void goto_convertt::convert(
513508
exprt assertion=code.op0();
514509
assertion.make_typecast(bool_typet());
515510
simplify(assertion, ns);
516-
INVARIANT(
511+
INVARIANT_WITH_DIAGNOSTICS(
517512
!assertion.is_false(),
518-
code.op0().find_source_location().as_string() + ": static assertion " +
519-
id2string(get_string_constant(code.op1())));
513+
"static assertion " + id2string(get_string_constant(code.op1())),
514+
code.op0().find_source_location());
520515
}
521516
else if(statement==ID_dead)
522517
copy(code, DEAD, dest);
@@ -689,10 +684,10 @@ void goto_convertt::convert_assign(
689684
if(rhs.id()==ID_side_effect &&
690685
rhs.get(ID_statement)==ID_function_call)
691686
{
692-
INVARIANT(
687+
INVARIANT_WITH_DIAGNOSTICS(
693688
rhs.operands().size() == 2,
694-
rhs.find_source_location().as_string() +
695-
": function_call sideeffect takes two operands");
689+
"function_call sideeffect takes two operands",
690+
rhs.find_source_location());
696691

697692
Forall_operands(it, rhs)
698693
clean_expr(*it, dest, mode);
@@ -781,10 +776,10 @@ void goto_convertt::convert_init(
781776
goto_programt &dest,
782777
const irep_idt &mode)
783778
{
784-
INVARIANT(
779+
INVARIANT_WITH_DIAGNOSTICS(
785780
code.operands().size() == 2,
786-
code.find_source_location().as_string() +
787-
": init statement takes two operands");
781+
"init statement takes two operands",
782+
code.find_source_location());
788783

789784
// make it an assignment
790785
codet assignment=code;
@@ -797,10 +792,10 @@ void goto_convertt::convert_cpp_delete(
797792
const codet &code,
798793
goto_programt &dest)
799794
{
800-
DATA_INVARIANT(
795+
INVARIANT_WITH_DIAGNOSTICS(
801796
code.operands().size() == 1,
802-
code.find_source_location().as_string() +
803-
": cpp_delete statement takes one operand");
797+
"cpp_delete statement takes one operand",
798+
code.find_source_location());
804799

805800
exprt tmp_op=code.op0();
806801

@@ -909,10 +904,10 @@ void goto_convertt::convert_loop_invariant(
909904
goto_programt no_sideeffects;
910905
clean_expr(invariant, no_sideeffects, mode);
911906

912-
INVARIANT(
907+
INVARIANT_WITH_DIAGNOSTICS(
913908
no_sideeffects.instructions.empty(),
914-
code.find_source_location().as_string() +
915-
": loop invariant is not side-effect free");
909+
"loop invariant is not side-effect free",
910+
code.find_source_location());
916911

917912
PRECONDITION(loop->is_goto());
918913
loop->guard.add(ID_C_spec_loop_invariant).swap(invariant);
@@ -1084,9 +1079,10 @@ void goto_convertt::convert_dowhile(
10841079
goto_programt &dest,
10851080
const irep_idt &mode)
10861081
{
1087-
INVARIANT(
1082+
INVARIANT_WITH_DIAGNOSTICS(
10881083
code.operands().size() == 2,
1089-
code.find_source_location().as_string() + ": dowhile takes two operands");
1084+
"dowhile takes two operands",
1085+
code.find_source_location());
10901086

10911087
// save source location
10921088
source_locationt condition_location=code.cond().find_source_location();
@@ -1253,9 +1249,7 @@ void goto_convertt::convert_switch(
12531249
{
12541250
const caset &case_ops=case_pair.second;
12551251

1256-
if(case_ops.empty())
1257-
throw incorrect_goto_program_exceptiont(
1258-
"switch case range cannot be empty", code.find_source_location());
1252+
assert(!case_ops.empty());
12591253

12601254
exprt guard_expr=case_guard(argument, case_ops);
12611255

@@ -1290,9 +1284,8 @@ void goto_convertt::convert_break(
12901284
goto_programt &dest,
12911285
const irep_idt &mode)
12921286
{
1293-
DATA_INVARIANT(
1294-
targets.break_set,
1295-
code.find_source_location().as_string() + ": break without target");
1287+
INVARIANT_WITH_DIAGNOSTICS(
1288+
targets.break_set, "break without target", code.find_source_location());
12961289

12971290
// need to process destructor stack
12981291
unwind_destructor_stack(
@@ -1315,10 +1308,10 @@ void goto_convertt::convert_return(
13151308
"return without target", code.find_source_location());
13161309
}
13171310

1318-
DATA_INVARIANT(
1311+
INVARIANT_WITH_DIAGNOSTICS(
13191312
code.operands().empty() || code.operands().size() == 1,
1320-
code.find_source_location().as_string() +
1321-
": return takes none or one operand");
1313+
"return takes none or one operand",
1314+
code.find_source_location());
13221315

13231316
code_returnt new_code(code);
13241317

@@ -1338,10 +1331,10 @@ void goto_convertt::convert_return(
13381331

13391332
if(targets.has_return_value)
13401333
{
1341-
INVARIANT(
1334+
INVARIANT_WITH_DIAGNOSTICS(
13421335
new_code.has_return_value(),
1343-
new_code.find_source_location().as_string() +
1344-
": function must return value");
1336+
"function must return value",
1337+
new_code.find_source_location());
13451338

13461339
// Now add a return node to set the return value.
13471340
goto_programt::targett t=dest.add_instruction();
@@ -1351,11 +1344,11 @@ void goto_convertt::convert_return(
13511344
}
13521345
else
13531346
{
1354-
INVARIANT(
1347+
INVARIANT_WITH_DIAGNOSTICS(
13551348
!new_code.has_return_value() ||
13561349
new_code.return_value().type().id() == ID_empty,
1357-
code.find_source_location().as_string() +
1358-
": function must not return value");
1350+
"function must not return value",
1351+
code.find_source_location());
13591352
}
13601353

13611354
// Need to process _entire_ destructor stack.
@@ -1372,9 +1365,10 @@ void goto_convertt::convert_continue(
13721365
goto_programt &dest,
13731366
const irep_idt &mode)
13741367
{
1375-
DATA_INVARIANT(
1368+
INVARIANT_WITH_DIAGNOSTICS(
13761369
targets.continue_set,
1377-
code.find_source_location().as_string() + ": continue without target");
1370+
"continue without target",
1371+
code.find_source_location());
13781372

13791373
// need to process destructor stack
13801374
unwind_destructor_stack(
@@ -1428,10 +1422,10 @@ void goto_convertt::convert_end_thread(
14281422
const codet &code,
14291423
goto_programt &dest)
14301424
{
1431-
DATA_INVARIANT(
1425+
INVARIANT_WITH_DIAGNOSTICS(
14321426
code.operands().empty(),
1433-
code.find_source_location().as_string() +
1434-
": end_thread expects no operands");
1427+
"end_thread expects no operands",
1428+
code.find_source_location());
14351429

14361430
copy(code, END_THREAD, dest);
14371431
}
@@ -1440,10 +1434,10 @@ void goto_convertt::convert_atomic_begin(
14401434
const codet &code,
14411435
goto_programt &dest)
14421436
{
1443-
DATA_INVARIANT(
1437+
INVARIANT_WITH_DIAGNOSTICS(
14441438
code.operands().empty(),
1445-
code.find_source_location().as_string() +
1446-
": atomic_begin expects no operands");
1439+
"atomic_begin expects no operands",
1440+
code.find_source_location());
14471441

14481442
copy(code, ATOMIC_BEGIN, dest);
14491443
}
@@ -1452,10 +1446,10 @@ void goto_convertt::convert_atomic_end(
14521446
const codet &code,
14531447
goto_programt &dest)
14541448
{
1455-
DATA_INVARIANT(
1449+
INVARIANT_WITH_DIAGNOSTICS(
14561450
code.operands().empty(),
1457-
code.find_source_location().as_string() +
1458-
": atomic_end expects no operands");
1451+
": atomic_end expects no operands",
1452+
code.find_source_location());
14591453

14601454
copy(code, ATOMIC_END, dest);
14611455
}
@@ -1465,11 +1459,6 @@ void goto_convertt::convert_ifthenelse(
14651459
goto_programt &dest,
14661460
const irep_idt &mode)
14671461
{
1468-
DATA_INVARIANT(
1469-
code.operands().size() == 3,
1470-
code.find_source_location().as_string() +
1471-
": ifthenelse takes three operands");
1472-
14731462
DATA_INVARIANT(code.then_case().is_not_nil(), "cannot accept an empty body");
14741463

14751464
bool has_else=
@@ -1865,10 +1854,11 @@ irep_idt goto_convertt::get_string_constant(const exprt &expr)
18651854
{
18661855
irep_idt result;
18671856

1868-
bool res = get_string_constant(expr, result);
1857+
const bool res = get_string_constant(expr, result);
18691858
INVARIANT_WITH_DIAGNOSTICS(
18701859
!res,
1871-
expr.find_source_location().as_string() + ": expected string constant",
1860+
"expected string constant",
1861+
expr.find_source_location(),
18721862
expr.pretty());
18731863

18741864
return result;

0 commit comments

Comments
 (0)