Re: Re: fnmatch.c includes alloca.h conditionally?

Paul Eggert <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs,gmane.comp.version-control.cvs.bugs
Message-ID <[email protected]>
Derek Robert Price <[email protected]> writes:

> I assumed that the Windows & AIX builtins will be "fast" for
> purposes of defining HAVE_ALLOCA.

Yes, that sounds right.

> I assumed that the hp_ux #ifdef stuff is defining a prototype for
> the version of alloca() in lib/alloca.

That's due to an old problem with the HP-UX C++ compiler.  I think
it'd be better to use a generic solutions for all C++ compilers, as
proposed below.

> Is there a good reason that alloca_.h defines alloca() to return a
> char * on most platforms even though alloca.c only defines alloca() to
> return a void *?

K&R C compatiblity.  We can remove it.

I looked into this and found a few more problems.  Many files don't
include <alloca.h> first, which is needed on AIX.  Rather than fix
them all, I fixed alloca.h so that it needn't be included first on
AIX.  (Not that I have an AIX host to test this on....)  Also, getdate
and setenv include <alloca.h> unconditionally without depending on the
alloca module.  Here's a proposed patch.

2004-05-04  Paul Eggert  <[email protected]>
	    Derek Price  <[email protected]>

	* lib/alloca.c: Include <alloca.h>, to get our interface.
 	* lib/alloca_.h: Use __alloca on AIX, so that we don't have to
	include <alloca.h> first.  Use C89 prototype for alloca; this
	requires including <stddef.h> for size_t.  Use extern "C" if C++.
	Use #elif for simplicity, since we can assume C89 now.
	Don't try to source the system alloca.h since it will not be found
	and to prevent recursively including its replacement.
 	* m4/alloca.m4 (gl_FUNC_ALLOCA): Define HAVE_ALLOCA_H always,
	for backward compatibility with older code.  We need our own
	alloca.h if _AIX is defined.
 	* lib/alloca_.h: Define HAVE_ALLOCA when a native version is found.
 	Don't try to source the system alloca.h since it will not be found and
 	to prevent recursively including its replacement.

	* lib/fnmatch.c: Include <alloca.h> instead of opencoding.
	* lib/regex.c: Likewise.
	* modules/getdate: Likewise.
	* modules/setenv: Likewise.

Index: lib/alloca.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/alloca.c,v
retrieving revision 1.13
diff -p -u -r1.13 alloca.c
--- lib/alloca.c	24 Nov 2003 21:38:44 -0000	1.13
+++ lib/alloca.c	4 May 2004 20:33:18 -0000
@@ -25,6 +25,8 @@
 # include <config.h>
 #endif
 
+#include <alloca.h>
+
 #include <string.h>
 #include <stdlib.h>
 
Index: lib/alloca_.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/alloca_.h,v
retrieving revision 1.4
diff -p -u -r1.4 alloca_.h
--- lib/alloca_.h	9 Aug 2003 08:57:49 -0000	1.4
+++ lib/alloca_.h	4 May 2004 20:33:18 -0000
@@ -1,5 +1,7 @@
 /* Memory allocation on the stack.
-   Copyright (C) 1995, 1999, 2001-2003 Free Software Foundation, Inc.
+
+   Copyright (C) 1995, 1999, 2001, 2002, 2003, 2004 Free Software
+   Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published
@@ -23,8 +25,8 @@
 #ifndef _ALLOCA_H
 # define _ALLOCA_H
 
-/* alloca(N) returns a pointer (void* or char*) to N bytes of memory
-   allocated on the stack, and which will last until the function returns.
+/* alloca (N) returns a pointer to N bytes of memory
+   allocated on the stack, which will last until the function returns.
    Use of alloca should be avoided:
      - inside arguments of function calls - undefined behaviour,
      - in inline functions - the allocation may actually last until the
@@ -34,35 +36,19 @@
        request, the program just crashes.
  */
 
-# ifdef __GNUC__
-#  ifndef alloca
-#   define alloca __builtin_alloca
-#  endif
-# else
-#  ifdef _MSC_VER
-#   include <malloc.h>
-#   define alloca _alloca
-#  else
-#   if HAVE_ALLOCA_H
-#    include <alloca.h>
-#   else
-#    ifdef _AIX
- #    pragma alloca
-#    else
-#     ifdef __hpux /* This section must match that of bison generated files. */
-#      ifdef __cplusplus
-extern "C" void *alloca (unsigned int);
-#      else /* not __cplusplus */
-extern void *alloca ();
-#      endif /* not __cplusplus */
-#     else /* not __hpux */
-#      ifndef alloca
-extern char *alloca ();
-#      endif
-#     endif /* __hpux */
-#    endif
-#   endif
-#  endif
+#ifdef __GNUC__
+# define alloca __builtin_alloca
+#elif defined _AIX
+# define alloca __alloca 
+#elif defined _MSC_VER
+# include <malloc.h>
+# define alloca _alloca
+#else
+# include <stddef.h>
+# ifdef  __cplusplus
+extern "C"
 # endif
