How to configure Visual Studio not to output warnings from libraries? - c++

Is there any way to prevent Visual Studio from printing out warnings from included libraries?
\Wall gives me loads of warnings from STL and Qt headers, although I only want to see those originating from my own code (i.e. the code which is part of the current Visual Studio project).

You can use pragma to set the warning levels for each file.
So before you include
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#include your files here
#pragma warning( pop )
More information here: http://msdn.microsoft.com/en-us/library/2c8f766e%28v=vs.80%29.aspx

That's the only portable way (if using -isystem with other compilers):
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: ...)
#endif
#include <Q...>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Hopefully they will implement isystem one of these days:
https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/14717934-add-a-cl-exe-option-for-system-headers-like-gcc-s

You can use warning level 4, it will only include warnings for your code.

Related

Visual Studio disable warnings for files in specific directories

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 )

Is using #pragma warning push/pop the right way to temporarily alter warning level?

Once in a while it's difficult to write C++ code that wouldn't emit warnings at all. Having warnings enabled is however a good idea. So it is often necessary to disable warnings around some specific construct and have them enables in all other pieces of code.
I've seen two ways of doing that so far.
The first one is to use #pragma warning( push ) and #pragma warning( pop ):
#pragma warning( push )
#pragma warning( disable: ThatWarning )
//code with ThatWarning here
#pragma warning( pop )
The second is to use #pragma warning( default ):
#pragma warning( disable: ThatWarning )
//code with ThatWarning here
#pragma warning( default: ThatWarning )
The problem I see in the second variant is that it discards the original warning level - the warning might have been off before that or its warning level might have been altered. Using default would discard those alterations.
The first approach looks clean. Are there any problems with it? Are there any better ways to achieve the same?
This will work with multiple compilers (and different versions of compilers).
Header "push"
#if defined(__clang__)
# pragma clang diagnostic push
#endif
#if defined(_MSC_VER)
# pragma warning(push)
#endif
#if defined(YOUR_FAVORITE_COMPILER)
# pragma your compiler push warning
#endif
Header "pop"
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
Some warning
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-variable"
# if __has_warning("-Wnew-special-warning")
# pragma clang diagnostic ignored "-Wnew-special-warning"
# endif
#endif
#if defined(_MSC_VER)
# pragma warning(disable: 4100) // unreferenced formal parameter
# if _MSC_VER > _MSC_SOME_VERSION
# pragma warning(disable: xxxx) // disable one more for special version
# endif
#endif
Usage
// This code reports warnings
// ...
#include <ignore_compiler_warning/push>
#include <ignore_compiler_warning/warning_type_1>
#include <ignore_compiler_warning/warning_type_2>
#include <ignore_compiler_warning/warning_type_3>
// This code ignores warnings type_{1,2,3}
// ...
#include <ignore_compiler_warning/pop>
// Back to reporting warnings
// ...
Additionally include guards can check that there is no double push/pop/disable-warning pragmas.
Update
Implementation
List of available warnings
CMake companion function to generate flags
Too late for sharptooth but for all the googlers out there:
#pragma warning ( suppress: ThatWarning )
// one single line with ThatWarning
is short for (generally since VS 2008, but in VS 2005 for Code Analyzer warnings only):
#pragma warning ( push )
#pragma warning ( disable: ThatWarning )
// one single line with ThatWarning
#pragma warning ( pop )
The first method is the best way to do it, IMO. I know of no problems with it.
Simply bear in mind that a #pragma is compiler specific so don't expect it to work on every compiler out there :)
The correct approach (although a bit ugly)
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( once: ThatWarning )
#endif
//code with ThatWarning here
#ifdef _MSC_VER
#pragma warning( pop )
#endif
You can disable specific warnings in the project or file options and this setting applies as the 'default' per those #pragmas at the relevant scope. Some of the warnings in VS2005 are so useless/annoying that this cleans up the output quite a bit, if using /W4.
This is in Properties under Configuration Properties -> C/C++ -> Advanced.
I have no problems with the first variant. May be the better way is to use the following:
#pragma warning( push )
#pragma warning( once: ThatWarning )
//code with ThatWarning here
#pragma warning( pop )
This will let you know that there're still warnings in the code, but the warning messages will not be so annoying. But that is the matter of taste.
The first approach allows you change the specific warning setting in a local scope. It first stores all the current warning state by push into stack, apply your warning modifications, then restore (pop) to last warning state.
#pragma warning( push )
#pragma warning( once: ThatWarning )
//code with ThatWarning here
#pragma warning( pop )

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 )

Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE

