diagnostic_consumer.h 2.9 KB

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