Reputation: 13690
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)
Upvotes: 2
Views: 5164
Reputation: 754440
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
Upvotes: 3