null_diagnostics.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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_NULL_DIAGNOSTICS_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_NULL_DIAGNOSTICS_H_
  6. #include "toolchain/diagnostics/emitter.h"
  7. namespace Carbon::Diagnostics {
  8. // Returns a singleton consumer that doesn't print its diagnostics.
  9. inline auto NullConsumer() -> Consumer& {
  10. struct SingletonConsumer : Consumer {
  11. auto HandleDiagnostic(Diagnostic /*d*/) -> void override {}
  12. };
  13. static auto* consumer = new SingletonConsumer;
  14. return *consumer;
  15. }
  16. // Returns a singleton emitter that doesn't print its diagnostics.
  17. template <typename LocT>
  18. inline auto NullEmitter() -> Emitter<LocT>& {
  19. class SingletonEmitter : public Emitter<LocT> {
  20. public:
  21. using Emitter<LocT>::Emitter;
  22. protected:
  23. // Converts a filename directly to the diagnostic location.
  24. auto ConvertLoc(LocT /*loc*/,
  25. Emitter<LocT>::ContextFnT /*context_fn*/) const
  26. -> ConvertedLoc override {
  27. return {.loc = {}, .last_byte_offset = -1};
  28. }
  29. };
  30. static auto* emitter = new SingletonEmitter(&NullConsumer());
  31. return *emitter;
  32. }
  33. } // namespace Carbon::Diagnostics
  34. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_NULL_DIAGNOSTICS_H_