diagnostic.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. namespace Carbon {
  8. auto DiagnosticLoc::FormatLocation(llvm::raw_ostream& out) const -> void {
  9. out << filename;
  10. if (line_number > 0) {
  11. out << ":" << line_number;
  12. if (column_number > 0) {
  13. out << ":" << column_number;
  14. }
  15. }
  16. }
  17. auto DiagnosticLoc::FormatSnippet(llvm::raw_ostream& out, int indent) const
  18. -> void {
  19. if (column_number == -1) {
  20. return;
  21. }
  22. // column_number is 1-based.
  23. int32_t column = column_number - 1;
  24. out.indent(indent);
  25. out << line << "\n";
  26. out.indent(indent + column);
  27. out << "^";
  28. // We want to ensure that we don't underline past the end of the line in
  29. // case of a multiline token.
  30. // TODO: Revisit this once we can reference multiple ranges on multiple
  31. // lines in a single diagnostic message.
  32. int underline_length =
  33. std::min(length, static_cast<int32_t>(line.size()) - column);
  34. for (int i = 1; i < underline_length; ++i) {
  35. out << '~';
  36. }
  37. out << '\n';
  38. }
  39. } // namespace Carbon