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

Code before:
  switch (loc_id.kind()) {
    case SemIR::LocId::Kind::None: {
      out << "LocId(<none>)";
      break;
    }

    case SemIR::LocId::Kind::ImportIRInstId: {
      auto import_ir_id =
          file.import_ir_insts().Get(loc_id.import_ir_inst_id()).ir_id();
      const auto* import_file = file.import_irs().Get(import_ir_id).sem_ir;
      out << "LocId(import from \"" << FormatEscaped(import_file->filename())
          << "\")";
      break;
    }

    case SemIR::LocId::Kind::NodeId: {
      auto token = file.parse_tree().node_token(loc_id.node_id());
      auto line = file.parse_tree().tokens().GetLineNumber(token);
      auto col = file.parse_tree().tokens().GetColumnNumber(token);
      const char* implicit = loc_id.is_desugared() ? " implicit" : "";
      out << "LocId(" << FormatEscaped(file.filename()) << ":" << line << ":"
          << col << implicit << ")";
      break;
    }

    case SemIR::LocId::Kind::InstId:
      CARBON_FATAL("unexpected LocId kind");
  }
  return out.TakeStr();
}

LLVM_DUMP_METHOD auto Dump(const File& file, ClassId class_id) -> std::string {
  RawStringOstream out;
  out << class_id;
  if (class_id.has_value()) {
    const auto& class_obj = file.classes().Get(class_id);
    out << ": " << class_obj << DumpNameIfValid(file, class_obj.name_id);
  }
  return out.TakeStr();
}

LLVM_DUMP_METHOD auto Dump(const File& file, ConstantId const_id)
    -> std::string {
  RawStringOstream out;
  out << const_id;
  if (!const_id.has_value()) {
    return out.TakeStr();
  }
  if (const_id.is_symbolic()) {
    const auto& symbolic = file.constant_values().GetSymbolicConstant(const_id);
    out << ": " << symbolic << '\n'

Review comment (security):
Shouldn't need `SemIR::`

```suggestion
LLVM_DUMP_METHOD auto Dump(const File& file, LocId loc_id)
    -> std::string {
  RawStringOstream out;
  // TODO: If the canonical location is None but the original is an InstId,
  // should we dump the InstId anyway even though it has no location? Is that
  // ever useful?
  loc_id = file.insts().GetCanonicalLocId(loc_id);
  switch (loc_id.kind()) {
    case LocId::Kind::None: {
      out << "LocId(<none>)";
      break;
    }

    case LocId::Kind::ImportIRInstId: {
      auto import_ir_id =
          file.import_ir_insts().Get(loc_id.import_ir_inst_id()).ir_id();
      const auto* import_file = file.import_irs().Get(import_ir_id).sem_ir;
      out << "LocId(import from \"" << FormatEscaped(import_file->filename())
          << "\")";
      break;
    }

    case LocId::Kind::NodeId: {
      auto token = file.parse_tree().node_token(loc_id.node_id());
      auto line = file.parse_tree().tokens().GetLineNumber(token);
      auto col = file.parse_tree().tokens().GetColumnNumber(token);
      const char* implicit = loc_id.is_desugared() ? " implicit" : "";
      out << "LocId(" << FormatEscaped(file.filename()) << ":" << line << ":"
          << col << implicit << ")";
      break;
    }

    case LocId::Kind::InstId:
```

Code after:
  return out.TakeStr();
}

LLVM_DUMP_METHOD auto Dump(const File& file, ConstantId const_id)
    -> std::string {
  RawStringOstream out;
  out << const_id;
  if (!const_id.has_value()) {
    return out.TakeStr();
  }
  if (const_id.is_symbolic()) {
    const auto& symbolic = file.constant_values().GetSymbolicConstant(const_id);
    out << ": " << symbolic << '\n'
        << Dump(file, symbolic.inst_id) << '\n'
        << DumpGenericSummary(file, symbolic.generic_id);
  } else if (const_id.is_concrete()) {
    out << ": " << Dump(file, file.constant_values().GetInstId(const_id));
  }
  return out.TakeStr();
}

LLVM_DUMP_METHOD auto Dump(const File& file, EntityNameId entity_name_id)
    -> std::string {
  RawStringOstream out;
  out << entity_name_id;
  if (entity_name_id.has_value()) {
    auto entity_name = file.entity_names().Get(entity_name_id);
    out << ": " << entity_name << DumpNameIfValid(file, entity_name.name_id);
  }
  return out.TakeStr();
}

LLVM_DUMP_METHOD auto Dump(const File& file, FacetTypeId facet_type_id)
    -> std::string {
  RawStringOstream out;
  out << facet_type_id;
  if (!facet_type_id.has_value()) {
    return out.TakeStr();
  }

  const auto& facet_type = file.facet_types().Get(facet_type_id);
  out << ": " << facet_type;
  for (auto impls : facet_type.extend_constraints) {
    out << "\n  - " << Dump(file, impls.interface_id);
    if (impls.specific_id.has_value()) {
      out << "; " << DumpSpecificSummary(file, impls.specific_id);
    }
    out << " (extend)";
  }
  for (auto impls : facet_type.self_impls_constraints) {
    out << "\n  - " << Dump(file, impls.interface_id);