Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"The medium is the message." -- Marshall McLuhan


devel / comp.lang.c / Re: How to use __attribute__ in portable way

SubjectAuthor
* How to use __attribute__ in portable waypozz
`* Re: How to use __attribute__ in portable wayjak
 `* Re: How to use __attribute__ in portable waypozz
  +* Re: How to use __attribute__ in portable waySpiros Bousbouras
  |+- Re: How to use __attribute__ in portable wayChris M. Thomasson
  |`- Re: How to use __attribute__ in portable waypozz
  `- Re: How to use __attribute__ in portable wayNick Bowler

1
How to use __attribute__ in portable way

<ukk74q$3bqlg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30320&group=comp.lang.c#30320

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.lang.c
Subject: How to use __attribute__ in portable way
Date: Mon, 4 Dec 2023 10:46:02 +0100
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <ukk74q$3bqlg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 4 Dec 2023 09:46:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4e906eff2ff34945188e9efd3cd6db30";
logging-data="3533488"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+E6PWx67N6p7cBAUrjguiZz7g5iVkwt5U="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MKJj/eBanAAd3Dh1KwghmV8pqu4=
Content-Language: it
 by: pozz - Mon, 4 Dec 2023 09:46 UTC

GCC 10 introduced "access" attribute[1] to check against array
out-of-bounds.

I'd like to use it in my code, but I don't know how to write the code to
avoind warnings if the compiler doesn't support this attribute (GGC <10
or not GCC compiler).

Is a macro ATTRIBUTE_ACCESS defined in a compiler.h file a good solution?

#if GCC_GREATER_10
# define ATTRIBUTE_ACCESS(...) __attribute__((access(__VA_ARGS__)))
#else
# define ATTRIBUTE_ACCESS(...)
#endif

[1] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

Re: How to use __attribute__ in portable way

<ukl1ft$3ggjc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30321&group=comp.lang.c#30321

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Mon, 4 Dec 2023 18:15:40 +0100
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <ukl1ft$3ggjc$1@dont-email.me>
References: <ukk74q$3bqlg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 4 Dec 2023 17:15:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c264fe5f8c80ecce0cc818bb8074e985";
logging-data="3687020"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19y/Z91Nf6hE9ZWeEh32kJC"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17.1
Cancel-Lock: sha1:AJerzf1iZHHeJK3xTCBVToASs30=
In-Reply-To: <ukk74q$3bqlg$1@dont-email.me>
 by: jak - Mon, 4 Dec 2023 17:15 UTC

pozz ha scritto:
> GCC 10 introduced "access" attribute[1] to check against array
> out-of-bounds.
>
> I'd like to use it in my code, but I don't know how to write the code to
> avoind warnings if the compiler doesn't support this attribute (GGC <10
> or not GCC compiler).
>
> Is a macro ATTRIBUTE_ACCESS defined in a compiler.h file a good solution?
>
> #if GCC_GREATER_10
> #  define ATTRIBUTE_ACCESS(...)   __attribute__((access(__VA_ARGS__)))
> #else
> #  define ATTRIBUTE_ACCESS(...)
> #endif
>
> [1] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

$ gcc --version | grep gcc
gcc (GCC) 11.4.0
^ ^ ^

$ cat /dev/null | gcc -E -dM - | grep "__GNUC"
#define __GNUC__ 11 <-----
#define __GNUC_WIDE_EXECUTION_CHARSET_NAME "UTF-16LE"
#define __GNUC_EXECUTION_CHARSET_NAME "UTF-8"
#define __GNUC_PATCHLEVEL__ 0 <-----
#define __GNUC_STDC_INLINE__ 1
#define __GNUC_MINOR__ 4 <-----

$ cat foo.c
#ifdef __GNUC__
#define INF "is gcc\n"
#if(__GNUC__ >= 10)
#define VER "minimum 10.0\n"
#if(__GNUC_MINOR__ >= 1)
#define SVER "minimum 10.1\n"
#define ATTRIBUTE_ACCESS(...) __attribute__((access(__VA_ARGS__)))
#else
#define SVER "bad subversion\n"
#define ATTRIBUTE_ACCESS(...)
#endif
#else
#define VER "bad version\n"
#define ATTRIBUTE_ACCESS(...)
#endif
#else
#define INF "no gcc\n"
#define ATTRIBUTE_ACCESS(...)
#endif
#include <stdio.h>

