Re: Making MSVC/clang-cl succeed AC_PROG_CC C11 discovery

Paul Eggert <[email protected]> Tue, 30 Apr 2024 10:43:54 -0700
Newsgroups gmane.comp.sysutils.autoconf.general
Organization UCLA Computer Science Department
Message-ID <[email protected]>
On 2024-04-26 08:10, Zack Weinberg wrote:

> I think what we should do here is fold AC_C_VARARRAYS into AC_PROG_CC.
> Take the test for VLAs completely out of _AC_C_C99_TEST_MAIN, but
> unconditionally *run* a test for VLAs as part of AC_PROG_CC.  If that
> test fails, and __STDC_NO_VLA__ was not defined by the compiler,
> then AC_DEFINE([__STDC_NO_VLA__], 1)

Although the basic strategy sounds reasonable, AC_PROG_CC is documented 
to operate by setting compiler options not via AC_DEFINE, so presumably 
this should put -D__STDC_NO_VLA__ into $CC rather than use AC_DEFINE.

I installed the attached patch, which is less ambitious but should 
address the original problem report. The attached patch doesn't preclude 
your suggestion, which can be done later as needed.
0001-Port-better-to-MSVC.patch (text/x-patch, 4.1 KB)
From e9fee73dba5d2156abc48734b5a9faff89dcdc11 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Thu, 25 Apr 2024 14:47:20 -0700
Subject: [PATCH] Port better to MSVC
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problems reported by Antonin Décimo in:
https://lists.gnu.org/r/autoconf/2024-04/msg00001.html
* lib/autoconf/c.m4 (_AC_C_C89_TEST_GLOBALS):
Do not test the value of __STDC__.
(_AC_C_C99_TEST_MAIN): Do not test for VLAs.
(_AC_C_C11_OPTIONS): Also test -std:c11.
---
 NEWS              |  7 +++++++
 doc/autoconf.texi |  9 +++++++--
 lib/autoconf/c.m4 | 17 +++++++----------
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/NEWS b/NEWS
index 01a058aa..3d968c4c 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,13 @@ GNU Autoconf NEWS - User visible changes.
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Backward incompatibilities
+
+*** AC_PROG_CC no longer tests for VLAs, or whether __STDC__ is defined.
+  This ports better to MSVC, which does not support variable length
+  arrays and does not define __STDC__.  Although C99 requires VLAs,
+  they are optional in C11 and later.  Programs can use AC_C_VARARRAYS
+  and __STDC_NO_VLA__ to use VLAs if available.
 
 * Noteworthy changes in release 2.72 (2023-12-22) [release]
 
diff --git a/doc/autoconf.texi b/doc/autoconf.texi
index 186c033b..0fd82056 100644
--- a/doc/autoconf.texi
+++ b/doc/autoconf.texi
@@ -7444,9 +7444,14 @@ the C standard, and @samp{no} if the compiler does not support compiling
 standard C at all.
 
 The tests for standard conformance are not comprehensive.  They test the
-values of @code{__STDC__} and @code{__STDC_VERSION__}, and a
+value of @code{__STDC_VERSION__}, and a
 representative sample of the language features added in each version of
-the C standard.  They do not test the C standard library, because the C
+the C standard.  They do not examine @code{__STDC__}
+because some compilers by default leave it undefined.
+They do not test for variable-length arrays,
+a C99 feature that was made optional in C11;
+if you need to use this feature when available, use @code{AC_C_VARARRAYS}.
+They do not test the C standard library, because the C
 compiler might be generating code for a ``freestanding environment''
 (in which most of the standard library is optional).  If you need to know
 whether a particular C standard header exists, use @code{AC_CHECK_HEADER}.
diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4
index d8eab7bb..d410f8bc 100644
--- a/lib/autoconf/c.m4
+++ b/lib/autoconf/c.m4
@@ -1120,12 +1120,8 @@ AC_DEFUN([_AC_C_C89_TEST_GLOBALS],
 [m4_divert_text([INIT_PREPARE],
 [[# Test code for whether the C compiler supports C89 (global declarations)
 ac_c_conftest_c89_globals='
-/* Does the compiler advertise C89 conformance?
-   Do not test the value of __STDC__, because some compilers set it to 0
-   while being otherwise adequately conformant. */
-#if !defined __STDC__
-# error "Compiler does not advertise C89 conformance"
-#endif
+/* Do not test the value of __STDC__, because some compilers define it to 0
+   or do not define it, while otherwise adequately conforming.  */
 
 #include <stddef.h>
 #include <stdarg.h>
@@ -1337,13 +1333,12 @@ ac_c_conftest_c99_main='
 
   ni.number = 58;
 
-  int dynamic_array[ni.number];
-  dynamic_array[0] = argv[0][0];
-  dynamic_array[ni.number - 1] = 543;
+  // Do not test for VLAs, as some otherwise-conforming compilers lack them.
+  // C code should instead use __STDC_NO_VLA__; see Autoconf manual.
 
   // work around unused variable warnings
   ok |= (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == '\''x'\''
-	 || dynamic_array[ni.number - 1] != 543);
+	 || ni.number != 58);
 '
 ]])])
 
@@ -1553,6 +1548,7 @@ m4_define([_AC_C_C99_OPTIONS], [
 # shell quotes around the group.
 #
 # GCC, Clang    -std=gnu11
+# MSVC          -std:c11
 #
 # For IBM XL C for AIX V16.1 or later, '-std=gnu11' should work if
 # the user configured with CC='xlclang'.  Otherwise, do not try
@@ -1562,6 +1558,7 @@ m4_define([_AC_C_C99_OPTIONS], [
 # _Noreturn, which is a win.
 m4_define([_AC_C_C11_OPTIONS], [
     -std=gnu11
+    -std:c11
 ])
 
 
-- 
2.40.1