public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Strict Aliasing document
@ 2010-12-31 12:59 Patrick Horgan
  2011-01-04 13:44 ` Andrew Haley
  0 siblings, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2010-12-31 12:59 UTC (permalink / raw)
  To: GCC-help

Some time ago I asked for reviews of a white paper on strict aliasing 
and the confusions people have about it, on this mailing list and got 
some great comments.  I've rewritten the paper in response to the 
comments and would love it if people would read it and give MORE 
comments about ways to improve it.  The intent is to provide something 
that I can refer people to when the issue comes up each time.  The 
document is :

http://dbp-consulting.com/StrictAliasing.pdf

Thanks,

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Strict Aliasing document
  2010-12-31 12:59 Strict Aliasing document Patrick Horgan
@ 2011-01-04 13:44 ` Andrew Haley
  2011-01-04 21:18   ` Patrick Horgan
  0 siblings, 1 reply; 16+ messages in thread
From: Andrew Haley @ 2011-01-04 13:44 UTC (permalink / raw)
  To: gcc-help

On 12/31/2010 09:04 AM, Patrick Horgan wrote:
> Some time ago I asked for reviews of a white paper on strict aliasing and the confusions people have about it, on this mailing list and got some great comments. I've rewritten the paper in response to the comments and would love it if people would read it and give MORE comments about ways to improve it. The intent is to provide something that I can refer people to when the issue comes up each time. The document is :
>
> http://dbp-consulting.com/StrictAliasing.pdf

This link is broken.

Andrew.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Strict Aliasing document
  2011-01-04 13:44 ` Andrew Haley
@ 2011-01-04 21:18   ` Patrick Horgan
  2011-01-05  9:36     ` Andrew Haley
  0 siblings, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2011-01-04 21:18 UTC (permalink / raw)
  To: gcc-help

On 01/04/2011 05:44 AM, Andrew Haley wrote:
> On 12/31/2010 09:04 AM, Patrick Horgan wrote:
>> Some time ago I asked for reviews of a white paper on strict aliasing 
>> and the confusions people have about it, on this mailing list and got 
>> some great comments. I've rewritten the paper in response to the 
>> comments and would love it if people would read it and give MORE 
>> comments about ways to improve it. The intent is to provide something 
>> that I can refer people to when the issue comes up each time. The 
>> document is :
>>
>> http://dbp-consulting.com/StrictAliasing.pdf
>
> This link is broken.
>
> Andrew.
>
I just did: wget http://dbp-consulting.com/StrictAliasing.pdf

and it came down fine.

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Strict Aliasing document
  2011-01-04 21:18   ` Patrick Horgan
@ 2011-01-05  9:36     ` Andrew Haley
  2011-01-07  0:28       ` Patrick Horgan
  0 siblings, 1 reply; 16+ messages in thread
From: Andrew Haley @ 2011-01-05  9:36 UTC (permalink / raw)
  To: gcc-help

On 01/04/2011 09:18 PM, Patrick Horgan wrote:
> On 01/04/2011 05:44 AM, Andrew Haley wrote:
>> On 12/31/2010 09:04 AM, Patrick Horgan wrote:
>>> Some time ago I asked for reviews of a white paper on strict aliasing and the confusions people have about it, on this mailing list and got some great comments. I've rewritten the paper in response to the comments and would love it if people would read it and give MORE comments about ways to improve it. The intent is to provide something that I can refer people to when the issue comes up each time. The document is :
>>>
>>> http://dbp-consulting.com/StrictAliasing.pdf
>>
>> This link is broken.
>>
>> Andrew.
>>
> I just did: wget http://dbp-consulting.com/StrictAliasing.pdf
>
> and it came down fine.

I got it now.

You're wrong about using memcpy(): in many cases gcc will elide calls
to memmcpy(), and indeed it does in your example!  There's no reason
to assume that the union version will always be faster.

It'd be better to define swapem as

typedef union {
     uint32_t as32bit;
     uint16_t as16bit[2];
} swapem;

which is standard in C and C++.

It's worth saying that just about every compiler supports type-punning
through a union, but it is *not* guaranteed by any standard.

Nice job.

Andrew.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Strict Aliasing document
  2011-01-05  9:36     ` Andrew Haley
