Selectively Disable Gcc Warnings For Only Part of a Translation Unit

Selectively disable GCC warnings for only part of a translation unit

This is possible in GCC since version 4.6, or around June 2010 in the trunk.

Here's an example:

#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wuninitialized"
foo(a); /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
foo(b); /* no diagnostic for this one */
#pragma GCC diagnostic pop
foo(c); /* error is given for this one */
#pragma GCC diagnostic pop
foo(d); /* depends on command line options */

Selectively remove a warning message using GCC

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.

How to supress warnings from some files when building with g++?

It depends how you're building your project, and which compiler. I'm assuming you're on Linux and you're using GCC. If not, similar techniques work for Visual Studio and other compilers too.

If you have a Makefile that you can easily modify, you can supply different compiler flags to build each file. Disabling a particular warning is as easy as adding a -Wno-<warning-name> to the build line, for example: -Wno-unused-local-typedefs.

If you can't easily modify your makefile, you can place #pragmas into your source code directly. For example, you can add a line like this to the top of the C++ file:

#pragma GCC diagnostic ignored "-Wno-unused-local-typedefs"

Of course, the real solution is to fix the warnings. There are very few warnings that aren't worth heeding: Often ignoring warnings will cause you more pain in the long run!

How to enable a specific gcc warnings for a specific directory or file?

This line should do it: (see here)

#pragma GCC diagnostic warning "-Wsign-conversion"

Put it in the affected files or in a header that is included in all you files

Partially disable pedantic warnings in gcc within source

maybe, you could use a macro which can do what you want to achieve in a portable manner.
here's a short example:

#include <stdio.h>

#define BINARY(N) strtol(#N, 0, 2)

int main()
{
unsigned int piece = BINARY(10010101);
printf("%u\n", piece);

return 0;
}

in theory, gcc should be able to optimize the calls to strtol away and you don't lose readability.

EDIT: It seems that gcc does NOT optimize the strtol calls away as of now. However, your performance loss should be negligible.

Cheers!



Related Topics



Leave a reply



Submit