From f85304e4e3a0e4e1bf15b91720df4a19e90b589f Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Wed, 8 Aug 2018 15:39:57 +0200 Subject: [PATCH] Remove redundant default which causes failures (#649) * Remove redundant default which causes failures * Fix old GCC warnings caused by poor analysis * Use __builtin_unreachable * Use BENCHMARK_UNREACHABLE() * Pull __has_builtin to benchmark.h too * Also move compiler identification macro to main header * Move custom compiler identification macro back --- include/benchmark/benchmark.h | 16 ++++++++++++++-- src/internal_macros.h | 11 ----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index aef995ec..7434a8a5 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -241,6 +241,18 @@ BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); #define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) #endif +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +#if defined(__GNUC__) || __has_builtin(__builtin_unreachable) + #define BENCHMARK_UNREACHABLE() __builtin_unreachable() +#elif defined(_MSC_VER) + #define BENCHMARK_UNREACHABLE() __assume(false) +#else + #define BENCHMARK_UNREACHABLE() ((void)0) +#endif + namespace benchmark { class BenchmarkReporter; class MemoryManager; @@ -1474,9 +1486,9 @@ inline const char* GetTimeUnitString(TimeUnit unit) { case kMicrosecond: return "us"; case kNanosecond: - default: return "ns"; } + BENCHMARK_UNREACHABLE(); } inline double GetTimeUnitMultiplier(TimeUnit unit) { @@ -1486,9 +1498,9 @@ inline double GetTimeUnitMultiplier(TimeUnit unit) { case kMicrosecond: return 1e6; case kNanosecond: - default: return 1e9; } + BENCHMARK_UNREACHABLE(); } } // namespace benchmark diff --git a/src/internal_macros.h b/src/internal_macros.h index b7e9203f..32089e69 100644 --- a/src/internal_macros.h +++ b/src/internal_macros.h @@ -11,9 +11,6 @@ #ifndef __has_feature #define __has_feature(x) 0 #endif -#ifndef __has_builtin -#define __has_builtin(x) 0 -#endif #if defined(__clang__) #if !defined(COMPILER_CLANG) @@ -87,14 +84,6 @@ #define BENCHMARK_MAYBE_UNUSED #endif -#if defined(COMPILER_GCC) || __has_builtin(__builtin_unreachable) - #define BENCHMARK_UNREACHABLE() __builtin_unreachable() -#elif defined(COMPILER_MSVC) - #define BENCHMARK_UNREACHABLE() __assume(false) -#else - #define BENCHMARK_UNREACHABLE() ((void)0) -#endif - // clang-format on #endif // BENCHMARK_INTERNAL_MACROS_H_