diagnostic_consumer.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_consumer.h"
  5. #include <algorithm>
  6. #include <cstdint>
  7. namespace Carbon {
  8. auto StreamDiagnosticConsumer::HandleDiagnostic(Diagnostic diagnostic) -> void {
  9. if (printed_diagnostic_) {
  10. *stream_ << "\n";
  11. } else {
  12. printed_diagnostic_ = true;
  13. }
  14. for (const auto& message : diagnostic.messages) {
  15. message.loc.FormatLocation(*stream_);
  16. switch (message.level) {
  17. case DiagnosticLevel::Error:
  18. *stream_ << ": error";
  19. break;
  20. case DiagnosticLevel::Warning:
  21. *stream_ << ": warning";
  22. break;
  23. case DiagnosticLevel::Note:
  24. *stream_ << ": note";
  25. break;
  26. case DiagnosticLevel::LocationInfo:
  27. break;
  28. }
  29. *stream_ << ": " << message.format_fn(message);
  30. if (include_diagnostic_kind_) {
  31. *stream_ << " [" << message.kind << "]";
  32. }
  33. *stream_ << "\n";
  34. // Don't include a snippet for location information to keep this diagnostic
  35. // more visually associated with the following diagnostic that it describes
  36. // and to better match C++ compilers.
  37. if (message.level != DiagnosticLevel::LocationInfo) {
  38. message.loc.FormatSnippet(*stream_);
  39. }
  40. }
  41. }
  42. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
  43. static auto* consumer = new StreamDiagnosticConsumer(
  44. llvm::errs(), /*include_diagnostic_kind=*/false);
  45. return *consumer;
  46. }
  47. } // namespace Carbon