Read flags using the GetFlag() style.
Change-Id: Ib07828b7df210f4cc8fc612be4954c19d578e2f1
Reviewed-on: https://code-review.googlesource.com/c/re2/+/46823
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/testing/dfa_test.cc b/re2/testing/dfa_test.cc
index caf3fa1..25f2311 100644
--- a/re2/testing/dfa_test.cc
+++ b/re2/testing/dfa_test.cc
@@ -36,7 +36,7 @@
TEST(Multithreaded, BuildEntireDFA) {
// Create regexp with 2^FLAGS_size states in DFA.
std::string s = "a";
- for (int i = 0; i < FLAGS_size; i++)
+ for (int i = 0; i < GetFlag(FLAGS_size); i++)
s += "[ab]";
s += "b";
Regexp* re = Regexp::Parse(s, Regexp::LikePerl, NULL);
@@ -54,14 +54,14 @@
}
// Build the DFA simultaneously in a bunch of threads.
- for (int i = 0; i < FLAGS_repeat; i++) {
+ for (int i = 0; i < GetFlag(FLAGS_repeat); i++) {
Prog* prog = re->CompileToProg(0);
ASSERT_TRUE(prog != NULL);
std::vector<std::thread> threads;
- for (int j = 0; j < FLAGS_threads; j++)
+ for (int j = 0; j < GetFlag(FLAGS_threads); j++)
threads.emplace_back(DoBuild, prog);
- for (int j = 0; j < FLAGS_threads; j++)
+ for (int j = 0; j < GetFlag(FLAGS_threads); j++)
threads[j].join();
// One more compile, to make sure everything is okay.
@@ -261,14 +261,14 @@
// Run the search simultaneously in a bunch of threads.
// Reuse same flags for Multithreaded.BuildDFA above.
- for (int i = 0; i < FLAGS_repeat; i++) {
+ for (int i = 0; i < GetFlag(FLAGS_repeat); i++) {
Prog* prog = re->CompileToProg(1<<n);
ASSERT_TRUE(prog != NULL);
std::vector<std::thread> threads;
- for (int j = 0; j < FLAGS_threads; j++)
+ for (int j = 0; j < GetFlag(FLAGS_threads); j++)
threads.emplace_back(DoSearch, prog, match, no_match);
- for (int j = 0; j < FLAGS_threads; j++)
+ for (int j = 0; j < GetFlag(FLAGS_threads); j++)
threads[j].join();
delete prog;
diff --git a/re2/testing/exhaustive_tester.cc b/re2/testing/exhaustive_tester.cc
index d75e23d..da0d436 100644
--- a/re2/testing/exhaustive_tester.cc
+++ b/re2/testing/exhaustive_tester.cc
@@ -80,7 +80,7 @@
if (!topwrapper_.empty())
regexp = StringPrintf(topwrapper_.c_str(), regexp.c_str());
- if (FLAGS_show_regexps) {
+ if (GetFlag(FLAGS_show_regexps)) {
printf("\r%s", regexp.c_str());
fflush(stdout);
}
@@ -135,7 +135,7 @@
tests_++;
if (!tester.TestInput(strgen_.Next())) {
failures_++;
- if (++bad_inputs >= FLAGS_max_bad_regexp_inputs)
+ if (++bad_inputs >= GetFlag(FLAGS_max_bad_regexp_inputs))
break;
}
}
diff --git a/re2/testing/random_test.cc b/re2/testing/random_test.cc
index 4920925..956275a 100644
--- a/re2/testing/random_test.cc
+++ b/re2/testing/random_test.cc
@@ -38,8 +38,8 @@
ExhaustiveTester t(maxatoms, maxops, alphabet, ops,
maxstrlen, stralphabet, wrapper, "");
- t.RandomStrings(FLAGS_stringseed, FLAGS_stringcount);
- t.GenerateRandom(FLAGS_regexpseed, FLAGS_regexpcount);
+ t.RandomStrings(GetFlag(FLAGS_stringseed), GetFlag(FLAGS_stringcount));
+ t.GenerateRandom(GetFlag(FLAGS_regexpseed), GetFlag(FLAGS_regexpcount));
printf("%d regexps, %d tests, %d failures [%d/%d str]\n",
t.regexps(), t.tests(), t.failures(), maxstrlen, (int)stralphabet.size());
EXPECT_EQ(0, t.failures());
diff --git a/re2/testing/regexp_benchmark.cc b/re2/testing/regexp_benchmark.cc
index d501f1f..93d189e 100644
--- a/re2/testing/regexp_benchmark.cc
+++ b/re2/testing/regexp_benchmark.cc
@@ -736,15 +736,15 @@
namespace re2 {
-void BM_PCRE_Compile(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, CompilePCRE); }
-void BM_Regexp_Parse(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, ParseRegexp); }
-void BM_Regexp_Simplify(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, SimplifyRegexp); }
-void BM_CompileToProg(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, CompileToProg); }
-void BM_CompileByteMap(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, CompileByteMap); }
-void BM_Regexp_Compile(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, CompileRegexp); }
-void BM_Regexp_SimplifyCompile(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, SimplifyCompileRegexp); }
-void BM_Regexp_NullWalk(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, NullWalkRegexp); }
-void BM_RE2_Compile(benchmark::State& state) { RunBuild(state, FLAGS_compile_regexp, CompileRE2); }
+void BM_PCRE_Compile(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), CompilePCRE); }
+void BM_Regexp_Parse(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), ParseRegexp); }
+void BM_Regexp_Simplify(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), SimplifyRegexp); }
+void BM_CompileToProg(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), CompileToProg); }
+void BM_CompileByteMap(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), CompileByteMap); }
+void BM_Regexp_Compile(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), CompileRegexp); }
+void BM_Regexp_SimplifyCompile(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), SimplifyCompileRegexp); }
+void BM_Regexp_NullWalk(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), NullWalkRegexp); }
+void BM_RE2_Compile(benchmark::State& state) { RunBuild(state, GetFlag(FLAGS_compile_regexp), CompileRE2); }
#ifdef USEPCRE
BENCHMARK(BM_PCRE_Compile)->ThreadRange(1, NumCPUs());
diff --git a/re2/testing/tester.cc b/re2/testing/tester.cc
index 8f9c310..18ced11 100644
--- a/re2/testing/tester.cc
+++ b/re2/testing/tester.cc
@@ -63,11 +63,11 @@
if (did_parse)
return cached_engines;
- if (FLAGS_regexp_engines.empty()) {
+ if (GetFlag(FLAGS_regexp_engines).empty()) {
cached_engines = ~0;
} else {
for (Engine i = static_cast<Engine>(0); i < kEngineMax; i++)
- if (FLAGS_regexp_engines.find(EngineName(i)) != std::string::npos)
+ if (GetFlag(FLAGS_regexp_engines).find(EngineName(i)) != std::string::npos)
cached_engines |= 1<<i;
}
@@ -199,7 +199,7 @@
error_ = true;
return;
}
- if (FLAGS_dump_prog) {
+ if (GetFlag(FLAGS_dump_prog)) {
LOG(INFO) << "Prog for "
<< " regexp "
<< CEscape(regexp_str_)
@@ -217,7 +217,7 @@
error_ = true;
return;
}
- if (FLAGS_dump_rprog)
+ if (GetFlag(FLAGS_dump_rprog))
LOG(INFO) << rprog_->Dump();
}
@@ -529,7 +529,7 @@
Result r;
RunSearch(i, text, context, anchor, &r);
if (ResultOkay(r, correct)) {
- if (FLAGS_log_okay)
+ if (GetFlag(FLAGS_log_okay))
LogMatch(r.skipped ? "Skipped: " : "Okay: ", i, text, context, anchor);
continue;
}
@@ -572,7 +572,10 @@
}
if (!all_okay) {
- if (FLAGS_max_regexp_failures > 0 && --FLAGS_max_regexp_failures == 0)
+ // This will be initialised once (after flags have been initialised)
+ // and that is desirable because we want to enforce a global limit.
+ static int max_regexp_failures = GetFlag(FLAGS_max_regexp_failures);
+ if (max_regexp_failures > 0 && --max_regexp_failures == 0)
LOG(QFATAL) << "Too many regexp failures.";
}
diff --git a/util/flags.h b/util/flags.h
index 0ae526d..3386b72 100644
--- a/util/flags.h
+++ b/util/flags.h
@@ -16,4 +16,11 @@
#define DECLARE_FLAG(type, name) \
namespace re2 { extern type FLAGS_##name; }
+namespace re2 {
+template <typename T>
+T GetFlag(const T& flag) {
+ return flag;
+}
+} // namespace re2
+
#endif // UTIL_FLAGS_H_
diff --git a/util/pcre.cc b/util/pcre.cc
index 203f8ab..04bbd4a 100644
--- a/util/pcre.cc
+++ b/util/pcre.cc
@@ -524,12 +524,12 @@
int match_limit = match_limit_;
if (match_limit <= 0) {
- match_limit = FLAGS_regexp_match_limit;
+ match_limit = GetFlag(FLAGS_regexp_match_limit);
}
int stack_limit = stack_limit_;
if (stack_limit <= 0) {
- stack_limit = FLAGS_regexp_stack_limit;
+ stack_limit = GetFlag(FLAGS_regexp_stack_limit);
}
pcre_extra extra = { 0 };