diagnostic_consumer.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. *stream_ << ": ";
  17. switch (message.level) {
  18. case DiagnosticLevel::Error:
  19. *stream_ << "ERROR: ";
  20. break;
  21. case DiagnosticLevel::Warning:
  22. *stream_ << "WARNING: ";
  23. break;
  24. case DiagnosticLevel::Note:
  25. break;
  26. }
  27. *stream_ << message.format_fn(message) << "\n";
  28. message.loc.FormatSnippet(*stream_);
  29. }
  30. }
  31. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
  32. static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
  33. return *consumer;
  34. }
  35. } // namespace Carbon