Search code examples
c++visual-studio-2017compiler-warningssuppress-warnings

VS2017 C++: Howto supress warnings after "Generating Code..."


I get warnings after the MSVC-Output window says "Generating Code...".

1>Note: including file: D:\FOO\INC\ippcc.h
1>Generating Code...
1>d:\FOO\inc\ipinctrlimpl.h(130): warning C4701: potentially uninitialized local variable 'hResult' used
1>d:\FOO\inc\iwatchdogimpl.h(158): warning C4702: unreachable code
1>   Creating library ..\..\LIB/FOO.lib and object ..\..\LIB/FOO.exp
1>FOO.vcxproj -> D:\FOO\FOO.dll
1>Done building project "FOO.vcxproj".

How can I supress these warnings without disabling them for the whole solution? I cannot touch the code myself, so fixing them is no option.

According to this post, the compiler is generating the machine code at that point. How is it even possible that these warnings are generated then? After all, the basic compilation is already done.

UPDATE:

Setting the global warning level to /W3 in the project settings rather than /W4 prevents those warnings (because they are Level 4 warnings).

Instead of globally setting /W3, I can also explicitly disable the warnings locally for the critical includes:

#pragma warning(push)
#pragma warning(disable : 4701 4702)
#include "CriticalInclude.h"
#pragma warning(pop)

But here comes the weird thing: Locally setting /W3 (or even /W1) via

#pragma warning(push, 3)
#include "CriticalInclude.h"
#pragma warning(pop)

does not prevent those warnings. Why?

It seems like pushing and popping a warning disable is somehow treated differently than pushing a new warning level.


Solution

  • So, as of my previous Update, locally reducing the warning level does not seem to carry on to the "Generating Code"-Stage.

    #pragma warning(push, 1)
    #include "CriticalInclude.h"
    #pragma warning(pop)
    

    However, explicitly disabling these warnings locally does have an effect on the generating code stage:

    #pragma warning(push)
    #pragma warning(disable : 4701 4702)
    #include "CriticalInclude.h"
    #pragma warning(pop)
    

    To me, this seems almost like a bug in the compiler.