Suppress -Wconversion for specific lines of code - c++

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)

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

Related

GCC #pragma or command options

If the compiler has some command-line flags and the code has some pragmas that are incompatible with those flags, which one will be used?
To be clearer: I am compiling with g++ -g -O2 -std=gnu++17 -static {files} – GCC version g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0.
If I write in my code #pragma GCC optimize("Ofast"), will the final code be compiled with -O2 or with -Ofast?
That depends on if it's above or below the pragma.
void this_will_be_compiled_with_O2() { stuff(); }
#pragma GCC optimize("Ofast")
void this_will_be_compiled_with_Ofast() { stuff(); }
Although not explicitly mentioned in the documentation, the description of the #pragma GCC reset_options directive implies that any #pragma GCC optimize directive will override the command line option(s):
#pragma GCC reset_options
    This pragma clears the current #pragma GCC target and #pragma GCC optimize to use the default switches as specified on the command line.

Avoid warnings from system include files using Intel C Compiler

I've posted this original question, but since it isn't an issue related to CMake, I'm rephrasing it:
I've this example:
#include <iostream>
#include <string>
int main()
{
std::string a("Text");
std::cout << a.c_str() << std::endl;
return 0;
}
which I'm trying to compile with ICC (icc (ICC) 19.1.1.217 20200306, tested using GCC 4.8, 7, 8 and 9 as base) using this line:
icc -o OUT -pedantic-errors -fmax-errors=1 -march=native -Wall -Werror -Wfatal-errors -Wextra -ftree-vectorize -g -Wl,--exclude-libs,ALL -O3 -DNDEBUG -fPIC -fvisibility=hidden -Wstrict-aliasing -std=gnu++17 main.cpp
But it triggers a warning that results in an error (because of -Werror). This is the output when using GCC 8 gcc (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3) as the base compiler:
icc: warning #10193: -vec is default; use -x and -ax to configure vectorization
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h(5052) (col. 50): error #2102: violation of ansi-alias rules
compilation aborted for main.cpp (code 4)
So, in order to compile, I must remove the -Wstrict-aliasing check. And just like that, there's a whole set of other checks that triggers a similar behavior (warnings from system include files).
My concern is that I'd really like to have those checks in place for my own code, but obviously, not for libraries over which I've no control.
A suggestion was to use -isystem or -isystem=/opt/rh/devtoolset-8/root/usr/include/c++/8, but that only modifies the error:
icc: warning #10193: -vec is default; use -x and -ax to configure vectorization
/lib/../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
And even CMake discourages explicitly the use of -isystem on system include files.
Any ideas of another flag that turns off those warnings for system include files?
Replacing icc by gcc (g++) fixes the issue.
Thanks for your help.

How can I enable option '-Werror' using a GCC pragma?

