|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Insert assert(false) at end of entry function |
| 4 | +
|
| 5 | +Author: Nathan Chong, Kareem Khazem |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +/// \file |
| 10 | +/// Insert final assert false into a function |
| 11 | + |
| 12 | +#include "insert_final_assert_false.h" |
| 13 | + |
| 14 | +#include <goto-programs/goto_model.h> |
| 15 | +#include <util/irep.h> |
| 16 | + |
| 17 | +#include <iterator> |
| 18 | +#include <list> |
| 19 | + |
| 20 | +insert_final_assert_falset::insert_final_assert_falset( |
| 21 | + message_handlert &_message_handler) |
| 22 | + : log(_message_handler) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +bool insert_final_assert_falset:: |
| 27 | +operator()(goto_modelt &goto_model, const std::string &function_to_instrument) |
| 28 | +{ |
| 29 | + const irep_idt entry(function_to_instrument); |
| 30 | + auto it = goto_model.goto_functions.function_map.find(entry); |
| 31 | + if(it == goto_model.goto_functions.function_map.end()) |
| 32 | + { |
| 33 | + log.error() << "insert-final-assert-false: could not find function " |
| 34 | + << "'" << function_to_instrument << "'" << messaget::eom; |
| 35 | + return true; |
| 36 | + } |
| 37 | + goto_programt &entry_function = (it->second).body; |
| 38 | + goto_programt::instructionst &instructions = entry_function.instructions; |
| 39 | + auto instr_it = instructions.end(); |
| 40 | + auto last_instruction = std::prev(instr_it); |
| 41 | + DATA_INVARIANT( |
| 42 | + last_instruction->is_end_function(), |
| 43 | + "last instruction in function should be END_FUNCTION"); |
| 44 | + source_locationt assert_location = last_instruction->source_location; |
| 45 | + assert_location.set_property_class(ID_assertion); |
| 46 | + assert_location.set_comment("insert-final-assert-false (should fail) "); |
| 47 | + goto_programt::instructiont false_assert = |
| 48 | + goto_programt::make_assertion(false_exprt(), assert_location); |
| 49 | + entry_function.insert_before_swap(last_instruction, false_assert); |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +bool insert_final_assert_false( |
| 54 | + goto_modelt &goto_model, |
| 55 | + const std::string &function_to_instrument, |
| 56 | + message_handlert &message_handler) |
| 57 | +{ |
| 58 | + insert_final_assert_falset ifaf(message_handler); |
| 59 | + return ifaf(goto_model, function_to_instrument); |
| 60 | +} |
0 commit comments