>From 9199bcd4edbe4127a08df58d8e24394fd039e10a Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 5 Oct 2019 18:56:52 -0700 Subject: [PATCH 2/2] Move the integer-type selection into c.m4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That way, glr.c can use it too. * data/skeletons/c.m4 (b4_int_type): Do not special-case ‘char’; it’s not worth the trouble, as clang complains about char subscripts. (b4_c99_int_type, b4_c99_int_type_define): New macros, taken from yacc.c. * data/skeletons/glr.c: Use b4_int_type_define. * data/skeletons/yacc.c (b4_int_type): Remove, since there’s no longer any need to redefine it. Use b4_c99_int_type_define rather than its body. --- data/skeletons/c.m4 | 77 +++++++++++++++++++++++++++++++++++++++++-- data/skeletons/glr.c | 2 ++ data/skeletons/yacc.c | 71 +-------------------------------------- 3 files changed, 78 insertions(+), 72 deletions(-) diff --git a/data/skeletons/c.m4 b/data/skeletons/c.m4 index 25b96a8f..851bc652 100644 --- a/data/skeletons/c.m4 +++ b/data/skeletons/c.m4 @@ -171,8 +171,7 @@ b4_parse_param_for([Decl], [Formal], [ YYUSE (Formal); # to MAX (included) in portable C code. Assume MIN and MAX fall in # 'int' range. m4_define([b4_int_type], -[m4_if(b4_ints_in($@, [0], [127]), [1], [char], - b4_ints_in($@, [-127], [127]), [1], [signed char], +[m4_if(b4_ints_in($@, [-127], [127]), [1], [signed char], b4_ints_in($@, [0], [255]), [1], [unsigned char], b4_ints_in($@, [-32767], [32767]), [1], [short], @@ -180,6 +179,80 @@ m4_define([b4_int_type], [int])]) +# b4_c99_int_type(MIN, MAX) +# ------------------------- +# Like b4_int_type, but for C99. +# b4_c99_int_type_define replaces b4_int_type with this. +m4_define([b4_c99_int_type], +[m4_if(b4_ints_in($@, [-127], [127]), [1], [yytype_int8], + b4_ints_in($@, [0], [255]), [1], [yytype_uint8], + + b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16], + b4_ints_in($@, [0], [65535]), [1], [yytype_uint16], + + [int])]) + +# b4_c99_int_type_define +# ---------------------- +# Define private types suitable for holding small integers in C99 or later. +m4_define([b4_c99_int_type_define], +[m4_copy_force([b4_c99_int_type], [b4_int_type])dnl +[/* On compilers that do not define __PTRDIFF_MAX__ etc., make sure + and (if available) are included + so that the code can choose integer types of a good width. */ + +#ifndef __PTRDIFF_MAX__ +# ifndef INT_MAX +# include /* INFRINGES ON USER NAME SPACE */ +# endif +# ifndef PTRDIFF_MAX +# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ +# include /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +#endif + +/* Narrow types that promote to a signed type and that can represent a + signed or unsigned integer of at least N bits. In tables they can + save space and decrease cache pressure. Promoting to a signed type + helps avoid bugs in integer arithmetic. */ + +#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST8_TYPE__ yytype_uint8; +#elif defined UINT_LEAST8_MAX && UINT_LEAST8_MAX <= INT_MAX +typedef uint_least8_t yytype_uint8; +#elif UCHAR_MAX <= INT_MAX +typedef unsigned char yytype_uint8; +#else +typedef short yytype_uint8; +#endif + +#if defined __INT_LEAST8_MAX__ && __INT_LEAST8_MAX__ <= __INT_MAX__ +typedef __INT_LEAST8_TYPE__ yytype_int8; +#elif defined INT_LEAST8_MAX && INT_LEAST8_MAX <= INT_MAX +typedef int_least8_t yytype_int8; +#else +typedef signed char yytype_int8; +#endif + +#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST16_TYPE__ yytype_uint16; +#elif defined UINT_LEAST16_MAX && UINT_LEAST16_MAX <= INT_MAX +typedef uint_least16_t yytype_uint16; +#elif USHRT_MAX <= INT_MAX +typedef unsigned short yytype_uint16; +#else +typedef int yytype_uint16; +#endif + +#if defined __INT_LEAST16_MAX__ && __INT_LEAST16_MAX__ <= __INT_MAX__ +typedef __INT_LEAST16_TYPE__ yytype_int16; +#elif defined INT_LEAST16_MAX && INT_LEAST16_MAX <= INT_MAX +typedef int_least16_t yytype_int16; +#else +typedef short yytype_int16; +#endif]]) + # b4_int_type_for(NAME) # --------------------- diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c index c3c1a9a4..af7a78c3 100644 --- a/data/skeletons/glr.c +++ b/data/skeletons/glr.c @@ -265,6 +265,8 @@ static YYLTYPE yyloc_default][]b4_yyloc_default;])[ #include #include +]b4_c99_int_type_define[ + #ifndef YY_ # if defined YYENABLE_NLS && YYENABLE_NLS # if ENABLE_NLS diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c index a0f7ffff..b1060511 100644 --- a/data/skeletons/yacc.c +++ b/data/skeletons/yacc.c @@ -106,25 +106,6 @@ m4_ifset([b4_parse_param], [b4_args(b4_parse_param), ])]) -## ------------ ## -## Data Types. ## -## ------------ ## - -# b4_int_type(MIN, MAX) -# --------------------- -# Return a narrow int type able to handle numbers ranging from -# MIN to MAX (included). Overwrite the version from c.m4, -# so that the code can use C99 types if available. -m4_define([b4_int_type], -[m4_if(b4_ints_in($@, [-127], [127]), [1], [yytype_int8], - b4_ints_in($@, [0], [255]), [1], [yytype_uint8], - - b4_ints_in($@, [-32767], [32767]), [1], [yytype_int16], - b4_ints_in($@, [0], [65535]), [1], [yytype_uint16], - - [int])]) - - ## ----------------- ## ## Semantic Values. ## ## ----------------- ## @@ -388,57 +369,7 @@ m4_if(b4_api_prefix, [yy], [], # undef short #endif -/* On compilers that do not define __PTRDIFF_MAX__ etc., include - and (if available) so that the code can - choose integer types of a good width. */ - -#ifndef __PTRDIFF_MAX__ -# include /* INFRINGES ON USER NAME SPACE */ -# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ -# include /* INFRINGES ON USER NAME SPACE */ -# endif -#endif - -/* Narrow types that promote to a signed type and that can represent a - signed or unsigned integer of at least N bits. In tables they can - save space and decrease cache pressure. Promoting to a signed type - helps avoid bugs in integer arithmetic. */ - -#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__ -typedef __UINT_LEAST8_TYPE__ yytype_uint8; -#elif defined UINT_LEAST8_MAX && UINT_LEAST8_MAX <= INT_MAX -typedef uint_least8_t yytype_uint8; -#elif UCHAR_MAX <= INT_MAX -typedef unsigned char yytype_uint8; -#else -typedef short yytype_uint8; -#endif - -#if defined __INT_LEAST8_MAX__ && __INT_LEAST8_MAX__ <= __INT_MAX__ -typedef __INT_LEAST8_TYPE__ yytype_int8; -#elif defined INT_LEAST8_MAX && INT_LEAST8_MAX <= INT_MAX -typedef int_least8_t yytype_int8; -#else -typedef signed char yytype_int8; -#endif - -#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__ -typedef __UINT_LEAST16_TYPE__ yytype_uint16; -#elif defined UINT_LEAST16_MAX && UINT_LEAST16_MAX <= INT_MAX -typedef uint_least16_t yytype_uint16; -#elif USHRT_MAX <= INT_MAX -typedef unsigned short yytype_uint16; -#else -typedef int yytype_uint16; -#endif - -#if defined __INT_LEAST16_MAX__ && __INT_LEAST16_MAX__ <= __INT_MAX__ -typedef __INT_LEAST16_TYPE__ yytype_int16; -#elif defined INT_LEAST16_MAX && INT_LEAST16_MAX <= INT_MAX -typedef int_least16_t yytype_int16; -#else -typedef short yytype_int16; -#endif +]b4_c99_int_type_define[ #ifndef YYPTRDIFF_T # if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__ -- 2.21.0