@@ -13,23 +13,8 @@ Date: February 2016
13
13
14
14
#include " contracts.h"
15
15
16
- #include < algorithm>
17
- #include < map>
18
-
19
- #include < analyses/local_bitvector_analysis.h>
20
- #include < analyses/local_may_alias.h>
21
-
22
- #include < ansi-c/c_expr.h>
23
-
24
- #include < goto-instrument/havoc_utils.h>
25
-
26
- #include < goto-programs/goto_inline.h>
27
- #include < goto-programs/goto_program.h>
28
- #include < goto-programs/remove_skip.h>
29
-
30
- #include < langapi/language_util.h>
31
-
32
16
#include < util/c_types.h>
17
+ #include < util/exception_utils.h>
33
18
#include < util/expr_util.h>
34
19
#include < util/find_symbols.h>
35
20
#include < util/format_expr.h>
@@ -43,11 +28,24 @@ Date: February 2016
43
28
#include < util/replace_symbol.h>
44
29
#include < util/std_code.h>
45
30
31
+ #include < goto-programs/goto_inline.h>
32
+ #include < goto-programs/goto_program.h>
33
+ #include < goto-programs/remove_skip.h>
34
+
35
+ #include < analyses/local_bitvector_analysis.h>
36
+ #include < analyses/local_may_alias.h>
37
+ #include < ansi-c/c_expr.h>
38
+ #include < goto-instrument/havoc_utils.h>
39
+ #include < langapi/language_util.h>
40
+
46
41
#include " havoc_assigns_clause_targets.h"
47
42
#include " instrument_spec_assigns.h"
48
43
#include " memory_predicates.h"
49
44
#include " utils.h"
50
45
46
+ #include < algorithm>
47
+ #include < map>
48
+
51
49
// / Decorator for \ref message_handlert that keeps track of warnings
52
50
// / occuring when inlining a function.
53
51
// /
@@ -729,7 +727,7 @@ code_contractst::create_ensures_instruction(
729
727
return std::make_pair (std::move (ensures_program), std::move (history));
730
728
}
731
729
732
- bool code_contractst::apply_function_contract (
730
+ void code_contractst::apply_function_contract (
733
731
const irep_idt &function,
734
732
const source_locationt &location,
735
733
goto_programt &function_body,
@@ -932,7 +930,6 @@ bool code_contractst::apply_function_contract(
932
930
933
931
// Add this function to the set of replaced functions.
934
932
summarized.insert (target_function);
935
- return false ;
936
933
}
937
934
938
935
void code_contractst::apply_loop_contract (
@@ -1225,17 +1222,14 @@ goto_functionst &code_contractst::get_goto_functions()
1225
1222
return goto_functions;
1226
1223
}
1227
1224
1228
- bool code_contractst::check_frame_conditions_function (const irep_idt &function)
1225
+ void code_contractst::check_frame_conditions_function (const irep_idt &function)
1229
1226
{
1230
1227
// Get the function object before instrumentation.
1231
1228
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
- }
1229
+
1230
+ INVARIANT (
1231
+ function_obj != goto_functions.function_map .end (),
1232
+ " Function '" + id2string (function) + " 'must exist in the goto program" );
1239
1233
1240
1234
const auto &goto_function = function_obj->second ;
1241
1235
auto &function_body = function_obj->second .body ;
@@ -1316,11 +1310,9 @@ bool code_contractst::check_frame_conditions_function(const irep_idt &function)
1316
1310
function_body.instructions .end (),
1317
1311
skip_function_paramst::YES,
1318
1312
cfg_info_opt);
1319
-
1320
- return false ;
1321
1313
}
1322
1314
1323
- bool code_contractst::enforce_contract (const irep_idt &function)
1315
+ void code_contractst::enforce_contract (const irep_idt &function)
1324
1316
{
1325
1317
// Add statements to the source function
1326
1318
// to ensure assigns clause is respected.
@@ -1333,13 +1325,9 @@ bool code_contractst::enforce_contract(const irep_idt &function)
1333
1325
const irep_idt original (function);
1334
1326
1335
1327
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
- }
1328
+ INVARIANT (
1329
+ old_function != goto_functions.function_map .end (),
1330
+ " Function to replace must exist in the program." );
1343
1331
1344
1332
std::swap (goto_functions.function_map [mangled], old_function->second );
1345
1333
goto_functions.function_map .erase (old_function);
@@ -1379,8 +1367,6 @@ bool code_contractst::enforce_contract(const irep_idt &function)
1379
1367
wrapper.parameter_identifiers = mangled_fun->second .parameter_identifiers ;
1380
1368
wrapper.body .add (goto_programt::make_end_function (sl));
1381
1369
add_contract_check (original, mangled, wrapper.body );
1382
-
1383
- return false ;
1384
1370
}
1385
1371
1386
1372
void code_contractst::add_contract_check (
@@ -1536,12 +1522,29 @@ void code_contractst::add_contract_check(
1536
1522
dest.destructive_insert (dest.instructions .begin (), check);
1537
1523
}
1538
1524
1539
- bool code_contractst::replace_calls (const std::set<std::string> &to_replace)
1525
+ void code_contractst::check_all_functions_found (
1526
+ const std::set<std::string> &functions) const
1527
+ {
1528
+ for (const auto &function : functions)
1529
+ {
1530
+ if (
1531
+ goto_functions.function_map .find (function) ==
1532
+ goto_functions.function_map .end ())
1533
+ {
1534
+ throw invalid_input_exceptiont (
1535
+ " Function '" + function + " ' was not found in the GOTO program." );
1536
+ }
1537
+ }
1538
+ }
1539
+
1540
+ void code_contractst::replace_calls (const std::set<std::string> &to_replace)
1540
1541
{
1541
1542
if (to_replace.empty ())
1542
- return false ;
1543
+ return ;
1543
1544
1544
- bool fail = false ;
1545
+ log .status () << " Replacing function calls with contracts" << messaget::eom;
1546
+
1547
+ check_all_functions_found (to_replace);
1545
1548
1546
1549
for (auto &goto_function : goto_functions.function_map )
1547
1550
{
@@ -1559,7 +1562,7 @@ bool code_contractst::replace_calls(const std::set<std::string> &to_replace)
1559
1562
if (found == to_replace.end ())
1560
1563
continue ;
1561
1564
1562
- fail |= apply_function_contract (
1565
+ apply_function_contract (
1563
1566
goto_function.first ,
1564
1567
ins->source_location (),
1565
1568
goto_function.second .body ,
@@ -1568,15 +1571,10 @@ bool code_contractst::replace_calls(const std::set<std::string> &to_replace)
1568
1571
}
1569
1572
}
1570
1573
1571
- if (fail)
1572
- return true ;
1573
-
1574
1574
for (auto &goto_function : goto_functions.function_map )
1575
1575
remove_skip (goto_function.second .body );
1576
1576
1577
1577
goto_functions.update ();
1578
-
1579
- return false ;
1580
1578
}
1581
1579
1582
1580
void code_contractst::apply_loop_contracts ()
@@ -1585,27 +1583,15 @@ void code_contractst::apply_loop_contracts()
1585
1583
apply_loop_contract (goto_function.first , goto_function.second );
1586
1584
}
1587
1585
1588
- bool code_contractst::enforce_contracts (const std::set<std::string> &to_enforce)
1586
+ void code_contractst::enforce_contracts (const std::set<std::string> &to_enforce)
1589
1587
{
1590
1588
if (to_enforce.empty ())
1591
- return false ;
1589
+ return ;
1592
1590
1593
- bool fail = false ;
1591
+ log . status () << " Enforcing contracts " << messaget ::eom ;
1594
1592
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
- }
1593
+ check_all_functions_found (to_enforce);
1606
1594
1607
- if (!fail)
1608
- fail = enforce_contract (function);
1609
- }
1610
- return fail;
1595
+ for (const auto &function : to_enforce)
1596
+ enforce_contract (function);
1611
1597
}
0 commit comments