Skip to content

Commit

Permalink
Read flags using the GetFlag() style.
Browse files Browse the repository at this point in the history
Change-Id: Ib07828b7df210f4cc8fc612be4954c19d578e2f1
Reviewed-on: https://code-review.googlesource.com/c/re2/+/46823
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Oct 11, 2019
1 parent 7908274 commit 9311259
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 28 deletions.
14 changes: 7 additions & 7 deletions re2/testing/dfa_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void DoBuild(Prog* prog) {
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);
Expand All @@ -54,14 +54,14 @@ TEST(Multithreaded, BuildEntireDFA) {
}

// 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.
Expand Down Expand Up @@ -261,14 +261,14 @@ TEST(Multithreaded, SearchDFA) {

// 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;
Expand Down
4 changes: 2 additions & 2 deletions re2/testing/exhaustive_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void ExhaustiveTester::HandleRegexp(const std::string& const_regexp) {
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);
}
Expand Down Expand Up @@ -135,7 +135,7 @@ void ExhaustiveTester::HandleRegexp(const std::string& const_regexp) {
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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions re2/testing/random_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ static void RandomTest(int maxatoms, int maxops,

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());
Expand Down
18 changes: 9 additions & 9 deletions re2/testing/regexp_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,15 @@ DEFINE_FLAG(std::string, compile_regexp, "(.*)-(\\d+)-of-(\\d+)",

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());
Expand Down
15 changes: 9 additions & 6 deletions re2/testing/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ static uint32_t Engines() {
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;
}

Expand Down Expand Up @@ -199,7 +199,7 @@ TestInstance::TestInstance(const StringPiece& regexp_str, Prog::MatchKind kind,
error_ = true;
return;
}
if (FLAGS_dump_prog) {
if (GetFlag(FLAGS_dump_prog)) {
LOG(INFO) << "Prog for "
<< " regexp "
<< CEscape(regexp_str_)
Expand All @@ -217,7 +217,7 @@ TestInstance::TestInstance(const StringPiece& regexp_str, Prog::MatchKind kind,
error_ = true;
return;
}
if (FLAGS_dump_rprog)
if (GetFlag(FLAGS_dump_rprog))
LOG(INFO) << rprog_->Dump();
}

Expand Down Expand Up @@ -529,7 +529,7 @@ bool TestInstance::RunCase(const StringPiece& text, const StringPiece& context,
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;
}
Expand Down Expand Up @@ -572,7 +572,10 @@ bool TestInstance::RunCase(const StringPiece& text, const StringPiece& context,
}

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.";
}

Expand Down
7 changes: 7 additions & 0 deletions util/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
4 changes: 2 additions & 2 deletions util/pcre.cc
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ int PCRE::TryMatch(const StringPiece& text,

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 };
Expand Down

0 comments on commit 9311259

Please sign in to comment.