Hi Collin,

> However, when doing the following commands in gettext:
> 
>     $ ./autopull.sh
>     $ ./autogen.sh
>     $ ./configure && make
> 
> I see the following error:
> 
>     $ gcc --version | sed 1q
>     gcc (GCC) 15.1.1 20250425 (Red Hat 15.1.1-1)
>     $ make
>     [...]
>     gcc -DHAVE_CONFIG_H -DEXEEXT=\"\" -I. -I..  -I../intl 
> -DDEPENDS_ON_LIBICONV=1 -DDEPENDS_ON_LIBINTL=1   -Wno-cast-qual 
> -Wno-conversion -Wno-float-equal -Wno-sign-compare -Wno-undef 
> -Wno-unused-function -Wno-unused-parameter -Wno-float-conversion 
> -Wimplicit-fallthrough -Wno-pedantic -Wno-sign-conversion -Wno-type-limits 
> -Wno-unused-const-variable -Wno-unsuffixed-float-constants -Wno-error -g -O2 
> -MT libgrt_a-xsize.o -MD -MP -MF .deps/libgrt_a-xsize.Tpo -c -o 
> libgrt_a-xsize.o `test -f 'xsize.c' || echo './'`xsize.c
>     In file included from string-desc-contains.c:20:
>     ../config.h:1218:21: error: attributes should be specified before the 
> declarator in a function definition
>      1218 | # define _GL_INLINE inline
>           |                     ^~~~~~
>     string-desc.h:48:32: note: in expansion of macro '_GL_INLINE'
>        48 | # define GL_STRING_DESC_INLINE _GL_INLINE
>           |                                ^~~~~~~~~~
>     string-desc.h:319:1: note: in expansion of macro 'GL_STRING_DESC_INLINE'
>       319 | GL_STRING_DESC_INLINE char
>           | ^~~~~~~~~~~~~~~~~~~~~
> ...
> It seems that doing this in a declaration is okay:
> 
>     GL_STRING_DESC_INLINE char
>     _sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
>         _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1);
> 
> But in the function definition it will not work:
> 
>     GL_STRING_DESC_INLINE char
>     _sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
>         _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
>     {
>       [...]
>     }
> 
> The attributes must be placed before the return type.

Thanks for the report. I had relied on the comments in lib/attribute.h,
and these comments did not cover the case of function definitions.

> I have attached two patches that seem to fix the issue, but have not
> pushed them in case I am misunderstanding the error.

While your fix is correct, I prefer to stick to the principle
"keep related things nearby together": since the attribute is about
the argument list, it's irritating to see it mentioned long before
the argument list. I prefer the attached patch.

> gettext still fails to build afterwards, but it seems like changing to
> rw_string_desc_t where applicable will fix it.

Yes. I just hadn't gotten around to pushing these changes.

Bruno

>From 66301a2e507024fbecfdba14a61eec1cfd2cb3c3 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 10 May 2025 15:06:47 +0200
Subject: [PATCH 1/2] string-desc: Improve a declaration.

* lib/string-desc.h (_sd_char_at): Use _GL_ATTRIBUTE_NONNULL, not
_GL_ATTRIBUTE_NONNULL_IF_NONZERO.
---
 ChangeLog         | 6 ++++++
 lib/string-desc.h | 5 +++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4a4d378fd3..c71bacdda9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-05-10  Bruno Haible  <br...@clisp.org>
+
+	string-desc: Improve a declaration.
+	* lib/string-desc.h (_sd_char_at): Use _GL_ATTRIBUTE_NONNULL, not
+	_GL_ATTRIBUTE_NONNULL_IF_NONZERO.
+
 2025-05-10  Bruno Haible  <br...@clisp.org>
 
 	qcopy-acl: Update link dependencies after yesterday's change.
diff --git a/lib/string-desc.h b/lib/string-desc.h
index 114f8f3b57..8bd6691914 100644
--- a/lib/string-desc.h
+++ b/lib/string-desc.h
@@ -20,7 +20,8 @@
 #define _STRING_DESC_H 1
 
 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE,