+void *alloca (size_t);
+#endif
 
 #endif /* _ALLOCA_H */
Index: lib/fnmatch.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/fnmatch.c,v
retrieving revision 1.26
diff -p -u -r1.26 fnmatch.c
--- lib/fnmatch.c	14 Jan 2004 22:15:48 -0000	1.26
+++ lib/fnmatch.c	4 May 2004 20:33:21 -0000
@@ -24,29 +24,13 @@
 # define _GNU_SOURCE	1
 #endif
 
-#ifdef __GNUC__
-# define alloca __builtin_alloca
-# define HAVE_ALLOCA 1
-#else
-# if defined HAVE_ALLOCA_H || defined _LIBC
-#  include <alloca.h>
-# else
-#  ifdef _AIX
- #  pragma alloca
-#  else
-#   ifndef alloca
-char *alloca ();
-#   endif
-#  endif
-# endif
-#endif
-
 #if ! defined __builtin_expect && __GNUC__ < 3
 # define __builtin_expect(expr, expected) (expr)
 #endif
 
 #include <fnmatch.h>
 
+#include <alloca.h>
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
Index: lib/regex.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/regex.c,v
retrieving revision 1.80
diff -p -u -r1.80 regex.c
--- lib/regex.c	10 Sep 2003 06:18:22 -0000	1.80
+++ lib/regex.c	4 May 2004 20:33:25 -0000
@@ -4,7 +4,7 @@
    internationalization features.)
 
    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002, 2003 Free Software Foundation, Inc.
+   2002, 2003, 2004 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -275,13 +275,7 @@ init_syntax_once (void)
 #  ifndef alloca
 
 /* Make alloca work the best possible way.  */
-#   ifdef __GNUC__
-#    define alloca __builtin_alloca
-#   else /* not __GNUC__ */
-#    if HAVE_ALLOCA_H
-#     include <alloca.h>
-#    endif /* HAVE_ALLOCA_H */
-#   endif /* not __GNUC__ */
+#   include <alloca.h>
 
 #  endif /* not alloca */
 
Index: m4/alloca.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/alloca.m4,v
retrieving revision 1.5
diff -p -u -r1.5 alloca.m4
--- m4/alloca.m4	8 Sep 2003 22:58:10 -0000	1.5
+++ m4/alloca.m4	4 May 2004 20:33:25 -0000
@@ -1,5 +1,5 @@
-# alloca.m4 serial 3
-dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
+# alloca.m4 serial 4
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
 dnl Public License, this file may be distributed as part of a program
@@ -20,13 +20,17 @@ AC_DEFUN([gl_FUNC_ALLOCA],
   # Define an additional variable used in the Makefile substitution.
 
   AC_EGREP_CPP([Need own alloca], [
-#if defined __GNUC__ || defined _MSC_VER || !HAVE_ALLOCA_H
+#if defined __GNUC__ || defined _AIX || defined _MSC_VER || !HAVE_ALLOCA_H
   Need own alloca
 #endif
     ],
     ALLOCA_H=alloca.h,
     ALLOCA_H=)
   AC_SUBST([ALLOCA_H])
+
+  AC_DEFINE(HAVE_ALLOCA_H, 1,
+    [Define HAVE_ALLOCA_H for backward compatibility with older code
+     that includes <alloca.h> only if HAVE_ALLOCA_H is defined.])
 ])
 
 # Prerequisites of lib/alloca.c.
Index: modules/getdate
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/getdate,v
retrieving revision 1.5
diff -p -u -r1.5 getdate
--- modules/getdate	31 Mar 2004 07:20:49 -0000	1.5
+++ modules/getdate	4 May 2004 20:33:26 -0000
@@ -13,6 +13,7 @@ timespec
 stdbool
 gettime
 mktime
+alloca
 unlocked-io
 
 configure.ac:
Index: modules/setenv
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/setenv,v
retrieving revision 1.5
diff -p -u -r1.5 setenv
--- modules/setenv	20 Jan 2004 17:05:34 -0000	1.5
+++ modules/setenv	4 May 2004 20:33:26 -0000
@@ -10,6 +10,7 @@ m4/setenv.m4
 
 Depends-on:
 allocsa
+alloca
 
 configure.ac:
 gt_FUNC_SETENV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.