check_internal.cpp 1.7 KB

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