I have a few files where I'd like to be strict about warnings, and I use GCC to build my project.
I've tried #pragma GCC diagnostic error "-Wall" as per 6.57.10 Diagnostic Pragmas, but it fails to account for some other enabled warning types:
foo.c:666:6: warning: passing argument 2 of 'bar' from incompatible pointer type [-Wincompatible-pointer-types]
Is there a way to enable -Werror for the file like it was supplied from the command line (or, at least, for the implicitly enabled set of warnings), so any warning would trigger an error?
For this case, you can use
#pragma GCC diagnostic error "-Wincompatible-pointer-types"
as for example in
#pragma GCC diagnostic error "-Wincompatible-pointer-types"
void foo(int * a)
{
}
void bar() {
foo("foo");
}
Using -Wall with this pragma is not supported. Only diagnostic options are supported, that are shown with -fdiagnostics-show-option (which is the default today anyway), as in your example warning above.
As a workaround, it turns out the current semantics of the -Wall option are described on the man page. In my case of GCC 8.3.0, it says that it enables the following options that could be used with the pragma afterwards:
-Waddress -Warray-bounds=1 (only with -O2) -Wbool-compare -Wbool-operation -Wc++11-compat -Wc++14-compat -Wcatch-value (C++ and Objective-C++ only) -Wchar-subscripts -Wcomment -Wduplicate-decl-specifier (C and Objective-C
only) -Wenum-compare (in C/ObjC; this is on by default in C++) -Wformat -Wint-in-bool-context -Wimplicit (C and Objective-C only) -Wimplicit-int (C and Objective-C only) -Wimplicit-function-declaration (C and Objective-C
only) -Winit-self (only for C++) -Wlogical-not-parentheses -Wmain (only for C/ObjC and unless -ffreestanding) -Wmaybe-uninitialized -Wmemset-elt-size -Wmemset-transposed-args -Wmisleading-indentation (only for C/C++)
-Wmissing-attributes -Wmissing-braces (only for C/ObjC) -Wmultistatement-macros -Wnarrowing (only for C++) -Wnonnull -Wnonnull-compare -Wopenmp-simd -Wparentheses -Wpointer-sign -Wreorder -Wrestrict -Wreturn-type
-Wsequence-point -Wsign-compare (only in C++) -Wsizeof-pointer-div -Wsizeof-pointer-memaccess -Wstrict-aliasing -Wstrict-overflow=1 -Wstringop-truncation -Wswitch -Wtautological-compare -Wtrigraphs -Wuninitialized
-Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wvolatile-register-var
It's likely that the list may change with time. On the other hand, diagnostic erroring over a version-dependent list may be a bad idea as it may break compilation for users that are likely to use some other versions of the toolchain than developers do (that -Werror is infamous for), so explicitly listing the desired warnings is good for public relations.

g++ Compilation option priority

when i build, using this line:
g++ -g -O2 -std=gnu++0x -static *.cpp
And my script had some other options like:
#pragma GCC optimize("O3")
#pragma comment(linker, ”/STACK:36777216“)
__attribute__((optimize("O3"))) void my_func()
{some code}
what will happen ? which one will be kept ?
Attributes or pragmas inside file take priority - so if you mark function with
__attribute__((optimize("O3")))
it will be compiled as if you compile it with -O3 regardless what you passed to gcc e.g. -O0.

How do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?

