diagnostic_emitter.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_DIAGNOSTIC_EMITTER_H_
  5. #define CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_EMITTER_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 SemIR::LocId to a DiagnosticLoc.
  16. class DiagnosticEmitter : public DiagnosticEmitterBase {
  17. public:
  18. explicit DiagnosticEmitter(
  19. Diagnostics::Consumer* consumer,
  20. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
  21. const SemIR::File* sem_ir)
  22. : DiagnosticEmitterBase(consumer),
  23. tree_and_subtrees_getters_(tree_and_subtrees_getters),
  24. sem_ir_(sem_ir) {}
  25. // If a byte offset is past the current last byte offset, advances forward.
  26. // Earlier offsets are ignored.
  27. auto AdvanceToken(Lex::TokenIndex token) -> void {
  28. last_token_ = std::max(last_token_, token);
  29. }
  30. protected:
  31. // Implements argument conversions for supported check-phase arguments.
  32. auto ConvertArg(llvm::Any arg) const -> llvm::Any override;
  33. // Implements `DiagnosticConverter::ConvertLoc`. Adds context for any imports
  34. // used in the current SemIR to get to the underlying code.
  35. //
  36. // For the last byte offset, this uses `last_token_` exclusively for imported
  37. // locations, or `loc` if it's in the same file and (for whatever reason)
  38. // later.
  39. auto ConvertLoc(LocIdForDiagnostics loc_id, ContextFnT context_fn) const
  40. -> Diagnostics::ConvertedLoc override;
  41. private:
  42. // Implements `ConvertLoc`, but without `last_token_` applied.
  43. auto ConvertLocImpl(SemIR::LocId loc_id, ContextFnT context_fn) const
  44. -> Diagnostics::ConvertedLoc;
  45. // Returns `ConvertedLoc` if `loc` points to a `ClangDiagnostic` instruction.
  46. auto TryConvertClangDiagnosticLoc(SemIR::LocId loc_id) const
  47. -> std::optional<Diagnostics::ConvertedLoc>;
  48. // Converts a node_id corresponding to a specific sem_ir to a diagnostic
  49. // location.
  50. auto ConvertLocInFile(SemIR::AbsoluteNodeId absolute_node_id, bool token_only,
  51. ContextFnT context_fn) const
  52. -> Diagnostics::ConvertedLoc;
  53. // Converters for each SemIR.
  54. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters_;
  55. // The current SemIR being processed.
  56. const SemIR::File* sem_ir_;
  57. // The last token encountered during processing.
  58. Lex::TokenIndex last_token_ = Lex::TokenIndex::None;
  59. };
  60. } // namespace Carbon::Check
  61. #endif // CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_EMITTER_H_