Gcc 7, -Wimplicit-Fallthrough Warnings, and Portable Way to Clear Them

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 disable warnings when compiling C code?

try to add -w option when compiling

http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

C / C++ compiler warnings: do you clean up all your code to remove them or leave them in?

I would clean up any warning.
Even the ones that you know are harmless (if such a thing exists) will give a bad impression of you to whoever will compile the code.

It one of the "smelly" signs I would look for if I had to work on someone else code.

If not real errors or potential future issues, it would be a sign of sloppiness

How do I best silence a warning about unused variables?

You can put it in "(void)var;" expression (does nothing) so that a compiler sees it is used. This is portable between compilers.

E.g.

void foo(int param1, int param2)
{
(void)param2;
bar(param1);
}

Or,

#define UNUSED(expr) do { (void)(expr); } while (0)
...

void foo(int param1, int param2)
{
UNUSED(param2);
bar(param1);
}

Using typecasting to remove gcc compiler warnings

The compiler is showing you potential problem spots, that you should consider changing, but not by casts. The problem in the code that you have is that in C, arithmetic is never done on type that are narrower than int: the compiler always performs an implicit conversion to int for them.

So effectively your code is converting the narrow unsigned types to int, does the operation and then converts them back to the unsigned type.

Generally doing arithmetic with narrow types is not a good idea. Only use them if storage size is a problem (usually that would be larger structures such as arrays). For local, more or less temporary variables these narrow types make no sense.

How can I suppress unused parameter warnings in C?

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
UNUSED(x);
...
}

What is the best way to suppress A Unused variable x warning?

I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is interesting that the author also mangles the unused variable name, so you can't inadvertently use it in the future.

Excerpt:

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

void dcc_mon_siginfo_handler(int UNUSED(whatsig))

How to query GCC warnings for C++?

Yes, your observations are correct.

Probably this is not the intended behavior, and if you care about this feature, then I suggest reporting it upstream.

Note that this works, however:

touch 1.cc
g++ -Wnon-virtual-dtor -Q --help=warning,c++ 1.cc

I.e. if there's an input file with a proper extension, then the correct compiler proper executable is invoked: cc1plus, not cc1. The latter is the default if no input files are present. I did some quick debugging, and here's how that happens:

// gcc.c:
driver::do_spec_on_infiles () const
{
...
for (i = 0; (int) i < n_infiles; i++)
{
...
/* Figure out which compiler from the file's suffix. */

input_file_compiler
= lookup_compiler (infiles[i].name, input_filename_length,
infiles[i].language);

if (input_file_compiler)
{
...
value = do_spec (input_file_compiler->spec);

And input_file_compiler at that point is the C compiler, because

p n_infiles
$9 = 1
(gdb) p infiles[0]
$10 = {name = 0x4cbfb0 "help-dummy", language = 0x4cbfae "c", incompiler = 0x58a920, compiled = false, preprocessed = false}

Here's how the dummy file got created (function process_command in the same file):

  if (n_infiles == 0
&& (print_subprocess_help || print_help_list || print_version))
{
/* Create a dummy input file, so that we can pass
the help option on to the various sub-processes. */
add_infile ("help-dummy", "c");
}

Portable way to tell the compiler that alignment is OK without supressing the warning?

As OP existing code base does not need strong type matching, simple defeat most type matching with void* which will quiet the warnings. Ref also @Ctx

 void DoSomethingWithBuffer(const byte* buff, size_t size) {
const word64* ptr = (void*) buff;
...
}


Related Topics



Leave a reply



Submit