From df60aeb2667e140a6c6ae93e9e1d8eb3d33d72ab Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Fri, 23 Mar 2018 11:45:15 +0000 Subject: [PATCH] Rely on compiler intrinsics to identify regex engine. (#555) Having the copts set on a per-target level can lead to ODR violations in some cases. Avoid this by ensuring the regex engine is picked through compiler intrinsics in the header directly. --- BUILD.bazel | 3 --- bazel/BUILD | 16 ---------------- bazel/have_regex.bzl | 7 ------- src/internal_macros.h | 1 + src/re.h | 24 ++++++++++++++++++------ test/BUILD | 4 +--- 6 files changed, 20 insertions(+), 35 deletions(-) delete mode 100644 bazel/BUILD delete mode 100644 bazel/have_regex.bzl diff --git a/BUILD.bazel b/BUILD.bazel index 883ccc44..35605ccb 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,7 +1,5 @@ licenses(["notice"]) -load("//bazel:have_regex.bzl", "have_regex_copts") - cc_library( name = "benchmark", srcs = glob([ @@ -9,7 +7,6 @@ cc_library( "src/*.h", ]), hdrs = ["include/benchmark/benchmark.h"], - copts = have_regex_copts(), strip_include_prefix = "include", visibility = ["//visibility:public"], ) diff --git a/bazel/BUILD b/bazel/BUILD deleted file mode 100644 index dcbd4376..00000000 --- a/bazel/BUILD +++ /dev/null @@ -1,16 +0,0 @@ -package(default_visibility = ["//:__subpackages__"]) - -config_setting( - name = "have_std_regex", - values = {"define": "google_benchmark.have_regex=std"}, -) - -config_setting( - name = "have_posix_regex", - values = {"define": "google_benchmark.have_regex=posix"}, -) - -config_setting( - name = "have_gnu_posix_regex", - values = {"define": "google_benchmark.have_regex=gnu_posix"}, -) diff --git a/bazel/have_regex.bzl b/bazel/have_regex.bzl deleted file mode 100644 index 6ea5ba01..00000000 --- a/bazel/have_regex.bzl +++ /dev/null @@ -1,7 +0,0 @@ -def have_regex_copts(): - return select({ - "//bazel:have_std_regex": ["-DHAVE_STD_REGEX"], - "//bazel:have_posix_regex": ["-DHAVE_POSIX_REGEX"], - "//bazel:have_gnu_posix_regex": ["-DHAVE_GNU_POSIX_REGEX"], - "//conditions:default": ["-DHAVE_STD_REGEX"], - }) diff --git a/src/internal_macros.h b/src/internal_macros.h index f299722c..248ef262 100644 --- a/src/internal_macros.h +++ b/src/internal_macros.h @@ -39,6 +39,7 @@ #elif defined(_WIN32) #define BENCHMARK_OS_WINDOWS 1 #elif defined(__APPLE__) + #define BENCHMARK_OS_APPLE 1 #include "TargetConditionals.h" #if defined(TARGET_OS_MAC) #define BENCHMARK_OS_MACOSX 1 diff --git a/src/re.h b/src/re.h index 01e97365..924d2f0b 100644 --- a/src/re.h +++ b/src/re.h @@ -17,19 +17,31 @@ #include "internal_macros.h" +#if !defined(HAVE_STD_REGEX) && \ + !defined(HAVE_GNU_POSIX_REGEX) && \ + !defined(HAVE_POSIX_REGEX) + // No explicit regex selection; detect based on builtin hints. + #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE) + #define HAVE_POSIX_REGEX 1 + #elif __cplusplus >= 199711L + #define HAVE_STD_REGEX 1 + #endif +#endif + // Prefer C regex libraries when compiling w/o exceptions so that we can // correctly report errors. -#if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && defined(HAVE_STD_REGEX) && \ +#if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \ + defined(BENCHMARK_HAVE_STD_REGEX) && \ (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX)) -#undef HAVE_STD_REGEX + #undef HAVE_STD_REGEX #endif #if defined(HAVE_STD_REGEX) -#include + #include #elif defined(HAVE_GNU_POSIX_REGEX) -#include + #include #elif defined(HAVE_POSIX_REGEX) -#include + #include #else #error No regular expression backend was found! #endif @@ -64,7 +76,7 @@ class Regex { #elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX) regex_t re_; #else -#error No regular expression backend implementation available + #error No regular expression backend implementation available #endif }; diff --git a/test/BUILD b/test/BUILD index 9e5d4936..c2682210 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,5 +1,3 @@ -load("//bazel:have_regex.bzl", "have_regex_copts") - NEEDS_GTEST_MAIN = [ "statistics_test.cc", ] @@ -8,7 +6,7 @@ TEST_COPTS = [ "-pedantic", "-pedantic-errors", "-std=c++11", -] + have_regex_copts() +] TEST_ARGS = ["--benchmark_min_time=0.01"]