|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit test utilities |
| 4 | +
|
| 5 | + Author: DiffBlue Limited. All rights reserved. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "require_goto_statements.h" |
| 10 | +#include "catch.hpp" |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <util/expr_iterator.h> |
| 14 | + |
| 15 | +/// Expand value of a function to include all child codets |
| 16 | +/// \param function_value: The value of the function (e.g. got by looking up |
| 17 | +/// the function in the symbol table and getting the value) |
| 18 | +/// \return: All ID_code statements in the tree rooted at \p function_value |
| 19 | +std::vector<codet> |
| 20 | +require_goto_statements::get_all_statements(const exprt &function_value) |
| 21 | +{ |
| 22 | + std::vector<codet> statements; |
| 23 | + for(auto sub_expression_it = function_value.depth_begin(); |
| 24 | + sub_expression_it != function_value.depth_end(); |
| 25 | + ++sub_expression_it) |
| 26 | + { |
| 27 | + if(sub_expression_it->id() == ID_code) |
| 28 | + { |
| 29 | + statements.push_back(to_code(*sub_expression_it)); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return statements; |
| 34 | +} |
| 35 | + |
| 36 | +/// Find assignment statements of the form \p structure_name.\component_name = |
| 37 | +/// \param statements: The statements to look through |
| 38 | +/// \param structure_name: The name of variable of type struct |
| 39 | +/// \param component_name: The name of the component that should be assigned |
| 40 | +/// \return: All the assignments to that component. |
| 41 | +std::vector<code_assignt> |
| 42 | +require_goto_statements::find_struct_component_assignments( |
| 43 | + const std::vector<codet> &statements, |
| 44 | + const irep_idt &structure_name, |
| 45 | + const irep_idt &component_name) |
| 46 | +{ |
| 47 | + std::vector<code_assignt> component_assignments; |
| 48 | + |
| 49 | + for(const auto &assignment : statements) |
| 50 | + { |
| 51 | + if(assignment.get_statement() == ID_assign) |
| 52 | + { |
| 53 | + const code_assignt &code_assign = to_code_assign(assignment); |
| 54 | + |
| 55 | + if(code_assign.lhs().id() == ID_member) |
| 56 | + { |
| 57 | + const auto &member_expr = to_member_expr(code_assign.lhs()); |
| 58 | + const auto &symbol = member_expr.symbol(); |
| 59 | + |
| 60 | + if( |
| 61 | + symbol.get_identifier() == structure_name && |
| 62 | + member_expr.get_component_name() == component_name) |
| 63 | + { |
| 64 | + component_assignments.push_back(code_assign); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return component_assignments; |
| 70 | +} |
| 71 | + |
| 72 | +/// For a given variable name, gets the assignments to it in the provided |
| 73 | +/// instructions. |
| 74 | +/// \param pointer_name: The name of the variable |
| 75 | +/// \param instructions: The instructions to look through |
| 76 | +/// \return: A structure that contains the null assignment if found, and a |
| 77 | +/// vector of all other assignments |
| 78 | +require_goto_statements::pointer_assignment_locationt |
| 79 | +require_goto_statements::find_pointer_assignments( |
| 80 | + const irep_idt &pointer_name, |
| 81 | + const std::vector<codet> &instructions) |
| 82 | +{ |
| 83 | + pointer_assignment_locationt locations; |
| 84 | + for(const codet &statement : instructions) |
| 85 | + { |
| 86 | + if(statement.get_statement() == ID_assign) |
| 87 | + { |
| 88 | + const code_assignt &code_assign = to_code_assign(statement); |
| 89 | + if( |
| 90 | + code_assign.lhs().id() == ID_symbol && |
| 91 | + to_symbol_expr(code_assign.lhs()).get_identifier() == pointer_name) |
| 92 | + { |
| 93 | + if( |
| 94 | + code_assign.rhs() == |
| 95 | + null_pointer_exprt(to_pointer_type(code_assign.lhs().type()))) |
| 96 | + { |
| 97 | + locations.null_assignment = code_assign; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + locations.non_null_assignments.push_back(code_assign); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return locations; |
| 108 | +} |
| 109 | + |
| 110 | +/// Find the declaration of the specific variable. |
| 111 | +/// \param variable_name: The name of the variable. |
| 112 | +/// \param entry_point_instructions: The statements to look through |
| 113 | +/// \return The declaration statement corresponding to that variable |
| 114 | +/// \throws no_decl_found_exceptiont if no declaration of the specific |
| 115 | +/// variable is found |
| 116 | +const code_declt &require_goto_statements::require_declaration_of_name( |
| 117 | + const irep_idt &variable_name, |
| 118 | + const std::vector<codet> &entry_point_instructions) |
| 119 | +{ |
| 120 | + for(const auto &statement : entry_point_instructions) |
| 121 | + { |
| 122 | + if(statement.get_statement() == ID_decl) |
| 123 | + { |
| 124 | + const auto &decl_statement = to_code_decl(statement); |
| 125 | + |
| 126 | + if(decl_statement.get_identifier() == variable_name) |
| 127 | + { |
| 128 | + return decl_statement; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + throw no_decl_found_exceptiont(variable_name.c_str()); |
| 133 | +} |
0 commit comments