diagnostic_consumer.cpp 966 B

1234567891011121314151617181920212223242526272829303132333435
  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. if (message.level == DiagnosticLevel::Error) {
  18. *stream_ << "ERROR: ";
  19. }
  20. *stream_ << message.format_fn(message) << "\n";
  21. message.loc.FormatSnippet(*stream_);
  22. }
  23. }
  24. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
  25. static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
  26. return *consumer;
  27. }
  28. } // namespace Carbon