sem_ir_diagnostic_converter.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_converter.h"
  9. #include "toolchain/parse/tree_node_diagnostic_converter.h"
  10. #include "toolchain/sem_ir/file.h"
  11. namespace Carbon::Check {
  12. // Handles the transformation of a SemIRLoc to a DiagnosticLoc.
  13. class SemIRDiagnosticConverter : public DiagnosticConverter<SemIRLoc> {
  14. public:
  15. explicit SemIRDiagnosticConverter(
  16. llvm::ArrayRef<Parse::NodeLocConverter*> node_converters,
  17. const SemIR::File* sem_ir)
  18. : node_converters_(node_converters), sem_ir_(sem_ir) {}
  19. // Converts an instruction's location to a diagnostic location, which will be
  20. // the underlying line of code. Adds context for any imports used in the
  21. // current SemIR to get to the underlying code.
  22. auto ConvertLoc(SemIRLoc loc, ContextFnT context_fn) const
  23. -> DiagnosticLoc override;
  24. // Implements argument conversions for supported check-phase arguments.
  25. auto ConvertArg(llvm::Any arg) const -> llvm::Any override;
  26. private:
  27. // Converts a node_id corresponding to a specific sem_ir to a diagnostic
  28. // location.
  29. auto ConvertLocInFile(const SemIR::File* sem_ir, Parse::NodeId node_id,
  30. bool token_only, ContextFnT context_fn) const
  31. -> DiagnosticLoc;
  32. // Converters for each SemIR.
  33. llvm::ArrayRef<Parse::NodeLocConverter*> node_converters_;
  34. // The current SemIR being processed.
  35. const SemIR::File* sem_ir_;
  36. };
  37. } // namespace Carbon::Check
  38. #endif // CARBON_TOOLCHAIN_CHECK_SEM_IR_DIAGNOSTIC_CONVERTER_H_