From 4f31803ebbf283e24602260e39e63a296d44b0f8 Mon Sep 17 00:00:00 2001 From: Vy Nguyen Date: Fri, 29 Oct 2021 06:48:56 -0400 Subject: [PATCH] Fix un-initted error in test and fix change the API previously proposed to use std::string instead of raw char* (#1266) * Fix un-initted error in test. Found by -Werror,-Wsometimes-uninitialized * Update spec_arg_test.cc * additional change: - Change the API on GetBenchmarkFilter and the `spec` to std::string because google C++ styleguide internally kind of discouraged using raw const char* --- include/benchmark/benchmark.h | 18 ++++++++++++------ src/benchmark.cc | 27 +++++++++++++++++++-------- test/spec_arg_test.cc | 6 +++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 12e879ea..b5b736f1 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -290,15 +290,15 @@ void Shutdown(); bool ReportUnrecognizedArguments(int argc, char** argv); // Returns the current value of --benchmark_filter. -const char* GetBenchmarkFilter(); +std::string GetBenchmarkFilter(); // Generate a list of benchmarks matching the specified --benchmark_filter flag // and if --benchmark_list_tests is specified return after printing the name // of each matching benchmark. Otherwise run each matching benchmark and // report the results. // -// spec : Specify the benchmarks to run. If users do not specify this arg or -// if it has value of NULL, then the value of FLAGS_benchmark_filter +// spec : Specify the benchmarks to run. If users do not specify this arg, +// then the value of FLAGS_benchmark_filter // will be used. // // The second and third overload use the specified 'display_reporter' and @@ -308,12 +308,18 @@ const char* GetBenchmarkFilter(); // 'file_reporter' is ignored. // // RETURNS: The number of matching benchmarks. -size_t RunSpecifiedBenchmarks(const char* spec = NULL); +size_t RunSpecifiedBenchmarks(); +size_t RunSpecifiedBenchmarks(std::string spec); + +size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter); size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, - const char* spec = NULL); + std::string spec); + +size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, + BenchmarkReporter* file_reporter); size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter, - const char* spec = NULL); + std::string spec); // If a MemoryManager is registered (via RegisterMemoryManager()), // it can be used to collect and report allocation metrics for a run of the diff --git a/src/benchmark.cc b/src/benchmark.cc index e7b96ca5..ca001a9e 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -429,20 +429,33 @@ ConsoleReporter::OutputOptions GetOutputOptions(bool force_no_color) { } // end namespace internal -size_t RunSpecifiedBenchmarks(const char* spec) { +size_t RunSpecifiedBenchmarks() { + return RunSpecifiedBenchmarks(nullptr, nullptr, FLAGS_benchmark_filter); +} + +size_t RunSpecifiedBenchmarks(std::string spec) { return RunSpecifiedBenchmarks(nullptr, nullptr, spec); } +size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter) { + return RunSpecifiedBenchmarks(display_reporter, nullptr, + FLAGS_benchmark_filter); +} + size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, - const char* spec) { + std::string spec) { return RunSpecifiedBenchmarks(display_reporter, nullptr, spec); } +size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, + BenchmarkReporter* file_reporter) { + return RunSpecifiedBenchmarks(display_reporter, file_reporter, + FLAGS_benchmark_filter); +} + size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter, - const char* spec_str) { - std::string spec = - spec_str != nullptr ? std::string(spec_str) : FLAGS_benchmark_filter; + std::string spec) { if (spec.empty() || spec == "all") spec = "."; // Regexp that matches all benchmarks @@ -498,9 +511,7 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, return benchmarks.size(); } -const char* GetBenchmarkFilter() { - return FLAGS_benchmark_filter.c_str(); -} +std::string GetBenchmarkFilter() { return FLAGS_benchmark_filter; } void RegisterMemoryManager(MemoryManager* manager) { internal::memory_manager = manager; diff --git a/test/spec_arg_test.cc b/test/spec_arg_test.cc index f1f0543e..624f17f0 100644 --- a/test/spec_arg_test.cc +++ b/test/spec_arg_test.cc @@ -55,10 +55,10 @@ static void BM_Chosen(benchmark::State& state) { BENCHMARK(BM_Chosen); int main(int argc, char** argv) { - const char* const flag = "BM_NotChosen"; + const std::string flag = "BM_NotChosen"; // Verify that argv specify --benchmark_filter=BM_NotChosen. - bool found; + bool found = false; for (int i = 0; i < argc; ++i) { if (strcmp("--benchmark_filter=BM_NotChosen", argv[i]) == 0) { found = true; @@ -71,7 +71,7 @@ int main(int argc, char** argv) { // Check that the current flag value is reported accurately via the // GetBenchmarkFilter() function. - if (strcmp(flag, benchmark::GetBenchmarkFilter()) != 0) { + if (flag != benchmark::GetBenchmarkFilter()) { std::cerr << "Seeing different value for flags. GetBenchmarkFilter() returns [" << benchmark::GetBenchmarkFilter() << "] expected flag=[" << flag