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/Support/Compiler.h" 00019 #include "llvm/ADT/StringRef.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 00029 /// install_fatal_error_handler - Installs a new error handler to be used 00030 /// whenever a serious (non-recoverable) error is encountered by LLVM. 00031 /// 00032 /// If you are using llvm_start_multithreaded, you should register the handler 00033 /// before doing that. 00034 /// 00035 /// If no error handler is installed the default is to print the error message 00036 /// to stderr, and call exit(1). If an error handler is installed then it is 00037 /// the handler's responsibility to log the message, it will no longer be 00038 /// printed to stderr. If the error handler returns, then exit(1) will be 00039 /// called. 00040 /// 00041 /// It is dangerous to naively use an error handler which throws an exception. 00042 /// Even though some applications desire to gracefully recover from arbitrary 00043 /// faults, blindly throwing exceptions through unfamiliar code isn't a way to 00044 /// achieve this. 00045 /// 00046 /// \param user_data - An argument which will be passed to the install error 00047 /// handler. 00048 void install_fatal_error_handler(fatal_error_handler_t handler, 00049 void *user_data = 0); 00050 00051 /// Restores default error handling behaviour. 00052 /// This must not be called between llvm_start_multithreaded() and 00053 /// llvm_stop_multithreaded(). 00054 void remove_fatal_error_handler(); 00055 00056 /// ScopedFatalErrorHandler - This is a simple helper class which just 00057 /// calls install_fatal_error_handler in its constructor and 00058 /// remove_fatal_error_handler in its destructor. 00059 struct ScopedFatalErrorHandler { 00060 explicit ScopedFatalErrorHandler(fatal_error_handler_t handler, 00061 void *user_data = 0) { 00062 install_fatal_error_handler(handler, user_data); 00063 } 00064 00065 ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); } 00066 }; 00067 00068 /// Reports a serious error, calling any installed error handler. These 00069 /// functions are intended to be used for error conditions which are outside 00070 /// the control of the compiler (I/O errors, invalid user input, etc.) 00071 /// 00072 /// If no error handler is installed the default is to print the message to 00073 /// standard error, followed by a newline. 00074 /// After the error handler is called this function will call exit(1), it 00075 /// does not return. 00076 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason); 00077 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason); 00078 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason); 00079 LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason); 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 llvm_unreachable_internal(const char *msg=0, 00085 const char *file=0, 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