|
| 1 | +// Copyright 2018 Author: Malte Mues <[email protected]> |
| 2 | + |
| 3 | +/// \file |
| 4 | +/// This file contains functions, that should support test for underlying |
| 5 | +/// c types, in cases where this is required for anlysis purpose. |
| 6 | +#ifndef CPROVER_UTIL_C_TYPES_UTIL_H |
| 7 | +#define CPROVER_UTIL_C_TYPES_UTIL_H |
| 8 | + |
| 9 | +#include "arith_tools.h" |
| 10 | +#include "invariant.h" |
| 11 | +#include "std_types.h" |
| 12 | +#include "type.h" |
| 13 | + |
| 14 | +#include <algorithm> |
| 15 | +#include <string> |
| 16 | + |
| 17 | +/// This function checks, whether this has been a char type in the c program. |
| 18 | +inline bool is_c_char(const typet &type) |
| 19 | +{ |
| 20 | + const irep_idt &c_type = type.get(ID_C_c_type); |
| 21 | + return is_signed_or_unsigned_bitvector(type) && |
| 22 | + (c_type == ID_char || c_type == ID_unsigned_char || |
| 23 | + c_type == ID_signed_char); |
| 24 | +} |
| 25 | + |
| 26 | +/// This function checks, whether the type |
| 27 | +/// has been a bool type in the c program. |
| 28 | +inline bool is_c_bool(const typet &type) |
| 29 | +{ |
| 30 | + return type.id() == ID_c_bool; |
| 31 | +} |
| 32 | + |
| 33 | +/// This function checks, whether the type is has been some kind of integer |
| 34 | +/// type in the c program. |
| 35 | +/// It considers the signed and unsigned verison of |
| 36 | +/// int, short, long and long long as integer types in c. |
| 37 | +inline bool is_c_int_derivate(const typet &type) |
| 38 | +{ |
| 39 | + const irep_idt &c_type = type.get(ID_C_c_type); |
| 40 | + return is_signed_or_unsigned_bitvector(type) && |
| 41 | + (c_type == ID_signed_int || c_type == ID_unsigned_int || |
| 42 | + c_type == ID_signed_short_int || c_type == ID_unsigned_int || |
| 43 | + c_type == ID_unsigned_short_int || c_type == ID_signed_long_int || |
| 44 | + c_type == ID_signed_long_long_int || c_type == ID_unsigned_long_int || |
| 45 | + c_type == ID_unsigned_long_long_int); |
| 46 | +} |
| 47 | + |
| 48 | +/// This function checks, whether type is a pointer and the target type |
| 49 | +/// of the pointer has been a char type in the c program. |
| 50 | +inline bool is_c_char_pointer(const typet &type) |
| 51 | +{ |
| 52 | + return is_pointer(type) && is_c_char(type.subtype()); |
| 53 | +} |
| 54 | + |
| 55 | +/// This function checks, whether type is a pointer and the target type |
| 56 | +/// has been some kind of int type in the c program. |
| 57 | +/// is_c_int_derivate answers is used for checking the int type. |
| 58 | +inline bool is_c_int_derivate_pointer(const typet &type) |
| 59 | +{ |
| 60 | + return is_pointer(type) && is_c_int_derivate(type.subtype()); |
| 61 | +} |
| 62 | + |
| 63 | +/// This function checks, whether the type |
| 64 | +/// has been an enum type in the c program. |
| 65 | +inline bool is_c_enum(const typet &type) |
| 66 | +{ |
| 67 | + return type.id() == ID_c_enum; |
| 68 | +} |
| 69 | + |
| 70 | +/// This function creates a constant representing the |
| 71 | +/// bitvector encoded integer value of a string in the enum. |
| 72 | +/// \param member_name is a string that should be in the enum. |
| 73 | +/// \param c_enum the enum type memeber_name is supposed to be part of. |
| 74 | +/// \return value a constant, that could be assigned as value for an expression |
| 75 | +/// with type c_enum. |
| 76 | +constant_exprt convert_memeber_name_to_enum_value( |
| 77 | + const std::string &member_name, |
| 78 | + const c_enum_typet &c_enum) |
| 79 | +{ |
| 80 | + for(const auto &enum_value : c_enum.members()) |
| 81 | + { |
| 82 | + if(id2string(enum_value.get_identifier()) == member_name) |
| 83 | + { |
| 84 | + mp_integer int_value = string2integer(id2string(enum_value.get_value())); |
| 85 | + return from_integer(int_value, c_enum); |
| 86 | + } |
| 87 | + } |
| 88 | + INVARIANT(false, "member_name must be a valid value in the c_enum."); |
| 89 | +} |
| 90 | + |
| 91 | +/// This function creates a constant representing either 0 or 1 as value of |
| 92 | +/// type type. |
| 93 | +/// \param bool_value: A string that is compared to "true" ignoring case. |
| 94 | +/// \param type: The type, the resulting constant is supposed to have. |
| 95 | +/// \return a constant of type \param type with either 0 or 1 as value. |
| 96 | +constant_exprt from_c_boolean_value(std::string bool_value, const typet &type) |
| 97 | +{ |
| 98 | + std::transform( |
| 99 | + bool_value.begin(), bool_value.end(), bool_value.begin(), ::tolower); |
| 100 | + if(bool_value == "true") |
| 101 | + { |
| 102 | + return from_integer(mp_integer(1), type); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + return from_integer(mp_integer(0), type); |
| 107 | + } |
| 108 | +} |
| 109 | +#endif // CPROVER_UTIL_C_TYPES_UTIL_H |
0 commit comments