[haiku-development] dtc port, bison and stdlib.h header guard

  • From: François Revol <revol@xxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Wed, 22 Aug 2012 12:58:43 +0200

Hi,
yesterday I tried to port dtc (the device tree compiler)
<http://git.jdl.com/gitweb/?p=dtc.git;a=summary>, and I had a strange
issue with bison:

...
         CC dtc-parser.tab.o
cc1: warnings being treated as errors
dtc-parser.tab.c:330:7: error: redundant redeclaration of 'malloc'
dtc-parser.tab.c:337:6: error: redundant redeclaration of 'free'
make: *** [dtc-parser.tab.o] Error 1



It seems the code it generates tries to re-declare malloc() and free()
if it didn't find glibc/linux/whatever's header guard for stdlib, which
is _STDLIB_H, and some other weird conditions.
Sadly it seems like ours is _STDLIB_H_, so obviously it doesn't notice
we already have it, even with an explicit include in the .y file.

Forcing the glibc guard define seems to work around it but it's not
really clean.

I'm not sure trying to fix bison is much better/simpler.

The simplest solution seems to align to glibc and switch the guard to
_STDLIB_H. I don't like it either but at least it shouldn't do any harm.

Comments?

François.

Full block of generated code below:

/* The parser invokes alloca or malloc; define the necessary symbols.  */

# ifdef YYSTACK_USE_ALLOCA
#  if YYSTACK_USE_ALLOCA
#   ifdef __GNUC__
#    define YYSTACK_ALLOC __builtin_alloca
#   elif defined __BUILTIN_VA_ARG_INCR
#    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
#   elif defined _AIX
#    define YYSTACK_ALLOC __alloca
#   elif defined _MSC_VER
#    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
#    define alloca _alloca
#   else
#    define YYSTACK_ALLOC alloca
#    if ! defined _ALLOCA_H && ! defined _STDLIB_H && (defined __STDC__
|| defined __C99__FUNC__ \
     || defined __cplusplus || defined _MSC_VER)
#     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
#     ifndef _STDLIB_H
#      define _STDLIB_H 1
#     endif
#    endif
#   endif
#  endif
# endif

# ifdef YYSTACK_ALLOC
   /* Pacify GCC's `empty if-body' warning.  */
#  define YYSTACK_FREE(Ptr) do { /* empty */; } while (YYID (0))
#  ifndef YYSTACK_ALLOC_MAXIMUM
    /* The OS might guarantee only one guard page at the bottom of the
stack,
       and a page size can be as small as 4096 bytes.  So we cannot safely
       invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
       to allow for a few compiler-allocated temporary stack slots.  */
#   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
#  endif
# else
#  define YYSTACK_ALLOC YYMALLOC
#  define YYSTACK_FREE YYFREE
#  ifndef YYSTACK_ALLOC_MAXIMUM
#   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
#  endif
#  if (defined __cplusplus && ! defined _STDLIB_H \
       && ! ((defined YYMALLOC || defined malloc) \
             && (defined YYFREE || defined free)))
#   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
#   ifndef _STDLIB_H
#    define _STDLIB_H 1
#   endif
#  endif
#  ifndef YYMALLOC
#   define YYMALLOC malloc
#   if ! defined malloc && ! defined _STDLIB_H && (defined __STDC__ ||
defined __C99__FUNC__ \
     || defined __cplusplus || defined _MSC_VER)
void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
#   endif
#  endif
#  ifndef YYFREE
#   define YYFREE free
#   if ! defined free && ! defined _STDLIB_H && (defined __STDC__ ||
defined __C99__FUNC__ \
     || defined __cplusplus || defined _MSC_VER)
void free (void *); /* INFRINGES ON USER NAME SPACE */
#   endif
#  endif
# endif
#endif /* ! defined yyoverflow || YYERROR_VERBOSE */

Other related posts: