mirror of https://github.com/google/benchmark.git
Fix #552 - GCC and Clang warn on possibly invalid offsetof usage.
This patch disables the -Winvalid-offsetof warning for GCC and Clang when using it to check the cache lines of the State object. Technically this usage of offsetof is undefined behavior until C++17. However, all major compilers support this application as an extension, as demonstrated by the passing static assert (If a compiler encounters UB during evaluation of a constant expression, that UB must be diagnosed). Unfortunately, Clang and GCC also produce a warning about it. This patch temporarily suppresses the warning using #pragma's in the source file (instead of globally suppressing the warning in the build systems). This way the warning is ignored for both CMake and Bazel builds without having to modify either build system.
This commit is contained in:
parent
68e228944e
commit
e668e2a1ba
|
@ -120,13 +120,6 @@ else()
|
|||
cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
|
||||
endif()
|
||||
|
||||
# GCC doesn't report invalid -Wno-warning flags, so check the positive
|
||||
# version, and if it exists add the negative.
|
||||
check_cxx_warning_flag(-Winvalid-offsetof)
|
||||
if (HAVE_CXX_FLAG_WINVALID_OFFSETOF)
|
||||
add_cxx_compiler_flag(-Wno-invalid-offsetof)
|
||||
endif()
|
||||
|
||||
# On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
|
||||
# predefined macro, which turns on all of the wonderful libc extensions.
|
||||
# However g++ doesn't do this in Cygwin so we have to define it ourselfs
|
||||
|
|
|
@ -311,10 +311,23 @@ State::State(size_t max_iters, const std::vector<int>& ranges, int thread_i,
|
|||
CHECK(max_iterations != 0) << "At least one iteration must be run";
|
||||
CHECK_LT(thread_index, threads) << "thread_index must be less than threads";
|
||||
|
||||
// Note: The use of offsetof below is technically undefined until C++17
|
||||
// because State is not 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, GCC and Clang also warn about this use of offsetof,
|
||||
// which must be suppressed.
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
|
||||
#endif
|
||||
// Offset tests to ensure commonly accessed data is on the first cache line.
|
||||
const int cache_line_size = 64;
|
||||
static_assert(offsetof(State, error_occurred_) <=
|
||||
(cache_line_size - sizeof(error_occurred_)), "");
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
void State::PauseTiming() {
|
||||
|
|
Loading…
Reference in New Issue