Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/formatter_chunks.cpp (C++)

Code before:
    return;
  }

  // Otherwise, create a new chunk and include it only if the parent is later
  // found to be used.
  AppendChildToParent(AddContent(/*include_in_output=*/false), parent_chunk_id);

  current_parent_id_ = parent_chunk_id;
  format();
  current_parent_id_ = None;

  // Reset the output stream so that the next call to `out()` creates a new
  // chunk.
  out_.reset();
}

auto FormatterChunks::AppendChildToParent(ChunkId child_chunk_id,
                                          ChunkId parent_chunk_id) -> void {
  CARBON_CHECK(!Get(parent_chunk_id).include_in_output);
  auto* children =
      std::get_if<llvm::SmallVector<ChunkId>>(&Get(parent_chunk_id).data);
  CARBON_CHECK(children);
  children->push_back(child_chunk_id);
}

auto FormatterChunks::AppendChildToCurrentParent(ChunkId chunk_id) -> void {
  if (current_parent_id_ != None) {
    // If the parent is not included, add the `chunk` to the parent's children
    // for conditional inclusion.
    if (!Get(current_parent_id_).include_in_output) {
      AppendChildToParent(chunk_id, current_parent_id_);
      return;
    }
  }

  // If the parent is already included, or there is no parent (this is not
  // currently a tentative chunk), include the chunk and all of its children.
  llvm::SmallVector<ChunkId> to_include = {chunk_id};
  while (!to_include.empty()) {
    auto& chunk_ref = Get(to_include.pop_back_val());
    if (chunk_ref.include_in_output) {
      continue;
    }
    chunk_ref.include_in_output = true;
    if (auto* children =
            std::get_if<llvm::SmallVector<ChunkId>>(&chunk_ref.data)) {
      to_include.append(*children);
      children->clear();
    }
  }
}

Review comment (suggestion):
This should probably be `child_chunk_id`, both to match the declaration, and because it makes the semantics clearer.

Code after:
    format();
    return;
  }

  // Otherwise, create a content `Chunk` and include it only if the parent is
  // later found to be used.
  AppendChildToParent(AddContent(/*include_in_output=*/false), parent_chunk_id);

  current_parent_id_ = parent_chunk_id;
  format();
  current_parent_id_ = None;

  // Reset the output stream so that the next call to `out()` creates a new
  // chunk.
  out_.reset();
}

auto FormatterChunks::AppendChildToParent(ChunkId child_chunk_id,
                                          ChunkId parent_chunk_id) -> void {
  CARBON_CHECK(!Get(parent_chunk_id).include_in_output);
  auto* children =
      std::get_if<llvm::SmallVector<ChunkId>>(&Get(parent_chunk_id).data);
  CARBON_CHECK(children);
  children->push_back(child_chunk_id);
}

auto FormatterChunks::AppendChildToCurrentParent(ChunkId child_chunk_id)
    -> void {
  if (current_parent_id_ != None) {
    // If the parent is not included, add the `chunk` to the parent's children
    // for conditional inclusion.
    if (!Get(current_parent_id_).include_in_output) {
      AppendChildToParent(child_chunk_id, current_parent_id_);
      return;
    }
  }

  // If the parent is already included, or there is no parent (this is not
  // currently a tentative chunk), include the chunk and all of its children.
  llvm::SmallVector<ChunkId> to_include = {child_chunk_id};
  while (!to_include.empty()) {
    auto& chunk_ref = Get(to_include.pop_back_val());
    if (chunk_ref.include_in_output) {
      continue;
    }
    chunk_ref.include_in_output = true;
    if (auto* children =
            std::get_if<llvm::SmallVector<ChunkId>>(&chunk_ref.data)) {
      to_include.append(*children);
      children->clear();
    }