Repository: carbon-language/carbon-lang
File: testing/file_test/file_test_base.cpp (C++)

Code before:
}

auto FileTestBase::GetLineNumberReplacements(
    llvm::ArrayRef<llvm::StringRef> filenames)
    -> llvm::SmallVector<LineNumberReplacement> {
  return {{.has_file = true,
           .re = std::make_shared<RE2>(
               llvm::formatv(R"(({0}):(\d+)?)", llvm::join(filenames, "|"))),
           .line_formatv = R"({0})"}};
}

// If `--file_tests` is set, transform it into a `--gtest_filter`.
static auto MaybeApplyFileTestsFlag(llvm::StringRef factory_name) -> void {
  if (absl::GetFlag(FLAGS_file_tests).empty()) {
    return;
  }
  RawStringOstream filter;
  llvm::ListSeparator sep(":");
  for (const auto& file : absl::GetFlag(FLAGS_file_tests)) {
    filter << sep << factory_name << "." << file;
  }
  absl::SetFlag(&FLAGS_gtest_filter, filter.TakeStr());
}

// Loads tests from the manifest file, and registers them for execution. The
// vector is taken as a parameter so that the address of entries is stable for
// the factory.
static auto RegisterTests(FileTestFactory* test_factory,
                          llvm::StringRef exe_path,
                          llvm::SmallVectorImpl<FileTestInfo>& tests)
    -> ErrorOr<Success> {
  if (absl::GetFlag(FLAGS_test_targets_file).empty()) {
    return Error("Missing --test_targets_file.");
  }
  CARBON_ASSIGN_OR_RETURN(auto test_manifest,
                          ReadFile(absl::GetFlag(FLAGS_test_targets_file)));

  // Prepare the vector first, so that the location of entries won't change.
  for (const auto& test_name :
       absl::StrSplit(test_manifest, "\n", absl::SkipEmpty())) {
    tests.push_back({.test_name = std::string(test_name)});
  }

  // Amend entries with factory functions.
  for (auto& test : tests) {
    llvm::StringRef test_name = test.test_name;
    test.factory_fn = [test_factory, exe_path, test_name]() {
      return test_factory->factory_fn(exe_path, test_name);
    };
    test.registered_test = testing::RegisterTest(
        test_factory->name, test_name.data(), nullptr, test_name.data(),

Review comment (suggestion):
```suggestion
// vector is taken as an output parameter so that the address of entries is stable for
```

Code after:
  }
}

auto FileTestBase::GetLineNumberReplacements(
    llvm::ArrayRef<llvm::StringRef> filenames)
    -> llvm::SmallVector<LineNumberReplacement> {
  return {{.has_file = true,
           .re = std::make_shared<RE2>(
               llvm::formatv(R"(({0}):(\d+)?)", llvm::join(filenames, "|"))),
           .line_formatv = R"({0})"}};
}

// If `--file_tests` is set, transform it into a `--gtest_filter`.
static auto MaybeApplyFileTestsFlag(llvm::StringRef factory_name) -> void {
  if (absl::GetFlag(FLAGS_file_tests).empty()) {
    return;
  }
  RawStringOstream filter;
  llvm::ListSeparator sep(":");
  for (const auto& file : absl::GetFlag(FLAGS_file_tests)) {
    filter << sep << factory_name << "." << file;
  }
  absl::SetFlag(&FLAGS_gtest_filter, filter.TakeStr());
}

// Loads tests from the manifest file, and registers them for execution. The
// vector is taken as an output parameter so that the address of entries is
// stable for the factory.
static auto RegisterTests(FileTestFactory* test_factory,
                          llvm::StringRef exe_path,
                          llvm::SmallVectorImpl<FileTestInfo>& tests)
    -> ErrorOr<Success> {
  if (absl::GetFlag(FLAGS_test_targets_file).empty()) {
    return Error("Missing --test_targets_file.");
  }
  CARBON_ASSIGN_OR_RETURN(auto test_manifest,
                          ReadFile(absl::GetFlag(FLAGS_test_targets_file)));

  // Prepare the vector first, so that the location of entries won't change.
  for (const auto& test_name :
       absl::StrSplit(test_manifest, "\n", absl::SkipEmpty())) {
    tests.push_back({.test_name = std::string(test_name)});
  }

  // Amend entries with factory functions.
  for (auto& test : tests) {
    llvm::StringRef test_name = test.test_name;
    test.factory_fn = [test_factory, exe_path, test_name]() {
      return test_factory->factory_fn(exe_path, test_name);
    };
    test.registered_test = testing::RegisterTest(