consumer.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_CONSUMER_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_CONSUMER_H_
  6. #include "common/ostream.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/diagnostic.h"
  9. namespace Carbon::Diagnostics {
  10. // Receives diagnostics as they are emitted.
  11. class Consumer {
  12. public:
  13. virtual ~Consumer() = 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, SortingConsumer 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 SortingConsumer. 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 StreamConsumer : public Consumer {
  34. public:
  35. explicit StreamConsumer(llvm::raw_ostream* stream) : stream_(stream) {}
  36. auto HandleDiagnostic(Diagnostic diagnostic) -> void override;
  37. auto Flush() -> void override { stream_->flush(); }
  38. auto set_stream(llvm::raw_ostream* stream) -> void { stream_ = stream; }
  39. auto set_include_diagnostic_kind(bool value) -> void {
  40. include_diagnostic_kind_ = value;
  41. }
  42. private:
  43. auto Print(const Message& message, llvm::StringRef prefix) -> void;
  44. llvm::raw_ostream* stream_;
  45. // Whether to include the diagnostic kind when printing.
  46. bool include_diagnostic_kind_ = false;
  47. // Whethere we've printed a diagnostic. Used for printing separators.
  48. bool printed_diagnostic_ = false;
  49. };
  50. // Returns a diagnostic consumer instance that prints to stderr.
  51. auto ConsoleConsumer() -> Consumer&;
  52. // Diagnostic consumer adaptor that tracks whether any errors have been
  53. // produced.
  54. class ErrorTrackingConsumer : public Consumer {
  55. public:
  56. explicit ErrorTrackingConsumer(Consumer& next_consumer)
  57. : next_consumer_(&next_consumer) {}
  58. auto HandleDiagnostic(Diagnostic diagnostic) -> void override {
  59. seen_error_ |= diagnostic.level == Level::Error;
  60. next_consumer_->HandleDiagnostic(std::move(diagnostic));
  61. }
  62. // Reset whether we've seen an error.
  63. auto Reset() -> void { seen_error_ = false; }
  64. // Returns whether we've seen an error since the last reset.
  65. auto seen_error() const -> bool { return seen_error_; }
  66. private:
  67. Consumer* next_consumer_;
  68. bool seen_error_ = false;
  69. };
  70. } // namespace Carbon::Diagnostics
  71. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_CONSUMER_H_