LLVM API Documentation
00001 //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines an API used to indicate fatal error conditions. Non-fatal 00011 // errors (most of them) should be handled through LLVMContext. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_ERRORHANDLING_H 00016 #define LLVM_SUPPORT_ERRORHANDLING_H 00017 00018 #include "llvm/ADT/StringRef.h" 00019 #include "llvm/Support/Compiler.h" 00020 #include <string> 00021 00022 namespace llvm { 00023 class Twine; 00024 00025 /// An error handler callback. 00026 typedef void (*fatal_error_handler_t)(void *user_data, 00027 const std::string& reason, 00028 bool gen_crash_diag); 00029 00030 /// install_fatal_error_handler - Installs a new error handler to be used 00031 /// whenever a serious (non-recoverable) error is encountered by LLVM. 00032 /// 00033 /// If no error handler is installed the default is to print the error message 00034 /// to stderr, and call exit(1). If an error handler is installed then it is 00035 /// the handler's responsibility to log the message, it will no longer be 00036 /// printed to stderr. If the error handler returns, then exit(1) will be 00037 /// called. 00038 /// 00039 /// It is dangerous to naively use an error handler which throws an exception. 00040 /// Even though some applications desire to gracefully recover from arbitrary 00041 /// faults, blindly throwing exceptions through unfamiliar code isn't a way to 00042 /// achieve this. 00043 /// 00044 /// \param user_data - An argument which will be passed to the install error 00045 /// handler. 00046 void install_fatal_error_handler(fatal_error_handler_t handler, 00047 void *user_data = nullptr); 00048 00049 /// Restores default error handling behaviour. 00050 void remove_fatal_error_handler(); 00051 00052 /// ScopedFatalErrorHandler - This is a simple helper class which just 00053 /// calls install_fatal_error_handler in its constructor and 00054 /// remove_fatal_error_handler in its destructor. 00055 struct ScopedFatalErrorHandler { 00056 explicit ScopedFatalErrorHandler(fatal_error_handler_t handler, 00057 void *user_data = nullptr) { 00058 install_fatal_error_handler(handler, user_data); 00059 } 00060 00061 ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); } 00062 }; 00063 00064 /// Reports a serious error, calling any installed error handler. These 00065 /// functions are intended to be used for error conditions which are outside 00066 /// the control of the compiler (I/O errors, invalid user input, etc.) 00067 /// 00068 /// If no error handler is installed the default is to print the message to 00069 /// standard error, followed by a newline. 00070 /// After the error handler is called this function will call exit(1), it 00071 /// does not return. 00072 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, 00073 bool gen_crash_diag = true); 00074 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason, 00075 bool gen_crash_diag = true); 00076 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason, 00077 bool gen_crash_diag = true); 00078 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason, 00079 bool gen_crash_diag = true); 00080 00081 /// This function calls abort(), and prints the optional message to stderr. 00082 /// Use the llvm_unreachable macro (that adds location info), instead of 00083 /// calling this function directly. 00084 LLVM_ATTRIBUTE_NORETURN void 00085 llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, 00086 unsigned line=0); 00087 } 00088 00089 /// Marks that the current location is not supposed to be reachable. 00090 /// In !NDEBUG builds, prints the message and location info to stderr. 00091 /// In NDEBUG builds, becomes an optimizer hint that the current location 00092 /// is not supposed to be reachable. On compilers that don't support 00093 /// such hints, prints a reduced message instead. 00094 /// 00095 /// Use this instead of assert(0). It conveys intent more clearly and 00096 /// allows compilers to omit some unnecessary code. 00097 #ifndef NDEBUG 00098 #define llvm_unreachable(msg) \ 00099 ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__) 00100 #elif defined(LLVM_BUILTIN_UNREACHABLE) 00101 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE 00102 #else 00103 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal() 00104 #endif 00105 00106 #endif