check_internal.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. auto CheckFailImpl(const char* kind, const char* file, int line,
  9. const char* condition_str, llvm::StringRef extra_message)
  10. -> void {
  11. // Render the final check string here.
  12. std::string message = llvm::formatv(
  13. "{0} failure at {1}:{2}{3}{4}{5}{6}\n", kind, file, line,
  14. llvm::StringRef(condition_str).empty() ? "" : ": ", condition_str,
  15. extra_message.empty() ? "" : ": ", extra_message);
  16. // This macro is defined by `--config=non-fatal-checks`.
  17. #ifdef CARBON_NON_FATAL_CHECKS
  18. #ifdef NDEBUG
  19. #error "--config=non-fatal-checks is incompatible with -c opt"
  20. #endif
  21. // TODO: It'd be nice to print the LLVM PrettyStackTrace, but LLVM doesn't
  22. // expose functionality to do so.
  23. llvm::sys::PrintStackTrace(llvm::errs());
  24. llvm::errs() << message;
  25. #else
  26. // Register another signal handler to print the message. This is because we
  27. // want it at the bottom of output, after LLVM's builtin stack output, rather
  28. // than the top.
  29. llvm::sys::AddSignalHandler(
  30. [](void* str) { llvm::errs() << reinterpret_cast<char*>(str); },
  31. const_cast<char*>(message.c_str()));
  32. // It's useful to exit the program with `std::abort()` for integration with
  33. // debuggers and other tools. We also assume LLVM's exit handling is
  34. // installed, which will stack trace on `std::abort()`.
  35. std::abort();
  36. #endif
  37. }
  38. } // namespace Carbon::Internal