@ 2011-01-07  0:28       ` Patrick Horgan
  2011-01-10 21:35         ` How do I find when the diagnostic pragmas first came into gcc? Patrick Horgan
  0 siblings, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2011-01-07  0:28 UTC (permalink / raw)
  To: gcc-help

Thanks to everyone who commented on the white paper, it's a lot better 
because of the comments I got here and on boost-users.

In particular I'd like to thank Václav Haisman, Thomas Heller who wrote 
the memcpy version of the routine I use in the document and pointed out 
that it will generate exactly the same assembler, and Andrew Haley who 
pointed out a more portable way to define the union, and also pointed 
out that gcc will elide the calls to memcpy.

Current version at http://dbp-consulting.com/StrictAliasing.pdf

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* How do I find when the diagnostic pragmas first came into gcc?
  2011-01-07  0:28       ` Patrick Horgan
@ 2011-01-10 21:35         ` Patrick Horgan
  2011-01-10 23:16           ` Jonathan Wakely
  2011-01-10 23:44           ` Patrick Horgan
  0 siblings, 2 replies; 16+ messages in thread
From: Patrick Horgan @ 2011-01-10 21:35 UTC (permalink / raw)
  To: gcc-help

I'm documenting the use of the diagnostic pragmas:

|#pragma GCC diagnostic |kind option
|#pragma GCC diagnostic push|
|#pragma GCC diagnostic pop|

on a boost page talking about how developers might deal with gcc 
warnings and was wondering how to figure out what test I could put into 
a #if that would avoid trying to use these pragmas in releases that did 
not support them yet.

Something like:

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#endif

// code here that generates a spurious warning controlled by -Wformat

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-10 21:35         ` How do I find when the diagnostic pragmas first came into gcc? Patrick Horgan
@ 2011-01-10 23:16           ` Jonathan Wakely
  2011-01-10 23:44           ` Patrick Horgan
  1 sibling, 0 replies; 16+ messages in thread
From: Jonathan Wakely @ 2011-01-10 23:16 UTC (permalink / raw)
  To: phorgan1; +Cc: gcc-help

On 10 January 2011 21:35, Patrick Horgan wrote:
> I'm documenting the use of the diagnostic pragmas:
>
> |#pragma GCC diagnostic |kind option
> |#pragma GCC diagnostic push|
> |#pragma GCC diagnostic pop|
>
> on a boost page talking about how developers might deal with gcc warnings
> and was wondering how to figure out what test I could put into a #if that
> would avoid trying to use these pragmas in releases that did not support
> them yet.
>
> Something like:
>
> #if defined(__GNUC__)
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wformat"
> #endif
>
> // code here that generates a spurious warning controlled by -Wformat
>
> #if defined(__GNUC__)
> #pragma GCC diagnostic pop
> #endif

You read the manual, obviously.

http://gcc.gnu.org/onlinedocs/

The Diagnostic Pragmas section in each release tells you what's supported:

http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Diagnostic-Pragmas.html
http://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Diagnostic-Pragmas.html
http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Diagnostic-Pragmas.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-10 21:35         ` How do I find when the diagnostic pragmas first came into gcc? Patrick Horgan
  2011-01-10 23:16           ` Jonathan Wakely
@ 2011-01-10 23:44           ` Patrick Horgan
  2011-01-11  0:48             ` Jonathan Wakely
  1 sibling, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2011-01-10 23:44 UTC (permalink / raw)
  To: gcc-help

On 01/10/2011 01:35 PM, Patrick Horgan wrote:
> I'm documenting the use of the diagnostic pragmas:
>
> |#pragma GCC diagnostic |kind option
> |#pragma GCC diagnostic push|
> |#pragma GCC diagnostic pop|
>
> on a boost page talking about how developers might deal with gcc 
> warnings and was wondering how to figure out what test I could put 
> into a #if that would avoid trying to use these pragmas in releases 
> that did not support them yet.
>
> Something like:
>
> #if defined(__GNUC__)
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wformat"
> #endif
>
> // code here that generates a spurious warning controlled by -Wformat
>
> #if defined(__GNUC__)
> #pragma GCC diagnostic pop
> #endif
My understanding is that:
pragma GCC system_header
is usable for releases 4 and greater, but that prior to 4.6, it had to 
be at file scope and then affected from that point forward.  At 4.6 and 
later it will be able to be inserted anywhere in the file.

pragma GCC diagnostic <warning|error|ignored> "-WSOMETYPEOFWARNING"
is usable at 4.1 or 4.2? and had to be put at file scope and affected 
from that point forward.  At 4.6 and later it can be put at any line in 
the file.

pragma GCC diagnostic <push|pop>
Not available until 4.6 and can be put at any point in the file.

Is all that right?

So what preprocessor tests can be used so that with -Wall which turns on 
-Wunknown-pragma I won't get warnings about unknown pragmas?

Patrick
>
>
>
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-10 23:44           ` Patrick Horgan
@ 2011-01-11  0:48             ` Jonathan Wakely
  2011-01-11  7:53               ` Patrick Horgan
  0 siblings, 1 reply; 16+ messages in thread
