Skip to content

Commit 2fddd15

Browse files
petr-bauchdanpoe
authored andcommitted
Improve type utility functions
Better names and small code refactoring.
1 parent af27d1f commit 2fddd15

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

src/ansi-c/c_typecast.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,15 @@ bool check_c_implicit_typecast(
213213
const irept &src_subtype =src_type.subtype();
214214

215215
if(src_subtype == dest_subtype)
216+
{
216217
return false;
218+
}
217219
else if(
218220
has_a_void_pointer(src_type) || // from void to anything
219221
has_a_void_pointer(dest_type)) // to void from anything
222+
{
220223
return false;
224+
}
221225
}
222226

223227
if(dest_type.id()==ID_array &&

src/util/c_types_util.h

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
// Copyright 2018 Author: Malte Mues <[email protected]>
1+
/*******************************************************************\
2+
3+
Module: API to expression classes
4+
5+
Author: Malte Mues <[email protected]>
6+
7+
\*******************************************************************/
28

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.
69
#ifndef CPROVER_UTIL_C_TYPES_UTIL_H
710
#define CPROVER_UTIL_C_TYPES_UTIL_H
811

12+
/// \file
13+
/// This file contains functions, that should support test for underlying
14+
/// c types, in cases where this is required for analysis purpose.
15+
916
#include "arith_tools.h"
1017
#include "invariant.h"
1118
#include "std_types.h"
@@ -15,7 +22,7 @@
1522
#include <string>
1623

1724
/// This function checks, whether this has been a char type in the c program.
18-
inline bool is_c_char(const typet &type)
25+
inline bool is_c_char_type(const typet &type)
1926
{
2027
const irep_idt &c_type = type.get(ID_C_c_type);
2128
return is_signed_or_unsigned_bitvector(type) &&
@@ -25,16 +32,16 @@ inline bool is_c_char(const typet &type)
2532

2633
/// This function checks, whether the type
2734
/// has been a bool type in the c program.
28-
inline bool is_c_bool(const typet &type)
35+
inline bool is_c_bool_type(const typet &type)
2936
{
3037
return type.id() == ID_c_bool;
3138
}
3239

33-
/// This function checks, whether the type is has been some kind of integer
40+
/// This function checks, whether the type has been some kind of integer
3441
/// type in the c program.
3542
/// It considers the signed and unsigned verison of
3643
/// int, short, long and long long as integer types in c.
37-
inline bool is_c_int_derivate(const typet &type)
44+
inline bool is_c_integral_type(const typet &type)
3845
{
3946
const irep_idt &c_type = type.get(ID_C_c_type);
4047
return is_signed_or_unsigned_bitvector(type) &&
@@ -47,63 +54,72 @@ inline bool is_c_int_derivate(const typet &type)
4754

4855
/// This function checks, whether type is a pointer and the target type
4956
/// of the pointer has been a char type in the c program.
50-
inline bool is_c_char_pointer(const typet &type)
57+
inline bool is_c_char_pointer_type(const typet &type)
5158
{
52-
return type.id() == ID_pointer && is_c_char(type.subtype());
59+
return type.id() == ID_pointer && is_c_char_type(type.subtype());
5360
}
5461

5562
/// This function checks, whether type is a pointer and the target type
5663
/// has been some kind of int type in the c program.
5764
/// is_c_int_derivate answers is used for checking the int type.
58-
inline bool is_c_int_derivate_pointer(const typet &type)
65+
inline bool is_c_integral_pointer_type(const typet &type)
5966
{
60-
return type.id() == ID_pointer && is_c_int_derivate(type.subtype());
67+
return type.id() == ID_pointer && is_c_integral_type(type.subtype());
6168
}
6269

6370
/// This function checks, whether the type
6471
/// has been an enum type in the c program.
65-
inline bool is_c_enum(const typet &type)
72+
inline bool is_c_enum_type(const typet &type)
6673
{
6774
return type.id() == ID_c_enum;
6875
}
6976

7077
/// This function creates a constant representing the
7178
/// bitvector encoded integer value of a string in the enum.
7279
/// \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.
80+
/// \param c_enum the enum type \p member_name is supposed to be part of.
81+
/// \return constant, that could be assigned as the value of an expression with
82+
/// type c_enum.
7683
constant_exprt convert_member_name_to_enum_value(
77-
const std::string &member_name,
84+
const irep_idt &member_name,
7885
const c_enum_typet &c_enum)
7986
{
8087
for(const auto &enum_value : c_enum.members())
8188
{
82-
if(id2string(enum_value.get_identifier()) == member_name)
89+
if(enum_value.get_identifier() == member_name)
8390
{
84-
mp_integer int_value = string2integer(id2string(enum_value.get_value()));
85-
return from_integer(int_value, c_enum);
91+
auto maybe_int_value = numeric_cast<mp_integer>(
92+
constant_exprt{enum_value.get_value(), typet{ID_bv}});
93+
CHECK_RETURN(maybe_int_value.has_value());
94+
return from_integer(*maybe_int_value, c_enum);
8695
}
8796
}
8897
INVARIANT(false, "member_name must be a valid value in the c_enum.");
8998
}
9099

100+
/// Convert id to a Boolean value
101+
/// \param bool_value: A string that is compared to "true" ignoring case.
102+
/// \return a constant of type Boolean
103+
bool id2boolean(const irep_idt &bool_value)
104+
{
105+
std::string string_value = id2string(bool_value);
106+
std::transform(
107+
string_value.begin(), string_value.end(), string_value.begin(), ::tolower);
108+
if(string_value == "true")
109+
return true;
110+
if(string_value == "false")
111+
return false;
112+
UNREACHABLE;
113+
}
114+
91115
/// This function creates a constant representing either 0 or 1 as value of
92116
/// type type.
93-
/// \param bool_value: A string that is compared to "true" ignoring case.
117+
/// \param bool_value: A Boolean value.
94118
/// \param type: The type, the resulting constant is supposed to have.
95119
/// \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)
120+
constant_exprt from_c_boolean_value(bool bool_value, const typet &type)
97121
{
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-
}
122+
return bool_value ? from_integer(mp_integer(1), type)
123+
: from_integer(mp_integer(0), type);
108124
}
109125
#endif // CPROVER_UTIL_C_TYPES_UTIL_H

src/util/std_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class namespacet;
2929
/// if the given typet is a constant
3030
inline bool is_constant(const typet &type)
3131
{
32-
return type.id() == ID_constant;
32+
return type.get_bool(ID_C_constant);
3333
}
3434

3535
/// The Boolean type

0 commit comments

Comments
 (0)