LLVM API Documentation

ErrorHandling.h
Go to the documentation of this file.
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 you are using llvm_start_multithreaded, you should register the handler
00034   /// before doing that.
00035   ///
00036   /// If no error handler is installed the default is to print the error message
00037   /// to stderr, and call exit(1).  If an error handler is installed then it is
00038   /// the handler's responsibility to log the message, it will no longer be
00039   /// printed to stderr.  If the error handler returns, then exit(1) will be
00040   /// called.
00041   ///
00042   /// It is dangerous to naively use an error handler which throws an exception.
00043   /// Even though some applications desire to gracefully recover from arbitrary
00044   /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
00045   /// achieve this.
00046   ///
00047   /// \param user_data - An argument which will be passed to the install error
00048   /// handler.
00049   void install_fatal_error_handler(fatal_error_handler_t handler,
00050                                    void *user_data = nullptr);
00051 
00052   /// Restores default error handling behaviour.
00053   /// This must not be called between llvm_start_multithreaded() and
00054   /// llvm_stop_multithreaded().
00055   void remove_fatal_error_handler();
00056 
00057   /// ScopedFatalErrorHandler - This is a simple helper class which just
00058   /// calls install_fatal_error_handler in its constructor and
00059   /// remove_fatal_error_handler in its destructor.
00060   struct ScopedFatalErrorHandler {
00061     explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
00062                                      void *user_data = nullptr) {
00063       install_fatal_error_handler(handler, user_data);
00064     }
00065 
00066     ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
00067   };
00068 
00069   /// Reports a serious error, calling any installed error handler. These
00070   /// functions are intended to be used for error conditions which are outside
00071   /// the control of the compiler (I/O errors, invalid user input, etc.)
00072   ///
00073   /// If no error handler is installed the default is to print the message to
00074   /// standard error, followed by a newline.
00075   /// After the error handler is called this function will call exit(1), it 
00076   /// does not return.
00077   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason,
00078                                                   bool gen_crash_diag = true);
00079   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason,
00080                                                   bool gen_crash_diag = true);
00081   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason,
00082                                                   bool gen_crash_diag = true);
00083   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason,
00084                                                   bool gen_crash_diag = true);
00085 
00086   /// This function calls abort(), and prints the optional message to stderr.
00087   /// Use the llvm_unreachable macro (that adds location info), instead of
00088   /// calling this function directly.
00089   LLVM_ATTRIBUTE_NORETURN void
00090   llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr,
00091                             unsigned line=0);
00092 }
00093 
00094 /// Marks that the current location is not supposed to be reachable.
00095 /// In !NDEBUG builds, prints the message and location info to stderr.
00096 /// In NDEBUG builds, becomes an optimizer hint that the current location
00097 /// is not supposed to be reachable.  On compilers that don't support
00098 /// such hints, prints a reduced message instead.
00099 ///
00100 /// Use this instead of assert(0).  It conveys intent more clearly and
00101 /// allows compilers to omit some unnecessary code.
00102 #ifndef NDEBUG
00103 #define llvm_unreachable(msg) \
00104   ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
00105 #elif defined(LLVM_BUILTIN_UNREACHABLE)
00106 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
00107 #else
00108 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
00109 #endif
00110 
00111 #endif