vlog_internal.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_COMMON_VLOG_INTERNAL_H_
  5. #define CARBON_COMMON_VLOG_INTERNAL_H_
  6. #include <cstdlib>
  7. #include "llvm/Support/raw_ostream.h"
  8. namespace Carbon::Internal {
  9. // Wraps a stream and exiting for fatal errors. Should only be used by check.h
  10. // macros.
  11. class VLoggingStream {
  12. public:
  13. // Internal type used in macros to dispatch to the `operator|` overload.
  14. struct Helper {};
  15. explicit VLoggingStream(llvm::raw_ostream* stream)
  16. // Prefix the buffer with the current bug report message.
  17. : stream_(stream) {}
  18. ~VLoggingStream() = default;
  19. // If the bool cast occurs, it's because the condition is false. This supports
  20. // && short-circuiting the creation of ExitingStream.
  21. explicit operator bool() const { return true; }
  22. // Forward output to llvm::errs.
  23. template <typename T>
  24. auto operator<<(const T& message) -> VLoggingStream& {
  25. *stream_ << message;
  26. return *this;
  27. }
  28. // Low-precedence binary operator overload used in vlog.h macros.
  29. friend auto operator|(Helper /*helper*/, VLoggingStream& /*stream*/) -> void {
  30. }
  31. private:
  32. [[noreturn]] auto Done() -> void;
  33. llvm::raw_ostream* stream_;
  34. };
  35. } // namespace Carbon::Internal
  36. // Raw logging stream. This should be used when building forms of vlog
  37. // macros.
  38. #define CARBON_VLOG_INTERNAL_STREAM(stream) \
  39. Carbon::Internal::VLoggingStream::Helper() | \
  40. Carbon::Internal::VLoggingStream(stream)
  41. #endif // CARBON_COMMON_VLOG_INTERNAL_H_