Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve warning suppressions for header units #3069

Merged
merged 9 commits into from
Sep 3, 2022

Conversation

StephanTLavavej
Copy link
Member

This is a perma-workaround for DevCom-10097021 / VSO-1581774 "C++ STL header <format> causes C4296 warning that need to be suppressed every time". @cdacamar is investigating the underlying compiler bug, where #pragmas within functions aren't properly stored within an IFC. In the meantime, if we consistently push/disable/pop around a function, we'll avoid this problem.

Most of our push/disable/pops are already outside functions, so only a couple of occurrences need to be changed:

  • <format>: Move push/disable/pop outside of _Write_integral.
  • <thread>: Move push/disable/pop outside _Start.
    • Also update comment to exactly match the printed warning.

And one more occurrence can avoid pragmas entirely (I verified that this continues to suppress the warning):

  • <regex>: Use _Analysis_assume_ instead of push/disable/pop.

Next, this isn't being directly motivated by user bug reports, but I'm nervous about the occasional use of #pragma warning(suppress : NUM) in the headers. This seems likely to have similar problems with header units, and @AnjuDel recently encountered apparently squirrelly behavior of suppress in ATL/MFC. I think we should consistently push/disable/pop outside functions, which is a simple convention (and not too verbose):

  • <ranges>: Replace 2x suppress with push/disable/pop outside of iota_view::size.
  • <span>: Replace suppress with push/disable/pop outside size_bytes, front, back.
  • <xstring>: Replace suppress with push/disable/pop outside of resize_and_overwrite.
    • Update comment due to the changed context.
  • <atomic>: Replace suppress with push/disable/pop outside of _Atomic_thread_fence.
  • <format>: Replace suppress with push/disable/pop outside of _Format_arg_store::_Store_impl.

Finally, one kind of suppression can be eliminated entirely, due to recent /analyze improvements:

  • We don't need to suppress C6326 with if constexpr.
Click to expand example:
D:\GitHub\STL\out\build\x64>type woof.cpp
template <typename T>
int func() {
    constexpr auto N = sizeof(T);
#ifdef USE_IF_CONSTEXPR
    if constexpr (N < 4) {
#else
    if (N < 4) {
#endif
        return 11;
    } else {
        return 22;
    }
}

int main() {
    (void) func<short>();
    (void) func<double>();
}
D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od woof.cpp
woof.cpp
woof.cpp(7): warning C4127: conditional expression is constant
woof.cpp(2): note: consider using 'if constexpr' statement instead
woof.cpp(16): note: see reference to function template instantiation 'int func<short>(void)' being compiled
D:\GitHub\STL\out\build\x64\woof.cpp(7) : warning C6326: Potential comparison of a constant with another constant.
D:\GitHub\STL\out\build\x64\woof.cpp(7) : warning C6326: Potential comparison of a constant with another constant.

D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od /DUSE_IF_CONSTEXPR woof.cpp
woof.cpp

D:\GitHub\STL\out\build\x64>

Verified this change:

D:\GitHub\STL\out\build\x64>type meow.cpp
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void print(const vector<int>& v) {
    for (const auto& e : v) {
        cout << e << ", ";
    }
    cout << "\n";
}

int main() {
    vector<int> result{-1, -1, -1, -1};

    vector<int> v{11, 22, 33, 44};
    print(v);
    reverse(v.begin(), v.end());
    print(v);
    ranges::reverse(v);
    print(v);

    print(result);
    reverse_copy(v.begin(), v.end(), result.begin());
    print(result);
    ranges::fill(result, -1);
    print(result);
    ranges::reverse_copy(v, result.begin());
    print(result);
}
D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od /I D:\GitHub\STL\stl\inc meow.cpp && meow
meow.cpp
11, 22, 33, 44,
44, 33, 22, 11,
11, 22, 33, 44,
-1, -1, -1, -1,
44, 33, 22, 11,
-1, -1, -1, -1,
44, 33, 22, 11,

Example:

    D:\GitHub\STL\out\build\x64>type woof.cpp
    template <typename T>
    int func() {
        constexpr auto N = sizeof(T);
    #ifdef USE_IF_CONSTEXPR
        if constexpr (N < 4) {
    #else
        if (N < 4) {
    #endif
            return 11;
        } else {
            return 22;
        }
    }

    int main() {
        (void) func<short>();
        (void) func<double>();
    }

    D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od woof.cpp
    woof.cpp
    woof.cpp(7): warning C4127: conditional expression is constant
    woof.cpp(2): note: consider using 'if constexpr' statement instead
    woof.cpp(16): note: see reference to function template instantiation 'int func<short>(void)' being compiled
    D:\GitHub\STL\out\build\x64\woof.cpp(7) : warning C6326: Potential comparison of a constant with another constant.
    D:\GitHub\STL\out\build\x64\woof.cpp(7) : warning C6326: Potential comparison of a constant with another constant.

    D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od /DUSE_IF_CONSTEXPR woof.cpp
    woof.cpp

    D:\GitHub\STL\out\build\x64>

Verified:

    D:\GitHub\STL\out\build\x64>type meow.cpp
    #include <algorithm>
    #include <iostream>
    #include <vector>
    using namespace std;

    void print(const vector<int>& v) {
        for (const auto& e : v) {
            cout << e << ", ";
        }
        cout << "\n";
    }

    int main() {
        vector<int> result{-1, -1, -1, -1};

        vector<int> v{11, 22, 33, 44};
        print(v);
        reverse(v.begin(), v.end());
        print(v);
        ranges::reverse(v);
        print(v);

        print(result);
        reverse_copy(v.begin(), v.end(), result.begin());
        print(result);
        ranges::fill(result, -1);
        print(result);
        ranges::reverse_copy(v, result.begin());
        print(result);
    }

    D:\GitHub\STL\out\build\x64>cl /EHsc /nologo /W4 /analyze /std:c++latest /MTd /Od /I D:\GitHub\STL\stl\inc meow.cpp && meow
    meow.cpp
    11, 22, 33, 44,
    44, 33, 22, 11,
    11, 22, 33, 44,
    -1, -1, -1, -1,
    44, 33, 22, 11,
    -1, -1, -1, -1,
    44, 33, 22, 11,
@StephanTLavavej StephanTLavavej added bug Something isn't working modules C++23 modules, C++20 header units labels Sep 2, 2022
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner September 2, 2022 00:05
@StephanTLavavej StephanTLavavej self-assigned this Sep 3, 2022
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working modules C++23 modules, C++20 header units
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants