Visual Studio disable warnings for files in specific directories - c++

We have a VS 2019 project which includes external source libraries which we add to our project. For instance we are using WTL (Windows Template Library). During compile time, these libraries produce huge numbers of warnings (particularly for members which do not have default values).
For those files, we would like to completely disable warnings, or at least specify which warnings should be excluded for certain directories, because
We are not going to change that code
There are so many warnings, that we can easily miss some from our own code that we should resolve
I saw a post about some flags that you should be able to pass to disable warnings for "external" files, but I do not see any options in the project settings.

There are several ways to disable the warning:
Project Properties->C/C++->General->Warning Level->select level
Here is the Warning Level:
Turn off all warnings (/W0): Turn off the display of all warning
messages. Level 1 (/W1): Display serious warning messages. Level 2
(/W2): Display level 1 warnings and some less serious warnings, such
as warnings about hidden class members. This is the default warning
level on the command line. Level 3 (/W3): Display level 2 warnings
and some less serious warnings, such as warnings about expressions
that always evaluate to true or false. Level 4 (/W4): Display all
level 3 warnings and informational warnings.
Or you could choose to disable specific warnings in Project Properties->C/C++->Advanced->Disable Specific Warnings
You could use warning pragma.
Syntax:
#pragma warning(
warning-specifier : warning-number-list
[; warning-specifier : warning-number-list ... ] )
#pragma warning( push [ , n ] )
#pragma warning( pop )
Also, you could refer to Microsoft about How to: Enable and Disable Code Analysis for Specific C/C++ Warnings.
To enable or disable a code analysis warning
2.1.Create a header file that lists all the code analysis warnings and their initial state, as shown in the following code:
// WarningState.h
#pragma warning ( default : 6001 )
#pragma warning ( disable : 6011 )
// more warnings here
// end of file
2.2.Include WarningState.h in the application header file. In this case, MyApplication.h represents the header file.
// MyApplication.h file
#include "WarningState.h"
// ...
// end of file
2.3.Include MyApplication.h file in the source code file. In this case, MyApplication.cpp represents the source file.
// MyApplication.cpp file
#include "MyApplication.h"
2.4.To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:
// MyApplication.cpp file
#include "MyApplication.h"
#pragma warning ( disable: 6001 )
#pragma warning ( default : 6001 )
To disable all code analysis warnings for included third-party files
Add the following code to your header file.
#include <codeanalysis\warnings.h>
#pragma warning( push )
#pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
#include <third-party include files here>
#pragma warning( pop )

Related

msvc pragma warning omitting 'default' in cpp file

I have some pragma warning (disable : xxx) in cpp files (not headers):
now if we omit pragma warning (default : xxx) in same cpp file, does that warning stay disabled across all cpp files in project or just in this cpp file?
also if we compile multiple projects, does that disabled warning affect all projects? assuming pragma in cpp file only not in headers.
for example, I have:
#ifdef NDEBUG // In release build using CrtDebug functions has no effect!
#define CRT_DBG_REPORT(...) 0
#pragma warning (disable : 6326) // Potential comparison of a constant with another constant
#pragma warning (disable : 26814) // The const variable can be computed at compile time
#pragma warning (disable : 26477) // Use nullptr rather than 0 or NULL
#pragma warning (disable : 4127) // conditional expression is constant
#pragma warning (disable : 4100) // unreferenced formal parameter
#else
#define CRT_DBG_REPORT _CrtDbgReportW
#endif // NDEBUG
I would like to omit setting back to 'default' but ensure it only disables the warnings for this cpp file.
edit
thanks to comment section suggestion...
If we enable Unity build, what is the behavior of "undefauled" (only disabled) cpp pragmas? Project properties -> Advanced -> Unity.
If the warnings are disabled in the cpp file, they will only affect the lines underneath the pragma (they won't affect other compilation units). Unity builds would probably cause an issue though (at a guess, can't test this right now).
Generally speaking, if you are enabling/disabling warnings via pragmas, this is probably the way to go:
#define PUSH_DISABLE_WARNINGS \
__pragma(warning(push)) \
__pragma(warning(disable : 6326)) \
__pragma(warning(disable : 26814)) \
__pragma(warning(disable : 26477)) \
__pragma(warning(disable : 4127)) \
__pragma(warning(disable : 4100))
#define POP_DISABLE_WARNINGS \
__pragma(warning(pop))
and then later on...
PUSH_DISABLE_WARNINGS
/* warnings will only be disabled here */
POP_DISABLE_WARNINGS

Why does #pragma warning(push,0) enable additional warnings in MSVC 2015?

