Correct __GNUC_PREREQ for __const__ and __noreturn__ attributes.

Submitted by Kang-Che Sung on March 20, 2017, 5:52 p.m.

Details

Message ID 20170320175239.5982-1-explorer09@gmail.com
State New
Headers show

Commit Message

Kang-Che Sung March 20, 2017, 5:52 p.m.
Although GCC 2.5 introduced 'const' and 'noreturn' function
attributes, the '__const__' and '__noreturn__' were not available
until 2.6.2. So check __GNU_PREREQ(2,7) for these.

Note: I'm here only to correct an inaccurate info. Because I guess
very few people would use such an old compiler now, I'm not bothered to
test, nor add support for __attribute__((const)) just for it's sake.

	* misc/sys/cdefs.h (__attribute_const__): Correct __GNUC_PREREQ
	to (2,7).
	(_Noreturn): Likewise.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
---
 ChangeLog        |  6 ++++++
 misc/sys/cdefs.h | 10 +++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

Comments

Carlos O'Donell March 20, 2017, 6:01 p.m.
On 03/20/2017 01:52 PM, Kang-Che Sung wrote:
> Although GCC 2.5 introduced 'const' and 'noreturn' function
> attributes, the '__const__' and '__noreturn__' were not available
> until 2.6.2. So check __GNU_PREREQ(2,7) for these.
> 
> Note: I'm here only to correct an inaccurate info. Because I guess
> very few people would use such an old compiler now, I'm not bothered to
> test, nor add support for __attribute__((const)) just for it's sake.

Kang-Che,

So you did not test this change with a gcc-2.5 compiler?

> 	* misc/sys/cdefs.h (__attribute_const__): Correct __GNUC_PREREQ
> 	to (2,7).
> 	(_Noreturn): Likewise.

Jakub, Marek,

Are either of you able to verify the correctness of this for us?

Thanks.
 
> Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
> ---
>  ChangeLog        |  6 ++++++
>  misc/sys/cdefs.h | 10 +++++++---
>  2 files changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/ChangeLog b/ChangeLog
> index 578714c4fb..e0ddc4497f 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,9 @@
> +2017-03-21  Kang-Che Sung  <explorer09@gmail.com>
> +
> +	* misc/sys/cdefs.h (__attribute_const__): Correct __GNUC_PREREQ
> +	to (2,7).
> +	(_Noreturn): Likewise.
> +
>  2017-03-20  Mike Frysinger  <vapier@gentoo.org>
>  
>  	[BZ #21275]
> diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
> index b03ebda22f..d040f72b70 100644
> --- a/misc/sys/cdefs.h
> +++ b/misc/sys/cdefs.h
> @@ -221,8 +221,10 @@
>  # define __attribute_pure__ /* Ignore */
>  #endif
>  
> -/* This declaration tells the compiler that the value is constant.  */
> -#if __GNUC_PREREQ (2,5)
> +/* This declaration tells the compiler that the value is constant.
> +   Although `const' attribute was introduced in gcc 2.5, we need at least
> +   gcc 2.6.2 for the `__const__' name.  */
> +#if __GNUC_PREREQ (2,7)
>  # define __attribute_const__ __attribute__ ((__const__))
>  #else
>  # define __attribute_const__ /* Ignore */
> @@ -398,7 +400,9 @@
>  #if (!defined _Noreturn \
>       && (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112 \
>       &&  !__GNUC_PREREQ (4,7))
> -# if __GNUC_PREREQ (2,8)
> +/* Although `noreturn' attribute was introduced in gcc 2.5, we need at least
> +   gcc 2.6.2 for the `__noreturn__' name.  */
> +# if __GNUC_PREREQ (2,7)
>  #  define _Noreturn __attribute__ ((__noreturn__))
>  # else
>  #  define _Noreturn
>
Joseph S. Myers March 20, 2017, 6:19 p.m.
On Mon, 20 Mar 2017, Carlos O'Donell wrote:

> On 03/20/2017 01:52 PM, Kang-Che Sung wrote:
> > Although GCC 2.5 introduced 'const' and 'noreturn' function
> > attributes, the '__const__' and '__noreturn__' were not available
> > until 2.6.2. So check __GNU_PREREQ(2,7) for these.
> > 
> > Note: I'm here only to correct an inaccurate info. Because I guess
> > very few people would use such an old compiler now, I'm not bothered to
> > test, nor add support for __attribute__((const)) just for it's sake.
> 
> Kang-Che,
> 
> So you did not test this change with a gcc-2.5 compiler?

It's not plausible to use glibc 2 with any compiler older than GCC 2.7 
(glibc 2 support was only added for GNU/Linux in some 2.7.2.x point 
release, ELF support for GNU/Linux was only added in 2.7, and I don't 
think ELF support for Hurd was present earlier either).
Kang-Che Sung March 21, 2017, 2:14 a.m.
On Tue, Mar 21, 2017 at 2:01 AM, Carlos O'Donell <carlos@redhat.com> wrote:
> On 03/20/2017 01:52 PM, Kang-Che Sung wrote:
>> Although GCC 2.5 introduced 'const' and 'noreturn' function
>> attributes, the '__const__' and '__noreturn__' were not available
>> until 2.6.2. So check __GNU_PREREQ(2,7) for these.
>>
>> Note: I'm here only to correct an inaccurate info. Because I guess
>> very few people would use such an old compiler now, I'm not bothered to
>> test, nor add support for __attribute__((const)) just for it's sake.
>
> Kang-Che,
>
> So you did not test this change with a gcc-2.5 compiler?

No, I only correct this based on the ONEWS entries in gcc package...

https://gcc.gnu.org/viewcvs/gcc/trunk/gcc/ONEWS?view=markup

...and a ChangeLog entry dated 1994...

> Thu Nov 10 18:02:58 1994  Michael Meissner  <meissner@pasta.osf.org>
> * c-common.c (decl_attributes): Check for attributes with leading
> and trailing double underscores.

...and a source diff of 2.6.2 proving that "__const__" is added here:

https://ftp.gnu.org/old-gnu/gcc/Version2.diffs/gcc-2.6.1-2.6.2.diff.gz

(By the way, this is my first time submitting a patch for glibc. So
excuse if I did something wrong)
Paul Eggert March 22, 2017, 1:55 a.m.
For what it's worth, Gnulib code uses __attribute__ ((__noreturn__)) for GCC 2.8 
and later, and  __attribute__ ((__const__)) for GCC 2.95 and later. I vaguely 
recall problems using these attributes in earlier GCC versions, though I'm 
afraid I don't remember the details. Perhaps Glibc wouldn't run into the 
problems, whatever they were.
Joseph S. Myers March 22, 2017, 5:32 p.m.
On Tue, 21 Mar 2017, Paul Eggert wrote:

> For what it's worth, Gnulib code uses __attribute__ ((__noreturn__)) for GCC
> 2.8 and later, and  __attribute__ ((__const__)) for GCC 2.95 and later. I
> vaguely recall problems using these attributes in earlier GCC versions, though
> I'm afraid I don't remember the details. Perhaps Glibc wouldn't run into the
> problems, whatever they were.

I think the 2.8 conditional may be in order to support it at the start of 
a function declaration (before the return type), i.e. compatibly with 
_Noreturn.
Kang-Che Sung March 24, 2017, 10:08 a.m.
On Thu, Mar 23, 2017 at 1:32 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Tue, 21 Mar 2017, Paul Eggert wrote:
>
>> For what it's worth, Gnulib code uses __attribute__ ((__noreturn__)) for GCC
>> 2.8 and later, and  __attribute__ ((__const__)) for GCC 2.95 and later. I
>> vaguely recall problems using these attributes in earlier GCC versions, though
>> I'm afraid I don't remember the details. Perhaps Glibc wouldn't run into the
>> problems, whatever they were.
>
> I think the 2.8 conditional may be in order to support it at the start of
> a function declaration (before the return type), i.e. compatibly with
> _Noreturn.

So I took some time and tested the __attribute__ syntax in gcc 2.7.2
(in the old Debian 1.1) and a custom-built gcc 2.8.0 (not fully
bootstrapped but enough to compile test code).

And yes you're correct; the prefix attribute syntax doesn't work until
2.8.0.

Thanks both Paul Eggert and Joseph Myers.

(By the way, should we add a comment about this info in the code?)
Kang-Che Sung March 24, 2017, 10:32 a.m.
To be precise, this correctly parses in gcc-2.7.2 according to my testing:

void __attribute__((__noreturn__)) f(void) {  exit(1); }

But this will not until gcc-2.8.0:

__attribute__((__noreturn__)) void f(void) { exit(1); }

Unfortunately the former way is not compatible with C11's _Noreturn syntax.
I mean I can't just "#define _Noreturn __attribute__((__noreturn__))" and
expect it work.

I guess that's it.

Patch hide | download patch | download mbox

diff --git a/ChangeLog b/ChangeLog
index 578714c4fb..e0ddc4497f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@ 
+2017-03-21  Kang-Che Sung  <explorer09@gmail.com>
+
+	* misc/sys/cdefs.h (__attribute_const__): Correct __GNUC_PREREQ
+	to (2,7).
+	(_Noreturn): Likewise.
+
 2017-03-20  Mike Frysinger  <vapier@gentoo.org>
 
 	[BZ #21275]
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index b03ebda22f..d040f72b70 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -221,8 +221,10 @@ 
 # define __attribute_pure__ /* Ignore */
 #endif
 
-/* This declaration tells the compiler that the value is constant.  */
-#if __GNUC_PREREQ (2,5)
+/* This declaration tells the compiler that the value is constant.
+   Although `const' attribute was introduced in gcc 2.5, we need at least
+   gcc 2.6.2 for the `__const__' name.  */
+#if __GNUC_PREREQ (2,7)
 # define __attribute_const__ __attribute__ ((__const__))
 #else
 # define __attribute_const__ /* Ignore */
@@ -398,7 +400,9 @@ 
 #if (!defined _Noreturn \
      && (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112 \
      &&  !__GNUC_PREREQ (4,7))
-# if __GNUC_PREREQ (2,8)
+/* Although `noreturn' attribute was introduced in gcc 2.5, we need at least
+   gcc 2.6.2 for the `__noreturn__' name.  */
+# if __GNUC_PREREQ (2,7)
 #  define _Noreturn __attribute__ ((__noreturn__))
 # else
 #  define _Noreturn