From: Jonathan Wakely @ 2011-01-11  0:48 UTC (permalink / raw)
  To: phorgan1; +Cc: gcc-help

On 10 January 2011 23:44, Patrick Horgan wrote:
> On 01/10/2011 01:35 PM, Patrick Horgan wrote:
>>
>> I'm documenting the use of the diagnostic pragmas:
>>
>> |#pragma GCC diagnostic |kind option
>> |#pragma GCC diagnostic push|
>> |#pragma GCC diagnostic pop|
>>
>> on a boost page talking about how developers might deal with gcc warnings
>> and was wondering how to figure out what test I could put into a #if that
>> would avoid trying to use these pragmas in releases that did not support
>> them yet.
>>
>> Something like:
>>
>> #if defined(__GNUC__)
>> #pragma GCC diagnostic push
>> #pragma GCC diagnostic ignored "-Wformat"
>> #endif
>>
>> // code here that generates a spurious warning controlled by -Wformat
>>
>> #if defined(__GNUC__)
>> #pragma GCC diagnostic pop
>> #endif
>
> My understanding is that:
> pragma GCC system_header
> is usable for releases 4 and greater, but that prior to 4.6, it had to be at
> file scope and then affected from that point forward.  At 4.6 and later it
> will be able to be inserted anywhere in the file.

It's worked since 3.0, maybe earlier.
I don't know of any change in semantics in 4.6, but I might have
missed something.

> pragma GCC diagnostic <warning|error|ignored> "-WSOMETYPEOFWARNING"
> is usable at 4.1 or 4.2? and had to be put at file scope and affected from

4.2

> that point forward.  At 4.6 and later it can be put at any line in the file.

I think that's right.

> pragma GCC diagnostic <push|pop>
> Not available until 4.6 and can be put at any point in the file.

Right.

> Is all that right?
>
> So what preprocessor tests can be used so that with -Wall which turns on
> -Wunknown-pragma I won't get warnings about unknown pragmas?

 __GNUC__ and __GNUC_MINOR__
