ErrorHandling.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines an API used to indicate fatal error conditions. Non-fatal
  15. // errors (most of them) should be handled through LLVMContext.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_ERRORHANDLING_H
  19. #define LLVM_SUPPORT_ERRORHANDLING_H
  20. #include "llvm/Support/Compiler.h"
  21. namespace llvm {
  22. class StringRef;
  23. class Twine;
  24. /// An error handler callback.
  25. typedef void (*fatal_error_handler_t)(void *user_data,
  26. const char *reason,
  27. bool gen_crash_diag);
  28. /// install_fatal_error_handler - Installs a new error handler to be used
  29. /// whenever a serious (non-recoverable) error is encountered by LLVM.
  30. ///
  31. /// If no error handler is installed the default is to print the error message
  32. /// to stderr, and call exit(1). If an error handler is installed then it is
  33. /// the handler's responsibility to log the message, it will no longer be
  34. /// printed to stderr. If the error handler returns, then exit(1) will be
  35. /// called.
  36. ///
  37. /// It is dangerous to naively use an error handler which throws an exception.
  38. /// Even though some applications desire to gracefully recover from arbitrary
  39. /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
  40. /// achieve this.
  41. ///
  42. /// \param user_data - An argument which will be passed to the install error
  43. /// handler.
  44. void install_fatal_error_handler(fatal_error_handler_t handler,
  45. void *user_data = nullptr);
  46. /// Restores default error handling behaviour.
  47. void remove_fatal_error_handler();
  48. /// ScopedFatalErrorHandler - This is a simple helper class which just
  49. /// calls install_fatal_error_handler in its constructor and
  50. /// remove_fatal_error_handler in its destructor.
  51. struct ScopedFatalErrorHandler {
  52. explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
  53. void *user_data = nullptr) {
  54. install_fatal_error_handler(handler, user_data);
  55. }
  56. ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
  57. };
  58. /// Reports a serious error, calling any installed error handler. These
  59. /// functions are intended to be used for error conditions which are outside
  60. /// the control of the compiler (I/O errors, invalid user input, etc.)
  61. ///
  62. /// If no error handler is installed the default is to print the message to
  63. /// standard error, followed by a newline.
  64. /// After the error handler is called this function will call abort(), it
  65. /// does not return.
  66. /// NOTE: The std::string variant was removed to avoid a <string> dependency.
  67. [[noreturn]] void report_fatal_error(const char *reason,
  68. bool gen_crash_diag = true);
  69. [[noreturn]] void report_fatal_error(StringRef reason,
  70. bool gen_crash_diag = true);
  71. [[noreturn]] void report_fatal_error(const Twine &reason,
  72. bool gen_crash_diag = true);
  73. /// Installs a new bad alloc error handler that should be used whenever a
  74. /// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM.
  75. ///
  76. /// The user can install a bad alloc handler, in order to define the behavior
  77. /// in case of failing allocations, e.g. throwing an exception. Note that this
  78. /// handler must not trigger any additional allocations itself.
  79. ///
  80. /// If no error handler is installed the default is to print the error message
  81. /// to stderr, and call exit(1). If an error handler is installed then it is
  82. /// the handler's responsibility to log the message, it will no longer be
  83. /// printed to stderr. If the error handler returns, then exit(1) will be
  84. /// called.
  85. ///
  86. ///
  87. /// \param user_data - An argument which will be passed to the installed error
  88. /// handler.
  89. void install_bad_alloc_error_handler(fatal_error_handler_t handler,
  90. void *user_data = nullptr);
  91. /// Restores default bad alloc error handling behavior.
  92. void remove_bad_alloc_error_handler();
  93. void install_out_of_memory_new_handler();
  94. /// Reports a bad alloc error, calling any user defined bad alloc
  95. /// error handler. In contrast to the generic 'report_fatal_error'
  96. /// functions, this function might not terminate, e.g. the user
  97. /// defined error handler throws an exception, but it won't return.
  98. ///
  99. /// Note: When throwing an exception in the bad alloc handler, make sure that
  100. /// the following unwind succeeds, e.g. do not trigger additional allocations
  101. /// in the unwind chain.
  102. ///
  103. /// If no error handler is installed (default), throws a bad_alloc exception
  104. /// if LLVM is compiled with exception support. Otherwise prints the error
  105. /// to standard error and calls abort().
  106. [[noreturn]] void report_bad_alloc_error(const char *Reason,
  107. bool GenCrashDiag = true);
  108. /// This function calls abort(), and prints the optional message to stderr.
  109. /// Use the llvm_unreachable macro (that adds location info), instead of
  110. /// calling this function directly.
  111. [[noreturn]] void
  112. llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
  113. unsigned line = 0);
  114. }
  115. /// Marks that the current location is not supposed to be reachable.
  116. /// In !NDEBUG builds, prints the message and location info to stderr.
  117. /// In NDEBUG builds, becomes an optimizer hint that the current location
  118. /// is not supposed to be reachable. On compilers that don't support
  119. /// such hints, prints a reduced message instead and aborts the program.
  120. ///
  121. /// Use this instead of assert(0). It conveys intent more clearly and
  122. /// allows compilers to omit some unnecessary code.
  123. #ifndef NDEBUG
  124. #define llvm_unreachable(msg) \
  125. ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
  126. #elif defined(LLVM_BUILTIN_UNREACHABLE)
  127. #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
  128. #else
  129. #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
  130. #endif
  131. #endif
  132. #ifdef __GNUC__
  133. #pragma GCC diagnostic pop
  134. #endif