I'd like to know what switch you pass to the gcc compiler to turn off unused variable warnings? I'm getting errors out of boost on windows and I do not want to touch the boost code:
C:\boost_1_52_0/boost/system/error_code.hpp: At global scope:
C:\boost_1_52_0/boost/system/error_code.hpp:214:36: error: 'boost::system::posix_category' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:215:36: error: 'boost::system::errno_ecat' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:216:36: error: 'boost::system::native_ecat' defined but not used [-Werror=unused-variable]
I tried using both -Wunused-value and -Wno-unused-value but neither suppressed the messages above.
What is the right command, here is my compile line:
g++ -g -fno-inline -Wall -Werror -Wextra -Wfloat-equal -Wshadow
-Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wno-conversion
-Wdisabled-optimization -Wredundant-decls -Wunused-value -Wno-deprecated
-IC:\\boost_1_52_0 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
-c -o op.o op.cpp
Perhaps the -Wall overrides my goal?
The -Wno-unused-variable switch usually does the trick. However, that is a very useful warning indeed if you care about these things in your project. It becomes annoying when GCC starts to warn you about things not in your code though.
I would recommend you keeping the warning on, but use -isystem instead of -I for include directories of third-party projects. That flag tells GCC not to warn you about the stuff you have no control over.
For example, instead of -IC:\\boost_1_52_0, say -isystem C:\\boost_1_52_0.
Sometimes you just need to suppress only some warnings and you want to keep other warnings, just to be safe. In your code, you can suppress the warnings for variables and even formal parameters by using GCC's unused attribute. Lets say you have this code snippet:
void func(unsigned number, const int version)
{
unsigned tmp;
std::cout << number << std::endl;
}
There might be a situation, when you need to use this function as a handler - which (imho) is quite common in C++ Boost library. Then you need the second formal parameter version, so the function's signature is the same as the template the handler requires, otherwise the compilation would fail. But you don't really need it in the function itself either...
The solution how to mark variable or the formal parameter to be excluded from warnings is this:
void func(unsigned number, const int version __attribute__((unused)))
{
unsigned tmp __attribute__((unused));
std::cout << number << std::endl;
}
GCC has many other parameters, you can check them in the man pages. This also works for the C programs, not only C++, and I think it can be used in almost every function, not just handlers. Go ahead and try it! ;)
P.S.: Lately I used this to suppress warnings of Boosts' serialization in template like this:
template <typename Archive>
void serialize(Archive &ar, const unsigned int version __attribute__((unused)))
EDIT: Apparently, I didn't answer your question as you needed, drak0sha done it better. It's because I mainly followed the title of the question, my bad. Hopefully, this might help other people, who get here because of that title... :)
If you're using gcc and want to disable the warning for selected code, you can use the #pragma compiler directive:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
( your problematic library includes )
#pragma GCC diagnostic pop
For code you control, you may also use __attribute__((unused)) to instruct the compiler that specific variables are not used.
See man gcc under Warning Options. There you have a whole bunch of unused
Warning Options
... -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable
If you prefix any of them with no-, it will disable this warning.
Many options have long names starting with -f or with -W---for example, -fmove-loop-invariants, -Wformat and so on. Most of these have both positive and negative forms; the negative form of -ffoo would be -fno-foo. This manual documents only one of these two forms, whichever one is not the default.
More detailed explanation can be found at Options to Request or Suppress Warnings
Use -Wno-unused-variable should work.
You can prefix the variables with '(void)'.
This can be useful if you don't have access to the build framework or you only want to affect a local change in behavior.
IE:
int main()
{
int unused1; //This will print warning
int unused2; //This will not print warning -
// |
(void) unused2; // <----------------------------
}
Output:
$ g++ -Wall test.cc
test.cc: In function ‘int main()’:
test.cc:4:7: warning: unused variable ‘unused1’ [-Wunused-variable]
4 | int unused1;
| ^~~~~~~
How do you disable the unused variable warnings coming out of gcc?
I'm getting errors out of boost on windows and I do not want to touch the boost code...
You visit Boost's Trac and file a bug report against Boost.
Your application is not responsible for clearing library warnings and errors. The library is responsible for clearing its own warnings and errors.
The compiler is already telling you, it's not value but variable. You are looking for -Wno-unused-variable. Also, try g++ --help=warnings to see a list of available options.
-Wall and -Wextra sets the stage in GCC and the subsequent -Wno-unused-variable may not take effect. For example, if you have:
CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \
-fno-strict-overflow -fno-strict-aliasing \
-Wall -Wextra \
-pthread \
-Wno-unused-label \
-Wno-unused-function \
-Wno-unused-parameter \
-Wno-unused-variable \
$(INC)
then GCC sees the instruction -Wall -Wextra and seems to ignore -Wno-unused-variable
This can instead look like this below and you get the desired effect of not being stopped in your compile on the unused variable:
CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \
-fno-strict-overflow -fno-strict-aliasing \
-pthread \
-Wno-unused-label \
-Wno-unused-function \
$(INC)
There is a good reason it is called a "warning" vs an "error". Failing the compile just because you code is not complete (say you are stubbing the algorithm out) can be defeating.
I recommend neither editing 3rd party code nor suppressing warnings globally. If you are using CMake, you can suppress specific warning only for the external library.
find_package(Boost REQUIRED)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(Boost::boost PUBLIC -Wno-unused-variable)
endif()
...
add_executable(main "main.cpp")
target_link_libraries(main Boost::boost)
See also FindBoost.cmake.