autoconf: AC_FUNC_ALLOCA: port to recent BSDs and remove obsolete AIX

From 5c070747d117a9a226a63dd53c1679e2a217c86d Mon Sep 17 00:00:00 2001
From: Paul Eggert <[EMAIL REDACTED]>
Date: Thu, 20 Dec 2012 21:21:04 -0800
Subject: [PATCH] AC_FUNC_ALLOCA: port to recent BSDs and remove obsolete AIX

* doc/autoconf.texi (Particular Functions): Remove the AIX case
from the recommended code, as the most recent version of the AIX
compiler that IBM still supports (V10.1 as of this writing) has
<alloca.h> and thus longer needs this, and the old suggestion
wasn't completely working anyway.  Remove obsolete discussion of
SVR3 libPW alloca and of SVR4 libucb alloca.
* lib/autoconf/functions.m4 (AC_FUNC_ALLOCA):
Rework to match documentation, including abovementioned AIX change.
Inconsistency with documentation reported by Steven G. Johnson in
As this adds stdlib.h, it should also fix the problems on recent
BSD platforms noted by Patrick Welche in
though the fix differs from NetBSD's current workaround.
Also, don't bother checking for alloca if <alloca.h> works,
as the latter implies the former.
(cherry picked from commit fd29dbd7d966f0fbde031a67955d77d50268c3d5)
 doc/autoconf.texi         | 23 +++++------------------
 lib/autoconf/functions.m4 | 34 +++++++++++++++++-----------------
 2 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index 93ab1ac4..5a101a00 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -4825,15 +4825,11 @@ in some cases how they respond when given certain arguments.
 @prindex @code{alloca}
 @c @caindex working_alloca_h
-Check how to get @code{alloca}.  Tries to get a builtin version by
-checking for @file{alloca.h} or the predefined C preprocessor macros
-@code{__GNUC__} and @code{_AIX}.  If this macro finds @file{alloca.h},
-it defines @code{HAVE_ALLOCA_H}.
-If those attempts fail, it looks for the function in the standard C
-library.  If any of those methods succeed, it defines
-@code{HAVE_ALLOCA}.  Otherwise, it sets the output variable
-@code{ALLOCA} to @samp{$@{LIBOBJDIR@}alloca.o} and defines
+Check for the @code{alloca} function.  Define @code{HAVE_ALLOCA_H} if
+@file{alloca.h} defines a working @code{alloca}.  If not, look for a
+builtin alternative.  If either method succeeds, define
+@code{HAVE_ALLOCA}.  Otherwise, set the output variable @code{ALLOCA} to
+@samp{$@{LIBOBJDIR@}alloca.o} and define
 @code{C_ALLOCA} (so programs can periodically call @samp{alloca (0)} to
 garbage collect).  This variable is separate from @code{LIBOBJS} so
 multiple programs can share the value of @code{ALLOCA} without needing
@@ -4841,13 +4837,6 @@ to create an actual library, in case only some of them use the code in
 @code{LIBOBJS}.  The @samp{$@{LIBOBJDIR@}} prefix serves the same
 purpose as in @code{LIBOBJS} (@pxref{AC_LIBOBJ vs LIBOBJS}).
-This macro does not try to get @code{alloca} from the System V R3
-@file{libPW} or the System V R4 @file{libucb} because those libraries
-contain some incompatible functions that cause trouble.  Some versions
-do not even contain @code{alloca} or contain a buggy version.  If you
-still want to use their @code{alloca}, use @code{ar} to extract
-@file{alloca.o} from them instead of compiling @file{alloca.c}.
 Source files that use @code{alloca} should start with a piece of code
 like the following, to declare it properly.
@@ -4866,8 +4855,6 @@ like the following, to declare it properly.
 #elif !defined alloca
 # ifdef __GNUC__
 #  define alloca __builtin_alloca
-# elif defined _AIX
-#  define alloca __alloca
 # elif defined _MSC_VER
 #  include <malloc.h>
 #  define alloca _alloca
diff --git a/lib/autoconf/functions.m4 b/lib/autoconf/functions.m4
index 355e9e79..aff5b98e 100644
--- a/lib/autoconf/functions.m4
+++ b/lib/autoconf/functions.m4
@@ -372,36 +372,36 @@ AC_CACHE_CHECK([for working alloca.h], ac_cv_working_alloca_h,
 if test $ac_cv_working_alloca_h = yes; then
-	    [Define to 1 if you have <alloca.h> and it should be used
-	     (not on Ultrix).])
+	    [Define to 1 if <alloca.h> works.])
 AC_CACHE_CHECK([for alloca], ac_cv_func_alloca_works,
-[[#ifdef __GNUC__
-# define alloca __builtin_alloca
-# ifdef _MSC_VER
+[if test $ac_cv_working_alloca_h = yes; then
+  ac_cv_func_alloca_works=yes
+[[#if defined STDC_HEADERS || defined HAVE_STDLIB_H
+# include <stdlib.h>
+#include <stddef.h>
+#ifndef alloca
+# ifdef __GNUC__
+#  define alloca __builtin_alloca
+# elif defined _MSC_VER
 #  include <malloc.h>
 #  define alloca _alloca
 # else
-#  ifdef HAVE_ALLOCA_H
-#   include <alloca.h>
-#  else
-#   ifdef _AIX
- #pragma alloca
-#   else
-#    ifndef alloca /* predefined by HP cc +Olibcalls */
-void *alloca (size_t);
-#    endif
-#   endif
+#  ifdef  __cplusplus
+extern "C"
 #  endif
+void *alloca (size_t);
 # endif
 ]],                               [[char *p = (char *) alloca (1);
 				    if (p) return 0;]])],
 if test $ac_cv_func_alloca_works = yes; then