check_internal.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 COMMON_CHECK_INTERNAL_H_
  5. #define COMMON_CHECK_INTERNAL_H_
  6. #include "llvm/Support/ErrorHandling.h"
  7. #include "llvm/Support/Signals.h"
  8. #include "llvm/Support/raw_ostream.h"
  9. namespace Carbon::Internal {
  10. // Wraps a stream and exiting for fatal errors. Should only be used by check.h
  11. // macros.
  12. class ExitingStream {
  13. public:
  14. // A tag type that renders as ": " in an ExitingStream, but only if it is
  15. // followed by additional output. Otherwise, it renders as "". Primarily used
  16. // when building macros around these streams.
  17. struct AddSeparator {};
  18. // Internal type used in macros to dispatch to the `operator|` overload.
  19. struct Helper {};
  20. [[noreturn]] ~ExitingStream() {
  21. llvm_unreachable(
  22. "Exiting streams should only be constructed by check.h macros that "
  23. "ensure the special operator| exits the program prior to their "
  24. "destruction!");
  25. }
  26. // Indicates that the program is exiting due to a bug in the program, rather
  27. // than, e.g., invalid input.
  28. auto TreatAsBug() -> ExitingStream& {
  29. treat_as_bug_ = true;
  30. return *this;
  31. }
  32. // If the bool cast occurs, it's because the condition is false. This supports
  33. // && short-circuiting the creation of ExitingStream.
  34. explicit operator bool() const { return true; }
  35. // Forward output to llvm::errs.
  36. template <typename T>
  37. auto operator<<(const T& message) -> ExitingStream& {
  38. if (separator_) {
  39. llvm::errs() << ": ";
  40. separator_ = false;
  41. }
  42. llvm::errs() << message;
  43. return *this;
  44. }
  45. auto operator<<(AddSeparator /*unused*/) -> ExitingStream& {
  46. separator_ = true;
  47. return *this;
  48. }
  49. // Low-precedence binary operator overload used in check.h macros to flush the
  50. // output and exit the program. We do this in a binary operator rather than
  51. // the destructor to ensure good debug info and backtraces for errors.
  52. [[noreturn]] friend auto operator|(Helper /*unused*/, ExitingStream& rhs) {
  53. // Finish with a newline.
  54. llvm::errs() << "\n";
  55. if (rhs.treat_as_bug_) {
  56. std::abort();
  57. } else {
  58. std::exit(-1);
  59. }
  60. }
  61. private:
  62. // Whether a separator should be printed if << is used again.
  63. bool separator_ = false;
  64. // Whether the program is exiting due to a bug.
  65. bool treat_as_bug_ = false;
  66. };
  67. } // namespace Carbon::Internal
  68. #endif // COMMON_CHECK_INTERNAL_H_