Disable compiler warnings with pragma GCC diagnostic in LTO builds - gcc

I'm having troubles disabling GCC warnings with LTO-enabled builds:
Some warnings (-Wstrict-overflow, -Wmaybe-uninitialized, ...) are
specified on the command line using the -W option switch for compiling
all files within a project. However, I'd like to disable them just on some specific code locations.
Without LTO mode, "#pragma GCC diagnostic ignored" (along with #pragma
push & pop) works smoothly. However, those pragma declarations seem to be ignored when the compiler is invoked back at link stage (LTO build).
Is there a workaround, or a better way to disable specific warning in
specific code locations?
Many thanks in advance :)

Related

Different compiler flags for different compilers?

I have a cc_library (tbb) that requires the compiler flag -mwaitpkg on some compilers (Clang) to compile successfully. At the same time, there are older versions of GCC (4.9) that do not know this flag, and therefore the compilation via GCC 4.9 leads to an error:
gcc: error: unrecognized command line option '-mwaitpkg'
In a more advanced Bazel setup, I guess one would work around this using hermetic toolchains. This way every toolchain could provide its own set of compiler flags. Nevertheless, I do not want to enforce any specific toolchain and I am not sure if this is the right way to go (move copts to toolchain?).
Also introducing a config would be a way to solve this problem. E.g. bazel build --config=waitpkg //.... But this would require that a user is aware of this config and also knows the details of using waitpkg.
What is a proper "Bazel-way" to handle different compiler flags for different compilers?
The flag '-mwaitpkg' is supported by GCC version 9.3, Clang* 12, and newer versions of those tools.
If you build Bazel with earlier versions of GCC, you should remove the flag otherwise it gives compilation errors.

How do I suppress compiler warnings in Xcode for generated files?

I am currently using flex/bison to generate a lexer & parser, with the whole project in Xcode. However the files generated by flex & bison produce a couple of compiler warnings when they are compiled. How can I suppress these warnings?
I know I can suppress warnings on a per-file basis through the 'Build Phases' tab, but the generated files don't appear here.
I tried adding the flag [-w] to the source file [ie, the .lpp and .ypp files], however this didn't work - Xcode understandably tried to pass that flag to bison, which it didn't like.
You can also turn off the warnings by embedding a pragma for the clang (or gcc) compiler to disable individual warnings.
For example, you could do the following a .lpp or .ypp file:
%{
#pragma clang diagnostic ignored "-Wunused-variable"
%}
...
%%
...
Where the %{ ... %} construct tells flex/bison to pass the line direct to the output.
References:
Disabling clang warnings
Selectively disabling gcc warnings

Eigen & GCC 5 : class std::binder2nd is deprecated

I just restarted working on a project which has been on hold for a few months. Last time I compiled it it was working just fine, without any error nor warning.
Yet when I tried to compile it earlier today I got this warning
attention : ‘template<class _Operation> class std::binder2nd’ is deprecated [-Wdeprecated-declarations]
This warning literally appears hundreds of time when including Eigen/Geometry, which I use all over my project
In file included from [...]/include/Eigen/src/Core/ArrayBase.h:109:0,
from [...]/include/Eigen/Core:350,
from [...]/include/Eigen/Geometry:4,
from [...]/include/[myproject]/types.hh:8,
from [...]/include/[myproject]/voronoi.hh:8
Since then I haven't updated Eigen (is used 3.2.4 which is still the last update today).
However, since last time I compiled it, GCC has been updated to 5.1.0 (I'm using archlinux)
Question:
Is there an issue with gcc 5.1.0 telling me std::binder2nd is deprecated
Should Eigen be updated ?
How can I silent those specific warning without loosing the verbosity of my build ?
ANSWER
I appreas that std::bind2nd is really deprecated and that a commit has been done to solve that in Eigen. This commit is however not yet merged with the master branch :/ (and doesn't solve the issue as some std::bind2nd are still present in Eigen's code)
Bottom line is: Eigen's last stable version is deprecated
Is there an issue with gcc 5.1.0 telling me std::binder2nd is deprecated
No, the C++ standard says it's deprecated in C++11, so if you compile in C++11 mode then it is supposed to be deprecated.
Should Eigen be updated ?
Yes. if it wants to be C++17 compatible, as std::bind2nd doesn't exist post-C++14 at all.
How can I silent those specific warning without loosing the verbosity of my build ?
Suppress the warning. Either compile with -Wno-deprecated-declarations on the command-line or do it in the source when including the Eigen headers:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <eigen/whatever.h>
#pragma GCC diagnostic pop
Or, as the other answer says, tell GCC to treat the Eigen headers as system headers, which happens automatically if they are in /usr/include, or are included with -isystem, or are included from another header that does:
#pragma GCC system_header
#include <eigen/whatever.h>
How can I silent those specific warning without loosing the verbosity ?
Edit the CMakeLists.txt file. Add this line somewhere after CMAKE_CXX_FLAGS is set.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
Previous answer mentioned adding this to #pragma or to command line. I am biased against #pragma because I find it hard later on to remember where I put it. So as general practice, I try to avoid #pragma. Adding to command line means you have to remember to type this everytime you recompile.
Instead of using the -I flag to include files use -isystem to include Eigen headers:
g++-5 -isystem/usr/include/eigen3 source_file_here.cpp
This flag is intended for system headers that don't conform to C standards but are considered false positives when warnings are generated. Eigen headers are used much like system headers and thus for most users the warnings are not helpful but merely an annoying false positive.
Credit goes to Ilya Popov's comment in the original question.

What is MSVC equivalent to gcc's -Wundef?

I'd like cl.exe to report warning in case undefined macro is encountered in preprocessor expression compiling c/c++ source. Like g++ -Wundef. Is it available?
Going through the list of all compiler warnings at http://msdn.microsoft.com/en-us/library/cfahxw6k.aspx is not an option.
MSVC's compiler option /wd4668 (to disable warning "C4668") should be equivalent to -Wundef, according to the documentation at http://msdn.microsoft.com/en-us/library/4dt9kyhy.aspx. [Based on the comment to the original question.]
Unfortunately, this warning is unusable, because it produces a lot of warnings in system header files. Unlike GCC/Clang, MSVC does not appear to have the ability to ignore warnings in system header files.

Removing Flag in GCC using Pragma

I want to remove a compiler flag for a praticular file in my project. How to do this using #pragma?
Sorry that this is late, but I just ran into the same issue on my project.
I couldn't find a #pragma to modify the list of compiler flags, but I could use GNU Make's ability to modify make variables on a per-target basis.
One of my files was taking forever to compile with -fvar-tracking-assignments (which was added to -O2 a few releases back). I was looking for a way to turn that off for one file; after looking (in vain) for an appropriate pragma, I simply added this line to my makefile to modify CXXFLAGS when compiling and linking those specific files:
ObtuseObj.o ObtuseObjTest.o ObtuseObjTest : CXXFLAGS += -fno-var-tracking-assignments
Hopefully this will be helpful to others!
Only flags that control warnings can be overridden using #pragma, see the diagnostic pragmas documentation page.
For instance, you could do:
#pragma GCC diagnostic ignored "-Wformat"
To make GCC ignore the warnings generated by mismatched formatting strings.
I'm not sure if gcc has such pragmas. I can't find any on the gcc page for pragmas.
If you are asking a question related to gcc next time, tag it under gcc as well. I would tag it but I can't. Tagging under gcc would get you many more responses.

Resources