]> Savannah Git Hosting - gnulib.git/commitdiff
attribute: Support ISO C 23 [[reproducible]] and [[unsequenced]].
authorBruno Haible <bruno@clisp.org>
Mon, 27 May 2024 09:40:13 +0000 (11:40 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 27 May 2024 09:40:13 +0000 (11:40 +0200)
* m4/gnulib-common.m4 (gl_COMMON_BODY): Define _GL_ATTR_reproducible,
_GL_ATTR_unsequenced, _GL_ATTRIBUTE_REPRODUCIBLE,
_GL_ATTRIBUTE_UNSEQUENCED.
* lib/attribute.h (UNSEQUENCED, REPRODUCIBLE): New macros.

ChangeLog
lib/attribute.h
m4/gnulib-common.m4

index ef8adbc21ecd3c1560a6433b7e9205c98b50ba05..44487acfd95b693f4c67e35d287c346ff21086cf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-05-27  Bruno Haible  <bruno@clisp.org>
+
+       attribute: Support ISO C 23 [[reproducible]] and [[unsequenced]].
+       * m4/gnulib-common.m4 (gl_COMMON_BODY): Define _GL_ATTR_reproducible,
+       _GL_ATTR_unsequenced, _GL_ATTRIBUTE_REPRODUCIBLE,
+       _GL_ATTRIBUTE_UNSEQUENCED.
+       * lib/attribute.h (UNSEQUENCED, REPRODUCIBLE): New macros.
+
 2024-05-25  Bruno Haible  <bruno@clisp.org>
 
        euidaccess: Fix test failure on native Windows.
index 710341ba417c042714c819b18fa69b67633b1970..a7076c8b7e2e3931911c5e9b3f398ef3df57f1a0 100644 (file)
 /* Applies to: function.  */
 #define ATTRIBUTE_ALWAYS_INLINE _GL_ATTRIBUTE_ALWAYS_INLINE
 
-/* It is OK for a compiler to omit duplicate calls with the same arguments.
+/* It is OK for a compiler to omit duplicate calls to the function with the
+   same arguments.
    This attribute is safe for a function that neither depends on
    nor affects observable state, and always returns exactly once -
    e.g., does not loop forever, and does not call longjmp.
-   (This attribute is stricter than ATTRIBUTE_PURE.)  */
+   (This attribute is stricter than ATTRIBUTE_PURE.  It is equivalent to
+   UNSEQUENCED for a function that has no pointer or array parameters.)  */
 /* Applies to: functions.  */
 #define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST
 
-/* It is OK for a compiler to omit duplicate calls with the same
+/* It is OK for a compiler to move calls to the function and to omit duplicate
+   calls to the function with the same arguments.
+   This attribute is safe for a function that is effectless, idempotent,
+   stateless, and independent; see ISO C 23 § 6.7.12.7 for a definition of
+   these terms.
+   (This attribute is stricter than REPRODUCIBLE.  It is equivalent to
+   ATTRIBUTE_CONST for a function that has no pointer or array parameters.)
+   See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
+   <https://stackoverflow.com/questions/76847905/>.  */
+/* Applies to: functions.  */
+#define UNSEQUENCED _GL_ATTRIBUTE_UNSEQUENCED
+
+/* It is OK for a compiler to omit duplicate calls to the function with the same
    arguments if observable state is not changed between calls.
-   This attribute is safe for a function that does not affect
-   observable state, and always returns exactly once.
-   (This attribute is looser than ATTRIBUTE_CONST.)  */
+   This attribute is safe for a function that does not affect observable state
+   and always returns exactly once.
+   (This attribute is looser than ATTRIBUTE_CONST.  It is equivalent to
+   REPRODUCIBLE for a function for which all pointer or array parameters have
+   'const'-qualified target types.)  */
 /* Applies to: functions.  */
 #define ATTRIBUTE_PURE _GL_ATTRIBUTE_PURE
 
+/* It is OK for a compiler to omit duplicate calls to the function with the same
+   arguments if observable state is not changed between calls.
+   This attribute is safe for a function that is effectless and idempotent; see
+   ISO C 23 § 6.7.12.7 for a definition of these terms.
+   (This attribute is looser than UNSEQUENCED.  It is equivalent to
+   ATTRIBUTE_PURE for a function for which all pointer or array parameters have
+   'const'-qualified target types.)
+   See also <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2956.htm> and
+   <https://stackoverflow.com/questions/76847905/>.  */
+/* Applies to: functions.  */
+#define REPRODUCIBLE _GL_ATTRIBUTE_REPRODUCIBLE
+
 /* The function is rarely executed.  */
 /* Applies to: functions.  */
 #define ATTRIBUTE_COLD _GL_ATTRIBUTE_COLD
index cb730449507799d5743445b5f6b16d8c9de84ab8..044ecc93f13180932025873eaddbd8f12124e4fe 100644 (file)
@@ -1,5 +1,5 @@
 # gnulib-common.m4
-# serial 93
+# serial 94
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -114,8 +114,10 @@ AC_DEFUN([gl_COMMON_BODY], [
 #  define _GL_ATTR_nothrow _GL_GNUC_PREREQ (3, 3)
 #  define _GL_ATTR_packed _GL_GNUC_PREREQ (2, 7)
 #  define _GL_ATTR_pure _GL_GNUC_PREREQ (2, 96)
+#  define _GL_ATTR_reproducible 0 /* not yet supported, as of GCC 14 */
 #  define _GL_ATTR_returns_nonnull _GL_GNUC_PREREQ (4, 9)
 #  define _GL_ATTR_sentinel _GL_GNUC_PREREQ (4, 0)
+#  define _GL_ATTR_unsequenced 0 /* not yet supported, as of GCC 14 */
 #  define _GL_ATTR_unused _GL_GNUC_PREREQ (2, 7)
 #  define _GL_ATTR_warn_unused_result _GL_GNUC_PREREQ (3, 4)
 # endif
@@ -206,7 +208,9 @@ AC_DEFUN([gl_COMMON_BODY], [
    This attribute is safe for a function that neither depends on nor affects
    observable state, and always returns exactly once - e.g., does not loop
    forever, and does not call longjmp.
-   (This attribute is stricter than _GL_ATTRIBUTE_PURE.)  */
+   (This attribute is stricter than _GL_ATTRIBUTE_PURE.  It is equivalent to
+   _GL_ATTRIBUTE_UNSEQUENCED for a function that has no pointer or array
+   parameters.)  */
 /* Applies to: functions.  */
 #ifndef _GL_ATTRIBUTE_CONST
 # if _GL_HAS_ATTRIBUTE (const)
@@ -505,9 +509,9 @@ AC_DEFUN([gl_COMMON_BODY], [
    minimizing the memory required.  */
 /* Applies to: struct members, struct, union,
    in C++ also: class.  */
+#ifndef _GL_ATTRIBUTE_PACKED
 /* Oracle Studio 12.6 miscompiles code with __attribute__ ((__packed__)) despite
    __has_attribute OK.  */
-#ifndef _GL_ATTRIBUTE_PACKED
 # if _GL_HAS_ATTRIBUTE (packed) && !defined __SUNPRO_C
 #  define _GL_ATTRIBUTE_PACKED __attribute__ ((__packed__))
 # else
@@ -515,12 +519,14 @@ AC_DEFUN([gl_COMMON_BODY], [
 # endif
 #endif
 
-/* _GL_ATTRIBUTE_PURE declares that It is OK for a compiler to omit duplicate
+/* _GL_ATTRIBUTE_PURE declares that it is OK for a compiler to omit duplicate
    calls to the function with the same arguments if observable state is not
    changed between calls.
-   This attribute is safe for a function that does not affect
-   observable state, and always returns exactly once.
-   (This attribute is looser than _GL_ATTRIBUTE_CONST.)  */
+   This attribute is safe for a function that does not affect observable state
+   and always returns exactly once.
+   (This attribute is looser than _GL_ATTRIBUTE_CONST.  It is equivalent to
+   _GL_ATTRIBUTE_REPRODUCIBLE for a function for which all pointer or array
+   parameters have 'const'-qualified target types.)  */
 /* Applies to: functions.  */
 #ifndef _GL_ATTRIBUTE_PURE
 # if _GL_HAS_ATTRIBUTE (pure)
@@ -530,6 +536,23 @@ AC_DEFUN([gl_COMMON_BODY], [
 # endif
 #endif
 
+/* _GL_ATTRIBUTE_REPRODUCIBLE declares that it is OK for a compiler to omit
+   duplicate calls to the function with the same arguments if observable state
+   is not changed between calls.
+   This attribute is safe for a function that is effectless and idempotent; see
+   ISO C 23 § 6.7.12.7 for a definition of these terms.
+   (This attribute is looser than _GL_ATTRIBUTE_UNSEQUENCED.  It is equivalent
+   to _GL_ATTRIBUTE_PURE for a function for which all pointer or array
+   parameters have 'const'-qualified target types.)  */
+/* Applies to: functions.  */
+#ifndef _GL_ATTRIBUTE_REPRODUCIBLE
+# if _GL_HAS_ATTRIBUTE (reproducible)
+#  define _GL_ATTRIBUTE_REPRODUCIBLE [[reproducible]]
+# else
+#  define _GL_ATTRIBUTE_REPRODUCIBLE
+# endif
+#endif
+
 /* _GL_ATTRIBUTE_RETURNS_NONNULL declares that the function's return value is
    a non-NULL pointer.  */
 /* Applies to: functions.  */
@@ -554,6 +577,24 @@ AC_DEFUN([gl_COMMON_BODY], [
 # endif
 #endif
 
+/* _GL_ATTRIBUTE_UNSEQUENCED declares that it is OK for a compiler to move
+   calls to the function and to omit duplicate calls to the function with the
+   same arguments.
+   This attribute is safe for a function that is effectless, idempotent,
+   stateless, and independent; see ISO C 23 § 6.7.12.7 for a definition of
+   these terms.
+   (This attribute is stricter than _GL_ATTRIBUTE_REPRODUCIBLE.  It is
+   equivalent to _GL_ATTRIBUTE_CONST for a function that has no pointer or
+   array parameters.)  */
+/* Applies to: functions.  */
+#ifndef _GL_ATTRIBUTE_UNSEQUENCED
+# if _GL_HAS_ATTRIBUTE (unsequenced)
+#  define _GL_ATTRIBUTE_UNSEQUENCED [[unsequenced]]
+# else
+#  define _GL_ATTRIBUTE_UNSEQUENCED
+# endif
+#endif
+
 /* A helper macro.  Don't use it directly.  */
 #ifndef _GL_ATTRIBUTE_UNUSED
 # if _GL_HAS_ATTRIBUTE (unused)