-   _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NONNULL_IF_NONZERO.  */
+   _GL_ATTRIBUTE_NODISCARD, _GL_ATTRIBUTE_NONNULL,
+   _GL_ATTRIBUTE_NONNULL_IF_NONZERO.  */
 #if !_GL_CONFIG_H_INCLUDED
  #error "Please include config.h first."
 #endif
@@ -318,7 +319,7 @@ sd_length (string_desc_t s)
    })
 GL_STRING_DESC_INLINE char
 _sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
-  _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
+  _GL_ATTRIBUTE_NONNULL ((2))
 {
   if (!(i >= 0 && i < s_nbytes))
     /* Invalid argument.  */
-- 
2.43.0

From 4ae4881d4c360c8675d5e2c397c193bc81660c3a Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Sat, 10 May 2025 17:12:28 +0200
Subject: [PATCH 2/2] string-desc, xstring-desc: Avoid GCC attributes in
 function definitions.

Reported by Collin Funk in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00126.html>.

* lib/attribute.h: Clarify the allowed positions of attributes in
function definitions.
* lib/string-desc.h (_sd_char_at): Add a declaration, to hold the
gcc attrribute.
* lib/xstring-desc.h (_xsd_c): Likewise.
---
 ChangeLog          | 11 +++++++++++
 lib/attribute.h    |  5 +++--
 lib/string-desc.h  |  4 +++-
 lib/xstring-desc.h |  6 +++++-
 4 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c71bacdda9..2d7696a717 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2025-05-10  Bruno Haible  <br...@clisp.org>
+
+	string-desc, xstring-desc: Avoid GCC attributes in function definitions.
+	Reported by Collin Funk in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00126.html>.
+	* lib/attribute.h: Clarify the allowed positions of attributes in
+	function definitions.
+	* lib/string-desc.h (_sd_char_at): Add a declaration, to hold the
+	gcc attrribute.
+	* lib/xstring-desc.h (_xsd_c): Likewise.
+
 2025-05-10  Bruno Haible  <br...@clisp.org>
 
 	string-desc: Improve a declaration.
diff --git a/lib/attribute.h b/lib/attribute.h
index ae7bbe8e2c..c85412d90a 100644
--- a/lib/attribute.h
+++ b/lib/attribute.h
@@ -50,8 +50,9 @@
              - In a function declaration/definition with a storage-class
                specifier: between the storage-class specifier and the return
                type.
-        - Or after the parameter list,
-          ??? but after ATTRIBUTE_NOTHROW if present.
+        - Or, in a function declaration:
+          after the parameter list,
+            ??? but after ATTRIBUTE_NOTHROW if present.
 
    In other declarations, such as variable declarations:
 
diff --git a/lib/string-desc.h b/lib/string-desc.h
index 8bd6691914..db6925d0d8 100644
--- a/lib/string-desc.h
+++ b/lib/string-desc.h
@@ -319,7 +319,9 @@ sd_length (string_desc_t s)
    })
 GL_STRING_DESC_INLINE char
 _sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
-  _GL_ATTRIBUTE_NONNULL ((2))
+  _GL_ATTRIBUTE_NONNULL ((2));
+GL_STRING_DESC_INLINE char
+_sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
 {
   if (!(i >= 0 && i < s_nbytes))
     /* Invalid argument.  */
diff --git a/lib/xstring-desc.h b/lib/xstring-desc.h
index 216865a267..17ea35e357 100644
--- a/lib/xstring-desc.h
+++ b/lib/xstring-desc.h
@@ -100,7 +100,11 @@ GL_XSTRING_DESC_INLINE
 _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_RETURNS_NONNULL
 char *
 _xsd_c (idx_t s_nbytes, const char *s_data)
-  _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
+  _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1);
+GL_XSTRING_DESC_INLINE
+_GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_RETURNS_NONNULL
+char *
+_xsd_c (idx_t s_nbytes, const char *s_data)
 {
   char *result = _sd_c (s_nbytes, s_data);
   if (result == NULL)
-- 
2.43.0

Reply via email to