Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines an API used to indicate fatal error conditions.  Non-fatal
0010 // errors (most of them) should be handled through LLVMContext.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_SUPPORT_ERRORHANDLING_H
0015 #define LLVM_SUPPORT_ERRORHANDLING_H
0016 
0017 #include "llvm/Support/Compiler.h"
0018 
0019 namespace llvm {
0020   class StringRef;
0021   class Twine;
0022 
0023   /// An error handler callback.
0024   typedef void (*fatal_error_handler_t)(void *user_data,
0025                                         const char *reason,
0026                                         bool gen_crash_diag);
0027 
0028   /// install_fatal_error_handler - Installs a new error handler to be used
0029   /// whenever a serious (non-recoverable) error is encountered by LLVM.
0030   ///
0031   /// If no error handler is installed the default is to print the error message
0032   /// to stderr, and call exit(1).  If an error handler is installed then it is
0033   /// the handler's responsibility to log the message, it will no longer be
0034   /// printed to stderr.  If the error handler returns, then exit(1) will be
0035   /// called.
0036   ///
0037   /// It is dangerous to naively use an error handler which throws an exception.
0038   /// Even though some applications desire to gracefully recover from arbitrary
0039   /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
0040   /// achieve this.
0041   ///
0042   /// \param user_data - An argument which will be passed to the install error
0043   /// handler.
0044   void install_fatal_error_handler(fatal_error_handler_t handler,
0045                                    void *user_data = nullptr);
0046 
0047   /// Restores default error handling behaviour.
0048   void remove_fatal_error_handler();
0049 
0050   /// ScopedFatalErrorHandler - This is a simple helper class which just
0051   /// calls install_fatal_error_handler in its constructor and
0052   /// remove_fatal_error_handler in its destructor.
0053   struct ScopedFatalErrorHandler {
0054     explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
0055                                      void *user_data = nullptr) {
0056       install_fatal_error_handler(handler, user_data);
0057     }
0058 
0059     ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
0060   };
0061 
0062 /// Reports a serious error, calling any installed error handler. These
0063 /// functions are intended to be used for error conditions which are outside
0064 /// the control of the compiler (I/O errors, invalid user input, etc.)
0065 ///
0066 /// If no error handler is installed the default is to print the message to
0067 /// standard error, followed by a newline.
0068 /// After the error handler is called this function will call abort(), it
0069 /// does not return.
0070 /// NOTE: The std::string variant was removed to avoid a <string> dependency.
0071 [[noreturn]] void report_fatal_error(const char *reason,
0072                                      bool gen_crash_diag = true);
0073 [[noreturn]] void report_fatal_error(StringRef reason,
0074                                      bool gen_crash_diag = true);
0075 [[noreturn]] void report_fatal_error(const Twine &reason,
0076                                      bool gen_crash_diag = true);
0077 
0078 /// Installs a new bad alloc error handler that should be used whenever a
0079 /// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM.
0080 ///
0081 /// The user can install a bad alloc handler, in order to define the behavior
0082 /// in case of failing allocations, e.g. throwing an exception. Note that this
0083 /// handler must not trigger any additional allocations itself.
0084 ///
0085 /// If no error handler is installed the default is to print the error message
0086 /// to stderr, and call exit(1).  If an error handler is installed then it is
0087 /// the handler's responsibility to log the message, it will no longer be
0088 /// printed to stderr.  If the error handler returns, then exit(1) will be
0089 /// called.
0090 ///
0091 ///
0092 /// \param user_data - An argument which will be passed to the installed error
0093 /// handler.
0094 void install_bad_alloc_error_handler(fatal_error_handler_t handler,
0095                                      void *user_data = nullptr);
0096 
0097 /// Restores default bad alloc error handling behavior.
0098 void remove_bad_alloc_error_handler();
0099 
0100 void install_out_of_memory_new_handler();
0101 
0102 /// Reports a bad alloc error, calling any user defined bad alloc
0103 /// error handler. In contrast to the generic 'report_fatal_error'
0104 /// functions, this function might not terminate, e.g. the user
0105 /// defined error handler throws an exception, but it won't return.
0106 ///
0107 /// Note: When throwing an exception in the bad alloc handler, make sure that
0108 /// the following unwind succeeds, e.g. do not trigger additional allocations
0109 /// in the unwind chain.
0110 ///
0111 /// If no error handler is installed (default), throws a bad_alloc exception
0112 /// if LLVM is compiled with exception support. Otherwise prints the error
0113 /// to standard error and calls abort().
0114 [[noreturn]] void report_bad_alloc_error(const char *Reason,
0115                                          bool GenCrashDiag = true);
0116 
0117 /// This function calls abort(), and prints the optional message to stderr.
0118 /// Use the llvm_unreachable macro (that adds location info), instead of
0119 /// calling this function directly.
0120 [[noreturn]] void
0121 llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
0122                           unsigned line = 0);
0123 }
0124 
0125 /// Marks that the current location is not supposed to be reachable.
0126 /// In !NDEBUG builds, prints the message and location info to stderr.
0127 /// In NDEBUG builds, if the platform does not support a builtin unreachable
0128 /// then we call an internal LLVM runtime function. Otherwise the behavior is
0129 /// controlled by the CMake flag
0130 ///   -DLLVM_UNREACHABLE_OPTIMIZE
0131 /// * When "ON" (default) llvm_unreachable() becomes an optimizer hint
0132 ///   that the current location is not supposed to be reachable: the hint
0133 ///   turns such code path into undefined behavior.  On compilers that don't
0134 ///   support such hints, prints a reduced message instead and aborts the
0135 ///   program.
0136 /// * When "OFF", a builtin_trap is emitted instead of an
0137 //    optimizer hint or printing a reduced message.
0138 ///
0139 /// Use this instead of assert(0). It conveys intent more clearly, suppresses
0140 /// diagnostics for unreachable code paths, and allows compilers to omit
0141 /// unnecessary code.
0142 #ifndef NDEBUG
0143 #define llvm_unreachable(msg) \
0144   ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
0145 #elif !defined(LLVM_BUILTIN_UNREACHABLE)
0146 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
0147 #elif LLVM_UNREACHABLE_OPTIMIZE
0148 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
0149 #else
0150 #define llvm_unreachable(msg)                                                  \
0151   do {                                                                         \
0152     LLVM_BUILTIN_TRAP;                                                         \
0153     LLVM_BUILTIN_UNREACHABLE;                                                  \
0154   } while (false)
0155 #endif
0156 
0157 #endif