check_internal.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_CHECK_INTERNAL_H_
  5. #define CARBON_COMMON_CHECK_INTERNAL_H_
  6. #include <cstdlib>
  7. #include "common/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 ExitingStream {
  12. public:
  13. // A tag type that renders as ": " in an ExitingStream, but only if it is
  14. // followed by additional output. Otherwise, it renders as "". Primarily used
  15. // when building macros around these streams.
  16. struct AddSeparator {};
  17. // Internal type used in macros to dispatch to the `operator|` overload.
  18. struct Helper {};
  19. ExitingStream()
  20. // Prefix the buffer with the current bug report message.
  21. : buffer_(buffer_str_) {}
  22. // Never called.
  23. [[noreturn]] ~ExitingStream();
  24. // If the bool cast occurs, it's because the condition is false. This supports
  25. // && short-circuiting the creation of ExitingStream.
  26. explicit operator bool() const { return true; }
  27. // Forward output to llvm::errs.
  28. template <typename T>
  29. auto operator<<(const T& message) -> ExitingStream& {
  30. if (separator_) {
  31. buffer_ << ": ";
  32. separator_ = false;
  33. }
  34. buffer_ << message;
  35. return *this;
  36. }
  37. auto operator<<(AddSeparator /*add_separator*/) -> ExitingStream& {
  38. separator_ = true;
  39. return *this;
  40. }
  41. // Low-precedence binary operator overload used in check.h macros to flush the
  42. // output and exit the program. We do this in a binary operator rather than
  43. // the destructor to ensure good debug info and backtraces for errors.
  44. [[noreturn]] friend auto operator|(Helper /*helper*/, ExitingStream& stream)
  45. -> void {
  46. stream.Done();
  47. }
  48. private:
  49. [[noreturn]] auto Done() -> void;
  50. // Whether a separator should be printed if << is used again.
  51. bool separator_ = false;
  52. std::string buffer_str_;
  53. llvm::raw_string_ostream buffer_;
  54. };
  55. } // namespace Carbon::Internal
  56. // Raw exiting stream. This should be used when building forms of exiting
  57. // macros. It evaluates to a temporary `ExitingStream` object that can be
  58. // manipulated, streamed into, and then will exit the program.
  59. #define CARBON_CHECK_INTERNAL_STREAM() \
  60. Carbon::Internal::ExitingStream::Helper() | Carbon::Internal::ExitingStream()
  61. #endif // CARBON_COMMON_CHECK_INTERNAL_H_