From 88b44ec5fb2ef9a25518be0260156c8a57e14f09 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Fri, 19 May 2017 12:54:47 +0200 Subject: [PATCH] fix definition of __SCHAR_MAX__ & friends The predefined macros __SCHAR_MAX__, __SHRT_MAX__, __WCHAR_MAX__ & __CHAR_BIT__ are defined based on the size of these types on the machine where sparse was compiled. But since sparse has flags like -m32/-m64 and it's not unlikely to have one day a flag like -march=, better to use the used by sparse for these types as stored in 'bits_in_char', 'bits_in_short', ... Signed-off-by: Luc Van Oostenryck --- lib.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib.c b/lib.c index 20eb72e9..99ecdcc4 100644 --- a/lib.c +++ b/lib.c @@ -832,11 +832,16 @@ static void predefined_sizeof(const char *name, unsigned bits) add_pre_buffer("#weak_define __SIZEOF_%s__ %d\n", name, bits/8); } -static void predefined_type_size(const char *name, const char *suffix, unsigned bits) +static void predefined_max(const char *name, const char *suffix, unsigned bits) { unsigned long long max = (1ULL << (bits - 1 )) - 1; add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name, max, suffix); +} + +static void predefined_type_size(const char *name, const char *suffix, unsigned bits) +{ + predefined_max(name, suffix, bits); predefined_sizeof(name, bits); } @@ -845,6 +850,10 @@ static void predefined_macros(void) add_pre_buffer("#define __CHECKER__ 1\n"); predefined_sizeof("SHORT", bits_in_short); + predefined_max("SHRT", "", bits_in_short); + predefined_max("SCHAR", "", bits_in_char); + predefined_max("WCHAR", "", bits_in_wchar); + add_pre_buffer("#weak_define __CHAR_BIT__ %d\n", bits_in_char); predefined_type_size("INT", "", bits_in_int); predefined_type_size("LONG", "L", bits_in_long); @@ -1068,12 +1077,6 @@ void create_builtin_stream(void) add_pre_buffer("#define __OPTIMIZE__ 1\n"); if (optimize_size) add_pre_buffer("#define __OPTIMIZE_SIZE__ 1\n"); - - /* GCC defines these for limits.h */ - add_pre_buffer("#weak_define __SHRT_MAX__ " STRINGIFY(__SHRT_MAX__) "\n"); - add_pre_buffer("#weak_define __SCHAR_MAX__ " STRINGIFY(__SCHAR_MAX__) "\n"); - add_pre_buffer("#weak_define __WCHAR_MAX__ " STRINGIFY(__WCHAR_MAX__) "\n"); - add_pre_buffer("#weak_define __CHAR_BIT__ " STRINGIFY(__CHAR_BIT__) "\n"); } static struct symbol_list *sparse_tokenstream(struct token *token) -- 2.11.4.GIT