consumer.cpp 1.4 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/consumer.h"
  5. #include <algorithm>
  6. #include <cstdint>
  7. namespace Carbon::Diagnostics {
  8. auto StreamConsumer::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 Level::Error:
  18. *stream_ << "error: ";
  19. break;
  20. case Level::Warning:
  21. *stream_ << "warning: ";
  22. break;
  23. case Level::Note:
  24. *stream_ << "note: ";
  25. break;
  26. case Level::LocationInfo:
  27. break;
  28. }
  29. *stream_ << message.Format();
  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 != Level::LocationInfo) {
  38. message.loc.FormatSnippet(*stream_);
  39. }
  40. }
  41. }
  42. auto ConsoleConsumer() -> Consumer& {
  43. static auto* consumer = new StreamConsumer(&llvm::errs());
  44. return *consumer;
  45. }
  46. } // namespace Carbon::Diagnostics