diagnostic_consumer.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. *stream_ << "note: ";
  26. break;
  27. case DiagnosticLevel::LocationInfo:
  28. break;
  29. }
  30. *stream_ << message.format_fn(message) << "\n";
  31. // Don't include a snippet for location information to keep this diagnostic
  32. // more visually associated with the following diagnostic that it describes
  33. // and to better match C++ compilers.
  34. if (message.level != DiagnosticLevel::LocationInfo) {
  35. message.loc.FormatSnippet(*stream_);
  36. }
  37. }
  38. }
  39. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer& {
  40. static auto* consumer = new StreamDiagnosticConsumer(llvm::errs());
  41. return *consumer;
  42. }
  43. } // namespace Carbon