diagnostic.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "toolchain/diagnostics/diagnostic.h"
  5. #include <algorithm>
  6. #include <cstdint>
  7. #include "llvm/ADT/Sequence.h"
  8. namespace Carbon::Diagnostics {
  9. auto Loc::FormatLocation(llvm::raw_ostream& out) const -> void {
  10. if (filename.empty()) {
  11. return;
  12. }
  13. out << filename;
  14. if (line_number > 0) {
  15. out << ":" << line_number;
  16. if (column_number > 0) {
  17. out << ":" << column_number;
  18. }
  19. }
  20. out << ": ";
  21. }
  22. auto Loc::FormatSnippet(llvm::raw_ostream& out, int indent) const -> void {
  23. if (!snippet.empty()) {
  24. llvm::StringRef snippet_ref = snippet;
  25. do {
  26. auto [snippet_line, rest] = snippet_ref.split('\n');
  27. out.indent(indent);
  28. out << snippet_line << "\n";
  29. snippet_ref = rest;
  30. } while (!snippet_ref.empty());
  31. return;
  32. }
  33. if (column_number == -1) {
  34. return;
  35. }
  36. // column_number is 1-based.
  37. const int caret_byte_offset = column_number - 1;
  38. out.indent(indent);
  39. int column = 0;
  40. int caret_column = 0;
  41. int underline_end_column = 0;
  42. int byte_offset = 0;
  43. for (char c : line) {
  44. // TODO: Handle tab characters.
  45. // TODO: Print Unicode characters directly, and use
  46. // llvm::sys::unicode::getColumnWidth to determine their width.
  47. if (std::isprint(static_cast<unsigned char>(c))) {
  48. out << c;
  49. ++column;
  50. } else {
  51. // TODO: Consider using ANSI colors to distinguish this from the program
  52. // text.
  53. int pos = out.tell();
  54. out << '<';
  55. llvm::write_hex(out, static_cast<unsigned char>(c),
  56. llvm::HexPrintStyle::Upper, 2);
  57. out << '>';
  58. column += out.tell() - pos;
  59. }
  60. ++byte_offset;
  61. if (byte_offset <= caret_byte_offset) {
  62. caret_column = column;
  63. }
  64. if (byte_offset <= caret_byte_offset + length) {
  65. underline_end_column = column;
  66. }
  67. }
  68. out << "\n";
  69. out.indent(indent + caret_column);
  70. out << "^";
  71. // TODO: Revisit this once we can reference multiple ranges in a single
  72. // diagnostic message.
  73. for (auto _ :
  74. llvm::seq(std::max(underline_end_column - caret_column - 1, 0))) {
  75. out << '~';
  76. }
  77. out << '\n';
  78. }
  79. } // namespace Carbon::Diagnostics