diagnostic_consumer.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(&llvm::errs());
  44. return *consumer;
  45. }
  46. } // namespace Carbon