Silence -Wc2y-extensions warning around __COUNTER__ (#2108)
clang-23 in pedantic mode now warns that __COUNTER__ macro is c2y
extension. This patch silences this warning around uses of this
macro.
Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>
diff --git a/AUTHORS b/AUTHORS
index ef90553..11d28f7 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -44,6 +44,7 @@
Jussi Knuuttila <jussi.knuuttila@gmail.com>
Kaito Udagawa <umireon@gmail.com>
Kishan Kumar <kumar.kishan@outlook.com>
+Kostiantyn Lazukin <konstantin.lazukin@gmail.com>
Lei Xu <eddyxu@gmail.com>
Marcel Jacobse <mjacobse@uni-bremen.de>
Matt Clarkson <mattyclarkson@gmail.com>
diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index 4b01592..52e49cc 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -67,6 +67,7 @@
Kaito Udagawa <umireon@gmail.com>
Kai Wolf <kai.wolf@gmail.com>
Kishan Kumar <kumar.kishan@outlook.com>
+Kostiantyn Lazukin <konstantin.lazukin@gmail.com>
Lei Xu <eddyxu@gmail.com>
Marcel Jacobse <mjacobse@uni-bremen.de>
Matt Clarkson <mattyclarkson@gmail.com>
diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h
index a67a24e..f7d1341 100644
--- a/include/benchmark/benchmark.h
+++ b/include/benchmark/benchmark.h
@@ -1484,14 +1484,29 @@
// ------------------------------------------------------
// Macro to register benchmarks
+// clang-format off
+#if defined(__clang__)
+#define BENCHMARK_DISABLE_COUNTER_WARNING \
+ _Pragma("GCC diagnostic push") \
+ _Pragma("GCC diagnostic ignored \"-Wunknown-warning-option\"") \
+ _Pragma("GCC diagnostic ignored \"-Wc2y-extensions\"")
+#define BENCHMARK_RESTORE_COUNTER_WARNING _Pragma("GCC diagnostic pop")
+#else
+#define BENCHMARK_DISABLE_COUNTER_WARNING
+#define BENCHMARK_RESTORE_COUNTER_WARNING
+#endif
+// clang-format on
+
// Check that __COUNTER__ is defined and that __COUNTER__ increases by 1
// every time it is expanded. X + 1 == X + 0 is used in case X is defined to be
// empty. If X is empty the expression becomes (+1 == +0).
+BENCHMARK_DISABLE_COUNTER_WARNING
#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
#else
#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
#endif
+BENCHMARK_RESTORE_COUNTER_WARNING
// Helpers for generating unique variable names
#define BENCHMARK_PRIVATE_NAME(...) \
@@ -1505,9 +1520,10 @@
BaseClass##_##Method##_Benchmark
#define BENCHMARK_PRIVATE_DECLARE(n) \
+ BENCHMARK_DISABLE_COUNTER_WARNING \
/* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \
static ::benchmark::Benchmark const* const BENCHMARK_PRIVATE_NAME(n) \
- BENCHMARK_UNUSED
+ BENCHMARK_RESTORE_COUNTER_WARNING BENCHMARK_UNUSED
#define BENCHMARK(...) \
BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
@@ -1695,9 +1711,11 @@
::benchmark::internal::make_unique<UniqueName>()))
#define BENCHMARK_TEMPLATE_INSTANTIATE_F(BaseClass, Method, ...) \
+ BENCHMARK_DISABLE_COUNTER_WARNING \
BENCHMARK_TEMPLATE_PRIVATE_INSTANTIATE_F( \
BaseClass, Method, BENCHMARK_PRIVATE_NAME(BaseClass##Method), \
- __VA_ARGS__)
+ __VA_ARGS__) \
+ BENCHMARK_RESTORE_COUNTER_WARNING
// This macro will define and register a benchmark within a fixture class.
#define BENCHMARK_F(BaseClass, Method) \