ATTRIBUTE_ACCESS(read_only, 1)
void foo(int arr[], size_t size)
{ for(int i=0;i<size;i++) printf("%d\n", arr[i]);
}

int main()
{ int iArr[]={1,2,3,4,5,6};
printf(INF VER SVER);
foo(iArr, 6);
}

$ ./foo
is gcc
minimum 10.0
minimum 10.1
1 2
3 4
5 6

....or I didn't understand the question, then sorry.

Re: How to use __attribute__ in portable way

<ukmktb$2ptm$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30324&group=comp.lang.c#30324

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Tue, 5 Dec 2023 08:53:15 +0100
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <ukmktb$2ptm$1@dont-email.me>
References: <ukk74q$3bqlg$1@dont-email.me> <ukl1ft$3ggjc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 5 Dec 2023 07:53:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="93b9795085b09d144256caf8a9450385";
logging-data="92086"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5SKgusTlg9nvAraP5/b3FUlCS5h1g/WE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:2kQLiytsR/7tC1fCPtMR8qMuDqg=
Content-Language: it
In-Reply-To: <ukl1ft$3ggjc$1@dont-email.me>
 by: pozz - Tue, 5 Dec 2023 07:53 UTC

Il 04/12/2023 18:15, jak ha scritto:
> pozz ha scritto:
>> GCC 10 introduced "access" attribute[1] to check against array
>> out-of-bounds.
>>
>> I'd like to use it in my code, but I don't know how to write the code
>> to avoind warnings if the compiler doesn't support this attribute (GGC
>> <10 or not GCC compiler).
>>
>> Is a macro ATTRIBUTE_ACCESS defined in a compiler.h file a good solution?
>>
>> #if GCC_GREATER_10
>> #  define ATTRIBUTE_ACCESS(...)   __attribute__((access(__VA_ARGS__)))
>> #else
>> #  define ATTRIBUTE_ACCESS(...)
>> #endif
>>
>> [1] https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
>
> $ gcc --version | grep gcc
> gcc (GCC) 11.4.0
>            ^ ^ ^
>
> $ cat /dev/null | gcc -E -dM - | grep "__GNUC"
> #define __GNUC__ 11                                    <-----
> #define __GNUC_WIDE_EXECUTION_CHARSET_NAME "UTF-16LE"
> #define __GNUC_EXECUTION_CHARSET_NAME "UTF-8"
> #define __GNUC_PATCHLEVEL__ 0                          <-----
> #define __GNUC_STDC_INLINE__ 1
> #define __GNUC_MINOR__ 4                               <-----
>
> $ cat foo.c
> #ifdef __GNUC__
>     #define INF "is gcc\n"
>     #if(__GNUC__ >= 10)
>         #define VER "minimum 10.0\n"
>         #if(__GNUC_MINOR__ >= 1)
>             #define SVER "minimum 10.1\n"
> #define ATTRIBUTE_ACCESS(...)   __attribute__((access(__VA_ARGS__)))
>         #else
>             #define SVER "bad subversion\n"
> #define ATTRIBUTE_ACCESS(...)
>         #endif
>     #else
>         #define VER "bad version\n"
> #define ATTRIBUTE_ACCESS(...)
>     #endif
> #else
>     #define INF "no gcc\n"
> #define ATTRIBUTE_ACCESS(...)
> #endif
> #include <stdio.h>
>
> ATTRIBUTE_ACCESS(read_only, 1)
> void  foo(int arr[], size_t size)
> {
>   for(int i=0;i<size;i++) printf("%d\n", arr[i]);
> }
>
> int main()
> {
>     int iArr[]={1,2,3,4,5,6};
>     printf(INF VER SVER);
>     foo(iArr, 6);
> }
>
> $ ./foo
> is gcc
> minimum 10.0
> minimum 10.1
> 1
> 2
> 3
> 4
> 5
> 6
>
> ...or I didn't understand the question, then sorry.
>
>

