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 15 #ifndef LLVM_SUPPORT_ERRORHANDLING_H 16 #define LLVM_SUPPORT_ERRORHANDLING_H 17 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Support/Compiler.h" 20 #include <string> 21 22 namespace llvm { 23 class Twine; 24 25 /// An error handler callback. 26 typedef void (*fatal_error_handler_t)(void *user_data, 27 const std::string& reason, 28 bool gen_crash_diag); 29 30 /// install_fatal_error_handler - Installs a new error handler to be used 31 /// whenever a serious (non-recoverable) error is encountered by LLVM. 32 /// 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 37 /// called. 38 /// 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 42 /// achieve this. 43 /// 44 /// \param user_data - An argument which will be passed to the install error 45 /// handler. 46 void install_fatal_error_handler(fatal_error_handler_t handler, 47 void *user_data = nullptr); 48 49 /// Restores default error handling behaviour. 50 void remove_fatal_error_handler(); 51 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); 59 } 60 61 ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); } 62 }; 63 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.) 67 /// 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 71 /// does not return. 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); 80 81 /// This function calls abort(), and prints the optional message to stderr. 82 /// Use the llvm_unreachable macro (that adds location info), instead of 83 /// calling this function directly. 84 LLVM_ATTRIBUTE_NORETURN void 85 llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, 86 unsigned line=0); 87 } 88 89 /// Marks that the current location is not supposed to be reachable. 90 /// In !NDEBUG builds, prints the message and location info to stderr. 91 /// In NDEBUG builds, becomes an optimizer hint that the current location 92 /// is not supposed to be reachable. On compilers that don't support 93 /// such hints, prints a reduced message instead. 94 /// 95 /// Use this instead of assert(0). It conveys intent more clearly and 96 /// allows compilers to omit some unnecessary code. 97 #ifndef NDEBUG 98 #define llvm_unreachable(msg) \ 99 ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__) 100 #elif defined(LLVM_BUILTIN_UNREACHABLE) 101 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE 102 #else 103 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal() 104 #endif 105 106 #endif 107