2

I use a header file that provides inline functions. These functions are not always save in respect to the GCC -Wconversion check.

Now I want to use the -Wconversion check for my code but want to suppress the warning I get for the include file. Edit: When I just add the conversion check to the compiler options I get the diagnostics, omitting -Wconversion gives me a clean compiler run.

Corresponding to this question I surrounded the include with some pragmas:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#include <lpc177x_8x_crc.h>
#pragma GCC diagnostic pop

Unfortunately this does not suppress the warnings.

warning: conversion to 'int32_t' from 'uint32_t' may change the sign of the result [-Wsign-conversion]

For an easy check you could even try this if you don't have CMSIS available:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
int32_t foo(void)
{
    uint32_t result;
    return result;
}
#pragma GCC diagnostic pop

The compiler command line arguments are:

arm-none-eabi-gcc.exe -mthumb -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wunreachable-code -W -Wextra -Wall -Wformat=0 -Wconversion -g -O0 -ffunction-sections -fdata-sections -g3 -mcpu=cortex-m3 -c foo.c -o foo.o

I use arm-none-abi-gcc version:

gcc version 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305] (GNU Tools for ARM Embedded Processors)

harper
  • 13,345
  • 8
  • 56
  • 105
  • I tried with gcc 4.8 on Linux, and cannot reproduce this issue. Could you please provide the exact message you get? Could it be a different one, that has nothing to do with -Wconversion? – pentadecagon Jan 10 '14 at 20:07

1 Answers1

3

Since the warning message identifies the relevant flag as -Wsign-conversion, you should add that to the pragmas.

#include <stdint.h>
extern int32_t foo(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(void)
{
    uint32_t result = 0;
    return result;
}
#pragma GCC diagnostic pop

If you comment out the second ignored and compile with -Wconversion, you get the error:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -Wconversion -c cvt.c
cvt.c: In function ‘foo’:
cvt.c:9:5: error: conversion to ‘int32_t’ from ‘uint32_t’ may change the sign of the result [-Werror=sign-conversion]
     return result;
     ^
cc1: all warnings being treated as errors
$

If you uncomment that pragma, you get no warnings or errors.

(Tested on Mac OS X 10.9.1 Mavericks with GCC 4.8.2 — YMMV!) I note that the Apple-supplied clang (version 'Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)') does not object with the second ignored pragma commented out, with -Wconversion or with -Wsign-conversion, and I tried dinking with the code passing a uint32_t parameter and assigning that to result before returning it, etc (so it isn't simply superior optimization recognizing that 0 is special, or that returning an uninitialized variable is undefined behavior, etc.):

#include <stdint.h>
extern int32_t foo(uint32_t v);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
//#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(uint32_t v)
{
    uint32_t result = v;
    return result;
}
#pragma GCC diagnostic pop
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • It works also for my compiler version. It's very strange that I need to disable disable more diagnostics (sign-conversion+conversion) than I used to enable at command line (conversion). – harper Jan 11 '14 at 08:59
  • I didn't check last night, but this morning, using just the `-Wsign-conversion` pragma is sufficient. Check out [Warning Options](http://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Warning-Options.html#Warning-Options) — beware, the list is not alphabetic. It describes `-Wconversion` and says `-Wno-sign-conversion` is to turn off warnings about signed to unsigned integer conversions in C (it is off in C++ unless explicitly enabled). And `g++` (4.8.2) doesn't generate the warnings even with `-Wsign-conversion` and both pragmas as comments. – Jonathan Leffler Jan 11 '14 at 17:10
  • 1
    Agreed, `-Wsign-conversion` is sufficient. But I was surprized that `-Wconversion` also enables `-Wsign-conversion` what I didn't enabled it at command line. Now I use `#pragma GCC ignored "-Wsign-conversion` to revert the change I did with the command line option `-Wconversion`. Strange, but the page you linked to documents this. – harper Jan 12 '14 at 08:50