http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-11  0:48             ` Jonathan Wakely
@ 2011-01-11  7:53               ` Patrick Horgan
  2011-01-11 10:10                 ` Jonathan Wakely
  0 siblings, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2011-01-11  7:53 UTC (permalink / raw)
  To: gcc-help

Thank you Jonathan.  I came up with this (if you have any improvements 
please comment):

First, the following code will have a warning about signed vs unsigned 
comparison when compiling.


#include <iostream>
int
main()
{
     size_t a = 7;
     int b = -3;
     if(a < b){
         std::cout << "a<b\n";
     }else{
         std::cout << "a!<b\n";
     }
     return 0;
}

$ g++ -Wall -o testit testit.cpp
testit.cpp:7:12: warning: comparison between signed and unsigned integer 
expressions
$

Compiling with -fdiagnostics-show-option results in the diagnostic:

$ g++ -Wall -fdiagnostics-show-option -o testit testit.cpp
testit.cpp:7:12: warning: comparison between signed and unsigned integer 
expressions [-Wsign-compare]
$

Knowing the argument controlling this error now is -Wsign-compare, we 
can rewrite like the following to get rid of the warning:

#include <iostream>
int
main()
{
     size_t a = 7;
     int b = -3;
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && 
__GNUC_MINOR__ >= 6))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#endif
     if(a < b){
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && 
__GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop
#endif
         std::cout << "a<b\n";
     }else{
         std::cout << "a!<b\n";
     }
     return 0;
}

When compiling again the warning is suppressed:

$ g++ -Wall -o testit testit.cpp
$

Ugly but effective.  I still say fix all the warnings.  Thank you.

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-11  7:53               ` Patrick Horgan
@ 2011-01-11 10:10                 ` Jonathan Wakely
  2011-01-11 11:03                   ` Jonathan Wakely
  2011-01-12  1:20                   ` Patrick Horgan
  0 siblings, 2 replies; 16+ messages in thread
From: Jonathan Wakely @ 2011-01-11 10:10 UTC (permalink / raw)
  To: phorgan1; +Cc: gcc-help

On 11 January 2011 07:52, Patrick Horgan wrote:
> int
> main()
> {
>    size_t a = 7;
>    int b = -3;
> #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__
>>= 6))

I use something like this:

#if ((__GNUC__*100)+__GNUC_MINOR__) >= 406

If the macros aren't defined they will evaluate to zero.

> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wsign-compare"
> #endif


> Ugly but effective.

I wrote this last week, which tames some of the ugliness:

#if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 405
# define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
# define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
#  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
     GCC_DIAG_PRAGMA(ignored x)
#  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
# else
#  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x)
#  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning x)
# endif
#else
# define GCC_DIAG_OFF(x)
# define GCC_DIAG_ON(x)
#endif

That gives convenient macros which are used like so:

GCC_DIAG_OFF("-Wsign-compare")
   if (a < b) {
GCC_DIAG_ON("-Wsign-compare")
       std::cout << "a<b\n";
   }

The macros use push/pop for 4.6+, or for 4.5 they just change the
behaviour to "ignored" then back to "warning".   They expand to
nothing for older versions of GCC or for non-GCC compilers.

The 4.5 behaviour is wrong if e.g. -Werror=sign-compare was being
used, because GCC_DIAG_ON re-enables a warning not an error. For my
purposes that was sufficient, it's probably not acceptable for library
code.

I've been using this for super-strict compilations with a modified GCC
that has extra warnings I've added. One of my extra warnings has too
many false positives, so I really needed to disable some known
warnings I wasn't going to fix.

For these super-strict builds, I need to temporarily disable warnings
in third-party libraries which I can't or don't want to change (Boost
in particular.) To do this I include them with -isystem instead of -I,
which has the same effect as adding the system_header pragma to them.
That allows me to make warnings fatal with -Werror without having to
worry about errors in code I don't own.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-11 10:10                 ` Jonathan Wakely
@ 2011-01-11 11:03                   ` Jonathan Wakely
  2011-01-12  1:20                   ` Patrick Horgan
  1 sibling, 0 replies; 16+ messages in thread
From: Jonathan Wakely @ 2011-01-11 11:03 UTC (permalink / raw)
  To: phorgan1; +Cc: gcc-help

On 11 January 2011 10:10, Jonathan Wakely wrote:
>
> I wrote this last week, which tames some of the ugliness:
>
> #if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 405

This check could probably be 402 not 405
(I don't have my custom warnings in any build older than 4.5, which is
why I checked 4.5)

> # define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
> # define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
> # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
>     GCC_DIAG_PRAGMA(ignored x)
> #  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
> # else
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x)
> #  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning x)
> # endif
> #else
> # define GCC_DIAG_OFF(x)
> # define GCC_DIAG_ON(x)
> #endif
>
> That gives convenient macros which are used like so:
>
> GCC_DIAG_OFF("-Wsign-compare")
>   if (a < b) {
> GCC_DIAG_ON("-Wsign-compare")
>       std::cout << "a<b\n";
>   }

