diagnostic_consumer.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. : stream_(&stream) {}
  37. auto HandleDiagnostic(Diagnostic diagnostic) -> void override;
  38. auto Flush() -> void override { stream_->flush(); }
  39. auto set_stream(llvm::raw_ostream* stream) -> void { stream_ = stream; }
  40. private:
  41. auto Print(const DiagnosticMessage& message, llvm::StringRef prefix) -> void;
  42. llvm::raw_ostream* stream_;
  43. // Whethere we've printed a diagnostic. Used for printing separators.
  44. bool printed_diagnostic_ = false;
  45. };
  46. // Returns a diagnostic consumer instance that prints to stderr.
  47. auto ConsoleDiagnosticConsumer() -> DiagnosticConsumer&;
  48. // Diagnostic consumer adaptor that tracks whether any errors have been
  49. // produced.
  50. class ErrorTrackingDiagnosticConsumer : public DiagnosticConsumer {
  51. public:
  52. explicit ErrorTrackingDiagnosticConsumer(DiagnosticConsumer& next_consumer)
  53. : next_consumer_(&next_consumer) {}
  54. auto HandleDiagnostic(Diagnostic diagnostic) -> void override {
  55. seen_error_ |= diagnostic.level == DiagnosticLevel::Error;
  56. next_consumer_->HandleDiagnostic(std::move(diagnostic));
  57. }
  58. // Reset whether we've seen an error.
  59. auto Reset() -> void { seen_error_ = false; }
  60. // Returns whether we've seen an error since the last reset.
  61. auto seen_error() const -> bool { return seen_error_; }
  62. private:
  63. DiagnosticConsumer* next_consumer_;
  64. bool seen_error_ = false;
  65. };
  66. } // namespace Carbon
  67. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_CONSUMER_H_