sem_ir_diagnostic_converter.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_CHECK_SEM_IR_DIAGNOSTIC_CONVERTER_H_
  5. #define CARBON_TOOLCHAIN_CHECK_SEM_IR_DIAGNOSTIC_CONVERTER_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "toolchain/check/diagnostic_helpers.h"
  8. #include "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/lex/token_index.h"
  10. #include "toolchain/parse/tree_and_subtrees.h"
  11. #include "toolchain/sem_ir/absolute_node_id.h"
  12. #include "toolchain/sem_ir/file.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. namespace Carbon::Check {
  15. // Handles the transformation of a SemIRLoc to a DiagnosticLoc.
  16. class SemIRDiagnosticConverter : public DiagnosticConverter<SemIRLoc> {
  17. public:
  18. explicit SemIRDiagnosticConverter(
  19. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> imported_trees_and_subtrees,
  20. const SemIR::File* sem_ir)
  21. : imported_trees_and_subtrees_(imported_trees_and_subtrees),
  22. sem_ir_(sem_ir) {}
  23. // Implements `DiagnosticConverter::ConvertLoc`. Adds context for any imports
  24. // used in the current SemIR to get to the underlying code.
  25. //
  26. // For the last byte offset, this uses `last_token_` exclusively for imported
  27. // locations, or `loc` if it's in the same file and (for whatever reason)
  28. // later.
  29. auto ConvertLoc(SemIRLoc loc, ContextFnT context_fn) const
  30. -> ConvertedDiagnosticLoc override;
  31. // Implements argument conversions for supported check-phase arguments.
  32. auto ConvertArg(llvm::Any arg) const -> llvm::Any override;
  33. // If a byte offset is past the current last byte offset, advances forward.
  34. // Earlier offsets are ignored.
  35. auto AdvanceToken(Lex::TokenIndex token) -> void {
  36. last_token_ = std::max(last_token_, token);
  37. }
  38. private:
  39. // Implements `ConvertLoc`, but without `last_token_` applied.
  40. auto ConvertLocImpl(SemIRLoc loc, ContextFnT context_fn) const
  41. -> ConvertedDiagnosticLoc;
  42. // Converts a node_id corresponding to a specific sem_ir to a diagnostic
  43. // location.
  44. auto ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id, bool token_only,
  45. ContextFnT context_fn) const -> ConvertedDiagnosticLoc;
  46. // Converters for each SemIR.
  47. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> imported_trees_and_subtrees_;
  48. // The current SemIR being processed.
  49. const SemIR::File* sem_ir_;
  50. // The last token encountered during processing.
  51. Lex::TokenIndex last_token_ = Lex::TokenIndex::None;
  52. };
  53. } // namespace Carbon::Check
  54. #endif // CARBON_TOOLCHAIN_CHECK_SEM_IR_DIAGNOSTIC_CONVERTER_H_