@@ -30,6 +30,7 @@ Date: February 2016
30
30
#include < langapi/language_util.h>
31
31
32
32
#include < util/c_types.h>
33
+ #include < util/exception_utils.h>
33
34
#include < util/expr_util.h>
34
35
#include < util/find_symbols.h>
35
36
#include < util/format_expr.h>
@@ -729,7 +730,7 @@ code_contractst::create_ensures_instruction(
729
730
return std::make_pair (std::move (ensures_program), std::move (history));
730
731
}
731
732
732
- bool code_contractst::apply_function_contract (
733
+ void code_contractst::apply_function_contract (
733
734
const irep_idt &function,
734
735
const source_locationt &location,
735
736
goto_programt &function_body,
@@ -932,7 +933,6 @@ bool code_contractst::apply_function_contract(
932
933
933
934
// Add this function to the set of replaced functions.
934
935
summarized.insert (target_function);
935
- return false ;
936
936
}
937
937
938
938
void code_contractst::apply_loop_contract (
@@ -1225,17 +1225,14 @@ goto_functionst &code_contractst::get_goto_functions()
1225
1225
return goto_functions;
1226
1226
}
1227
1227
1228
- bool code_contractst::check_frame_conditions_function (const irep_idt &function)
1228
+ void code_contractst::check_frame_conditions_function (const irep_idt &function)
1229
1229
{
1230
1230
// Get the function object before instrumentation.
1231
1231
auto function_obj = goto_functions.function_map .find (function);
1232
- if (function_obj == goto_functions.function_map .end ())
1233
- {
1234
- log.error () << " Could not find function '" << function
1235
- << " ' in goto-program; not enforcing contracts."
1236
- << messaget::eom;
1237
- return true ;
1238
- }
1232
+
1233
+ INVARIANT (
1234
+ function_obj != goto_functions.function_map .end (),
1235
+ " Function '" + id2string (function) + " 'must exist in the goto program" );
1239
1236
1240
1237
const auto &goto_function = function_obj->second ;
1241
1238
auto &function_body = function_obj->second .body ;
@@ -1316,11 +1313,9 @@ bool code_contractst::check_frame_conditions_function(const irep_idt &function)
1316
1313
function_body.instructions .end (),
1317
1314
skip_function_paramst::YES,
1318
1315
cfg_info_opt);
1319
-
1320
- return false ;
1321
1316
}
1322
1317
1323
- bool code_contractst::enforce_contract (const irep_idt &function)
1318
+ void code_contractst::enforce_contract (const irep_idt &function)
1324
1319
{
1325
1320
// Add statements to the source function
1326
1321
// to ensure assigns clause is respected.
@@ -1333,13 +1328,9 @@ bool code_contractst::enforce_contract(const irep_idt &function)
1333
1328
const irep_idt original (function);
1334
1329
1335
1330
auto old_function = goto_functions.function_map .find (original);
1336
- if (old_function == goto_functions.function_map .end ())
1337
- {
1338
- log.error () << " Could not find function '" << function
1339
- << " ' in goto-program; not enforcing contracts."
1340
- << messaget::eom;
1341
- return true ;
1342
- }
1331
+ INVARIANT (
1332
+ old_function != goto_functions.function_map .end (),
1333
+ " Function to replace must exist in the program." );
1343
1334
1344
1335
std::swap (goto_functions.function_map [mangled], old_function->second );
1345
1336
goto_functions.function_map .erase (old_function);
@@ -1379,8 +1370,6 @@ bool code_contractst::enforce_contract(const irep_idt &function)
1379
1370
wrapper.parameter_identifiers = mangled_fun->second .parameter_identifiers ;
1380
1371
wrapper.body .add (goto_programt::make_end_function (sl));
1381
1372
add_contract_check (original, mangled, wrapper.body );
1382
-
1383
- return false ;
1384
1373
}
1385
1374
1386
1375
void code_contractst::add_contract_check (
@@ -1536,12 +1525,29 @@ void code_contractst::add_contract_check(
1536
1525
dest.destructive_insert (dest.instructions .begin (), check);
1537
1526
}
1538
1527
1539
- bool code_contractst::replace_calls (const std::set<std::string> &to_replace)
1528
+ void code_contractst::check_all_functions_found (
1529
+ const std::set<std::string> &functions) const
1530
+ {
1531
+ for (const auto &function : functions)
1532
+ {
1533
+ if (
1534
+ goto_functions.function_map .find (function) ==
1535
+ goto_functions.function_map .end ())
1536
+ {
1537
+ throw invalid_input_exceptiont (
1538
+ " Function '" + function + " ' was not found in the GOTO program." );
1539
+ }
1540
+ }
1541
+ }
1542
+
1543
+ void code_contractst::replace_calls (const std::set<std::string> &to_replace)
1540
1544
{
1541
1545
if (to_replace.empty ())
1542
- return false ;
1546
+ return ;
1547
+
1548
+ log.status () << " Replacing function calls with contracts" << messaget::eom;
1543
1549
1544
- bool fail = false ;
1550
+ check_all_functions_found (to_replace) ;
1545
1551
1546
1552
for (auto &goto_function : goto_functions.function_map )
1547
1553
{
@@ -1559,7 +1565,7 @@ bool code_contractst::replace_calls(const std::set<std::string> &to_replace)
1559
1565
if (found == to_replace.end ())
1560
1566
continue ;
1561
1567
1562
- fail |= apply_function_contract (
1568
+ apply_function_contract (
1563
1569
goto_function.first ,
1564
1570
ins->source_location (),
1565
1571
goto_function.second .body ,
@@ -1568,15 +1574,10 @@ bool code_contractst::replace_calls(const std::set<std::string> &to_replace)
1568
1574
}
1569
1575
}
1570
1576
1571
- if (fail)
1572
- return true ;
1573
-
1574
1577
for (auto &goto_function : goto_functions.function_map )
1575
1578
remove_skip (goto_function.second .body );
1576
1579
1577
1580
goto_functions.update ();
1578
-
1579
- return false ;
1580
1581
}
1581
1582
1582
1583
void code_contractst::apply_loop_contracts ()
@@ -1585,27 +1586,15 @@ void code_contractst::apply_loop_contracts()
1585
1586
apply_loop_contract (goto_function.first , goto_function.second );
1586
1587
}
1587
1588
1588
- bool code_contractst::enforce_contracts (const std::set<std::string> &to_enforce)
1589
+ void code_contractst::enforce_contracts (const std::set<std::string> &to_enforce)
1589
1590
{
1590
1591
if (to_enforce.empty ())
1591
- return false ;
1592
+ return ;
1592
1593
1593
- bool fail = false ;
1594
+ log. status () << " Enforcing contracts " << messaget ::eom ;
1594
1595
1595
- for (const auto &function : to_enforce)
1596
- {
1597
- auto goto_function = goto_functions.function_map .find (function);
1598
- if (goto_function == goto_functions.function_map .end ())
1599
- {
1600
- fail = true ;
1601
- log.error () << " Could not find function '" << function
1602
- << " ' in goto-program; not enforcing contracts."
1603
- << messaget::eom;
1604
- continue ;
1605
- }
1596
+ check_all_functions_found (to_enforce);
1606
1597
1607
- if (!fail)
1608
- fail = enforce_contract (function);
1609
- }
1610
- return fail;
1598
+ for (const auto &function : to_enforce)
1599
+ enforce_contract (function);
1611
1600
}
0 commit comments