ErrorHandling.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines an API used to indicate fatal error conditions. Non-fatal
  11. // errors (most of them) should be handled through LLVMContext.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_ERRORHANDLING_H
  15. #define LLVM_SUPPORT_ERRORHANDLING_H
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include <string>
  19. namespace llvm {
  20. class Twine;
  21. /// An error handler callback.
  22. typedef void (*fatal_error_handler_t)(void *user_data,
  23. const std::string& reason,
  24. bool gen_crash_diag);
  25. /// install_fatal_error_handler - Installs a new error handler to be used
  26. /// whenever a serious (non-recoverable) error is encountered by LLVM.
  27. ///
  28. /// If no error handler is installed the default is to print the error message
  29. /// to stderr, and call exit(1). If an error handler is installed then it is
  30. /// the handler's responsibility to log the message, it will no longer be
  31. /// printed to stderr. If the error handler returns, then exit(1) will be
  32. /// called.
  33. ///
  34. /// It is dangerous to naively use an error handler which throws an exception.
  35. /// Even though some applications desire to gracefully recover from arbitrary
  36. /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
  37. /// achieve this.
  38. ///
  39. /// \param user_data - An argument which will be passed to the install error
  40. /// handler.
  41. void install_fatal_error_handler(fatal_error_handler_t handler,
  42. void *user_data = nullptr);
  43. /// Restores default error handling behaviour.
  44. void remove_fatal_error_handler();
  45. /// ScopedFatalErrorHandler - This is a simple helper class which just
  46. /// calls install_fatal_error_handler in its constructor and
  47. /// remove_fatal_error_handler in its destructor.
  48. struct ScopedFatalErrorHandler {
  49. explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
  50. void *user_data = nullptr) {
  51. install_fatal_error_handler(handler, user_data);
  52. }
  53. ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
  54. };
  55. /// Reports a serious error, calling any installed error handler. These
  56. /// functions are intended to be used for error conditions which are outside
  57. /// the control of the compiler (I/O errors, invalid user input, etc.)
  58. ///
  59. /// If no error handler is installed the default is to print the message to
  60. /// standard error, followed by a newline.
  61. /// After the error handler is called this function will call exit(1), it
  62. /// does not return.
  63. LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason,
  64. bool gen_crash_diag = true);
  65. LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason,
  66. bool gen_crash_diag = true);
  67. LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason,
  68. bool gen_crash_diag = true);
  69. LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason,
  70. bool gen_crash_diag = true);
  71. /// This function calls abort(), and prints the optional message to stderr.
  72. /// Use the llvm_unreachable macro (that adds location info), instead of
  73. /// calling this function directly.
  74. LLVM_ATTRIBUTE_NORETURN void
  75. llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr,
  76. unsigned line=0);
  77. }
  78. /// Marks that the current location is not supposed to be reachable.
  79. /// In !NDEBUG builds, prints the message and location info to stderr.
  80. /// In NDEBUG builds, becomes an optimizer hint that the current location
  81. /// is not supposed to be reachable. On compilers that don't support
  82. /// such hints, prints a reduced message instead.
  83. ///
  84. /// Use this instead of assert(0). It conveys intent more clearly and
  85. /// allows compilers to omit some unnecessary code.
  86. #ifndef NDEBUG
  87. #define llvm_unreachable(msg) \
  88. ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
  89. #elif defined(LLVM_BUILTIN_UNREACHABLE)
  90. #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
  91. #else
  92. #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
  93. #endif
  94. #endif