Oops, this example assumes the pragma can occur at function scope
which is only true for 4.6, so if you do want to make the macros work
with 4.2 to 4.5 then they should only be used at namespace scope.

Another option would be to have a file-scope GCC_DIAG_OFF_FOO for GCC
4.2 - 4.5 (which expands to nothing for 4.6+) and a finer-grained
GCC_DIAG_OFF_BAR for GCC 4.6+ (which expands to nothing for pre-4.6)
but then library authors have to maintain two OFF/ON pairs for each
warning they want to disable.

If you only care about supporting 4.6+ then life is easier and
GCC_DIAG_ON doesn't even need an argument (as it just pops the last
OFF)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-11 10:10                 ` Jonathan Wakely
  2011-01-11 11:03                   ` Jonathan Wakely
@ 2011-01-12  1:20                   ` Patrick Horgan
  2011-01-12 10:08                     ` Jonathan Wakely
  2011-01-13  0:21                     ` Patrick Horgan
  1 sibling, 2 replies; 16+ messages in thread
From: Patrick Horgan @ 2011-01-12  1:20 UTC (permalink / raw)
  To: gcc-help, Jonathan Wakely

On 01/11/2011 02:10 AM, Jonathan Wakely wrote:
> ... elision by patrick...
> I use something like this:
>
> #if ((__GNUC__*100)+__GNUC_MINOR__)>= 406
>
> If the macros aren't defined they will evaluate to zero.
Thanks, I was thinking of something similar, but this is nothing 
compared to the awesomeness that follows.
> ... more elision by patrick ...
> I wrote this last week, which tames some of the ugliness:
>
> #if ((__GNUC__ * 100) + __GNUC_MINOR__)>= 405
> # define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
> # define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
> # if ((__GNUC__ * 100) + __GNUC_MINOR__)>= 406
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
>       GCC_DIAG_PRAGMA(ignored x)
> #  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
> # else
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x)
> #  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning x)
> # endif
> #else
> # define GCC_DIAG_OFF(x)
> # define GCC_DIAG_ON(x)
> #endif
This is great, and supports the use to turn warnings off wonderfully.  
 From reading this I assume that 4.5 is the point at which the pragmas 
were able to be put at any scope instead of just file scope.  Is this 
also true for #pragma GCC system_header as well, that prior to 4.5 it 
had to be at file scope, but from 4.6 forward it can be at any scope?

Also, can I steal, with attribution of course, GCC_DIAG_OFF/ON(x)?

Also is there an implementation define nesting limit to #pragma GCC 
diagnostic push as there is in the similar microsoft compiler #pragma 
warning push?  (Their limit is about 50 I think).  That seems like it 
would be plenty, but it's been hit in boost code that was doing tmp.

Also, what if there are too many pops?  Is that an error, or ignored?

Also did you consider making them so that you could use them like:

GCC_DIAG_OFF(sign-compare)
    if (a<  b) {
GCC_DIAG_ON(sign-compare)
        std::cout<<  "a<b\n";
    }


just to save the four keystrokes that would be common to all?  Something 
like:

#define str(s) #s
#define joinstr(x,y) str(x ## y)

#if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 405
# define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
# define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
#  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
          GCC_DIAG_PRAGMA(ignored joinstr(-W,x))
#  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
# else
#  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored joinstr(-W,x))
#  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning joinstr(-W,x))
# endif
#else
# define GCC_DIAG_OFF(x)
# define GCC_DIAG_ON(x)
#endif

I know it's a little silly, but I get carried away wanting the compiler 
to do stuff for me.

Patrick

> That gives convenient macros which are used like so:
>
> GCC_DIAG_OFF("-Wsign-compare")
>     if (a<  b) {
> GCC_DIAG_ON("-Wsign-compare")
>         std::cout<<  "a<b\n";
>     }
>
> The macros use push/pop for 4.6+, or for 4.5 they just change the
> behaviour to "ignored" then back to "warning".   They expand to
> nothing for older versions of GCC or for non-GCC compilers.
>
> The 4.5 behaviour is wrong if e.g. -Werror=sign-compare was being
> used, because GCC_DIAG_ON re-enables a warning not an error. For my
> purposes that was sufficient, it's probably not acceptable for library
> code.
>
> I've been using this for super-strict compilations with a modified GCC
> that has extra warnings I've added. One of my extra warnings has too
> many false positives, so I really needed to disable some known
> warnings I wasn't going to fix.
>
> For these super-strict builds, I need to temporarily disable warnings
> in third-party libraries which I can't or don't want to change (Boost
> in particular.) To do this I include them with -isystem instead of -I,
> which has the same effect as adding the system_header pragma to them.
> That allows me to make warnings fatal with -Werror without having to
> worry about errors in code I don't own.
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-12  1:20                   ` Patrick Horgan
@ 2011-01-12 10:08                     ` Jonathan Wakely
  2011-01-13  0:21                     ` Patrick Horgan
  1 sibling, 0 replies; 16+ messages in thread
