diagnostic_consumer.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_CONSUMER_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_CONSUMER_H_
  6. #include "common/ostream.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/diagnostic.h"
  9. namespace Carbon {
  10. // Receives diagnostics as they are emitted.
  11. class DiagnosticConsumer {
  12. public:
  13. virtual ~DiagnosticConsumer() = default;
  14. // Handle a diagnostic.
  15. //
  16. // This relies on moves of the Diagnostic. At present, diagnostics are
  17. // allocated on the stack, so their lifetime is that of HandleDiagnostic.
  18. // However, SortingDiagnosticConsumer needs a longer lifetime, until all
  19. // diagnostics have been produced. As a consequence, it needs to either copy
  20. // or move the Diagnostic, and right now we're moving due to the overhead of
  21. // notes.
  22. //
  23. // At present, there is no persistent storage of diagnostics because IDEs
  24. // would be fine with diagnostics being printed immediately and discarded,
  25. // without SortingDiagnosticConsumer. If this becomes a performance issue, we
  26. // may want to investigate alternative ownership models that address both IDE
  27. // and CLI user needs.
  28. virtual auto HandleDiagnostic(Diagnostic diagnostic) -> void = 0;
  29. // Flushes any buffered input.
  30. virtual auto Flush() -> void {}
  31. };
  32. // A diagnostic consumer that prints to a stream.
  33. class StreamDiagnosticConsumer : public DiagnosticConsumer {
  34. public:
  35. explicit StreamDiagnosticConsumer(llvm::raw_ostream& stream,
  36. bool include_diagnostic_kind)
  37. : stream_(&stream), include_diagnostic_kind_(include_diagnostic_kind) {}
  38. auto HandleDiagnostic(Diagnostic diagnostic) -> void override;
  39. auto Flush() -> void override { stream_->flush(); }
  40. auto set_stream(llvm::raw_ostream* stream) -> void { stream_ = stream; }
  41. private:
  42. auto Print(const DiagnosticMessage& message, llvm::StringRef prefix) -> void;
  43. llvm::raw_ostream* stream_;
  44. // Whether to include the diagnostic kind when printing.
  45. bool include_diagnostic_kind_;
  46. // Whethere we've printed a diagnostic. Used for printing separators.
  47. bool printed_diagnostic_ = false;
  48. };
  49. // Returns a diagnostic consumer instance that prints to stderr.
  50. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer&;
  51. // Diagnostic consumer adaptor that tracks whether any errors have been
  52. // produced.
  53. class ErrorTrackingDiagnosticConsumer : public DiagnosticConsumer {
  54. public:
  55. explicit ErrorTrackingDiagnosticConsumer(DiagnosticConsumer& next_consumer)
  56. : next_consumer_(&next_consumer) {}
  57. auto HandleDiagnostic(Diagnostic diagnostic) -> void override {
  58. seen_error_ |= diagnostic.level == DiagnosticLevel::Error;
  59. next_consumer_->HandleDiagnostic(std::move(diagnostic));
  60. }
  61. // Reset whether we've seen an error.
  62. auto Reset() -> void { seen_error_ = false; }
  63. // Returns whether we've seen an error since the last reset.
  64. auto seen_error() const -> bool { return seen_error_; }
  65. private:
  66. DiagnosticConsumer* next_consumer_;
  67. bool seen_error_ = false;
  68. };
  69. } // namespace Carbon
  70. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_CONSUMER_H_