This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
-Wold-style-casts and system headers
- From: Anthony Foiani <tkil at scrye dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 18 Jun 2013 10:14:48 -0600
- Subject: -Wold-style-casts and system headers
- Reply-to: Anthony Foiani <tkil at scrye dot com>
Greetings.
I'm using -Wold-style-casts on my project, and I've found that I get
warnings in my code when certain macros are expanded.
These macros are defined in headers under /usr/include; my reading of
the manual is that these ought to get some immunity from some
warnings:
"Macros defined in a system header are immune to a few warnings
wherever they are expanded. This immunity is granted on an ad-hoc
basis, when we find that a warning generates lots of false positives
because of code in macros defined in system headers."
-- http://gcc.gnu.org/onlinedocs/cpp/System-Headers.html
But it seems there is some degree of judgment ("ad-hoc basis")
involved -- it's not clear whether that's regarding the header files,
individual macros, or individual warnings.
The case I hit today was from zlib.h, which has the following macro
definition:
#define deflateInit(strm, level) \
deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream))
When expanded into my sample code:
// https://gist.github.com/tkil/5806218
#include <string.h>
#include <zlib.h>
int main()
{
z_stream_s strm;
memset( &strm, 0, sizeof( strm ) );
const int rc = deflateInit( &strm, Z_DEFAULT_COMPRESSION );
return rc;
}
I got:
$ g++ -Wold-style-cast -o g++-warnings-check g++-warnings-check.cpp -lz
g++-warnings-check.cpp: In function ʽint main()ʼ:
g++-warnings-check.cpp:49:20: warning: use of old-style cast [-Wold-style-cast]
A few months ago, I ran into this <sys/select.h> as well; in that
case, I could easily enough rewrite the needed operations for private
use, so I did so.
I'm aware that I can disable that warning for the single file, or even
for a single region in the file using pragmas, but I would like to
understand why g++ isn't applying the "system header" rule to that
macro.
Best regards,
Anthony Foiani