Skip to content

Commit c952f55

Browse files
committed
Remove some preconditions and appropriately type the expressions.
1 parent fe4a218 commit c952f55

File tree

4 files changed

+81
-84
lines changed

4 files changed

+81
-84
lines changed

src/goto-programs/goto_convert_exceptions.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ void goto_convertt::convert_msc_try_finally(
1818
goto_programt &dest,
1919
const irep_idt &mode)
2020
{
21-
DATA_INVARIANT(
21+
INVARIANT_WITH_DIAGNOSTICS(
2222
code.operands().size() == 2,
23-
code.find_source_location().as_string() +
24-
": msc_try_finally expects two arguments");
23+
"msc_try_finally expects two arguments",
24+
code.find_source_location());
2525

2626
goto_programt tmp;
2727
tmp.add_instruction(SKIP)->source_location=code.source_location();
@@ -55,10 +55,10 @@ void goto_convertt::convert_msc_try_except(
5555
goto_programt &dest,
5656
const irep_idt &mode)
5757
{
58-
DATA_INVARIANT(
58+
INVARIANT_WITH_DIAGNOSTICS(
5959
code.operands().size() == 3,
60-
code.find_source_location().as_string() +
61-
": msc_try_except expects three arguments");
60+
"msc_try_except expects three arguments",
61+
code.find_source_location());
6262

6363
convert(to_code(code.op0()), dest, mode);
6464

@@ -70,9 +70,8 @@ void goto_convertt::convert_msc_leave(
7070
goto_programt &dest,
7171
const irep_idt &mode)
7272
{
73-
DATA_INVARIANT(
74-
targets.leave_set,
75-
code.find_source_location().as_string() + ": leave without target");
73+
INVARIANT_WITH_DIAGNOSTICS(
74+
targets.leave_set, "leave without target", code.find_source_location());
7675

7776
// need to process destructor stack
7877
for(std::size_t d=targets.destructor_stack.size();
@@ -94,10 +93,10 @@ void goto_convertt::convert_try_catch(
9493
goto_programt &dest,
9594
const irep_idt &mode)
9695
{
97-
DATA_INVARIANT(
96+
INVARIANT_WITH_DIAGNOSTICS(
9897
code.operands().size() >= 2,
99-
code.find_source_location().as_string() +
100-
": try_catch expects at least two arguments");
98+
"try_catch expects at least two arguments",
99+
code.find_source_location());
101100

102101
// add the CATCH-push instruction to 'dest'
103102
goto_programt::targett catch_push_instruction=dest.add_instruction();
@@ -155,10 +154,10 @@ void goto_convertt::convert_CPROVER_try_catch(
155154
goto_programt &dest,
156155
const irep_idt &mode)
157156
{
158-
DATA_INVARIANT(
157+
INVARIANT_WITH_DIAGNOSTICS(
159158
code.operands().size() == 2,
160-
code.find_source_location().as_string() +
161-
": CPROVER_try_catch expects two arguments");
159+
"CPROVER_try_catch expects two arguments",
160+
code.find_source_location());
162161

163162
// this is where we go after 'throw'
164163
goto_programt tmp;
@@ -229,10 +228,10 @@ void goto_convertt::convert_CPROVER_try_finally(
229228
goto_programt &dest,
230229
const irep_idt &mode)
231230
{
232-
DATA_INVARIANT(
231+
INVARIANT_WITH_DIAGNOSTICS(
233232
code.operands().size() == 2,
234-
code.find_source_location().as_string() +
235-
": CPROVER_try_finally expects two arguments");
233+
"CPROVER_try_finally expects two arguments",
234+
code.find_source_location());
236235

237236
// first put 'finally' code onto destructor stack
238237
targets.destructor_stack.push_back(to_code(code.op1()));

src/goto-programs/goto_convert_side_effect.cpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ void goto_convertt::remove_assignment(
6060
statement==ID_assign_bitxor ||
6161
statement==ID_assign_bitor)
6262
{
63-
INVARIANT(
63+
INVARIANT_WITH_DIAGNOSTICS(
6464
expr.operands().size() == 2,
65-
expr.find_source_location().as_string() + ": " + id2string(statement) +
66-
" takes two arguments");
65+
id2string(statement) + " expects two arguments",
66+
expr.find_source_location());
6767

6868
irep_idt new_id;
6969

@@ -138,16 +138,16 @@ void goto_convertt::remove_pre(
138138
bool result_is_used,
139139
const irep_idt &mode)
140140
{
141-
DATA_INVARIANT(
141+
INVARIANT_WITH_DIAGNOSTICS(
142142
expr.operands().size() == 1,
143-
expr.find_source_location().as_string() +
144-
": preincrement/predecrement must have one operand");
143+
"preincrement/predecrement must have one operand",
144+
expr.find_source_location());
145145

146146
const irep_idt statement=expr.get_statement();
147147

148148
DATA_INVARIANT(
149149
statement == ID_preincrement || statement == ID_predecrement,
150-
" expected preincrement or predecrement");
150+
"expects preincrement or predecrement");
151151

152152
exprt rhs;
153153
rhs.add_source_location()=expr.source_location();
@@ -227,16 +227,16 @@ void goto_convertt::remove_post(
227227

228228
// we have ...(op++)...
229229

230-
DATA_INVARIANT(
230+
INVARIANT_WITH_DIAGNOSTICS(
231231
expr.operands().size() == 1,
232-
expr.find_source_location().as_string() +
233-
": postincrement/postdecrement must have one operand");
232+
"postincrement/postdecrement must have one operand",
233+
expr.find_source_location());
234234

235235
const irep_idt statement=expr.get_statement();
236236

237237
DATA_INVARIANT(
238238
statement == ID_postincrement || statement == ID_postdecrement,
239-
" expected postincrement or postdecrement");
239+
"expects postincrement or postdecrement");
240240

241241
exprt rhs;
242242
rhs.add_source_location()=expr.source_location();
@@ -326,12 +326,13 @@ void goto_convertt::remove_function_call(
326326
const irep_idt &mode,
327327
bool result_is_used)
328328
{
329+
INVARIANT_WITH_DIAGNOSTICS(
330+
expr.operands().size() == 2,
331+
"function_call expects two operands",
332+
expr.find_source_location());
333+
329334
if(!result_is_used)
330335
{
331-
DATA_INVARIANT(
332-
expr.operands().size() == 2,
333-
expr.find_source_location().as_string() +
334-
": function_call expects two operands");
335336
code_function_callt call(nil_exprt(), expr.op0(), expr.op1().operands());
336337
call.add_source_location()=expr.source_location();
337338
convert_function_call(call, dest, mode);
@@ -341,14 +342,10 @@ void goto_convertt::remove_function_call(
341342

342343
// get name of function, if available
343344

344-
DATA_INVARIANT(
345+
INVARIANT_WITH_DIAGNOSTICS(
345346
expr.id() == ID_side_effect && expr.get(ID_statement) == ID_function_call,
346-
expr.find_source_location().as_string() + ": expected function call");
347-
348-
DATA_INVARIANT(
349-
!expr.operands().empty(),
350-
expr.find_source_location().as_string() +
351-
": function_call expects at least one operand");
347+
"expects function call",
348+
expr.find_source_location());
352349

353350
std::string new_base_name = "return_value";
354351
irep_idt new_symbol_mode = mode;
@@ -430,7 +427,7 @@ void goto_convertt::remove_cpp_delete(
430427
side_effect_exprt &expr,
431428
goto_programt &dest)
432429
{
433-
DATA_INVARIANT(expr.operands().size() == 1, "cpp_delete expected 1 operand");
430+
DATA_INVARIANT(expr.operands().size() == 1, "cpp_delete expects one operand");
434431

435432
codet tmp(expr.get_statement());
436433
tmp.add_source_location()=expr.source_location();
@@ -483,10 +480,10 @@ void goto_convertt::remove_temporary_object(
483480
goto_programt &dest)
484481
{
485482
const irep_idt &mode = expr.get(ID_mode);
486-
DATA_INVARIANT(
483+
INVARIANT_WITH_DIAGNOSTICS(
487484
expr.operands().size() <= 1,
488-
expr.find_source_location().as_string() +
489-
": temporary_object takes 0 or 1 operands");
485+
"temporary_object takes zero or one operands",
486+
expr.find_source_location());
490487

491488
symbolt &new_symbol = new_tmp_symbol(
492489
expr.type(), "obj", dest, expr.find_source_location(), mode);
@@ -500,10 +497,10 @@ void goto_convertt::remove_temporary_object(
500497

501498
if(expr.find(ID_initializer).is_not_nil())
502499
{
503-
INVARIANT(
500+
INVARIANT_WITH_DIAGNOSTICS(
504501
expr.operands().empty(),
505-
expr.find_source_location().as_string() +
506-
": temporary_object takes 0 operands");
502+
"temporary_object takes zero operands",
503+
expr.find_source_location());
507504
exprt initializer=static_cast<const exprt &>(expr.find(ID_initializer));
508505
replace_new_object(new_symbol.symbol_expr(), initializer);
509506

@@ -524,15 +521,15 @@ void goto_convertt::remove_statement_expression(
524521
// The expression is copied into a temporary before the
525522
// scope is destroyed.
526523

527-
DATA_INVARIANT(
524+
INVARIANT_WITH_DIAGNOSTICS(
528525
expr.operands().size() == 1,
529-
expr.find_source_location().as_string() +
530-
": statement_expression takes 1 operand");
526+
"statement_expression takes one operand",
527+
expr.find_source_location());
531528

532-
DATA_INVARIANT(
529+
INVARIANT_WITH_DIAGNOSTICS(
533530
expr.op0().id() == ID_code,
534-
expr.find_source_location().as_string() +
535-
": statement_expression takes code as operand");
531+
"statement_expression takes code as operand",
532+
expr.find_source_location());
536533

537534
codet &code=to_code(expr.op0());
538535

@@ -543,15 +540,15 @@ void goto_convertt::remove_statement_expression(
543540
return;
544541
}
545542

546-
DATA_INVARIANT(
543+
INVARIANT_WITH_DIAGNOSTICS(
547544
code.get_statement() == ID_block,
548-
code.find_source_location().as_string() +
549-
": statement_expression takes block as operand");
545+
"statement_expression takes block as operand",
546+
code.find_source_location());
550547

551-
DATA_INVARIANT(
548+
INVARIANT_WITH_DIAGNOSTICS(
552549
!code.operands().empty(),
553-
expr.find_source_location().as_string() +
554-
": statement_expression takes non-empty block as operand");
550+
"statement_expression takes non-empty block as operand",
551+
expr.find_source_location());
555552

556553
// get last statement from block, following labels
557554
codet &last=to_code_block(code).find_last_statement();

src/goto-programs/goto_inline_class.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void goto_inlinet::parameter_assignments(
5454

5555
const irep_idt &identifier=parameter.get_identifier();
5656

57-
INVARIANT(
57+
DATA_INVARIANT(
5858
!identifier.empty(),
5959
source_location.as_string() + ": no identifier for function parameter");
6060

@@ -842,7 +842,7 @@ void goto_inlinet::goto_inline_logt::copy_from(
842842
{
843843
DATA_INVARIANT(
844844
it2 != to.instructions.end(),
845-
"'to' target function is not alllowed to be empty");
845+
"'to' target function is not allowed to be empty");
846846
DATA_INVARIANT(
847847
it1->location_number == it2->location_number,
848848
"both functions' instruction should point to the same source");

src/goto-programs/goto_program.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ std::ostream &goto_programt::output_instruction(
165165
instruction.code.find(ID_exception_list).get_sub();
166166
DATA_INVARIANT(
167167
instruction.targets.size() == exception_list.size(),
168-
"size of target list");
168+
"unexpected discrepancy between sizes of instruction"
169+
"targets and exception list");
169170
for(instructiont::targetst::const_iterator
170171
gt_it=instruction.targets.begin();
171172
gt_it!=instruction.targets.end();
@@ -223,10 +224,11 @@ void goto_programt::get_decl_identifiers(
223224
if(it->is_decl())
224225
{
225226
DATA_INVARIANT(
226-
it->code.get_statement() == ID_decl, "declaration statements");
227+
it->code.get_statement() == ID_decl,
228+
"expected statement to be declaration statement");
227229
DATA_INVARIANT(
228230
it->code.operands().size() == 1,
229-
"declaration statement expects 1 operand");
231+
"declaration statement expects one operand");
230232
const symbol_exprt &symbol_expr=to_symbol_expr(it->code.op0());
231233
decl_identifiers.insert(symbol_expr.get_identifier());
232234
}
@@ -237,26 +239,24 @@ void parse_lhs_read(const exprt &src, std::list<exprt> &dest)
237239
{
238240
if(src.id()==ID_dereference)
239241
{
240-
PRECONDITION(src.operands().size() == 1);
241-
dest.push_back(src.op0());
242+
dest.push_back(to_dereference_expr(src).pointer());
242243
}
243244
else if(src.id()==ID_index)
244245
{
245-
PRECONDITION(src.operands().size() == 2);
246-
dest.push_back(src.op1());
247-
parse_lhs_read(src.op0(), dest);
246+
auto &index_expr = to_index_expr(src);
247+
dest.push_back(index_expr.index());
248+
parse_lhs_read(index_expr.array(), dest);
248249
}
249250
else if(src.id()==ID_member)
250251
{
251-
PRECONDITION(src.operands().size() == 1);
252-
parse_lhs_read(src.op0(), dest);
252+
parse_lhs_read(to_member_expr(src).compound(), dest);
253253
}
254254
else if(src.id()==ID_if)
255255
{
256-
PRECONDITION(src.operands().size() == 3);
257-
dest.push_back(src.op0());
258-
parse_lhs_read(src.op1(), dest);
259-
parse_lhs_read(src.op2(), dest);
256+
auto &if_expr = to_if_expr(src);
257+
dest.push_back(if_expr.cond());
258+
parse_lhs_read(if_expr.true_case(), dest);
259+
parse_lhs_read(if_expr.false_case(), dest);
260260
}
261261
}
262262

@@ -346,9 +346,9 @@ void objects_read(
346346
else if(src.id()==ID_dereference)
347347
{
348348
// this reads what is pointed to plus the pointer
349-
PRECONDITION(src.operands().size() == 1);
350-
dest.push_back(src);
351-
objects_read(src.op0(), dest);
349+
auto &deref = to_dereference_expr(src);
350+
dest.push_back(deref);
351+
objects_read(deref.pointer(), dest);
352352
}
353353
else
354354
{
@@ -376,9 +376,9 @@ void objects_written(
376376
{
377377
if(src.id()==ID_if)
378378
{
379-
PRECONDITION(src.operands().size() == 3);
380-
objects_written(src.op1(), dest);
381-
objects_written(src.op2(), dest);
379+
auto &if_expr = to_if_expr(src);
380+
objects_written(if_expr.true_case(), dest);
381+
objects_written(if_expr.false_case(), dest);
382382
}
383383
else
384384
dest.push_back(src);
@@ -557,7 +557,8 @@ void goto_programt::compute_target_numbers()
557557
if(i.is_target())
558558
{
559559
i.target_number=++cnt;
560-
DATA_INVARIANT(i.target_number != 0, "instruction's number cannot be 0");
560+
DATA_INVARIANT(
561+
i.target_number != 0, "GOTO instruction target cannot be zero");
561562
}
562563
}
563564

@@ -571,10 +572,10 @@ void goto_programt::compute_target_numbers()
571572
if(t!=instructions.end())
572573
{
573574
DATA_INVARIANT(
574-
t->target_number != 0, "instruction's number cannot be 0");
575+
t->target_number != 0, "instruction's number cannot be zero");
575576
DATA_INVARIANT(
576577
t->target_number != instructiont::nil_target,
577-
"instruction cannot be an invalid target");
578+
"GOTO instruction target cannot be nil_target");
578579
}
579580
}
580581
}

0 commit comments

Comments
 (0)