MCVE:
#pragma warning(push,0)
#include <QWidget>
#pragma warning(pop)
#include <QVariant>
int main(int argc, char *argv[])
{
}
compiling with Visual Studio 2015 (choose "Qt Application" as project type if it matters) and /W4 i get 2xWarning C4100 (unreferenced formal parameter) for argc and argv which is what i expect and are actually interested in. But in addition i get 5xWarning C4251(class X needs to have dll-interface to be used by clients of struct Y) somewhere inside qvariant.h.
With different included files from Qt i can produce different warnings, so it's not just C4251. I was not able to reproduce it with standard library includes yet. The warnings disappear if i remove/comment the #pragma warning-lines or set the projects /W flag to a lower level. The level pushed seems to have no effect, so e.g. with /W0 and #pragma warning(push,4) i get no warnings.
What i would have expected is, that #pragma warning(push,0) disables all warnings inside <QWidget> and #pragma warning(pop) restores the initial state. Instead, Visual Studio seems to somehow detect that those files are from an external library (or is it something inside the Qt code?), but as soon as there is a push/pop pair the pushed warning-level is ignored and instead the initially set warning level via /W flag is used.
I came across this problem initially when using CMake and cotire, which automatically generates a header file to be used as precompiled header. So in my code i don't use #pragma warning (push/pop) which compiles fine without Qt-Warnings. But the generated file wraps the includes in a push/pop pair and thus generates about 200 Warnings in my project. I'm trying to understand why this happens in order to find a solution which requires me neither to wrap all my manual includes in push/pop nor to maintain a private modification to cotire.

Visual Studio 2015 won't suppress error C4996

Visual Studio 2015 Community Edition gives the following error when compiling in debug, but not when compiling in release:
std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
I was able to trace back the source of this error to lines 214 and 242 of this third party library I'm using to write bitmap images. I don't fully understand what is going on in these parts, so I'd rather not mess with it.
I'm trying to disable this error, but Visual Studio won't let me. I've tried the following solutions that were suggested in the documentation, on StackOverflow, or elsewhere:
Add 4996 to the "Disable Specific Warnings" field in Project Settings > Configuration Properties > C/C++ > Advanced.
Add /wd4996 to the "Command Arguments" field in Project Settings > Configuration Properties > Debugging.
Add #pragma warning (disable : 4996) at the top of the offending file, and/or above the offending function.
Add _SCL_SECURE_NO_WARNINGS, _SCL_NONSTDC_NO_WARNINGS, _SCL_OBSOLETE_NO_WARNINGS, _SCL_SECURE_NO_WARNINGS_GLOBAL, and combinations thereof to the "Preprocessor Definitions" field in Project Settings > Configuration Properties > C/C++ > Preprocessor.
Add the definitions from the previous solution with a #define directive to the top of the offending file.
Add the definitions from the previous solution but prefixed with /D or with -D to the "Command Arguments" field.
But none of this fixes the issue for me.
What could possibly be the reason for Visual Studio to keep insisting on displaying this error?
define NO_WARN_MBCS_MFC_DEPRECATION
Disabling warning 4996 has no effect on std::copy warnings. To suppress this warning place the following at the top of your source file:
#define _SECURE_SCL_DEPRECATE 0
#include <algorithm>
Add _CRT_NONSTDC_NO_WARNINGS to preprocessor definitions.
In your stdafx.h:
#pragma warning( push )
#pragma warning( disable: 4996)
#include <algorithm>
#pragma warning( pop )
Worked for me VS2015 update 3

What is the best solution for suppressing warning from a MS include (C4201 in mmsystem.h)

I am tired of having to look at warnings during our compilations - warnings that come from MS include files.
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\mmsystem.h(1840): warning C4201: nonstandard extension used : nameless struct/union"
I have seen this thread that suggests changing the header itself (but then each of my team mates has to do it and we have to do it on the build server - not to mention it is a glorious HACK)
Is there a better way? I don't want to turn it off globally - just to suppress it for certain files or directories.
Any suggestions?
EDIT
For some stupid reason I didn't think I could set warning levels across include files. Thanks - that does the trick.
Something like
#pragma warning(push, disable: 4201)
#include <mmsystem.h>
#pragma warning(pop)
How about using #pragma warning extension in VC++?
http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
#pragma warning (push, 2) // Temporarily setting warning level 2
#include <mmsystem.h>
#pragma warning (pop) // Restore back
You may also try ..
#pragma warning (disable: 4201)
#include <mmsystem.h>
#pragma warning (default)
With VS 2017 Community, to avoid warning C4083, I use :
#pragma warning(push)
#pragma warning(disable:4201)
#include <dxvahd.h>
#pragma warning(pop)

Is there a way to configure the details of MSVS static code analysis?

Static code analysis tool in MSVS (for C++) has plenty of false positives, and some of them are in Windows SDK files. Is there a way to configure it in order to improve quality and ignore stable SDK files?
Finally I found what I was looking for - here is the answer directly from MSDN's http://msdn.microsoft.com/en-us/library/zyhb0b82.aspx (VS2010 specific):
#include <codeanalysis\warnings.h>
#pragma warning( push )
#pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
#include <third-party include files here>
#pragma warning( pop )