Repository: google/benchmark
File: src/benchmark.cc (C++)

Code before:
  internal::memory_manager = manager;
}

void AddCustomContext(const std::string& key, const std::string& value) {
  if (internal::global_context == nullptr) {
    internal::global_context = new std::map<std::string, std::string>();
  }
  if (!internal::global_context->emplace(key, value).second) {
    std::cerr << "Failed to add custom context \"" << key << "\" as it already "
              << "exists with value \"" << value << "\"\n";
  }
}

namespace {

inline TimeUnit GetTimeUnitFromString(const std::string& unit_string) {
  if (unit_string == "s") {
    return kSecond;
  } else if (unit_string == "ms") {
    return kMillisecond;
  } else if (unit_string == "us") {
    return kMicrosecond;
  } else if (unit_string == "ns") {
    return kNanosecond;
  }
  BENCHMARK_UNREACHABLE();
}

}  // namespace

namespace internal {

void PrintUsageAndExit() {
  fprintf(stdout,
          "benchmark"
          " [--benchmark_list_tests={true|false}]\n"
          "          [--benchmark_filter=<regex>]\n"
          "          [--benchmark_min_time=<min_time>]\n"
          "          [--benchmark_repetitions=<num_repetitions>]\n"
          "          [--benchmark_enable_random_interleaving={true|false}]\n"
          "          [--benchmark_report_aggregates_only={true|false}]\n"
          "          [--benchmark_display_aggregates_only={true|false}]\n"
          "          [--benchmark_format=<console|json|csv>]\n"
          "          [--benchmark_out=<filename>]\n"
          "          [--benchmark_out_format=<json|console|csv>]\n"
          "          [--benchmark_color={auto|true|false}]\n"
          "          [--benchmark_counters_tabular={true|false}]\n"
          "          [--benchmark_perf_counters=<counter>,...]\n"
          "          [--benchmark_context=<key>=<value>,...]\n"
          "          [--benchmark_time_unit={ns|us|ms|s}]\n"
          "          [--v=<verbosity>]\n");

Review comment (suggestion):
consider a better error message that whatever the unreachable macro would give.

ie, instead of doing the check when reading the flag, and then calling this, call this with the flag and use CHECK to do the check within this function.

Code after:
TimeUnit GetDefaultTimeUnit() { return default_time_unit; }

void SetDefaultTimeUnit(TimeUnit unit) { default_time_unit = unit; }

std::string GetBenchmarkFilter() { return FLAGS_benchmark_filter; }

void RegisterMemoryManager(MemoryManager* manager) {
  internal::memory_manager = manager;
}

void AddCustomContext(const std::string& key, const std::string& value) {
  if (internal::global_context == nullptr) {
    internal::global_context = new std::map<std::string, std::string>();
  }
  if (!internal::global_context->emplace(key, value).second) {
    std::cerr << "Failed to add custom context \"" << key << "\" as it already "
              << "exists with value \"" << value << "\"\n";
  }
}

namespace internal {

void (*HelperPrintf)();

void PrintUsageAndExit() {
  if (HelperPrintf) {
    HelperPrintf();
  } else {
    fprintf(stdout,
            "benchmark"
            " [--benchmark_list_tests={true|false}]\n"
            "          [--benchmark_filter=<regex>]\n"
            "          [--benchmark_min_time=<min_time>]\n"
            "          [--benchmark_repetitions=<num_repetitions>]\n"
            "          [--benchmark_enable_random_interleaving={true|false}]\n"
            "          [--benchmark_report_aggregates_only={true|false}]\n"
            "          [--benchmark_display_aggregates_only={true|false}]\n"
            "          [--benchmark_format=<console|json|csv>]\n"
            "          [--benchmark_out=<filename>]\n"
            "          [--benchmark_out_format=<json|console|csv>]\n"
            "          [--benchmark_color={auto|true|false}]\n"
            "          [--benchmark_counters_tabular={true|false}]\n"
            "          [--benchmark_context=<key>=<value>,...]\n"
            "          [--benchmark_time_unit={ns|us|ms|s}]\n"
            "          [--v=<verbosity>]\n");
  }
  exit(0);
}

void SetDefaultTimeUnitFromFlag(const std::string& time_unit_flag) {
  if (time_unit_flag == "s") {