Yes, thank you. I was wondering if there was a "standard" way to manage
this compiler dependent things.

Re: How to use __attribute__ in portable way

<eNnvcwqhtx3goGBT0@bongo-ra.co>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30325&group=comp.lang.c#30325

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Tue, 5 Dec 2023 12:53:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <eNnvcwqhtx3goGBT0@bongo-ra.co>
References: <ukk74q$3bqlg$1@dont-email.me> <ukl1ft$3ggjc$1@dont-email.me> <ukmktb$2ptm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 5 Dec 2023 12:53:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d3a16ad1b914979abfcbfda09c32002e";
logging-data="181532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/IYn8FwWcCtUvm0qDMOK2Z"
Cancel-Lock: sha1:NNOHw45EmcgjHoB5ReCDd6jWz/k=
In-Reply-To: <ukmktb$2ptm$1@dont-email.me>
X-Server-Commands: nowebcancel
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Tue, 5 Dec 2023 12:53 UTC

On Tue, 5 Dec 2023 08:53:15 +0100
pozz <pozzugno@gmail.com> wrote:
> Yes, thank you. I was wondering if there was a "standard" way to manage
> this compiler dependent things.

How standard is "standard" ? If it's compiler dependent then it cannot be
completely standard. I guess you could hope for a common convention for C
compilers to describe their version number but I don't know of such a
convention and I'm not even sure that such could exist in principle because
not all software projects use the same version format and version formats and
numbers won't necessarily mean the same thing between different projects
anyway.

<ukl1ft$3ggjc$1@dont-email.me> gave you what you need but you can also
have a look at gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html .

--
The director of photography was probably a camcorder taped onto a skateboard
and pushed forward until it hits a wall.
www.imdb.com/review/rw1008884

Re: How to use __attribute__ in portable way

<uko582$d8cl$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30333&group=comp.lang.c#30333

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Tue, 5 Dec 2023 13:38:09 -0800
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <uko582$d8cl$2@dont-email.me>
References: <ukk74q$3bqlg$1@dont-email.me> <ukl1ft$3ggjc$1@dont-email.me>
<ukmktb$2ptm$1@dont-email.me> <eNnvcwqhtx3goGBT0@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Dec 2023 21:38:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ef2c7d03227d49d7dc4c9023755bfb94";
logging-data="434581"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cG84yoFXEDHJnhMoS5UXm2dG4netqCyo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:0ciyX+SLeMy7o6hrRkxLR2oLjWk=
In-Reply-To: <eNnvcwqhtx3goGBT0@bongo-ra.co>
Content-Language: en-US
 by: Chris M. Thomasson - Tue, 5 Dec 2023 21:38 UTC

On 12/5/2023 4:53 AM, Spiros Bousbouras wrote:
> On Tue, 5 Dec 2023 08:53:15 +0100
> pozz <pozzugno@gmail.com> wrote:
>> Yes, thank you. I was wondering if there was a "standard" way to manage
>> this compiler dependent things.
>
> How standard is "standard" ?

Indeed! :^)

> If it's compiler dependent then it cannot be
> completely standard. I guess you could hope for a common convention for C
> compilers to describe their version number but I don't know of such a
> convention and I'm not even sure that such could exist in principle because
> not all software projects use the same version format and version formats and
> numbers won't necessarily mean the same thing between different projects
> anyway.
>
> <ukl1ft$3ggjc$1@dont-email.me> gave you what you need but you can also
> have a look at gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html .
>

Re: How to use __attribute__ in portable way

