diagnostic_loc_converter.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_DIAGNOSTIC_LOC_CONVERTER_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_DIAGNOSTIC_LOC_CONVERTER_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/parse/tree_and_subtrees.h"
  10. #include "toolchain/sem_ir/absolute_node_id.h"
  11. #include "toolchain/sem_ir/file.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. namespace Carbon::SemIR {
  14. // Converter from compact location information into a diagnostic location
  15. // describing a filename, line location, and potentially a sequence of imports.
  16. // Such diagnostics locations are used to render user-facing diagnostics and
  17. // also locations for stack trace in crash diagnostics.
  18. class DiagnosticLocConverter {
  19. public:
  20. // Information about an import within which the location was found.
  21. struct ImportLoc {
  22. Diagnostics::Loc loc;
  23. // TODO: Include the name of the imported library in this information so it
  24. // can be included in the diagnostic.
  25. };
  26. // Information about a location that has been converted from a LocId to a
  27. // diagnostic location.
  28. struct LocAndImports {
  29. llvm::SmallVector<ImportLoc> imports;
  30. Diagnostics::ConvertedLoc loc;
  31. };
  32. // `tree_and_subtrees_getters` and `sem_ir` must not be null.
  33. explicit DiagnosticLocConverter(
  34. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters,
  35. const File* sem_ir)
  36. : tree_and_subtrees_getters_(tree_and_subtrees_getters),
  37. sem_ir_(sem_ir) {}
  38. // Converts the given location into a sequence of import locations and a final
  39. // diagnostic location.
  40. auto ConvertWithImports(LocId loc_id, bool token_only) const -> LocAndImports;
  41. // Converts the given location into a diagnostic location.
  42. auto Convert(LocId loc_id, bool token_only) const
  43. -> Diagnostics::ConvertedLoc;
  44. private:
  45. // Converts an `absolute_node_id` in either a Carbon file or C++ import to a
  46. // diagnostic location.
  47. auto ConvertImpl(AbsoluteNodeId absolute_node_id, bool token_only) const
  48. -> Diagnostics::ConvertedLoc;
  49. // Converts a `node_id` corresponding to a specific check IR to a diagnostic
  50. // location.
  51. auto ConvertImpl(CheckIRId check_ir_id, Parse::NodeId node_id,
  52. bool token_only) const -> Diagnostics::ConvertedLoc;
  53. // Converts a location pointing into C++ code to a diagnostic location.
  54. auto ConvertImpl(ClangSourceLocId clang_source_loc_id) const
  55. -> Diagnostics::ConvertedLoc;
  56. // Converters for each SemIR.
  57. const Parse::GetTreeAndSubtreesStore* tree_and_subtrees_getters_;
  58. // The current SemIR being processed.
  59. const File* sem_ir_;
  60. };
  61. } // namespace Carbon::SemIR
  62. #endif // CARBON_TOOLCHAIN_SEM_IR_DIAGNOSTIC_LOC_CONVERTER_H_