check_internal.cpp 1.4 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 "common/ostream.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. auto CheckFailImpl(const char* kind, const char* file, int line,
  13. const char* condition_str, llvm::StringRef extra_message)
  14. -> void {
  15. // Render the final check string here.
  16. std::string message = llvm::formatv(
  17. "{0} failure at {1}:{2}{3}{4}{5}{6}\n", kind, file, line,
  18. llvm::StringRef(condition_str).empty() ? "" : ": ", condition_str,
  19. extra_message.empty() ? "" : ": ", extra_message);
  20. // Register another signal handler to print the message. This is because we
  21. // want it at the bottom of output, after LLVM's builtin stack output, rather
  22. // than the top.
  23. llvm::sys::AddSignalHandler(PrintAfterStackTrace,
  24. const_cast<char*>(message.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