1207618Srdivacky//===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===// 2198090Srdivacky// 3198090Srdivacky// The LLVM Compiler Infrastructure 4198090Srdivacky// 5198090Srdivacky// This file is distributed under the University of Illinois Open Source 6198090Srdivacky// License. See LICENSE.TXT for details. 7198090Srdivacky// 8198090Srdivacky//===----------------------------------------------------------------------===// 9198090Srdivacky// 10207618Srdivacky// This file defines an API used to indicate fatal error conditions. Non-fatal 11207618Srdivacky// errors (most of them) should be handled through LLVMContext. 12198090Srdivacky// 13198090Srdivacky//===----------------------------------------------------------------------===// 14198090Srdivacky 15198090Srdivacky#ifndef LLVM_SUPPORT_ERRORHANDLING_H 16198090Srdivacky#define LLVM_SUPPORT_ERRORHANDLING_H 17198090Srdivacky 18252723Sdim#include "llvm/ADT/StringRef.h" 19198090Srdivacky#include "llvm/Support/Compiler.h" 20198090Srdivacky#include <string> 21198090Srdivacky 22198090Srdivackynamespace llvm { 23198090Srdivacky class Twine; 24198090Srdivacky 25198090Srdivacky /// An error handler callback. 26207618Srdivacky typedef void (*fatal_error_handler_t)(void *user_data, 27252723Sdim const std::string& reason, 28252723Sdim bool gen_crash_diag); 29198090Srdivacky 30207618Srdivacky /// install_fatal_error_handler - Installs a new error handler to be used 31198090Srdivacky /// whenever a serious (non-recoverable) error is encountered by LLVM. 32198090Srdivacky /// 33198090Srdivacky /// If you are using llvm_start_multithreaded, you should register the handler 34198090Srdivacky /// before doing that. 35198090Srdivacky /// 36198090Srdivacky /// If no error handler is installed the default is to print the error message 37198090Srdivacky /// to stderr, and call exit(1). If an error handler is installed then it is 38198090Srdivacky /// the handler's responsibility to log the message, it will no longer be 39198090Srdivacky /// printed to stderr. If the error handler returns, then exit(1) will be 40198090Srdivacky /// called. 41198090Srdivacky /// 42198090Srdivacky /// It is dangerous to naively use an error handler which throws an exception. 43198090Srdivacky /// Even though some applications desire to gracefully recover from arbitrary 44198090Srdivacky /// faults, blindly throwing exceptions through unfamiliar code isn't a way to 45198090Srdivacky /// achieve this. 46198090Srdivacky /// 47198090Srdivacky /// \param user_data - An argument which will be passed to the install error 48198090Srdivacky /// handler. 49207618Srdivacky void install_fatal_error_handler(fatal_error_handler_t handler, 50207618Srdivacky void *user_data = 0); 51198090Srdivacky 52198090Srdivacky /// Restores default error handling behaviour. 53198090Srdivacky /// This must not be called between llvm_start_multithreaded() and 54198090Srdivacky /// llvm_stop_multithreaded(). 55207618Srdivacky void remove_fatal_error_handler(); 56198090Srdivacky 57212904Sdim /// ScopedFatalErrorHandler - This is a simple helper class which just 58212904Sdim /// calls install_fatal_error_handler in its constructor and 59212904Sdim /// remove_fatal_error_handler in its destructor. 60212904Sdim struct ScopedFatalErrorHandler { 61212904Sdim explicit ScopedFatalErrorHandler(fatal_error_handler_t handler, 62212904Sdim void *user_data = 0) { 63212904Sdim install_fatal_error_handler(handler, user_data); 64212904Sdim } 65212904Sdim 66212904Sdim ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); } 67212904Sdim }; 68212904Sdim 69198090Srdivacky /// Reports a serious error, calling any installed error handler. These 70198090Srdivacky /// functions are intended to be used for error conditions which are outside 71198090Srdivacky /// the control of the compiler (I/O errors, invalid user input, etc.) 72198090Srdivacky /// 73198090Srdivacky /// If no error handler is installed the default is to print the message to 74198090Srdivacky /// standard error, followed by a newline. 75198090Srdivacky /// After the error handler is called this function will call exit(1), it 76198090Srdivacky /// does not return. 77252723Sdim LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, 78252723Sdim bool gen_crash_diag = true); 79252723Sdim LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason, 80252723Sdim bool gen_crash_diag = true); 81252723Sdim LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason, 82252723Sdim bool gen_crash_diag = true); 83252723Sdim LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason, 84252723Sdim bool gen_crash_diag = true); 85198090Srdivacky 86198090Srdivacky /// This function calls abort(), and prints the optional message to stderr. 87198090Srdivacky /// Use the llvm_unreachable macro (that adds location info), instead of 88198090Srdivacky /// calling this function directly. 89218893Sdim LLVM_ATTRIBUTE_NORETURN void llvm_unreachable_internal(const char *msg=0, 90218893Sdim const char *file=0, 91218893Sdim unsigned line=0); 92198090Srdivacky} 93198090Srdivacky 94221345Sdim/// Marks that the current location is not supposed to be reachable. 95221345Sdim/// In !NDEBUG builds, prints the message and location info to stderr. 96221345Sdim/// In NDEBUG builds, becomes an optimizer hint that the current location 97221345Sdim/// is not supposed to be reachable. On compilers that don't support 98221345Sdim/// such hints, prints a reduced message instead. 99198090Srdivacky/// 100221345Sdim/// Use this instead of assert(0). It conveys intent more clearly and 101221345Sdim/// allows compilers to omit some unnecessary code. 102198090Srdivacky#ifndef NDEBUG 103200581Srdivacky#define llvm_unreachable(msg) \ 104200581Srdivacky ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__) 105221345Sdim#elif defined(LLVM_BUILTIN_UNREACHABLE) 106221345Sdim#define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE 107198090Srdivacky#else 108200581Srdivacky#define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal() 109198090Srdivacky#endif 110198090Srdivacky 111198090Srdivacky#endif 112