@@ -11,12 +11,7 @@ Date: February 2016
11
11
// / \file
12
12
// / Verify and use annotated invariants and pre/post-conditions
13
13
14
- #include " code_contracts.h"
15
- #include " loop_utils.h"
16
-
17
14
#include < algorithm>
18
- #include < cstring>
19
- #include < iostream>
20
15
#include < unordered_set>
21
16
22
17
#include < analyses/local_may_alias.h>
@@ -32,6 +27,9 @@ Date: February 2016
32
27
#include < util/pointer_predicates.h>
33
28
#include < util/replace_symbol.h>
34
29
30
+ #include " code_contracts.h"
31
+ #include " loop_utils.h"
32
+
35
33
// / Predicate to be used with the exprt::visit() function. The function
36
34
// / found_return_value() will return `true` iff this predicate is called on an
37
35
// / expr that contains `__CPROVER_return_value`.
@@ -70,12 +68,15 @@ static void check_apply_invariants(
70
68
PRECONDITION (!loop.empty ());
71
69
72
70
// find the last back edge
73
- goto_programt::targett loop_end = loop_head;
74
- for (loopt::const_iterator it = loop.begin (); it != loop.end (); ++it)
75
- if (
76
- (*it)->is_goto () && (*it)->get_target () == loop_head &&
77
- (*it)->location_number > loop_end->location_number )
78
- loop_end = *it;
71
+ goto_programt::targett loop_end=loop_head;
72
+ for (loopt::const_iterator
73
+ it=loop.begin ();
74
+ it!=loop.end ();
75
+ ++it)
76
+ if ((*it)->is_goto () &&
77
+ (*it)->get_target ()==loop_head &&
78
+ (*it)->location_number >loop_end->location_number )
79
+ loop_end=*it;
79
80
80
81
// see whether we have an invariant
81
82
exprt invariant = static_cast <const exprt &>(
@@ -138,7 +139,7 @@ static void check_apply_invariants(
138
139
139
140
// change the back edge into assume(false) or assume(guard)
140
141
loop_end->targets .clear ();
141
- loop_end->type = ASSUME;
142
+ loop_end->type = ASSUME;
142
143
if (loop_head->is_goto ())
143
144
loop_end->set_condition (false_exprt ());
144
145
else
@@ -149,10 +150,11 @@ bool code_contractst::has_contract(const irep_idt fun_name)
149
150
{
150
151
const symbolt &function_symbol = ns.lookup (fun_name);
151
152
const code_typet &type = to_code_type (function_symbol.type );
152
- const irept assigns = type.find (ID_C_spec_assigns);
153
- const irept ensures = type. find (ID_C_spec_ensures) ;
153
+ if ( type.find (ID_C_spec_assigns). is_not_nil ())
154
+ return true ;
154
155
155
- return ensures.is_not_nil () || assigns.is_not_nil ();
156
+ return type.find (ID_C_spec_requires).is_not_nil () ||
157
+ type.find (ID_C_spec_ensures).is_not_nil ();
156
158
}
157
159
158
160
bool code_contractst::apply_contract (
@@ -223,7 +225,7 @@ bool code_contractst::apply_contract(
223
225
}
224
226
225
227
// Replace formal parameters
226
- code_function_callt::argumentst::const_iterator a_it =
228
+ code_function_callt::argumentst::const_iterator a_it=
227
229
call.arguments ().begin ();
228
230
for (code_typet::parameterst::const_iterator p_it = type.parameters ().begin ();
229
231
p_it != type.parameters ().end () && a_it != call.arguments ().end ();
@@ -260,11 +262,7 @@ bool code_contractst::apply_contract(
260
262
const exprt::operandst &targets = assigns.operands ();
261
263
for (const exprt &curr_op : targets)
262
264
{
263
- if (curr_op.id () == ID_symbol)
264
- {
265
- assigns_tgts.insert (curr_op);
266
- }
267
- else if (curr_op.id () == ID_dereference)
265
+ if (curr_op.id () == ID_symbol || curr_op.id () == ID_dereference)
268
266
{
269
267
assigns_tgts.insert (curr_op);
270
268
}
@@ -335,7 +333,7 @@ const symbolt &code_contractst::new_tmp_symbol(
335
333
symbol_table);
336
334
}
337
335
338
- exprt create_alias_expression (
336
+ static exprt create_alias_expression (
339
337
const exprt &assigns,
340
338
const exprt &lhs,
341
339
std::vector<exprt> &aliasable_references)
@@ -344,9 +342,9 @@ exprt create_alias_expression(
344
342
exprt running = false_exprt ();
345
343
for (auto aliasable : aliasable_references)
346
344
{
347
- exprt leftPtr = address_of_exprt{lhs};
348
- exprt rightPtr = aliasable;
349
- exprt same = same_object (leftPtr, rightPtr );
345
+ exprt left_ptr = address_of_exprt{lhs};
346
+ exprt right_ptr = aliasable;
347
+ exprt same = same_object (left_ptr, right_ptr );
350
348
351
349
if (first_iter)
352
350
{
@@ -372,13 +370,12 @@ void code_contractst::populate_assigns_references(
372
370
std::vector<exprt> &created_references)
373
371
{
374
372
const code_typet &type = to_code_type (function_symbol.type );
375
- exprt assigns = static_cast <const exprt &>(type.find (ID_C_spec_assigns));
373
+ const exprt &assigns =
374
+ static_cast <const exprt &>(type.find (ID_C_spec_assigns));
376
375
377
- exprt::operandst &targets = assigns.operands ();
378
- for (exprt curr_op : targets)
376
+ const exprt::operandst &targets = assigns.operands ();
377
+ for (const exprt & curr_op : targets)
379
378
{
380
- exprt op_addr = address_of_exprt{curr_op};
381
-
382
379
// Declare a new symbol to stand in for the reference
383
380
symbol_exprt standin = new_tmp_symbol (
384
381
pointer_type (curr_op.type ()),
@@ -391,14 +388,15 @@ void code_contractst::populate_assigns_references(
391
388
goto_programt::make_decl (standin, function_symbol.location ));
392
389
393
390
created_decls.add (goto_programt::make_assignment (
394
- code_assignt (standin, std::move (op_addr)), function_symbol.location ));
391
+ code_assignt (standin, std::move (address_of_exprt{curr_op})),
392
+ function_symbol.location ));
395
393
396
394
// Add a map entry from the original operand to the new symbol
397
395
created_references.push_back (standin);
398
396
}
399
397
}
400
398
401
- void code_contractst::instrument_assn_statement (
399
+ void code_contractst::instrument_assigns_statement (
402
400
goto_programt::instructionst::iterator &instruction_iterator,
403
401
goto_programt &program,
404
402
exprt &assigns,
@@ -407,7 +405,7 @@ void code_contractst::instrument_assn_statement(
407
405
{
408
406
INVARIANT (
409
407
instruction_iterator->is_assign (),
410
- " The first argument of instrument_assn_statement should always be"
408
+ " The first argument of instrument_assigns_statement should always be"
411
409
" an assignment" );
412
410
const exprt &lhs = instruction_iterator->get_assign ().lhs ();
413
411
if (freely_assignable_exprs.find (lhs) != freely_assignable_exprs.end ())
@@ -569,7 +567,7 @@ bool code_contractst::check_for_looped_mallocs(const goto_programt &program)
569
567
const irep_idt &called_name =
570
568
to_symbol_expr (call.function ()).get_identifier ();
571
569
572
- if (std::strcmp ( called_name. c_str (), " malloc " ) == 0 )
570
+ if (called_name == " malloc " )
573
571
{
574
572
malloc_calls.push_back (instruction);
575
573
}
@@ -666,15 +664,15 @@ bool code_contractst::add_pointer_checks(const std::string &function_name)
666
664
667
665
// Insert aliasing assertions
668
666
for (; instruction_iterator != program.instructions .end ();
669
- std::advance ( instruction_iterator, 1 ) )
667
+ ++ instruction_iterator)
670
668
{
671
669
if (instruction_iterator->is_decl ())
672
670
{
673
671
freely_assignable_exprs.insert (instruction_iterator->get_decl ().symbol ());
674
672
}
675
673
else if (instruction_iterator->is_assign ())
676
674
{
677
- instrument_assn_statement (
675
+ instrument_assigns_statement (
678
676
instruction_iterator,
679
677
program,
680
678
assigns,
@@ -779,7 +777,7 @@ void code_contractst::add_contract_check(
779
777
static_cast <const exprt &>(gf.type .find (ID_C_spec_ensures));
780
778
INVARIANT (
781
779
ensures.is_not_nil () || assigns.is_not_nil (),
782
- " Code conract enforcement is trivial without an ensures or assigns "
780
+ " Code contract enforcement is trivial without an ensures or assigns "
783
781
" clause." );
784
782
785
783
// build:
@@ -810,7 +808,7 @@ void code_contractst::add_contract_check(
810
808
replace_symbolt replace;
811
809
812
810
// decl ret
813
- if (gf.type .return_type () != empty_typet ())
811
+ if (gf.type .return_type ()!= empty_typet ())
814
812
{
815
813
symbol_exprt r = new_tmp_symbol (
816
814
gf.type .return_type (),
@@ -820,7 +818,7 @@ void code_contractst::add_contract_check(
820
818
.symbol_expr ();
821
819
check.add (goto_programt::make_decl (r, skip->source_location ));
822
820
823
- call.lhs () = r;
821
+ call.lhs ()= r;
824
822
825
823
symbol_exprt ret_val (CPROVER_PREFIX " return_value" , call.lhs ().type ());
826
824
replace.insert (ret_val, r);
0 commit comments