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

Fix msvc unreachable code warnings #1841

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/range/v3/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ namespace ranges
#define RANGES_ENSURE(...) RANGES_ENSURE_MSG((__VA_ARGS__), #__VA_ARGS__)
#endif

#ifndef RANGES_EXPECT_UNREACHABLE_RETURN
#if defined(NDEBUG) && defined(_MSC_VER)
// In release builds, RANGES_EXPECT(false) for msvc invokes an intrinsic that indicates
// subsequent operations are unreachable. Do not invoke a return after its evaluation,
// lest the compiler warn that the return is unreachable.
#define RANGES_EXPECT_UNREACHABLE_RETURN(RETURN_VALUE) RANGES_EXPECT(false)
#else
#define RANGES_EXPECT_UNREACHABLE_RETURN(RETURN_VALUE) \
do \
{ \
return (RANGES_EXPECT(false), RETURN_VALUE); \
} while(false)
#endif
#endif // RANGES_EXPECT_UNREACHABLE_RETURN

#define RANGES_DECLTYPE_AUTO_RETURN(...) \
->decltype(__VA_ARGS__) \
{ \
Expand Down
2 changes: 1 addition & 1 deletion include/range/v3/detail/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ namespace ranges
template<typename Fun, typename Proj = indexed_element_fn>
constexpr int variant_visit_(std::size_t, variant_nil, Fun, Proj = {})
{
return (RANGES_EXPECT(false), 0);
RANGES_EXPECT_UNREACHABLE_RETURN(0);
}
template<typename Data, typename Fun, typename Proj = indexed_element_fn>
constexpr int variant_visit_(std::size_t n, Data & self, Fun fun, Proj proj = {})
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ rv3_add_test(test.bug1322 bug1322 bug1322.cpp)
rv3_add_test(test.bug1335 bug1335 bug1335.cpp)
rv3_add_test(test.bug1633 bug1633 bug1633.cpp)
rv3_add_test(test.bug1729 bug1729 bug1729.cpp)
rv3_add_test(test.bug1762 bug1762 bug1762.cpp)
rv3_add_test(test.bug1814 bug1814 bug1814.cpp)
rv3_add_test(test.bug-guard bug-guard bug-guard.cpp)
17 changes: 17 additions & 0 deletions test/bug1762.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if defined(_MSC_VER)
// Enable level 4 "unreachable code" warning
#pragma warning(error : 4702)
#elif defined(__clang__)
#pragma clang diagnostic error "-Wunreachable-code-aggressive"
#elif defined(__GNUC__)
// Unsupported and ignored in gcc >= 5
#pragma GCC diagnostic error "-Wunreachable-code"
#endif

#include <range/v3/range/conversion.hpp>
#include <range/v3/view/generate_n.hpp>

int main()
{
ranges::views::generate_n(rand, 5) | ranges::to<std::vector>();
}
22 changes: 22 additions & 0 deletions test/bug1814.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#if defined(_MSC_VER)
// Enable level 4 "unreachable code" warning
#pragma warning(error : 4702)
#elif defined(__clang__)
#pragma clang diagnostic error "-Wunreachable-code-aggressive"
#elif defined(__GNUC__)
// Unsupported and ignored in gcc >= 5
#pragma GCC diagnostic error "-Wunreachable-code"
#endif

#include <vector>

#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/transform.hpp>

int main()
{
const std::vector<std::vector<int>> v{{{1, 2, 3}}, {{4, 5, 6}}};
v | ranges::views::transform([](const auto & sub_v) { return sub_v; }) |
ranges::views::join | ranges::to_vector;
}