Selectively remove a warning message using GCC

This piece of code:

Int32 status;
printf("status: %x", status)

gives me the following warning:

jpegthread.c:157: warning: format '%x' expects type 'unsigned int', but argument 3 has type 'Int32'

I know I can get rid of it by casting the type, but is it possible with a GCC compiler flag to get rid of that particular type of warning, and still use -Wall?

122650 次浏览

I presume you are looking for the

#ifdef WIN32
#pragma warning (disable: #num of the warning)
#endif

Equivalent in GCC...

You can search for options such as -Wno-conversion -Wno-format-security that do the job in 3.8 Options to Request or Suppress Warnings.

But in terms of the #pragma directive:

I quote from the GCC mailing list from Google:

GCC does not, currently, provide the #pragma facility you are looking for.

Do not lose hope! There are viable alternatives.

The first best way to fix the code so it no longer emits the warning. Alas, you've stated you cannot do this. :-(

NOTE: Have warnings turned up as verbose as your team can tolerate! [see below]

The next best way to ignore the undesired warning is to post-process the output of GCC to a script (such as a Perl script) that strips out the specific, exact warning you want to ignore.

The next way to ignore the undesired warning is to disable the warning for that translation unit. -Wno-foozle-mcgoogle, just for that particular translation unit. That's a mighty big hammer, though. And if the warning is in a header file, it may be pervasive throughout your entire project -- to which I'd direct you to the post-processing script solution (assuming you are disallowed from fixing the code).

So currently no, there is no #pragma directive to disable specific warnings. Rather than using -Wall you could turn on as many warnings as you can minus specific ones.

http://www.network-theory.co.uk/docs/gccintro/gccintro_31.html

Or fix the code

I used the following CFLAGS:

-Wall -Wformat=0

If you need that code to work portable then you should cast the argument to unsigned int, as the int type may have a different size than Int32 on some platforms.

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx.

From the GCC Warning Options:

You can request many specific warnings with options beginning -W, for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning -Wno- to turn off warnings; for example, -Wno-implicit. This manual lists only one of the two forms, whichever is not the default.

For your case the warning in question is -Wformat

-Wformat

Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string specified, and that the conversions specified in the format string make sense. This includes standard functions, and others specified by format attributes (see Function Attributes), in the printf, scanf, strftime and strfmon (an X/Open extension, not in the C standard) families (or other target-specific families). Which functions are checked without format attributes having been specified depends on the standard version selected, and such checks of functions without the attribute specified are disabled by -ffreestanding or -fno-builtin.

The formats are checked against the format features supported by GNU libc version 2.2. These include all ISO C90 and C99 features, as well as features from the Single Unix Specification and some BSD and GNU extensions. Other library implementations may not support all these features; GCC does not support warning about features that go beyond a particular library's limitations. However, if -pedantic is used with -Wformat, warnings will be given about format features not in the selected standard version (but not for strfmon formats, since those are not in any version of the C standard). See Options Controlling C Dialect.

It looks like the GCC manual does provide a way to do this with a #pragma, actually (contrary to what Aiden Bell says):

6.62.13 Diagnostic Pragmas

E.g., for the -Wuninitialized warning, you can do...

#pragma GCC diagnostic ignored "-Wuninitialized"

... to suppress the warning, or...

#pragma GCC diagnostic warning "-Wuninitialized"

... to treat it as a warning (not an error) even if you're building with -Werror.