Skip to content

Commit

Permalink
Suppress unexpected compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxwa committed Dec 7, 2024
1 parent ce77fc8 commit aeec7ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
31 changes: 30 additions & 1 deletion proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,40 @@ struct proxy_helper {
return static_cast<add_qualifier_t<proxy<F>, Q>>(
std::forward<add_qualifier_t<A, Q>>(a));
} else {
// Note: The use of offsetof below is technically undefined until C++20
// because proxy may not be a standard layout type. However, all compilers
// currently provide well-defined behavior as an extension (which is
// demonstrated since constexpr evaluation must diagnose all undefined
// behavior). However, various compilers also warn about this use of
// offsetof, which must be suppressed.
#if defined(__INTEL_COMPILER)
#pragma warning push
#pragma warning(disable : 1875)
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#elif defined(__NVCC__)
#pragma nv_diagnostic push
#pragma nv_diag_suppress 1427
#elif defined(__NVCOMPILER)
#pragma diagnostic push
#pragma diag_suppress offset_in_non_POD_nonstandard
#endif // defined(__INTEL_COMPILER)
constexpr std::size_t offset = offsetof(proxy<F>, ia_);
#if defined(__INTEL_COMPILER)
#pragma warning pop
#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#elif defined(__NVCC__)
#pragma nv_diagnostic pop
#elif defined(__NVCOMPILER)
#pragma diagnostic pop
#endif // defined(__INTEL_COMPILER)
return reinterpret_cast<add_qualifier_t<proxy<F>, Q>>(
*(reinterpret_cast<add_qualifier_ptr_t<std::byte, Q>>(
static_cast<add_qualifier_ptr_t<
typename facade_traits<F>::indirect_accessor, Q>>(
std::addressof(a))) - offsetof(proxy<F>, ia_)));
std::addressof(a))) - offset));
}
}
};
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_executable(msft_proxy_tests
proxy_invocation_tests.cpp
proxy_lifetime_tests.cpp
proxy_reflection_tests.cpp
proxy_regression_tests.cpp
proxy_traits_tests.cpp
)
target_include_directories(msft_proxy_tests PRIVATE .)
Expand Down
19 changes: 19 additions & 0 deletions tests/proxy_regression_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include <gtest/gtest.h>
#include "proxy.h"

// https://github.com/microsoft/proxy/issues/213
TEST(ProxyRegressionTests, TestUnexpectedCompilerWarning) {
struct MyTrivialFacade : pro::facade_builder
::add_convention<pro::operator_dispatch<"()">, void(), void() const>
::support_copy<pro::constraint_level::trivial>
::support_relocation<pro::constraint_level::trivial>
::support_destruction<pro::constraint_level::trivial>
::build {};
int side_effect = 0;
pro::proxy<MyTrivialFacade> p = pro::make_proxy<MyTrivialFacade>([&] { side_effect = 1; });
(*p)();
EXPECT_EQ(side_effect, 1);
}

0 comments on commit aeec7ba

Please sign in to comment.