Building a gnulib testdir with optimizing MSVC, I also see these compilation
errors:

C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-acosf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-asinf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-atan2f.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-atanf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-ceil1.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-cosf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-coshf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-expf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-floor1.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-fmodf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-log10f.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-log2.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-log2f.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-logf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-powf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-sinf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-sinhf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-sqrtf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-tanf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-tanhf.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-wcscmp.c(24): error 
C2099: initializer is not a constant
C:\cygwin64\home\bruno\testdir-all-for-mingw\gltests\test-wcsncmp.c(24): error 
C2099: initializer is not a constant

This patch fixes them.


2026-10-17  Bruno Haible  <[email protected]>

        tests: Fix some compilation errors with optimizing MSVC.
        * tests/signature.h (DISABLE_INTRINSIC): New macro.
        (SIGNATURE_CHECK2): Invoke it.

diff --git a/tests/signature.h b/tests/signature.h
index 9f81e8d32c..156dd0a724 100644
--- a/tests/signature.h
+++ b/tests/signature.h
@@ -43,6 +43,23 @@
 # define SIGNATURE_CHECK1(fn, ret, args, id) \
   SIGNATURE_CHECK2 (fn, ret, args, id) /* macroexpand line */
 # define SIGNATURE_CHECK2(fn, ret, args, id) \
+  DISABLE_INTRINSIC (fn) \
   _GL_UNUSED static ret (*signature_check ## id) args = fn
+# if defined _MSC_VER
+/* With MSVC, when intrinsics are enabled (e.g. through option '-O2' without
+   option '-Oi-'), use of an intrinsic function as an initializer produces an
+   "error C2099: initializer is not a constant".  To avoid this error, disable
+   this particular intrinsic function.
+   Documentation:
+   <https://learn.microsoft.com/en-us/cpp/intrinsics/compiler-intrinsics>
+   <https://learn.microsoft.com/en-us/cpp/preprocessor/function-c-cpp>
+   <https://learn.microsoft.com/en-us/cpp/preprocessor/warning>
+   
<https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword>
  */
+#  define DISABLE_INTRINSIC(fn)  \
+     __pragma (warning (suppress : 4163)) \
+     __pragma (function (fn))
+# else
+#  define DISABLE_INTRINSIC(fn)
+# endif
 
 #endif /* SIGNATURE_CHECK */




Reply via email to