<ukpdfe$lvgd$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30340&group=comp.lang.c#30340

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Wed, 6 Dec 2023 10:04:47 +0100
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <ukpdfe$lvgd$1@dont-email.me>
References: <ukk74q$3bqlg$1@dont-email.me> <ukl1ft$3ggjc$1@dont-email.me>
<ukmktb$2ptm$1@dont-email.me> <eNnvcwqhtx3goGBT0@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 6 Dec 2023 09:04:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="373cc6b8bf235fbf9769bbad98cea512";
logging-data="720397"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fOml6iLYI1jE4lCoZYXvWHgkqbFgftPo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1SqXt+atgMYHECQNaI53l98fwzI=
Content-Language: it
In-Reply-To: <eNnvcwqhtx3goGBT0@bongo-ra.co>
 by: pozz - Wed, 6 Dec 2023 09:04 UTC

Il 05/12/2023 13:53, Spiros Bousbouras ha scritto:
> On Tue, 5 Dec 2023 08:53:15 +0100
> pozz <pozzugno@gmail.com> wrote:
>> Yes, thank you. I was wondering if there was a "standard" way to manage
>> this compiler dependent things.
>
> How standard is "standard" ?

Indeed I wrote standard in quotes...

If it's compiler dependent then it cannot be
> completely standard. I guess you could hope for a common convention for C
> compilers to describe their version number but I don't know of such a
> convention and I'm not even sure that such could exist in principle because
> not all software projects use the same version format and version formats and
> numbers won't necessarily mean the same thing between different projects
> anyway.
>
> <ukl1ft$3ggjc$1@dont-email.me> gave you what you need but you can also
> have a look at gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html .
>

Re: How to use __attribute__ in portable way

<uksvf7$1bcc9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=30345&group=comp.lang.c#30345

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nbow...@draconx.ca (Nick Bowler)
Newsgroups: comp.lang.c
Subject: Re: How to use __attribute__ in portable way
Date: Thu, 7 Dec 2023 17:30:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <uksvf7$1bcc9$1@dont-email.me>
References: <ukk74q$3bqlg$1@dont-email.me> <ukl1ft$3ggjc$1@dont-email.me>
<ukmktb$2ptm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Dec 2023 17:30:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="70fdd74bfe8b4811687aef4644d5e0aa";
logging-data="1421705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pPyZ+OQDF1KIFWLdWDgcr"
User-Agent: Pan/0.149 (Bellevue; 4c157ba git@gitlab.gnome.org:GNOME/pan.git)
Cancel-Lock: sha1:jQoVHzAkfTCSAsWoaE4cr3x8iWU=
 by: Nick Bowler - Thu, 7 Dec 2023 17:30 UTC

On Tue, 5 Dec 2023 08:53:15 +0100, pozz wrote:
> Il 04/12/2023 18:15, jak ha scritto:
>> pozz ha scritto:
>>> GCC 10 introduced "access" attribute[1] to check against array
>>> out-of-bounds.
>>>
>>> I'd like to use it in my code, but I don't know how to write the code
>>> to avoind warnings if the compiler doesn't support this attribute (GGC
>>> <10 or not GCC compiler).
>>>
>>> Is a macro ATTRIBUTE_ACCESS defined in a compiler.h file a good
>>> solution?
>>>
>>> #if GCC_GREATER_10 #  define ATTRIBUTE_ACCESS(...)  
>>> __attribute__((access(__VA_ARGS__)))
>>> #else #  define ATTRIBUTE_ACCESS(...)
>>> #endif
[...]
> Yes, thank you. I was wondering if there was a "standard" way to manage
> this compiler dependent things.

Instead of checking for specific compiler versions (such checks are never
particularly accurate[1]), gcc provides a built-in way[2] to directly query
in the preprocessor whether or not a particular attribute is supported.
This is designed specifically for scenarios like yours:

#ifdef __has_attribute
# if __has_attribute(access)
# define use_newfangled_gcc_access_attribute 1
# endif
#endif

/* ... */

#if use_newfangled_gcc_access_attribute
/* use the attribute */
#else
/* don't use the attribute */
#endif

[1] for example, clang defines the gcc version macros pretending to be gcc 4.2,
but it does not implement every single feature of gcc 4.2, and it also
includes many features that are only available in newer versions of gcc.

[2] https://gcc.gnu.org/onlinedocs/gcc-13.2.0/cpp/_005f_005fhas_005fattribute.html

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor