Skip to content

Commit 998a0ef

Browse files
committed
Adds source location to function contracts
Function contracts inject assertions into the goto program to map pre- and postconditions. These injected assertions must include original source-code location and meaningful comments for usability. Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
1 parent b524ce7 commit 998a0ef

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/goto-instrument/code_contracts.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ bool code_contractst::apply_function_contract(
441441
code_assertt(requires),
442442
assertion,
443443
symbol_table.lookup_ref(function).mode);
444+
assertion.instructions.front().source_location = requires.source_location();
445+
assertion.instructions.back().source_location.set_comment(
446+
"Check requires clause");
447+
assertion.instructions.front().source_location.set_property_class(
448+
ID_precondition);
444449
auto lines_to_iterate = assertion.instructions.size();
445450
goto_program.insert_before_swap(target, assertion);
446451
std::advance(target, lines_to_iterate);
@@ -563,7 +568,8 @@ void code_contractst::instrument_assign_statement(
563568
alias_assertion.add(goto_programt::make_assertion(
564569
assigns_clause.alias_expression(lhs),
565570
instruction_iterator->source_location));
566-
571+
alias_assertion.instructions.back().source_location.set_comment(
572+
"Check that " + from_expr(ns, lhs.id(), lhs) + " is assignable");
567573
int lines_to_iterate = alias_assertion.instructions.size();
568574
program.insert_before_swap(instruction_iterator, alias_assertion);
569575
std::advance(instruction_iterator, lines_to_iterate);
@@ -626,6 +632,9 @@ void code_contractst::instrument_call_statement(
626632
goto_programt alias_assertion;
627633
alias_assertion.add(goto_programt::make_assertion(
628634
alias_expr, instruction_iterator->source_location));
635+
alias_assertion.instructions.back().source_location.set_comment(
636+
"Check that " + from_expr(ns, alias_expr.id(), alias_expr) +
637+
" is assignable");
629638
program.insert_before_swap(instruction_iterator, alias_assertion);
630639
++instruction_iterator;
631640
}
@@ -661,6 +670,8 @@ void code_contractst::instrument_call_statement(
661670
goto_programt alias_assertion;
662671
alias_assertion.add(goto_programt::make_assertion(
663672
compatible, instruction_iterator->source_location));
673+
alias_assertion.instructions.back().source_location.set_comment(
674+
"Check compatibility of assigns clause with the called function");
664675
program.insert_before_swap(instruction_iterator, alias_assertion);
665676
++instruction_iterator;
666677
}
@@ -1003,8 +1014,13 @@ void code_contractst::add_contract_check(
10031014

10041015
// get all the relevant instructions related to history variables
10051016
auto assertion = code_assertt(ensures);
1017+
assertion.add_source_location() = ensures.source_location();
10061018
ensures_pair = create_ensures_instruction(
10071019
assertion, ensures.source_location(), wrapper_fun, function_symbol.mode);
1020+
ensures_pair.first.instructions.back().source_location.set_comment(
1021+
"Check ensures clause");
1022+
ensures_pair.first.instructions.front().source_location.set_property_class(
1023+
ID_postcondition);
10081024

10091025
// add all the history variable initializations
10101026
check.destructive_append(ensures_pair.second);

src/goto-instrument/code_contracts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Date: February 2016
2323
#include <goto-programs/goto_functions.h>
2424
#include <goto-programs/goto_model.h>
2525

26+
#include <langapi/language_util.h>
27+
2628
#include <util/message.h>
2729
#include <util/namespace.h>
2830
#include <util/pointer_expr.h>

0 commit comments

Comments
 (0)