This allows to have a single function to output the size, the type, the maximal value, ... --- lib.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/lib.c b/lib.c index db33e58b2..1156f6f6c 100644 --- a/lib.c +++ b/lib.c @@ -1144,6 +1144,15 @@ static char **handle_switch(char *arg, char **next) return next; } +#define PTYPE_SIZEOF (1U << 0) +#define PTYPE_T (1U << 1) +#define PTYPE_SIZEOF_T (PTYPE_SIZEOF|PTYPE_T) +#define PTYPE_MAX (1U << 2) +#define PTYPE_MIN (1U << 3) +#define PTYPE_TYPE (1U << 4) +#define PTYPE_WIDTH (1U << 4) +#define PTYPE_ALL (PTYPE_MAX|PTYPE_SIZEOF|PTYPE_WIDTH) + static void predefined_sizeof(const char *name, const char *suffix, unsigned bits) { char buf[32]; @@ -1169,19 +1178,30 @@ static void predefined_max(const char *name, const char *suffix, unsigned bits) predefine(buf, 1, "%#llx%s", max, suffix); } -static void predefined_type_size(const char *name, const char *suffix, unsigned bits) -{ - predefined_max(name, suffix, bits); - predefined_sizeof(name, "", bits); - predefined_width(name, bits); -} - static void predefined_type(const char *name, struct symbol *type) { const char *typename = builtin_typename(type); add_pre_buffer("#weak_define __%s_TYPE__ %s\n", name, typename); } +static void predefined_ctype(const char *name, struct symbol *type, int flags) +{ + unsigned bits = type->bit_size; + + if (flags & PTYPE_SIZEOF) { + const char *suffix = (flags & PTYPE_T) ? "_T" : ""; + predefined_sizeof(name, suffix, bits); + } + if (flags & PTYPE_MAX) { + const char *suffix = builtin_type_suffix(type); + predefined_max(name, suffix, bits); + } + if (flags & PTYPE_TYPE) + predefined_type(name, type); + if (flags & PTYPE_WIDTH) + predefined_width(name, bits); +} + static void predefined_macros(void) { predefine("__CHECKER__", 0, "1"); @@ -1223,26 +1243,23 @@ static void predefined_macros(void) break; } - predefined_sizeof("SHORT", "", bits_in_short); - predefined_max("SHRT", "", bits_in_short); - predefined_width("SHRT", bits_in_short); - predefined_max("SCHAR", "", bits_in_char); - predefined_width("SCHAR", bits_in_char); + predefined_ctype("SHORT", &short_ctype, PTYPE_SIZEOF); + predefined_ctype("SHRT", &short_ctype, PTYPE_MAX|PTYPE_WIDTH); + predefined_ctype("SCHAR", &char_ctype, PTYPE_MAX|PTYPE_WIDTH); predefined_sizeof("WCHAR", "_T", bits_in_wchar); predefined_max("WCHAR", "", bits_in_wchar); predefined_width("WCHAR", bits_in_wchar); + predefine("__CHAR_BIT__", 1, "%d", bits_in_char); - predefined_type_size("INT", "", bits_in_int); - predefined_type_size("LONG", "L", bits_in_long); - predefined_type_size("LONG_LONG", "LL", bits_in_longlong); + predefined_ctype("INT", &int_ctype, PTYPE_ALL); + predefined_ctype("LONG", &long_ctype, PTYPE_ALL); + predefined_ctype("LONG_LONG", &llong_ctype, PTYPE_ALL); predefined_sizeof("INT128", "", 128); - predefined_sizeof("SIZE", "_T", bits_in_pointer); - predefined_width( "SIZE", bits_in_pointer); - predefined_sizeof("PTRDIFF", "_T", bits_in_pointer); - predefined_width( "PTRDIFF", bits_in_pointer); + predefined_ctype("SIZE", size_t_ctype, PTYPE_ALL|PTYPE_T|PTYPE_TYPE); + predefined_ctype("PTRDIFF", &ptr_ctype, PTYPE_ALL|PTYPE_T); predefined_sizeof("POINTER", "", bits_in_pointer); predefined_sizeof("FLOAT", "", bits_in_float); @@ -1275,8 +1292,6 @@ static void create_builtin_stream(void) // Temporary hack add_pre_buffer("#define _Pragma(x)\n"); - predefined_type("SIZE", size_t_ctype); - /* add the multiarch include directories, if any */ if (multiarch_dir && *multiarch_dir) { add_pre_buffer("#add_system \"/usr/include/%s\"\n", multiarch_dir); -- 2.19.0