Unverified Commit df60aeb2 by Dominic Hamon Committed by GitHub

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.
parent e668e2a1
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"],
)
......
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"},
)
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"],
})
......@@ -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
......
......@@ -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 <regex>
#include <regex>
#elif defined(HAVE_GNU_POSIX_REGEX)
#include <gnuregex.h>
#include <gnuregex.h>
#elif defined(HAVE_POSIX_REGEX)
#include <regex.h>
#include <regex.h>
#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
};
......
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"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment