From: Bruno Haible <bruno@clisp.org>
Date: Wed, 14 Sep 2022 22:50:41 +0000 (+0200)
Subject: time: Fix compilation errors with clang/MSVC.
X-Git-Tag: v1.0~2017
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=e441260eab816e9b6d202fe7ac288ec2a7b72f34;p=gnulib.git

time: Fix compilation errors with clang/MSVC.

* m4/time_h.m4 (gl_TIME_H): Test whether asctime_r and ctime_r are
declared.
* lib/time.in.h (asctime_r, ctime_r): Skip _GL_WARN_ON_USE invocations
when these functions are not declared.
---

diff --git a/ChangeLog b/ChangeLog
index 37d3ff7aa2..a995e6844b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-09-14  Bruno Haible  <bruno@clisp.org>
+
+	time: Fix compilation errors with clang/MSVC.
+	* m4/time_h.m4 (gl_TIME_H): Test whether asctime_r and ctime_r are
+	declared.
+	* lib/time.in.h (asctime_r, ctime_r): Skip _GL_WARN_ON_USE invocations
+	when these functions are not declared.
+
 2022-09-14  Bruno Haible  <bruno@clisp.org>
 
 	verify: Avoid syntax error due to static_assert with clang in C++ mode.
diff --git a/lib/time.in.h b/lib/time.in.h
index 6d4c771963..6aa67498f5 100644
--- a/lib/time.in.h
+++ b/lib/time.in.h
@@ -435,8 +435,10 @@ _GL_WARN_ON_USE (asctime, "asctime can overrun buffers in some cases - "
 # endif
 # if defined GNULIB_POSIXCHECK
 #  undef asctime_r
+#  if HAVE_RAW_DECL_ASCTIME_R
 _GL_WARN_ON_USE (asctime_r, "asctime_r can overrun buffers in some cases - "
                  "better use strftime (or even sprintf) instead");
+#  endif
 # endif
 # if defined GNULIB_POSIXCHECK
 #  undef ctime
@@ -445,8 +447,10 @@ _GL_WARN_ON_USE (ctime, "ctime can overrun buffers in some cases - "
 # endif
 # if defined GNULIB_POSIXCHECK
 #  undef ctime_r
+#  if HAVE_RAW_DECL_CTIME_R
 _GL_WARN_ON_USE (ctime_r, "ctime_r can overrun buffers in some cases - "
                  "better use strftime (or even sprintf) instead");
+#  endif
 # endif
 
 #endif
diff --git a/m4/time_h.m4 b/m4/time_h.m4
index 98d7b6e01b..4ac8fd0075 100644
--- a/m4/time_h.m4
+++ b/m4/time_h.m4
@@ -2,7 +2,7 @@
 
 # Copyright (C) 2000-2001, 2003-2007, 2009-2022 Free Software Foundation, Inc.
 
-# serial 19
+# serial 20
 
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
@@ -19,6 +19,12 @@ AC_DEFUN_ONCE([gl_TIME_H],
   gl_NEXT_HEADERS([time.h])
   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_TIMESPEC])
 
+  dnl Check for declarations of anything we want to poison if the
+  dnl corresponding gnulib module is not in use.
+  gl_WARN_ON_USE_PREPARE([[
+#include <time.h>
+    ]], [asctime_r ctime_r])
+
   AC_REQUIRE([AC_C_RESTRICT])
 
   AC_CACHE_CHECK([for TIME_UTC in <time.h>],