tree_node_diagnostic_converter.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_
  5. #define CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_
  6. #include <utility>
  7. #include "toolchain/diagnostics/diagnostic_emitter.h"
  8. #include "toolchain/lex/tokenized_buffer.h"
  9. #include "toolchain/parse/tree.h"
  10. #include "toolchain/parse/tree_and_subtrees.h"
  11. namespace Carbon::Parse {
  12. class NodeLoc {
  13. public:
  14. // NOLINTNEXTLINE(google-explicit-constructor)
  15. NodeLoc(NodeId node_id) : NodeLoc(node_id, false) {}
  16. NodeLoc(NodeId node_id, bool token_only)
  17. : node_id_(node_id), token_only_(token_only) {}
  18. // TODO: Have some other way of representing diagnostic that applies to a file
  19. // as a whole.
  20. // NOLINTNEXTLINE(google-explicit-constructor)
  21. NodeLoc(InvalidNodeId node_id) : NodeLoc(node_id, false) {}
  22. auto node_id() const -> NodeId { return node_id_; }
  23. auto token_only() const -> bool { return token_only_; }
  24. private:
  25. NodeId node_id_;
  26. bool token_only_;
  27. };
  28. class NodeLocConverter : public DiagnosticConverter<NodeLoc> {
  29. public:
  30. explicit NodeLocConverter(
  31. const Lex::TokenizedBuffer* tokens, llvm::StringRef filename,
  32. llvm::function_ref<const Parse::TreeAndSubtrees&()> get_tree_and_subtrees)
  33. : token_converter_(tokens),
  34. filename_(filename),
  35. get_tree_and_subtrees_(get_tree_and_subtrees) {}
  36. // Implements `DiagnosticConverter::ConvertLoc`.
  37. auto ConvertLoc(NodeLoc node_loc, ContextFnT context_fn) const
  38. -> ConvertedDiagnosticLoc override {
  39. // Support the invalid token as a way to emit only the filename, when there
  40. // is no line association.
  41. if (!node_loc.node_id().is_valid()) {
  42. return {{.filename = filename_}, -1};
  43. }
  44. const auto& tree = get_tree_and_subtrees_();
  45. if (node_loc.token_only()) {
  46. return token_converter_.ConvertLoc(
  47. tree.tree().node_token(node_loc.node_id()), context_fn);
  48. }
  49. // Construct a location that encompasses all tokens that descend from this
  50. // node (including the root).
  51. Lex::TokenIndex start_token = tree.tree().node_token(node_loc.node_id());
  52. Lex::TokenIndex end_token = start_token;
  53. for (NodeId desc : tree.postorder(node_loc.node_id())) {
  54. Lex::TokenIndex desc_token = tree.tree().node_token(desc);
  55. if (!desc_token.is_valid()) {
  56. continue;
  57. }
  58. if (desc_token < start_token) {
  59. start_token = desc_token;
  60. } else if (desc_token > end_token) {
  61. end_token = desc_token;
  62. }
  63. }
  64. auto start_loc = token_converter_.ConvertLoc(start_token, context_fn);
  65. if (start_token == end_token) {
  66. return start_loc;
  67. }
  68. auto end_loc = token_converter_.ConvertLoc(end_token, context_fn);
  69. start_loc.last_byte_offset = end_loc.last_byte_offset;
  70. // For multiline locations we simply return the rest of the line for now
  71. // since true multiline locations are not yet supported.
  72. if (start_loc.loc.line_number != end_loc.loc.line_number) {
  73. start_loc.loc.length =
  74. start_loc.loc.line.size() - start_loc.loc.column_number + 1;
  75. } else {
  76. if (start_loc.loc.column_number != end_loc.loc.column_number) {
  77. start_loc.loc.length = end_loc.loc.column_number + end_loc.loc.length -
  78. start_loc.loc.column_number;
  79. }
  80. }
  81. return start_loc;
  82. }
  83. private:
  84. Lex::TokenDiagnosticConverter token_converter_;
  85. llvm::StringRef filename_;
  86. // Returns a lazily constructed TreeAndSubtrees.
  87. llvm::function_ref<const Parse::TreeAndSubtrees&()> get_tree_and_subtrees_;
  88. };
  89. } // namespace Carbon::Parse
  90. #endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_