What is the best way to disable the warnings generated via _CRT_SECURE_NO_DEPRECATE that allows them to be reinstated with ease and will work across Visual Studio versions?
If you don't want to pollute your source code (after all this warning presents only with Microsoft compiler), add _CRT_SECURE_NO_WARNINGS symbol to your project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".
Also you can define it just before you include a header file which generates this warning.
You should add something like this
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
And just a small remark, make sure you understand what this warning stands for, and maybe, if you don't intend to use other compilers than MSVC, consider using safer version of functions i.e. strcpy_s instead of strcpy.
You could disable the warnings temporarily in places where they appear by using
#pragma warning(push)
#pragma warning(disable: warning-code) //4996 for _CRT_SECURE_NO_WARNINGS equivalent
// deprecated code here
#pragma warning(pop)
so you don't disable all warnings, which can be harmful at times.
i work on a multi platform project, so i can't use _s function and i don't want pollute my code with visual studio specific code.
my solution is disable the warning 4996 on the visual studio project. go to Project -> Properties -> Configuration properties -> C/C++ -> Advanced -> Disable specific warning add the value 4996.
if you use also the mfc and/or atl library (not my case) define before include mfc _AFX_SECURE_NO_DEPRECATE and before include atl _ATL_SECURE_NO_DEPRECATE.
i use this solution across visual studio 2003 and 2005.
p.s. if you use only visual studio the secure template overloads could be a good solution.
You can also use the Secure Template Overloads, they will help you replace the unsecure calls with secure ones anywhere it is possible to easily deduce buffer size (static arrays).
Just add the following:
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
Then fix the remaining warnings by hand, by using the _s functions.
Combination of #[macbirdie] and #[Adrian Borchardt] answer. Which proves to be very useful in production environment (not messing up previously existing warning, especially during cross-platform compile)
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(push)
#pragma warning(disable: 4996) // Disable deprecation
#endif
//... // ...
strcat(base, cat); // Sample depreciated code
//... // ...
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(pop) // Renable previous depreciations
#endif
For the warning by warning case, It's wise to restore it to default at some point, since you are doing it on a case by case basis.
#pragma warning(disable: 4996) /* Disable deprecation */
// Code that causes it goes here
#pragma warning(default: 4996) /* Restore default */
The best way to do this is by a simple check and assess. I usually do something like this:
#ifndef _DEPRECATION_DISABLE /* One time only */
#define _DEPRECATION_DISABLE /* Disable deprecation true */
#if (_MSC_VER >= 1400) /* Check version */
#pragma warning(disable: 4996) /* Disable deprecation */
#endif /* #if defined(NMEA_WIN) && (_MSC_VER >= 1400) */
#endif /* #ifndef _DEPRECATION_DISABLE */
All that is really required is the following:
#pragma warning(disable: 4996)
Hasn't failed me yet; Hope this helps
you can disable security check. go to
Project -> Properties -> Configuration properties -> C/C++ -> Code Generation -> Security Check
and select Disable Security Check (/GS-)
You can define the _CRT_SECURE_NO_WARNINGS symbol to suppress them and undefine it to reinstate them back.
Another late answer... Here's how Microsoft uses it in their wchar.h. Notice they also disable Warning C6386:
__inline _CRT_INSECURE_DEPRECATE_MEMORY(wmemcpy_s) wchar_t * __CRTDECL
wmemcpy(_Out_opt_cap_(_N) wchar_t *_S1, _In_opt_count_(_N) const wchar_t *_S2, _In_ size_t _N)
{
#pragma warning( push )
#pragma warning( disable : 4996 6386 )
return (wchar_t *)memcpy(_S1, _S2, _N*sizeof(wchar_t));
#pragma warning( pop )
}