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.
This commit is contained in:
Dominic Hamon 2018-03-23 11:45:15 +00:00 committed by GitHub
parent e668e2a1ba
commit df60aeb266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 35 deletions

View File

@ -1,7 +1,5 @@
licenses(["notice"]) licenses(["notice"])
load("//bazel:have_regex.bzl", "have_regex_copts")
cc_library( cc_library(
name = "benchmark", name = "benchmark",
srcs = glob([ srcs = glob([
@ -9,7 +7,6 @@ cc_library(
"src/*.h", "src/*.h",
]), ]),
hdrs = ["include/benchmark/benchmark.h"], hdrs = ["include/benchmark/benchmark.h"],
copts = have_regex_copts(),
strip_include_prefix = "include", strip_include_prefix = "include",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )

View File

@ -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"},
)

View File

@ -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"],
})

View File

@ -39,6 +39,7 @@
#elif defined(_WIN32) #elif defined(_WIN32)
#define BENCHMARK_OS_WINDOWS 1 #define BENCHMARK_OS_WINDOWS 1
#elif defined(__APPLE__) #elif defined(__APPLE__)
#define BENCHMARK_OS_APPLE 1
#include "TargetConditionals.h" #include "TargetConditionals.h"
#if defined(TARGET_OS_MAC) #if defined(TARGET_OS_MAC)
#define BENCHMARK_OS_MACOSX 1 #define BENCHMARK_OS_MACOSX 1

View File

@ -17,19 +17,31 @@
#include "internal_macros.h" #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 // Prefer C regex libraries when compiling w/o exceptions so that we can
// correctly report errors. // 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)) (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX))
#undef HAVE_STD_REGEX #undef HAVE_STD_REGEX
#endif #endif
#if defined(HAVE_STD_REGEX) #if defined(HAVE_STD_REGEX)
#include <regex> #include <regex>
#elif defined(HAVE_GNU_POSIX_REGEX) #elif defined(HAVE_GNU_POSIX_REGEX)
#include <gnuregex.h> #include <gnuregex.h>
#elif defined(HAVE_POSIX_REGEX) #elif defined(HAVE_POSIX_REGEX)
#include <regex.h> #include <regex.h>
#else #else
#error No regular expression backend was found! #error No regular expression backend was found!
#endif #endif
@ -64,7 +76,7 @@ class Regex {
#elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX) #elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX)
regex_t re_; regex_t re_;
#else #else
#error No regular expression backend implementation available #error No regular expression backend implementation available
#endif #endif
}; };

View File

@ -1,5 +1,3 @@
load("//bazel:have_regex.bzl", "have_regex_copts")
NEEDS_GTEST_MAIN = [ NEEDS_GTEST_MAIN = [
"statistics_test.cc", "statistics_test.cc",
] ]
@ -8,7 +6,7 @@ TEST_COPTS = [
"-pedantic", "-pedantic",
"-pedantic-errors", "-pedantic-errors",
"-std=c++11", "-std=c++11",
] + have_regex_copts() ]
TEST_ARGS = ["--benchmark_min_time=0.01"] TEST_ARGS = ["--benchmark_min_time=0.01"]