tree_node_diagnostic_converter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "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 NodeLocationConverter : public DiagnosticConverter<NodeLocation> {
  30. public:
  31. explicit NodeLocationConverter(const Lex::TokenizedBuffer* tokens,
  32. llvm::StringRef filename,
  33. const Tree* parse_tree)
  34. : token_converter_(tokens),
  35. filename_(filename),
  36. parse_tree_(parse_tree) {}
  37. // Map the given token into a diagnostic location.
  38. auto ConvertLocation(NodeLocation node_location, ContextFnT context_fn) const
  39. -> 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_converter_.ConvertLocation(
  47. parse_tree_->node_token(node_location.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 =
  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 =
  66. token_converter_.ConvertLocation(start_token, context_fn);
  67. if (start_token == end_token) {
  68. return start_loc;
  69. }
  70. DiagnosticLocation end_loc =
  71. token_converter_.ConvertLocation(end_token, context_fn);
  72. // For multiline locations we simply return the rest of the line for now
  73. // since true multiline locations are not yet supported.
  74. if (start_loc.line_number != end_loc.line_number) {
  75. start_loc.length = start_loc.line.size() - start_loc.column_number + 1;
  76. } else {
  77. if (start_loc.column_number != end_loc.column_number) {
  78. start_loc.length =
  79. end_loc.column_number + end_loc.length - start_loc.column_number;
  80. }
  81. }
  82. return start_loc;
  83. }
  84. private:
  85. Lex::TokenDiagnosticConverter token_converter_;
  86. llvm::StringRef filename_;
  87. const Tree* parse_tree_;
  88. };
  89. } // namespace Carbon::Parse
  90. #endif // CARBON_TOOLCHAIN_PARSE_TREE_NODE_DIAGNOSTIC_CONVERTER_H_