Repository: carbon-language/carbon-lang
File: toolchain/check/diagnostic_emitter.cpp (C++)

Code before:
      loc_id.kind() != SemIR::LocId::Kind::InstId && loc_id.is_token_only();

  auto final_node_id = absolute_node_ids.pop_back_val();
  for (const auto& absolute_node_id : absolute_node_ids) {
    if (!absolute_node_id.node_id().has_value()) {
      // TODO: Add an "In implicit import of prelude." note for the case where
      // we don't have a location.
      continue;
    }
    // TODO: Include the name of the imported library in the diagnostic.
    auto diag_loc = ConvertLocInFile(absolute_node_id, token_only, context_fn);
    CARBON_DIAGNOSTIC(InImport, LocationInfo, "in import");
    context_fn(diag_loc.loc, InImport);
  }

  return ConvertLocInFile(final_node_id, token_only, context_fn);
}

auto DiagnosticEmitter::ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id,
                                         bool token_only,
                                         ContextFnT /*context_fn*/) const
    -> Diagnostics::ConvertedLoc {
  if (absolute_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
    // Special handling of Clang source locations.
    // TODO: Add `InCppImport` here using `context_fn` similar to `InImport`
    // instead of a note.
    clang::SourceLocation clang_loc = sem_ir_->clang_source_locs().Get(
        absolute_node_id.clang_source_loc_id());

    CARBON_CHECK(sem_ir_->cpp_ast());
    clang::PresumedLoc presumed_loc =
        sem_ir_->cpp_ast()->getSourceManager().getPresumedLoc(clang_loc);

    return Diagnostics::ConvertedLoc{
        .loc = {.filename = presumed_loc.getFilename(),
                .line_number = static_cast<int32_t>(presumed_loc.getLine())},
        // TODO: Set `last_byte_offset` based on the `import Cpp` location.
        .last_byte_offset = 0};
  }

  const auto& tree_and_subtrees =
      tree_and_subtrees_getters_[absolute_node_id.check_ir_id().index]();
  return tree_and_subtrees.NodeToDiagnosticLoc(absolute_node_id.node_id(),
                                               token_only);
}

auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
    std::string library_name;
    if (*library_name_id == SemIR::LibraryNameId::Default) {
      library_name = "default library";

Review comment (refactor):
Really the format should be identical to what `InImport` is already doing, so I think the goal should be to eliminate `InCppImport`. So I'll suggest a different TODO:

```suggestion
    // TODO: Refactor to add an `InImport` pointing at the `Cpp` import, and eliminate `InCppImport`.
```

Code after:
      loc_id.kind() != SemIR::LocId::Kind::InstId && loc_id.is_token_only();

  auto final_node_id = absolute_node_ids.pop_back_val();
  for (const auto& absolute_node_id : absolute_node_ids) {
    if (!absolute_node_id.node_id().has_value()) {
      // TODO: Add an "In implicit import of prelude." note for the case where
      // we don't have a location.
      continue;
    }
    // TODO: Include the name of the imported library in the diagnostic.
    auto diag_loc = ConvertLocInFile(absolute_node_id, token_only, context_fn);
    CARBON_DIAGNOSTIC(InImport, LocationInfo, "in import");
    context_fn(diag_loc.loc, InImport);
  }

  return ConvertLocInFile(final_node_id, token_only, context_fn);
}

auto DiagnosticEmitter::ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id,
                                         bool token_only,
                                         ContextFnT /*context_fn*/) const
    -> Diagnostics::ConvertedLoc {
  if (absolute_node_id.check_ir_id() == SemIR::CheckIRId::Cpp) {
    // Special handling of Clang source locations.
    // TODO: Refactor to add an `InImport` pointing at the `Cpp` import, and
    // eliminate `InCppImport`.
    clang::SourceLocation clang_loc = sem_ir_->clang_source_locs().Get(
        absolute_node_id.clang_source_loc_id());

    CARBON_CHECK(sem_ir_->cpp_ast());
    clang::PresumedLoc presumed_loc =
        sem_ir_->cpp_ast()->getSourceManager().getPresumedLoc(clang_loc);

    return Diagnostics::ConvertedLoc{
        .loc = {.filename = presumed_loc.getFilename(),
                .line_number = static_cast<int32_t>(presumed_loc.getLine())},
        // TODO: Set `last_byte_offset` based on the `import Cpp` location.
        .last_byte_offset = 0};
  }

  const auto& tree_and_subtrees =
      tree_and_subtrees_getters_[absolute_node_id.check_ir_id().index]();
  return tree_and_subtrees.NodeToDiagnosticLoc(absolute_node_id.node_id(),
                                               token_only);
}

auto DiagnosticEmitter::ConvertArg(llvm::Any arg) const -> llvm::Any {
  if (auto* library_name_id = llvm::any_cast<SemIR::LibraryNameId>(&arg)) {
    std::string library_name;
    if (*library_name_id == SemIR::LibraryNameId::Default) {
      library_name = "default library";