From: Jonathan Wakely @ 2011-01-12 10:08 UTC (permalink / raw)
  To: phorgan1; +Cc: gcc-help

On 12 January 2011 01:20, Patrick Horgan wrote:
> On 01/11/2011 02:10 AM, Jonathan Wakely wrote:
>> I wrote this last week, which tames some of the ugliness:
>>
>> #if ((__GNUC__ * 100) + __GNUC_MINOR__)>= 405
>> # define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
>> # define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
>> # if ((__GNUC__ * 100) + __GNUC_MINOR__)>= 406
>> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
>>      GCC_DIAG_PRAGMA(ignored x)
>> #  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
>> # else
>> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored x)
>> #  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning x)
>> # endif
>> #else
>> # define GCC_DIAG_OFF(x)
>> # define GCC_DIAG_ON(x)
>> #endif
>
> This is great, and supports the use to turn warnings off wonderfully.  From
> reading this I assume that 4.5 is the point at which the pragmas were able
> to be put at any scope instead of just file scope.  Is this also true for

No, see my follow-up mail.

> #pragma GCC system_header as well, that prior to 4.5 it had to be at file
> scope, but from 4.6 forward it can be at any scope?

As I said in another mail, I'm not sure if the system_header semantics
have changed.

> Also, can I steal, with attribution of course, GCC_DIAG_OFF/ON(x)?

Certainly.

> Also is there an implementation define nesting limit to #pragma GCC
> diagnostic push as there is in the similar microsoft compiler #pragma
> warning push?  (Their limit is about 50 I think).  That seems like it would
> be plenty, but it's been hit in boost code that was doing tmp.
>
> Also, what if there are too many pops?  Is that an error, or ignored?

I don't know, you'll have to try.

> Also did you consider making them so that you could use them like:
>
> GCC_DIAG_OFF(sign-compare)
>   if (a<  b) {
> GCC_DIAG_ON(sign-compare)
>       std::cout<<  "a<b\n";
>   }
>
>
> just to save the four keystrokes that would be common to all?  Something
> like:
>
> #define str(s) #s
> #define joinstr(x,y) str(x ## y)
>
> #if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 405
> # define GCC_DIAG_DO_PRAGMA(x) _Pragma (#x)
> # define GCC_DIAG_PRAGMA(x) GCC_DIAG_DO_PRAGMA(GCC diagnostic x)
> # if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(push) \
>         GCC_DIAG_PRAGMA(ignored joinstr(-W,x))
> #  define GCC_DIAG_ON(x) GCC_DIAG_PRAGMA(pop)
> # else
> #  define GCC_DIAG_OFF(x) GCC_DIAG_PRAGMA(ignored joinstr(-W,x))
> #  define GCC_DIAG_ON(x)  GCC_DIAG_PRAGMA(warning joinstr(-W,x))
> # endif
> #else
> # define GCC_DIAG_OFF(x)
> # define GCC_DIAG_ON(x)
> #endif
>
> I know it's a little silly, but I get carried away wanting the compiler to
> do stuff for me.

