location.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp/location.h"
  5. #include "toolchain/sem_ir/absolute_node_id.h"
  6. #include "toolchain/sem_ir/ids.h"
  7. namespace Carbon::Check {
  8. namespace {
  9. struct FileInfo {
  10. const SemIR::File* sem_ir;
  11. clang::SourceLocation start_loc;
  12. };
  13. } // namespace
  14. // Map a CheckIRId into information about the corresponding file in both SemIR
  15. // and Clang's source manager.
  16. static auto GetFileInfo(Context& context, SemIR::CheckIRId ir_id) -> FileInfo {
  17. const SemIR::File* sem_ir = &context.sem_ir();
  18. int file_index = 0;
  19. // If the file is imported, locate it in our imports map.
  20. if (ir_id != context.sem_ir().check_ir_id()) {
  21. auto import_id = context.check_ir_map().Get(ir_id);
  22. CARBON_CHECK(import_id.has_value());
  23. file_index = import_id.index + 1;
  24. sem_ir = context.import_irs().Get(import_id).sem_ir;
  25. CARBON_CHECK(sem_ir, "Node location in nonexistent IR");
  26. }
  27. // If we've seen this file before, reuse the same FileID.
  28. auto& file_start_locs = context.cpp_carbon_file_locations();
  29. if (static_cast<int>(file_start_locs.size()) <= file_index) {
  30. // Never valid; prepare a slot for the caching below.
  31. file_start_locs.resize(file_index + 1);
  32. } else if (file_start_locs[file_index].isValid()) {
  33. return {.sem_ir = sem_ir, .start_loc = file_start_locs[file_index]};
  34. }
  35. // We've not seen this file before. Create a corresponding virtual file in
  36. // Clang's source manager.
  37. // TODO: Consider recreating the complete import path instead of only the
  38. // final entry.
  39. const auto& source = sem_ir->parse_tree().tokens().source();
  40. auto& src_mgr = context.ast_context().getSourceManager();
  41. auto file_id = src_mgr.createFileID(
  42. llvm::MemoryBufferRef(source.text(), source.filename()));
  43. auto file_start_loc = src_mgr.getLocForStartOfFile(file_id);
  44. file_start_locs[file_index] = file_start_loc;
  45. return {.sem_ir = sem_ir, .start_loc = file_start_loc};
  46. }
  47. auto GetCppLocation(Context& context, SemIR::LocId loc_id)
  48. -> clang::SourceLocation {
  49. if (!context.sem_ir().clang_ast_unit()) {
  50. return clang::SourceLocation();
  51. }
  52. // Break down the `LocId` into an import path. If that ends in a C++ location,
  53. // we can just return that directly.
  54. llvm::SmallVector<SemIR::AbsoluteNodeId> absolute_node_ids =
  55. SemIR::GetAbsoluteNodeId(&context.sem_ir(), loc_id);
  56. if (absolute_node_ids.back().check_ir_id() == SemIR::CheckIRId::Cpp) {
  57. return context.sem_ir().clang_source_locs().Get(
  58. absolute_node_ids.back().clang_source_loc_id());
  59. }
  60. // This is a location in Carbon code; get or create a corresponding file in
  61. // Clang and build a corresponding location.
  62. auto absolute_node_id = absolute_node_ids.back();
  63. auto [ir, start_loc] = GetFileInfo(context, absolute_node_id.check_ir_id());
  64. const auto& tree = ir->parse_tree();
  65. auto offset =
  66. tree.tokens().GetByteOffset(tree.node_token(absolute_node_id.node_id()));
  67. return start_loc.getLocWithOffset(offset);
  68. }
  69. } // namespace Carbon::Check