1 //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an API used to indicate fatal error conditions. Non-fatal
11 // errors (most of them) should be handled through LLVMContext.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_ERRORHANDLING_H
16 #define LLVM_SUPPORT_ERRORHANDLING_H
18 #include "llvm/Support/Compiler.h"
25 /// An error handler callback.
26 typedef void (*fatal_error_handler_t)(void *user_data,
27 const std::string& reason,
30 /// install_fatal_error_handler - Installs a new error handler to be used
31 /// whenever a serious (non-recoverable) error is encountered by LLVM.
33 /// If no error handler is installed the default is to print the error message
34 /// to stderr, and call exit(1). If an error handler is installed then it is
35 /// the handler's responsibility to log the message, it will no longer be
36 /// printed to stderr. If the error handler returns, then exit(1) will be
39 /// It is dangerous to naively use an error handler which throws an exception.
40 /// Even though some applications desire to gracefully recover from arbitrary
41 /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
44 /// \param user_data - An argument which will be passed to the install error
46 void install_fatal_error_handler(fatal_error_handler_t handler,
47 void *user_data = nullptr);
49 /// Restores default error handling behaviour.
50 void remove_fatal_error_handler();
52 /// ScopedFatalErrorHandler - This is a simple helper class which just
53 /// calls install_fatal_error_handler in its constructor and
54 /// remove_fatal_error_handler in its destructor.
55 struct ScopedFatalErrorHandler {
56 explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
57 void *user_data = nullptr) {
58 install_fatal_error_handler(handler, user_data);
61 ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
64 /// Reports a serious error, calling any installed error handler. These
65 /// functions are intended to be used for error conditions which are outside
66 /// the control of the compiler (I/O errors, invalid user input, etc.)
68 /// If no error handler is installed the default is to print the message to
69 /// standard error, followed by a newline.
70 /// After the error handler is called this function will call exit(1), it
72 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason,
73 bool gen_crash_diag = true);
74 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason,
75 bool gen_crash_diag = true);
76 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason,
77 bool gen_crash_diag = true);
78 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason,
79 bool gen_crash_diag = true);
81 /// Installs a new bad alloc error handler that should be used whenever a
82 /// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM.
84 /// The user can install a bad alloc handler, in order to define the behavior
85 /// in case of failing allocations, e.g. throwing an exception. Note that this
86 /// handler must not trigger any additional allocations itself.
88 /// If no error handler is installed the default is to print the error message
89 /// to stderr, and call exit(1). If an error handler is installed then it is
90 /// the handler's responsibility to log the message, it will no longer be
91 /// printed to stderr. If the error handler returns, then exit(1) will be
95 /// \param user_data - An argument which will be passed to the installed error
97 void install_bad_alloc_error_handler(fatal_error_handler_t handler,
98 void *user_data = nullptr);
100 /// Restores default bad alloc error handling behavior.
101 void remove_bad_alloc_error_handler();
103 /// Reports a bad alloc error, calling any user defined bad alloc
104 /// error handler. In contrast to the generic 'report_fatal_error'
105 /// functions, this function is expected to return, e.g. the user
106 /// defined error handler throws an exception.
108 /// Note: When throwing an exception in the bad alloc handler, make sure that
109 /// the following unwind succeeds, e.g. do not trigger additional allocations
110 /// in the unwind chain.
112 /// If no error handler is installed (default), then a bad_alloc exception
113 /// is thrown if LLVM is compiled with exception support, otherwise an assertion
115 void report_bad_alloc_error(const char *Reason, bool GenCrashDiag = true);
117 /// This function calls abort(), and prints the optional message to stderr.
118 /// Use the llvm_unreachable macro (that adds location info), instead of
119 /// calling this function directly.
120 LLVM_ATTRIBUTE_NORETURN void
121 llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
125 /// Marks that the current location is not supposed to be reachable.
126 /// In !NDEBUG builds, prints the message and location info to stderr.
127 /// In NDEBUG builds, becomes an optimizer hint that the current location
128 /// is not supposed to be reachable. On compilers that don't support
129 /// such hints, prints a reduced message instead.
131 /// Use this instead of assert(0). It conveys intent more clearly and
132 /// allows compilers to omit some unnecessary code.
134 #define llvm_unreachable(msg) \
135 ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
136 #elif defined(LLVM_BUILTIN_UNREACHABLE)
137 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
139 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()