I didn't do that, as my use case is actually much simpler, but it
seems like a nice improvement.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: How do I find when the diagnostic pragmas first came into gcc?
  2011-01-12  1:20                   ` Patrick Horgan
  2011-01-12 10:08                     ` Jonathan Wakely
@ 2011-01-13  0:21                     ` Patrick Horgan
  2011-01-13  2:40                       ` Is it possible for a macro to tell file/namespace scope from other scopes? Patrick Horgan
  1 sibling, 1 reply; 16+ messages in thread
From: Patrick Horgan @ 2011-01-13  0:21 UTC (permalink / raw)
  To: gcc-help

On 01/11/2011 05:20 PM, Patrick Horgan wrote:
> ... elision by patrick ...
> This is great, and supports the use to turn warnings off wonderfully.  
> From reading this I assume that 4.5 is the point at which the pragmas 
> were able to be put at any scope instead of just file scope.  Is this 
> also true for #pragma GCC system_header as well, that prior to 4.5 it 
> had to be at file scope, but from 4.6 forward it can be at any scope?
Ok.  I get it.  It has to be 4.6 to be at any scope, not 4.5
> ... elision by patrick ...
>
> Also is there an implementation defined nesting limit to #pragma GCC 
> diagnostic push as there is in the similar microsoft compiler #pragma 
> warning push?  (Their limit is about 50 I think).  That seems like it 
> would be plenty, but it's been hit in boost code that was doing tmp.
I read the source and found that the only limit is available memory for 
the stack, since each time something is pushed the stack is xrealloc'd 
(the libiberty version of realloc that prints an error to stderr and 
exits on failure), one bigger, and by the size of an index to that stack 
that can be held in a signed integer.  Funny they didn't use unsigned 
integer, so the limit would be higher.  It's one of my pet peeves, when 
someone means something to function as a counter from 0-N positive, but 
uses an int.  It lacks elegance.  Oh, well, I wouldn't like people 
dissecting my code!  lol!
>
> Also, what if there are too many pops?  Is that an error, or ignored?
Also reading the source, reverts to state as set by command line 
arguments when the diagnostic stack is empty.

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Is it possible for a macro to tell file/namespace scope from other scopes?
  2011-01-13  0:21                     ` Patrick Horgan
@ 2011-01-13  2:40                       ` Patrick Horgan
  0 siblings, 0 replies; 16+ messages in thread
From: Patrick Horgan @ 2011-01-13  2:40 UTC (permalink / raw)
  To: gcc-help

On the subject of the difference in behavior in #pragma GCC diagnostic 
before 4.6 when they had to be at file/namespace scope, and 4.6 and 
later when they could be included at any scope, is there any way to 
determine if you are or are not at file/namespace scope in a test from a 
preprocessor macro?  I'm guessing not, but if there was it could make 
for a beautiful solution to using these things, in that you could have 
GCC_DIAG_ON/OFF(x) macros that would have appropriate implementations 
for appropriate scopes automagically.  So, anyone able to dispel my 
preconceptions and tell me it's possible?

Patrick

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2011-01-13  2:40 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-31 12:59 Strict Aliasing document Patrick Horgan
2011-01-04 13:44 ` Andrew Haley
2011-01-04 21:18   ` Patrick Horgan
2011-01-05  9:36     ` Andrew Haley
2011-01-07  0:28       ` Patrick Horgan
2011-01-10 21:35         ` How do I find when the diagnostic pragmas first came into gcc? Patrick Horgan
2011-01-10 23:16           ` Jonathan Wakely
2011-01-10 23:44           ` Patrick Horgan
2011-01-11  0:48             ` Jonathan Wakely
2011-01-11  7:53               ` Patrick Horgan
2011-01-11 10:10                 ` Jonathan Wakely
2011-01-11 11:03                   ` Jonathan Wakely
2011-01-12  1:20                   ` Patrick Horgan
2011-01-12 10:08                     ` Jonathan Wakely
2011-01-13  0:21                     ` Patrick Horgan
2011-01-13  2:40                       ` Is it possible for a macro to tell file/namespace scope from other scopes? Patrick Horgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).