Bruno Haible via Gnulib discussion list <bug-gnulib@gnu.org> writes:

> This patch fixes this problem, by distinguishing read-only strings and
> writable strings.
>
> Obviously, I did not want to duplicate the functions, e.g. sd_length
> to sd_length and rwsd_length. A single function should be applicable
> to both read-only strings and writable strings. Unfortunately the C type
> system is not helpful in this direction. But I could make it work by using
> a combination of
>   - statement-expressions,
>   - _Generic,
>   - typeof,
>   - and inline functions.
> Thus the implementation is more complex than before, but the programs that
> use 'string-desc' get better type safety with fewer casts.

Thanks, I think that clearly differentiating the two is good as well.

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
          | ^~~~~~~~~~~~~~~~~~~~~
    In file included from string-buffer.c:19:
    ../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.

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

gettext still fails to build afterwards, but it seems like changing to
rw_string_desc_t where applicable will fix it. So not a bug.

Collin

>From 26eca499193953b4ff881a192b1b1fb1ee7e86bc Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 9 May 2025 21:09:57 -0700
Subject: [PATCH 1/2] string-desc: Fix compile error.

* lib/string-desc.h (_sd_char_at): Place attributes before the return
type.
---
 ChangeLog         | 6 ++++++
 lib/string-desc.h | 3 +--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bb1f55cce0..eaea7ce9c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2025-05-09  Collin Funk  <collin.fu...@gmail.com>
+
+	string-desc: Fix compile error.
+	* lib/string-desc.h (_sd_char_at): Place attributes before the return
+	type.
+
 2025-05-09  Paul Eggert  <egg...@cs.ucla.edu>
 
 	acl-tests: link with $(FILE_HAS_ACL_LIB)
diff --git a/lib/string-desc.h b/lib/string-desc.h
index 114f8f3b57..4d65466778 100644
--- a/lib/string-desc.h
+++ b/lib/string-desc.h
@@ -316,9 +316,8 @@ sd_length (string_desc_t s)
    ({typeof (s) _s_ = (s); \
      _sd_char_at (_s_._nbytes, _s_._data, i); \
    })
-GL_STRING_DESC_INLINE char
+GL_STRING_DESC_INLINE _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1) char
 _sd_char_at (idx_t s_nbytes, const char *s_data, idx_t i)
-  _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
 {
   if (!(i >= 0 && i < s_nbytes))
     /* Invalid argument.  */
-- 
2.49.0

>From 087464e49f645f587a41b00ea99e858150a5d2cf Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Fri, 9 May 2025 21:11:50 -0700
Subject: [PATCH 2/2] xstring-desc: Fix compile error.

* lib/xstring-desc.h (_xsd_c): Place attributes before the return type.
---
 ChangeLog          | 3 +++
 lib/xstring-desc.h | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index eaea7ce9c2..4a595f0947 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2025-05-09  Collin Funk  <collin.fu...@gmail.com>
 
+	xstring-desc: Fix compile error.
+	* lib/xstring-desc.h (_xsd_c): Place attributes before the return type.
+
 	string-desc: Fix compile error.
 	* lib/string-desc.h (_sd_char_at): Place attributes before the return
 	type.
diff --git a/lib/xstring-desc.h b/lib/xstring-desc.h
index 216865a267..1110bf4e0e 100644
--- a/lib/xstring-desc.h
+++ b/lib/xstring-desc.h
@@ -98,9 +98,9 @@ xsd_copy (string_desc_t s)
 
 GL_XSTRING_DESC_INLINE
 _GL_ATTRIBUTE_DEALLOC_FREE _GL_ATTRIBUTE_RETURNS_NONNULL
+_GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
 char *
 _xsd_c (idx_t s_nbytes, const char *s_data)
-  _GL_ATTRIBUTE_NONNULL_IF_NONZERO (2, 1)
 {
   char *result = _sd_c (s_nbytes, s_data);
   if (result == NULL)
-- 
2.49.0

Reply via email to