From aeec7ba74dc233cb74cfac46d290fd2e6accc460 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Sat, 7 Dec 2024 23:21:48 +0800 Subject: [PATCH 1/2] Suppress unexpected compiler warnings --- proxy.h | 31 ++++++++++++++++++++++++++++++- tests/CMakeLists.txt | 1 + tests/proxy_regression_tests.cpp | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/proxy_regression_tests.cpp diff --git a/proxy.h b/proxy.h index 67606ec6..49d5792e 100644 --- a/proxy.h +++ b/proxy.h @@ -656,11 +656,40 @@ struct proxy_helper { return static_cast, Q>>( std::forward>(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, 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, Q>>( *(reinterpret_cast>( static_cast::indirect_accessor, Q>>( - std::addressof(a))) - offsetof(proxy, ia_))); + std::addressof(a))) - offset)); } } }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2bdfb5b3..6c188e35 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 .) diff --git a/tests/proxy_regression_tests.cpp b/tests/proxy_regression_tests.cpp new file mode 100644 index 00000000..7d4e93ff --- /dev/null +++ b/tests/proxy_regression_tests.cpp @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#include +#include "proxy.h" + +// https://github.com/microsoft/proxy/issues/213 +TEST(ProxyRegressionTests, TestUnexpectedCompilerWarning) { + struct MyTrivialFacade : pro::facade_builder + ::add_convention, void(), void() const> + ::support_copy + ::support_relocation + ::support_destruction + ::build {}; + int side_effect = 0; + pro::proxy p = pro::make_proxy([&] { side_effect = 1; }); + (*p)(); + EXPECT_EQ(side_effect, 1); +} From c6aaa7a3e5a543d112106e5318a6c82a7000abfe Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Sat, 7 Dec 2024 23:34:37 +0800 Subject: [PATCH 2/2] Fix NVHPC --- proxy.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/proxy.h b/proxy.h index 49d5792e..c9d421e0 100644 --- a/proxy.h +++ b/proxy.h @@ -668,23 +668,27 @@ struct proxy_helper { #elif defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Winvalid-offsetof" -#elif defined(__NVCC__) +#endif // defined(__INTEL_COMPILER) +#if defined(__NVCC__) #pragma nv_diagnostic push #pragma nv_diag_suppress 1427 -#elif defined(__NVCOMPILER) +#endif // defined(__NVCC__) +#if defined(__NVCOMPILER) #pragma diagnostic push #pragma diag_suppress offset_in_non_POD_nonstandard -#endif // defined(__INTEL_COMPILER) +#endif // defined(__NVCOMPILER) constexpr std::size_t offset = offsetof(proxy, ia_); #if defined(__INTEL_COMPILER) #pragma warning pop #elif defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop -#elif defined(__NVCC__) +#endif // defined(__INTEL_COMPILER) +#if defined(__NVCC__) #pragma nv_diagnostic pop -#elif defined(__NVCOMPILER) +#endif // defined(__NVCC__) +#if defined(__NVCOMPILER) #pragma diagnostic pop -#endif // defined(__INTEL_COMPILER) +#endif // defined(__NVCOMPILER) return reinterpret_cast, Q>>( *(reinterpret_cast>( static_cast