So, You Want to Suppress This Warning in Visual C++
This is a translation of an article written by Dmitry Meshcheryakov, an ABBYY employee and first published here: "ABBYY blog. So, You Want to Suppress This Warning in Visual C++...". Translation done and published with permission of the copyright holder.
We decided to publish this article in our knowledge base because it gives a very clear explanation of the essence of PVS-Studio's V665 diagnostic.
A common situation: you've just written a piece of spotless code, but Visual C++ emits a warning on it. Rewriting the code a bit usually helps get rid of the warning - usually, but not always. Then you have only one way out which is to suppress it.
Let's see what capabilities Visual C++ provides for that purpose and what mistakes are common among programmers when using them.
The most obvious method is to suppress the warning in the project settings at the project level. It works, but not very well. First, the warning will be suppressed in the whole project, including all the header files the project includes. Second, copying the code into another project will bring the warning back. It will inevitably happen if header files (which, for example, contain template implementations) contain code you need to include (#include) into every project using them.
Another method is to suppress the warning in the project settings at the file level. This method is even worse than the previous one. First, the warning will be suppressed for the whole translation unit, i.e. in this particular file and all the header files it includes. Second, you'll get the same troubles with code copying. Third, once your project includes more than a few files, the probability of losing this setting during project conversion for a new Visual C++ version gets just about 1.
The last thing you can do is use #pragma warning. Programmers usually use it like this:
#pragma warning (disable: 9000) // code triggering C9000 warning #pragma warning (default: 9000)
... and feel very pleased with themselves: the warning suppressed, the code ready, the warning restored - take profit.
It is in fact an epic FAIL. Now it's time for you to read attentively (yes, read attentively - instead of copying-and-pasting code from everywhere you like) the description of #pragma warning (default). It says that this construct
- Sets the warning at the default severity level and
- Turns on the warning.
Levels first. In Visual C++, each warning is assigned a number from 1 to 4 to specify the warning's severity level. Warnings of Level 1 are considered to be the most severe, and severity is thought to drop as the level number grows. Each warning has a default severity level. The construct
#pragma warning(Level: Warning)
sets the warning to the specified level and turns it on, i.e. the level is not nailed to the warning - you can change it if you wish.
The compiler has a setting (Warning Level) that specifies the threshold such that only warnings with level not higher that this threshold are emitted. When this parameter is set at A, a warning for a particular code line is shown only if it is permitted for this line and its level is A or lower.
Besides, a number of Visual C++ warnings are disabled by default, for they are generated even in the safest code and everyone is sick and tired of them. Anyone who is about to protest against the very idea of local suppression of concrete warnings should realize and feel this fact deeply before protesting.
Let's see how the FAIL with #pragma warning (default) manifests itself.
FAIL 1. The warning C9001 is disabled by default. The code in the header file uses #pragma warning(default:9001) to "restore" the warning suppressed in a small code piece.
Why would it do so when the warning is already disabled? The list of Visual C++ warnings disabled by default changes from version to version as more and more warnings get added into it. If code was initially written for Visual C++ 7 where C9001 was enabled by default and now is compiled in Visual C++ 10 where it is disabled, then this construct is meaningless and or it could be a piece of legacy code.
It will result in #pragma warning(default) force-enabling the warning disabled by default.
FAIL 2. The warning C9002 has a default Level 3, while the project is compiled with a Level 2 threshold, i.e. with the instruction "to show warnings of Level 2 and lower". Developers were thinking it over for a long time and finally concluded that C9002 was actually serious enough to promote it to Level 2, that is, to force-increase its severity. Correspondingly, each project includes a standard header file with the instruction #pragma warning(2:9002) which then gets into all the translation units.
A bit further in the text of a translation unit there is the instruction #pragma warning(default:9002) which sets the warning level back to 3, so the warning is not generated during compilation with a Level 2 threshold. This warning reported a critical defect, by the way. Smile and wave. It can work backwards as well. For example, a warning was given a higher level (3 instead of 2) so that it is not shown in projects compiled with a Level 2 threshold (i.e. its severity was lowered), but #pragma warning(default) sets the level back to 2, and you'll start seeing it again.
FAIL 3. The warning C9003 is by default enabled, yet it is implemented in such a bad way that nobody can recall a case when it was helpful. Developers dare to suppress it everywhere by using #pragma warning(disable:9003) in the common header file. #pragma warning(default:9003) occurs further in the translation unit and enables the warning.
What is especially nice, these situations always occur at the right moment - when switching from one compiler version to another or when trying to include a heap of third-party code, which is already troublesome enough, into the project; while the FAIL 2 scenario simply suppresses the warning, which may result in missing a severe error in the code and helping the user get a Darwin Award.
Warnings must be suppressed in the following way:
#pragma warning(push) // fine vacancies: www.abbyy.ru/vacancy #pragma warning(disable:9000) // code with warning C9000 #pragma warning(pop)
The first instruction saves the current state of warning settings, the second disables the necessary warning, the third restores the previously saved state. The state is restored completely: you get back all the changes done at higher levels, while warnings disabled by default are not enabled.
It looks scary, but you'll do anything not to miss that very billion-dollar bug, won't you?