What is MSVC equivalent to gcc's -Wundef? - visual-studio

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.

Related

Disable compiler warnings with pragma GCC diagnostic in LTO builds

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 :)

Which gfortran compiler flag is associated with the warning "Illegal preprocessor directive"?

When using pFUnit (3.2.9) to test my Fortran code, I get many "Illegal preprocessor directive" warnings, e.g.
Warning: Illegal preprocessor directive
/path/to/my/file/test.f90:37:2:
#line 26 "/path/to/my/file/test.f90"
1
The code compiles and runs fine, so I would like to turn off these warnings, while still seeing other compiler warnings. Which gfortran compiler flag turns this specific warning off? I am working with gfortran 7.3.1.
This is not the kind of warning that one should turn off because not using a preprocessor has usually very bad consequences on codes that use the most common directives like #define and #if. And as far as I know it isn't possible to turn it off.
It is much wiser to enable the preprocessor using the -cpp flag. Not only the warnings will stop but you will get the correct line numbers in further diagnostics as well, the line numbers will refer to your original code.

Clang returns different compiler errors for system include files

main.cpp:
#include "test.h"
void main () {narrowingConversion ();}
include/test.h:
void narrowingConversion () {int i = 1; char a[1] = {i};}
Clang compiles the above code successfuly when including the include folder as a system folder:
clang++ -std=c++0x -isystem./include main.cpp
But clang fails when the folder is included normally:
clang++ -std=c++0x -I./include main.cpp
./include/test.h:1:54: error: non-constant-expression cannot be narrowed from type 'int' to 'char' in initializer list [-Wc++11-narrowing]
Question: Why does clang behaves differently for system and non system files?
Clang suppresses warnings for system headers by default. It seems to deem C++11 narrowing a non-fatal error and to suppress diagnostics about that in this context.
As per the manual, to see this when the header was #included from a directory deemed to be a system one (which -Isystem specifies), you'd need to enable this option:
-Wsystem-headers
Enable warnings from system headers.
This flag is probably inherited from GCC, to which it was added in 2000. The rationale for that was:
The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment. All warnings, other than those generated by ‘#warning’ (see Diagnostics), are suppressed while GCC is processing a system header. Macros defined in a system header are immune to a few warnings wherever they are expanded. […]
m7913d found the equivalent section of the Clang documentation, which doesn't give as much exposition as GCC did:
More information can be found here: Controlling Diagnostics in System Headers
But the core result is the same:
Warnings are suppressed when they occur in system headers.
And, as we've seen, Clang seems to consider C++11 narrowing not to be a hard error, and to suppress it for system headers in the absence of -Wsystem-headers.

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

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