check_internal.cpp 1.2 KB

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