void OutputFileMap::UpdateForIncremental()

in tools/worker/output_file_map.cc [68:166]


void OutputFileMap::UpdateForIncremental(const std::string &path) {
  bool derived =
      path.find(".derived_output_file_map.json") != std::string::npos;

  nlohmann::json new_output_file_map;
  std::map<std::string, std::string> incremental_outputs;
  std::map<std::string, std::string> incremental_inputs;
  bool emitting_module = false;

  // The empty string key is used to represent outputs that are for the whole
  // module, rather than for a particular source file.
  nlohmann::json module_map;
  // Derive the swiftdeps file name from the .output-file-map.json name.
  auto new_path = ReplaceExtension(path, ".swiftdeps", /*all_extensions=*/true);
  auto swiftdeps_path = MakeIncrementalOutputPath(new_path, derived);
  module_map["swift-dependencies"] = swiftdeps_path;
  new_output_file_map[""] = module_map;

  for (auto &element : json_.items()) {
    auto src = element.key();
    auto outputs = element.value();

    nlohmann::json src_map;
    std::string swiftdeps_path;

    // Process the outputs for the current source file.
    for (auto &output : outputs.items()) {
      auto kind = output.key();
      auto path = output.value().get<std::string>();

      if (kind == "object") {
        // If the file kind is "object", we want to update the path to point to
        // the incremental storage area and then add a "swift-dependencies"
        // in the same location.
        auto new_path = MakeIncrementalOutputPath(path, derived);
        src_map[kind] = new_path;
        incremental_outputs[path] = new_path;

        if (swiftdeps_path.empty()) {
          swiftdeps_path = ReplaceExtension(new_path, ".swiftdeps");
        }
      } else if (kind == "swiftdoc" || kind == "swiftinterface" ||
                 kind == "swiftmodule" || kind == "swiftsourceinfo") {
        // Module/interface outputs should be moved to the incremental storage
        // area without additional processing.
        auto new_path = MakeIncrementalOutputPath(path, derived);
        src_map[kind] = new_path;
        incremental_outputs[path] = new_path;
        emitting_module = true;

        if (swiftdeps_path.empty()) {
          swiftdeps_path = ReplaceExtension(new_path, ".swiftdeps");
        }
      } else if (kind == "swift-dependencies") {
        // If there was already a "swift-dependencies" entry present, ignore it.
        // (This shouldn't happen because the build rules won't do this, but
        // check just in case.)
        std::cerr << "There was a 'swift-dependencies' entry for " << src
                  << ", but the build rules should not have done this; "
                  << "ignoring it.\n";
      } else {
        // Otherwise, just copy the mapping over verbatim.
        src_map[kind] = path;
      }
    }

    // When split compiling both output_file_maps need src level swiftdeps
    if (!swiftdeps_path.empty()) {
      src_map["swift-dependencies"] = swiftdeps_path;
    }

    new_output_file_map[src] = src_map;
  }

  // If we don't generate a swiftmodule, don't try to copy those files
  if (emitting_module) {
    auto swiftmodule_path =
        ReplaceExtension(path, ".swiftmodule", /*all_extensions=*/true);
    auto copied_swiftmodule_path =
        MakeIncrementalOutputPath(swiftmodule_path, derived);
    incremental_inputs[swiftmodule_path] = copied_swiftmodule_path;

    auto swiftdoc_path =
        ReplaceExtension(path, ".swiftdoc", /*all_extensions=*/true);
    auto copied_swiftdoc_path =
        MakeIncrementalOutputPath(swiftdoc_path, derived);
    incremental_inputs[swiftdoc_path] = copied_swiftdoc_path;

    auto swiftsourceinfo_path =
        ReplaceExtension(path, ".swiftsourceinfo", /*all_extensions=*/true);
    auto copied_swiftsourceinfo_path =
        MakeIncrementalOutputPath(swiftsourceinfo_path, derived);
    incremental_inputs[swiftsourceinfo_path] = copied_swiftsourceinfo_path;
  }

  json_ = new_output_file_map;
  incremental_outputs_ = incremental_outputs;
  incremental_inputs_ = incremental_inputs;
}