check_internal.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #include "common/check_internal.h"
  5. #include "llvm/Support/ErrorHandling.h"
  6. #include "llvm/Support/Signals.h"
  7. namespace Carbon::Internal {
  8. // Prints the buffered message.
  9. static auto PrintAfterStackTrace(void* str) -> void {
  10. llvm::errs() << reinterpret_cast<char*>(str);
  11. }
  12. ExitingStream::~ExitingStream() {
  13. llvm_unreachable(
  14. "Exiting streams should only be constructed by check.h macros that "
  15. "ensure the special operator| exits the program prior to their "
  16. "destruction!");
  17. }
  18. auto ExitingStream::Done() -> void {
  19. buffer_ << "\n";
  20. // Register another signal handler to print the buffered message. This is
  21. // because we want it at the bottom of output, after LLVM's builtin stack
  22. // output, rather than the top.
  23. llvm::sys::AddSignalHandler(PrintAfterStackTrace,
  24. const_cast<char*>(buffer_str_.c_str()));
  25. // It's useful to exit the program with `std::abort()` for integration with
  26. // debuggers and other tools. We also assume LLVM's exit handling is
  27. // installed, which will stack trace on `std::abort()`.
  28. std::abort();
  29. }
  30. } // namespace Carbon::Internal