tree_node_location_translator.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_LOCATION_TRANSLATOR_H_
  5. #define CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_
  6. #include "toolchain/diagnostics/diagnostic_emitter.h"
  7. #include "toolchain/lex/tokenized_buffer.h"
  8. #include "toolchain/parse/tree.h"
  9. namespace Carbon::Parse {
  10. class NodeLocation {
  11. public:
  12. // NOLINTNEXTLINE(google-explicit-constructor)
  13. NodeLocation(NodeId node_id) : NodeLocation(node_id, false) {}
  14. NodeLocation(NodeId node_id, bool token_only)
  15. : node_id_(node_id), token_only_(token_only) {}
  16. // TODO: Have some other way of representing diagnostic that applies to a file
  17. // as a whole.
  18. // NOLINTNEXTLINE(google-explicit-constructor)
  19. NodeLocation(InvalidNodeId node_id) : NodeLocation(node_id, false) {}
  20. auto node_id() const -> NodeId { return node_id_; }
  21. auto token_only() const -> bool { return token_only_; }
  22. private:
  23. NodeId node_id_;
  24. bool token_only_;
  25. };
  26. inline auto TokenOnly(NodeId node_id) -> NodeLocation {
  27. return NodeLocation(node_id, true);
  28. }
  29. class NodeLocationTranslator
  30. : public DiagnosticLocationTranslator<NodeLocation> {
  31. public:
  32. explicit NodeLocationTranslator(const Lex::TokenizedBuffer* tokens,
  33. llvm::StringRef filename,
  34. const Tree* parse_tree)
  35. : token_translator_(tokens),
  36. filename_(filename),
  37. parse_tree_(parse_tree) {}
  38. // Map the given token into a diagnostic location.
  39. auto GetLocation(NodeLocation node_location) -> DiagnosticLocation override {
  40. // Support the invalid token as a way to emit only the filename, when there
  41. // is no line association.
  42. if (!node_location.node_id().is_valid()) {
  43. return {.filename = filename_};
  44. }
  45. if (node_location.token_only()) {
  46. return token_translator_.GetLocation(
  47. parse_tree_->node_token(node_location.node_id()));
  48. }
  49. // Construct a location that encompasses all tokens that descend from this
  50. // node (including the root).
  51. Lex::TokenIndex start_token =
  52. parse_tree_->node_token(node_location.node_id());
  53. Lex::TokenIndex end_token = start_token;
  54. for (NodeId desc : parse_tree_->postorder(node_location.node_id())) {
  55. Lex::TokenIndex desc_token = parse_tree_->node_token(desc);
  56. if (!desc_token.is_valid()) {
  57. continue;
  58. }
  59. if (desc_token < start_token) {
  60. start_token = desc_token;
  61. } else if (desc_token > end_token) {
  62. end_token = desc_token;
  63. }
  64. }
  65. DiagnosticLocation start_loc = token_translator_.GetLocation(start_token);
  66. if (start_token == end_token) {
  67. return start_loc;
  68. }
  69. DiagnosticLocation end_loc = token_translator_.GetLocation(end_token);
  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.line_number != end_loc.line_number) {
  73. start_loc.length = start_loc.line.size() - start_loc.column_number + 1;
  74. } else {
  75. if (start_loc.column_number != end_loc.column_number) {
  76. start_loc.length =
  77. end_loc.column_number + end_loc.length - start_loc.column_number;
  78. }
  79. }
  80. return start_loc;
  81. }
  82. private:
  83. Lex::TokenLocationTranslator token_translator_;
  84. llvm::StringRef filename_;
  85. const Tree* parse_tree_;
  86. };
  87. } // namespace Carbon::Parse
  88. #endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_LOCATION_TRANSLATOR_H_