Search code examples
c++visual-studiovisual-studio-2019

Visual Studio 2019 standard library generates warnings


I want to make project with maximal warning level and treat warnings as errors.

There is my CmakeLists.txt file:

cmake_minimum_required (VERSION 3.17.0 FATAL_ERROR)
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} /Wall /WX /EHcs")

project(pedantic)
set(CMAKE_CXX_STANDARD 17)

add_executable (pedantic main.cpp)

There is my main.cpp file:

#include <string>
#include <iostream>

int main()
{
    std::string str = "abc";
    std::cout << str;
    return 0;
}

I generate the solution using the following command:

cmake ../ -G "Visual Studio 16 2019" -DCMAKE_BUILD_TYPE=Release

The output is:

-- Configuring done
-- Generating done
-- Build files have been written to: D:/Programming/Progz/C++/pedantic/build_x64_VS2019

After that I build this program:

cmake --build . --config Release

And I get a lot of warnings:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(118): error C2220: the following warning is treated as an error [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(118): warning C4710: 'void __cdecl std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ostream(103): warning C4710: 'class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::basic_ostream<char,struct std::char_traits<char> >::flush(void) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\buil
d_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\exception(316): warning C4710: '__cdecl std::bad_array_new_length::bad_array_new_length(void) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(131): warning C4710: 'void __cdecl std::_Throw_bad_array_new_length(void)': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(511): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(519): warning C4710: '__int64 __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const * __ptr64,__int64) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(523): warning C4710: 'int __cdecl std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(531): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(534): warning C4710: 'void __cdecl std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4475): warning C4710: 'void __cdecl std::_Xlen_string(void)': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(3230): warning C4710: 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __ptr64 __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::assign(char const * __ptr64 c
onst,unsigned __int64) __ptr64': function not inlined [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(229): warning C4711: function 'void * __ptr64 __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019
\pedantic.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xstring(4947): warning C4711: function 'class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::operator<<<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_ostream<char,struct std::char_tr
aits<char> > & __ptr64,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __ptr64)' selected for automatic inline expansion [D:\Programming\Progz\C++\pedantic\build_x64_VS2019\pedantic.vcxproj]

All of them comes from MSVS native standard library. I believe this compiler behavior is totally unacceptable.

How can I get rid of these warnings without changing warning level?


Solution

  • MSVC enables a ton of 'informational' warnings that are 'off-by-default' when you use /Wall. See Microsoft Docs.

    If you want to make use of 'maximal warnings' with MSVC, then you need to suppress a bunch of them selectively. For my GitHub projects, I use this but have a bunch of suppressions typically managed in a header using a #pragma.

    // Off by default warnings
    #pragma warning(disable : 4619 4616 4061 4265 4365 4571 4623 4625 4626 4628 4668 4710 4711 4746 4774 4820 4987 5026 5027 5031 5032 5039 5045 5219 5246 5264 26812)
    // C4619/4616 #pragma warning warnings
    // C4061 enumerator 'X' in switch of enum 'X' is not explicitly handled by a case label
    // C4265 class has virtual functions, but destructor is not virtual
    // C4365 signed/unsigned mismatch
    // C4571 behavior change
    // C4623 default constructor was implicitly defined as deleted
    // C4625 copy constructor was implicitly defined as deleted
    // C4626 assignment operator was implicitly defined as deleted
    // C4628 digraphs not supported
    // C4668 not defined as a preprocessor macro
    // C4710 function not inlined
    // C4711 selected for automatic inline expansion
    // C4746 volatile access of '<expression>' is subject to /volatile:<iso|ms> setting
    // C4774 format string expected in argument 3 is not a string literal
    // C4820 padding added after data member
    // C4987 nonstandard extension used
    // C5026 move constructor was implicitly defined as deleted
    // C5027 move assignment operator was implicitly defined as deleted
    // C5031/5032 push/pop mismatches in windows headers
    // C5039 pointer or reference to potentially throwing function passed to extern C function under - EHc
    // C5045 Spectre mitigation warning
    // C5219 implicit conversion from 'int' to 'float', possible loss of data
    // C5246 the initialization of a subobject should be wrapped in braces
    // C5264 'const' variable is not used
    // 26812: The enum type 'x' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).
    

    Alternatively, you'll have to suppress a lot of stuff via the command-line:

    if( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
        set(DisableWarnings "/wd4365" "/wd4514" "/wd4625" "/wd4626" "/wd4627" "/wd4710" "/wd4820" "/wd5026" "/wd5027" "/wd5039" "/wd5045")
        if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.34)
            list(APPEND DisableWarnings "/wd5262" "/wd5264")
        endif()
        target_compile_options(yourtarget PRIVATE ${DisableWarnings})
    

    For the best conformance out of MSVC, use /permissive- /Zc:__cplusplus /Zc:inline /Zc:preprocessor as well.

    One more note, if you make 3.20 your minimum supported CMake, then you can count on there not being any /W3 or /GR in the default CL flags. That way, you can use target_compile_options instead of playing games with CMAKE_CXX_FLAGS. /EHsc is always there for MSVC-like.