How do I find when the diagnostic pragmas first came into gcc?

Patrick Horgan phorgan1@yahoo.com
Tue Jan 11 07:53:00 GMT 2011


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



More information about the Gcc-help mailing list